# Ask php cli to parse HTTP request

- Thread: https://externals.io/message/96914
- Original archive: https://news-web.php.net/php.internals/96914
- Started by Aaron Lewis on 2016-11-16T10:50:40+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #96914 — Aaron Lewis — 2016-11-16T10:50:40+00:00

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

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

---

## #96915 — Joe Watkins — 2016-11-16T11:08:26+00:00

URL: https://externals.io/message/96914#96915 · In reply to: #96914

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

---

## #96917 — Aaron Lewis — 2016-11-16T11:38:24+00:00

URL: https://externals.io/message/96914#96917 · In reply to: #96915

I'm referring to PHP-FPM, what about that?


> 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:
> > […]
>



-- 
Best Regards,
Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
Finger Print:   9F67 391B B770 8FF6 99DC  D92D 87F6 2602 1371 4D33

---

## #96918 — Marco Pivetta — 2016-11-16T12:43:59+00:00

URL: https://externals.io/message/96914#96918 · In reply to: #96914

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.



> 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

---

## #96925 — Aaron Lewis — 2016-11-16T15:00:49+00:00

URL: https://externals.io/message/96914#96925 · In reply to: #96918

Thanks Marco.

But I'm looking for the C implementation ..


> 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.
>
>
> 
> > […]
>



-- 
Best Regards,
Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
Finger Print:   9F67 391B B770 8FF6 99DC  D92D 87F6 2602 1371 4D33

---

## #96927 — Marco Pivetta — 2016-11-16T15:18:24+00:00

URL: https://externals.io/message/96914#96927 · In reply to: #96925

One question then: why?

Working userland impl, just use it.



> Thanks Marco.
>
> But I'm looking for the C implementation ..
>
> 
> > […]
> psr7/blob/master/README.md?
> > […]
>
>
>
> --
> Best Regards,
> Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
> Finger Print:   9F67 391B B770 8FF6 99DC  D92D 87F6 2602 1371 4D33

---

## #96931 — Thomas Hruska — 2016-11-16T16:32:38+00:00

URL: https://externals.io/message/96914#96931 · In reply to: #96914

> 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/
