Ask php cli to parse HTTP request

php.internals

Aaron Lewis

9 years ago
I have a file that contains a HTTP request, ``` GET /xxx.php Host: xxx Content-Type: xxx ... ``` I would like to ask PHP cli to parse the HTTP request from a file, and setup $_FILES, $_POST, $_SERVER etc. What should I do? I'm familiar with PHP extensions, so I'm capable of modifying SAPI myself. So far I've found sapi_post_entry, but I couldn't get the whole picture. Where's the entry point of request parsing?
-- Best Regards, Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/ Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33

Joe Watkins

9 years ago
Morning, Ordinarily, we don't parse that stuff, because the server communicates via CGI/FCGI or some other server specific interface (apache). The CLI server does though, I'd look there for inspiration ... there's also an HTTP parser included in there ... Cheers Joe On Wed, Nov 16, 2016 at 10:50 AM, Aaron Lewis <the.warl0ck.1989@gmail.com> wrote:

Aaron Lewis

9 years ago
I'm referring to PHP-FPM, what about that? On Wed, Nov 16, 2016 at 7:08 PM, Joe Watkins <pthreads@pthreads.org> wrote:
> Morning, > > Ordinarily, we don't parse that stuff, because the server communicates via > CGI/FCGI or some other server specific interface (apache). > > The CLI server does though, I'd look there for inspiration ... there's also > an HTTP parser included in there ... > > Cheers > Joe > > On Wed, Nov 16, 2016 at 10:50 AM, Aaron Lewis <the.warl0ck.1989@gmail.com> > wrote: >> >> I have a file that contains a HTTP request, >> >> ``` >> GET /xxx.php >> Host: xxx >> Content-Type: xxx >> ... >> ``` >> >> I would like to ask PHP cli to parse the HTTP request from a file, and >> setup $_FILES, $_POST, $_SERVER etc. >> What should I do? I'm familiar with PHP extensions, so I'm capable of >> modifying SAPI myself. >> >> So far I've found sapi_post_entry, but I couldn't get the whole >> picture. Where's the entry point of request parsing? >> >> >> -- >> Best Regards, >> Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/ >> Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33 >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >
-- Best Regards, Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/ Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33

Marco Pivetta

9 years ago
Maybe something like https://github.com/guzzle/psr7/blob/master/README.md? There are additional middlewares that can extract from a request and populate super-globals for legacy app support purposes. On 16 Nov 2016 11:50, "Aaron Lewis" <the.warl0ck.1989@gmail.com> wrote:

Aaron Lewis

9 years ago
Thanks Marco. But I'm looking for the C implementation .. On Wed, Nov 16, 2016 at 8:43 PM, Marco Pivetta <ocramius@gmail.com> wrote:
> Maybe something like https://github.com/guzzle/psr7/blob/master/README.md? > There are additional middlewares that can extract from a request and > populate super-globals for legacy app support purposes. > > > On 16 Nov 2016 11:50, "Aaron Lewis" <the.warl0ck.1989@gmail.com> wrote: >> >> I have a file that contains a HTTP request, >> >> ``` >> GET /xxx.php >> Host: xxx >> Content-Type: xxx >> ... >> ``` >> >> I would like to ask PHP cli to parse the HTTP request from a file, and >> setup $_FILES, $_POST, $_SERVER etc. >> What should I do? I'm familiar with PHP extensions, so I'm capable of >> modifying SAPI myself. >> >> So far I've found sapi_post_entry, but I couldn't get the whole >> picture. Where's the entry point of request parsing? >> >> >> -- >> Best Regards, >> Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/ >> Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33 >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >
-- Best Regards, Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/ Finger Print: 9F67 391B B770 8FF6 99DC D92D 87F6 2602 1371 4D33

Marco Pivetta

9 years ago
One question then: why? Working userland impl, just use it. On 16 Nov 2016 16:01, "Aaron Lewis" <the.warl0ck.1989@gmail.com> wrote:

Thomas Hruska

9 years ago
On 11/16/2016 3:50 AM, Aaron Lewis wrote:
> I have a file that contains a HTTP request, > > ``` > GET /xxx.php > Host: xxx > Content-Type: xxx > ... > ``` > > I would like to ask PHP cli to parse the HTTP request from a file, and > setup $_FILES, $_POST, $_SERVER etc. > What should I do? I'm familiar with PHP extensions, so I'm capable of > modifying SAPI myself.
The Ultimate Web Scraper Toolkit over on GitHub can get you about 75% of the way there in pure userland in a few lines of application code with the WebServer class: https://github.com/cubiclesoft/ultimate-web-scraper/ Possible approach: $fp = fopen("/path/to/file.txt", "rb"); stream_set_blocking($fp, 0); $server = new WebServer(); // If you want to allow gzipped/deflated input. //$server->EnableCompression(true); // If you are actually processing file input... //$server->SetCacheDir("/path/to/cache/"); $client = $server->InitNewClient(); $client->fp = $fp; do { $result = $server->Wait(); if (count($result["removed"])) { // File contains an invalid request. // Maybe bail out more gently? echo "Invalid request.\n"; exit(); } } while (!$client->requestcomplete); // Now use $client's public variables 'cookievars' and 'requestvars' to load various superglobals. There isn't a pure structure though for $_GET and $_POST. ... is_uploaded_file(), move_uploaded_file(), sesssions, and other useful built-ins might also not work as expected. Hence the 75% metric from earlier.
-- Thomas Hruska CubicleSoft President I've got great, time saving software that you will find useful. http://cubiclesoft.com/