Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96931 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 59007 invoked from network); 16 Nov 2016 16:32:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Nov 2016 16:32:57 -0000 Authentication-Results: pb1.pair.com smtp.mail=thruska@cubiclesoft.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=thruska@cubiclesoft.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain cubiclesoft.com designates 149.56.142.28 as permitted sender) X-PHP-List-Original-Sender: thruska@cubiclesoft.com X-Host-Fingerprint: 149.56.142.28 28.ip-149-56-142.net Received: from [149.56.142.28] ([149.56.142.28:54068] helo=28.ip-149-56-142.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F1/04-05303-B2A8C285 for ; Wed, 16 Nov 2016 11:32:46 -0500 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: thruska@cubiclesoft.com) with ESMTPSA id 7B0483E897 To: Aaron Lewis , PHP Internals References: Message-ID: <43873d88-fdcc-b63a-2302-428871b01b55@cubiclesoft.com> Date: Wed, 16 Nov 2016 09:32:38 -0700 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Ask php cli to parse HTTP request From: thruska@cubiclesoft.com (Thomas Hruska) 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/