# PHP build-in HTTP server and the HEAD method

- Thread: https://externals.io/message/62952
- Original archive: https://news-web.php.net/php.internals/62952
- Started by Ivan Enderlin @ Hoa on 2012-09-12T08:54:53+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #62952 — Ivan Enderlin @ Hoa — 2012-09-12T08:54:53+00:00

URL: https://externals.io/message/62952#62952

Hello,

It is probably me but it seems like the build-in HTTP server does not 
well support the HEAD method. Here is my following test case. First, the 
foo.php file:

    <?php

    var_dump($_SERVER['REQUEST_METHOD']);


And then:

    $ # Run the server.
    $ php -S 127.0.0.1:8888 -t . foo.php > /dev/null 2>&1 &

    $ # Test with POST.
    $ curl -v -X POST 127.0.0.1:8888
    * About to connect() to 127.0.0.1 port 8888 (#0)
    *   Trying 127.0.0.1...
    * connected
    * Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
     > POST / HTTP/1.1
     > User-Agent: curl/7.24.0 (...) libcurl/7.24.0 OpenSSL/0.9.8r
    zlib/1.2.5
     > Host: 127.0.0.1:8888
     > Accept: */*
     >
    < HTTP/1.1 200 OK
    < Host: 127.0.0.1:8888
    < Connection: close
    < X-Powered-By: PHP/5.5.0-dev
    < Content-type: text/html
    <
    string(4) "POST"
    * Closing connection #0
    $ # It works, cool.

    $ # Now, test with HEAD.
    $ curl -v -X HEAD 127.0.0.1:8888
    * About to connect() to 127.0.0.1 port 8888 (#0)
    *   Trying 127.0.0.1...
    * connected
    * Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
     > HEAD / HTTP/1.1
     > User-Agent: curl/7.24.0 (...) libcurl/7.24.0 OpenSSL/0.9.8r
    zlib/1.2.5
     > Host: 127.0.0.1:8888
     > Accept: */*
     >
    < HTTP/1.1 200 OK
    < Host: 127.0.0.1:8888
    < Connection: close
    < X-Powered-By: PHP/5.5.0-dev
    < Content-type: text/html
    <
    * Closing connection #0


I have to admit that I don't understand, even by looking at the source code.

Thoughts?
Thanks.

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #62953 — Damien Tournoud — 2012-09-12T09:15:38+00:00

URL: https://externals.io/message/62952#62953 · In reply to: #62952

On Wed, Sep 12, 2012 at 10:54 AM, Ivan Enderlin @ Hoa
<ivan.enderlin@hoa-project.net> wrote:
> Hello,
>
> It is probably me but it seems like the build-in HTTP server does not well
> support the HEAD method. Here is my following test case. First, the foo.php
> file:
>
>    <?php
>
>    var_dump($_SERVER['REQUEST_METHOD']);

By definition, a HEAD request MUST NOT return a message-body.

This works as expected:

<?php
header("X-Request-Method: " . $_SERVER["REQUEST_METHOD"]);
?>

Here is how it works: main/SAPI.c has code that set
SG(request_info).headers_only = 1 for HEAD requests, and as a
consequence php_request_shutdown() does not flush the output buffer
(if any) to the client. You can force a body to be sent (but that's
likely not going to work with every SAPI) by not having an output
buffer or by calling `ob_end_flush()` yourself.

Damien

---

## #62955 — Sebastian Krebs — 2012-09-12T09:20:54+00:00

URL: https://externals.io/message/62952#62955 · In reply to: #62952

Hi,

As far as I can see everything works as expected: Because HEAD-requests
should not send any content, you don't get any.

Regards,
Sebastian

2012/9/12 Ivan Enderlin @ Hoa <ivan.enderlin@hoa-project.net>

> Hello,
>
> It is probably me but it seems like the build-in HTTP server does not well
> support the HEAD method. Here is my following test case. First, the foo.php
> file:
>
>    <?php
>
>    var_dump($_SERVER['REQUEST_**METHOD']);
>
>
> And then:
>
>    $ # Run the server.
>    $ php -S 127.0.0.1:8888 -t . foo.php > /dev/null 2>&1 &
>
>    $ # Test with POST.
>    $ curl -v -X POST 127.0.0.1:8888
>    * About to connect() to 127.0.0.1 port 8888 (#0)
>    *   Trying 127.0.0.1...
>    * connected
>    * Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> > […]
>    zlib/1.2.5
> > […]
>    < HTTP/1.1 200 OK
>    < Host: 127.0.0.1:8888
>    < Connection: close
>    < X-Powered-By: PHP/5.5.0-dev
>    < Content-type: text/html
>    <
>    string(4) "POST"
>    * Closing connection #0
>    $ # It works, cool.
>
>    $ # Now, test with HEAD.
>    $ curl -v -X HEAD 127.0.0.1:8888
>    * About to connect() to 127.0.0.1 port 8888 (#0)
>    *   Trying 127.0.0.1...
>    * connected
>    * Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> > […]
>    zlib/1.2.5
> > […]
>    < HTTP/1.1 200 OK
>    < Host: 127.0.0.1:8888
>    < Connection: close
>    < X-Powered-By: PHP/5.5.0-dev
>    < Content-type: text/html
>    <
>    * Closing connection #0
>
>
> I have to admit that I don't understand, even by looking at the source
> code.
>
> Thoughts?
> Thanks.
>
> --
> Ivan Enderlin
> Developer of Hoa
> http://hoa.42/ or http://hoa-project.net/
>
> PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
> http://disc.univ-fcomte.fr/ and http://www.inria.fr/
>
> Member of HTML and WebApps Working Group of W3C
> http://w3.org/
>
>


-- 
github.com/KingCrunch

---

## #62956 — Ivan Enderlin @ Hoa — 2012-09-12T11:44:45+00:00

URL: https://externals.io/message/62952#62956 · In reply to: #62953

> On Wed, Sep 12, 2012 at 10:54 AM, Ivan Enderlin @ Hoa
> <ivan.enderlin@hoa-project.net> wrote:
> > […]
> By definition, a HEAD request MUST NOT return a message-body.
Oh grrr, I feel stupid now ;-). I forget this.

> This works as expected:
>
> <?php
> header("X-Request-Method: " . $_SERVER["REQUEST_METHOD"]);
> ?>
>
> Here is how it works: main/SAPI.c has code that set
> SG(request_info).headers_only = 1 for HEAD requests, and as a
> consequence php_request_shutdown() does not flush the output buffer
> (if any) to the client.
Ok.

> You can force a body to be sent (but that's
> likely not going to work with every SAPI) by not having an output
> buffer or by calling `ob_end_flush()` yourself.
Yes but that was not my goal.

Thank you for your reply :-).

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #62957 — Ivan Enderlin @ Hoa — 2012-09-12T11:45:27+00:00

URL: https://externals.io/message/62952#62957 · In reply to: #62955

> Hi,
Hi,

>
> As far as I can see everything works as expected: Because HEAD-requests
> should not send any content, you don't get any.
Yup, as Damien said.

Thank you.

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/
