Download the PHP package icanboogie/http without Composer
On this page you can find all versions of the php package icanboogie/http. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download icanboogie/http
More information about icanboogie/http
Files in icanboogie/http
Package http
Short Description Provides an API to handle HTTP requests.
License BSD-3-Clause
Homepage http://icanboogie.org/
Informations about the package http
HTTP
The icanboogie/http package provides a foundation to handle HTTP requests, with representations for requests, request files, responses, and headers. The package also lay the foundation of
The following example is an overview of request processing:
Installation
Request
A request is represented by a Request instance. The initial request is usually created from
the $_SERVER
array, while sub requests are created from arrays of Request::OPTION_*
or
RequestOptions::OPTION_*
options.
Safe and idempotent requests
Safe methods are HTTP methods that don't modify resources.
For instance, using GET
or HEAD
on a resource URL, should NEVER change the resource.
The is_safe
property may be used to check if a request is safe or not.
An idempotent HTTP method is an HTTP method that can be called many times without different outcomes. It would not matter if the method is called only once, or ten times over. The result should be the same.
The is_idempotent
property may be used to check if a request is idempotent or not.
A request with changed properties
Requests are for the most part immutable, the with()
method creates an instance copy with changed
properties.
Request parameters
Whether they're sent as part of the query string, the POST body, or the path info, parameters sent
along a request are collected in arrays. The query_params
, request_params
, and path_params
properties give you access to these parameters.
You can access each type of parameter as follows:
All the request parameters are also available through the params
property, which merges the
query, request and path parameters:
Used as an array, the Request instance provides these parameters as well, but returns null
when a parameter is not defined:
Of course, the request is also an iterator:
Request files
Files associated with a request are collected in a FileList instance. The initial request
created with $_SERVER
obtain its files from $_FILES
. For custom requests, files are defined
using OPTION_FILES
.
File represents uploaded files, and pretend uploaded files, with a single API.
The is_uploaded
property helps you set them apart.
The is_valid
property is a simple way to check if a file is valid. The move()
method
lets you move the file out of the temporary folder or around the filesystem.
The match()
method is used to check if a file matches a MIME type, a MIME class, or a file
extension:
The method also handles sets, and returns true
if there is any match:
File instances can be converted into arrays with the to_array()
method:
Request context
Because requests may be nested, the request context offers a safe place where you can store the state of your application that is relative to a request. For instance, the context can store the relative site, page, route, dispatcher…
The following example demonstrates how to store a value in a request context:
Obtaining a response
A response is the result of a Responder's respond()
method. An exception is thrown when a
response can't be provided; for example, NotFound.
Response
The response to a request is represented by a Response instance. The response body can
either be null
, a string (or Stringable
), or a Closure
.
Note: Contrary to Response instances or completely mutable.
The header and body are sent by invoking the response:
Response status
The response status is represented by a Status instance. It may be defined as an HTTP response
code such as 200
, an array such as [ 200, "Ok" ]
, or a string such as "200 Ok"
.
Streaming the response body
When a large response body needs to be streamed, it is recommended to use a closure as a response body instead of a huge string that would consume a lot of memory.
About the Content-Length
header field
Before v2.3.2 the Content-Length
header field was added automatically when it was computable,
for instance when the body was a string or an instance implementing __toString()
.
Starting v2.3.2, this is no longer the case, and the header field has to be defined when required.
This was decided to prevent a bug with Apache+FastCGI+DEFLATE where the Content-Length
field
wasn't adjusted although the body was compressed. Also, in most cases it is not such a good idea
to define that field for generated content because it prevents the response from being sent as
compressed chunks.
Redirect response
A redirect response may be created using a RedirectResponse instance.
Delivering a file
A file may be delivered using a FileResponse instance. Cache control and range requests
are handled automatically; you only have to provide the pathname of the file, or a SplFileInfo
instance, and a request.
The OPTION_FILENAME
option may be used to force downloading. Of course, utf-8 strings are
supported:
The following options are also available:
-
OPTION_ETAG
: Specifies theETag
header field of the response. If it is not defined, the SHA-384 of the file is used instead. -
OPTION_EXPIRES
: Specifies the expiration date as aDateTime
instance or a relative date such as+3 month
, which maps to theExpires
header field. Themax-age
directive of theCache-Control
header field is computed from the current time. If it is not definedDEFAULT_EXPIRES
is used instead ("+1 month"). OPTION_MIME
: Specifies the MIME of the file, which maps to theContent-Type
header field. If it is not defined the MIME is guessed usingfinfo::file()
.
The following properties are available:
-
modified_time
: Returns the last modified timestamp of the file. is_modified
: Whether the file has been modified since the last response. The value is computed using the request header fieldsIf-None-Match
andIf-Modified-Since
, and the propertiesmodified_time
andetag
.
Headers
Here is an overview of header usage. Details are available in the Headers documentation.
Exceptions
The HTTP package defines the following exceptions:
- ClientError: thrown when a client error occurs.
- NotFound: thrown when a resource is not found. For instance, this exception is thrown by the dispatcher when it fails to resolve a request into a response.
- SecurityError.
- SecurityError.
- MethodNotAllowed: thrown when an HTTP method is not supported.
- ServerError: throw when a server error occurs.
- ServiceUnavailable: thrown when a server is currently unavailable (because it is overloaded or down for maintenance).
- ForceRedirect: thrown when a redirect is absolutely required.
- StatusCodeNotValid: thrown when an HTTP status code is not valid.
Exceptions defined by the package implement the ICanBoogie\HTTP\Exception
interface.
Using this interface, one can easily catch HTTP-related exceptions:
Continuous Integration
The project is continuously tested by GitHub actions.
Code of Conduct
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you're expected to uphold this code.
Contributing
See CONTRIBUTING for details.
License
icanboogie/http is released under the BSD-3-Clause.
All versions of http with dependencies
ext-mbstring Version *
icanboogie/prototype Version ^5.0
icanboogie/event Version ^4.0
icanboogie/datetime Version ^1.2