zend-expressive icon indicating copy to clipboard operation
zend-expressive copied to clipboard

HTTP/1.1 response to the Client of 1.0

Open ngyuki opened this issue 6 years ago • 2 comments

# create project from zend-expressive-skeleton
composer create-project zendframework/zend-expressive-skeleton .

# run container
docker run --rm -p 8080:80 -v "$PWD:/app" -w "/app" php:apache sh -c '
  a2enmod rewrite
  rmdir /var/www/html
  ln -s /app/public /var/www/html
  exec apache2-foreground
'

# run curl in other terminal, and see response
curl -i -0 http://localhost:8080/

curl request is HTTP/1.0, but zend-expressive has HTTP/1.1 response.

HTTP/1.1 200 OK
Date: Wed, 26 Jul 2017 10:46:48 GMT
Server: Apache/2.4.10 (Debian)
X-Powered-By: PHP/7.1.7
Content-Length: 151
Content-Type: application/json

:

Next, I tried ApacheBench.

ab -c 1 -n 1 http://localhost:8080/

Very very slow.

:
Requests per second:    0.20 [#/sec] (mean)
Time per request:       5021.594 [ms] (mean)
Time per request:       5021.594 [ms] (mean, across all concurrent requests)
:

ApacheBench expect active close of server, but server does not close until KeepAliveTimeout seconds.


This problem can solved by fix response protocol version based on request protocol version.

// pipeline.php

use Psr\Http\Message\ServerRequestInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;

$app->pipe(function (ServerRequestInterface $request, DelegateInterface $delegate) {
    return $delegate->process($request)->withProtocolVersion($request->getProtocolVersion());
});

ngyuki avatar Jul 30 '17 05:07 ngyuki

I can confirm the slow speed for the php:apache container. Using the php internal server or a nginx container I didn't encounter this issue. Also forcing a http 1.0 return does fix the slow apache bench speed.

curl request is HTTP/1.0, but zend-expressive has HTTP/1.1 response.

This is interesting though. I didn't know this and searched for it.

RFC 2616: Applications that are at least conditionally compliant with this specification SHOULD use an HTTP-Version of "HTTP/1.1" in their messages, and MUST do so for any message that is not compatible with HTTP/1.0. For more details on when to send specific HTTP-Version values, see RFC 2145.

RFC 2145 An HTTP server SHOULD send a response version equal to the highest version for which the server is at least conditionally compliant, and whose major version is less than or equal to the one received in the request. An HTTP server MUST NOT send a version for which it is not at least conditionally compliant. A server MAY send a 505 (HTTP Version Not Supported) response if cannot send a response using the major version used in the client's request.

An HTTP server MAY send a lower response version, if it is known or suspected that the client incorrectly implements the HTTP specification, but this should not be the default, and this SHOULD NOT be done if the request version is HTTP/1.1 or greater.

So, if the client sends an HTTP/1.0 request, either an HTTP/1.0 response or HTTP/1.1 is acceptable, but HTTP/1.1 is preferred.

Enabling the HTTP KeepAlive feature for apache bench gave me fast results as with the internal php server and nginx: ab -k -c 1 -n 1 http://localhost:8080/

geerteltink avatar Jul 30 '17 07:07 geerteltink

This repository has been closed and moved to mezzio/mezzio; a new issue has been opened at https://github.com/mezzio/mezzio/issues/13.

weierophinney avatar Dec 31 '19 20:12 weierophinney