mezzio / mezzio

PSR-15 Middleware Microframework
https://docs.mezzio.dev/mezzio/
BSD 3-Clause "New" or "Revised" License
347 stars 53 forks source link

HTTP/1.1 response to the Client of 1.0 #13

Open weierophinney opened 4 years ago

weierophinney commented 4 years ago
# 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());
});

Originally posted by @ngyuki at https://github.com/zendframework/zend-expressive/issues/502

weierophinney commented 4 years ago

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/


Originally posted by @geerteltink at https://github.com/zendframework/zend-expressive/issues/502#issuecomment-318885485