Review PSR-7

Si Nguyen

at GO1

PSR-7

  • PSR-7 is a new 'PHP standard recommendation'
  • It describes how to create PHP representations of a HTTP Request and a HTTP response.
  • PSR-7 gets a lot of things right, and is very close to nailing the abstract data model behind HTTP
  • PSR-7 is to provide a set of common interfaces for HTTP messages as described in RFC 7230 and RCF 7231.

HTTP

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems.[1] HTTP is the foundation of data communication for the World Wide Web.

HTTP

HTTP

The HTTP header consists of:

  • A request of response line
    • an http request line: method, url and version
    • a response line: version, code and response pharse
  • A MIME header
    • A MIME header is comprised of zero or more MIME fields.
    • A MIME field is composed of a field name, a colon and zero or more field values.
    • The values in a field are separated by commas.

HTTP by Example

GET /GO1P-572/ HTTP/1.1
Host: com.apiom-test.s3-website-us-east-1.amazonaws.com
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,vi;q=0.6,cs;q=0.4
Cookie: __zlcmid=WYevYYlGONoaMB; _ga=GA1.2.1541119812.1441346929; _gat=1
If-None-Match: "db72b57d602ef57f571965257a13b1a2"
If-Modified-Since: Mon, 05 Oct 2015 04:35:00 GMT
HTTP/1.1 304 Not Modified
x-amz-id-2: HhdJ9sRg9afS0sp0nmnXJ+2IKUmExPOb7NPr4M7PwnHDaALBf1AopnnDn4E1ZouWPD1FHP6nhHs=
x-amz-request-id: FDA42C9D468DBFF9
Date: Mon, 05 Oct 2015 07:10:49 GMT
Last-Modified: Mon, 05 Oct 2015 04:35:00 GMT
x-amz-expiration: expiry-date="Tue, 20 Oct 2015 00:00:00 GMT", rule-id="Rule for the Entire Bucket"
ETag: "db72b57d602ef57f571965257a13b1a2"
Server: AmazonS3

Request Headers

Response Headers

HTTP by Example

HTTP Message in PHP

Client-side HTTP support

  • PHP stream
  • The cURL extension
  • ext/http

Server-side HTTP support

  • Server APIs (SAPI) to interpret incoming HTTP requests.
  • SAPI design abstracts common input source such as cookies, query string agrs, and url-encode via super globals ($_COOKIE, $_GET, ...)

HTTP Message in PHP

Why bother?

  • Projects use PHP's super-globals directly.
  • Projects will create implementations from scratch.
  • Projects may require a specific HTTP client/server library that provides HTTP message implementations.
  • Projects may create adapters for common HTTP message implementations.

PSR-7 Goals

  • Provide the interfaces needed for describing HTTP messages.
  • Focus on practical applications and usability.
  • Define the interfaces to model all elements of the HTTP message and URI specifications.
  • Ensure that the API does not impose arbitrary limits on HTTP messages. For example, some HTTP message bodies can be too large to store in memory, so we must account for this.
  • Provide useful abstractions both for handling incoming requests for server-side applications and for sending outgoing requests in HTTP clients.

HTTP message interfaces

Message

  • An HTTP message is either a request from a client to a server or a response from a server to a client.
  • Both Psr\Http\Message\RequestInterface and Psr\Http\Message\ResponseInterface extend Psr\Http\Message\MessageInterface

 

HTTP message interfaces

HTTP Headers

  • HTTP messages include case-insensitive header field names
  • Headers are retrieved by name from classes implementing the MessageInterface in a case-insensitive manner
  • Support multi-values

 

$message = $message->withHeader('foo', 'bar');

echo $message->getHeaderLine('foo');
// Outputs: bar

echo $message->getHeaderLine('FOO');
// Outputs: bar

$message = $message->withHeader('fOO', 'baz');
echo $message->getHeaderLine('foo');
// Outputs: baz

HTTP message interfaces

Streams

  • HTTP messages consist of a start-line, headers, and a body.
  • The body of an HTTP message can be very small or extremely large

HTTP message interfaces

Request Targets and URIs

  • origin-form
  • absolute-form
  • authority-form
  • asterisk-form

HTTP message interfaces

  • Server-side Requests
  • Uploaded files

References

  • http://www.php-fig.org/psr/psr-7/
  • https://github.com/php-fig/http-message/tree/master/src
  • http://tools.ietf.org/html/rfc7230
  • http://tools.ietf.org/html/rfc7231
  • http://tools.ietf.org/html/rfc3986

Review PSR-7

By Nguyen Tien Si