Example #1
0
 function read_requests($client, $options = [])
 {
     $options['server'] = stream_socket_get_name($client, true);
     $dt = CY_Util_Stream::http_read($client, $options);
     if ($dt['errno'] !== 0) {
         return $dt;
     }
     if (empty($dt['data'])) {
         return array('errno' => CYE_DATA_EMPTY);
     }
     $o = new http\Message($dt['data']);
     $this->version = $o->getHttpVersion();
     $this->method = $o->getRequestMethod();
     $headers = $o->getHeaders();
     if (!empty($headers['Content-Length'])) {
         if (isset($headers['Expect'])) {
             return array('errno' => CYE_EXPECT_FAIL);
         }
         if (empty($o->getBody())) {
             mp_log(CYE_ERROR, "Bad req:" . $options['server'] . " " . str_replace("\r\n", "\\r\\n", $dt['data']));
         }
     }
     $this->keepalive = isset($headers['Connection']) && strcasecmp($headers['Connection'], 'keep-alive') == 0;
     $this->compress = isset($headers['Accept-Encoding']) && strpos($headers['Accept-Encoding'], 'gzip') !== false;
     if (empty($headers['Host'])) {
         return array('errno' => CYE_ACCESS_DENIED);
     }
     $parts = parse_url('http://' . $headers['Host'] . $o->getRequestUrl());
     $_SERVER['REQUEST_URI'] = $o->getRequestUrl();
     $_SERVER['QUERY_STRING'] = '';
     if (!empty($parts['query'])) {
         $_SERVER['QUERY_STRING'] = $query = $parts['query'];
         parse_str($query, $_GET);
     }
     if (!empty($o->getBody())) {
         if (isset($headers['Content-Type']) && strpos($headers['Content-Type'], 'multipart/form-data') !== false) {
             // grab multipart boundary from content type header
             preg_match('/boundary=(.*)$/', $headers['Content-Type'], $matches);
             // content type is probably regular form-encoded
             if (count($matches)) {
                 $boundary = $matches[1];
                 $_POST = cy_parse_http_multipart($o->getBody(), $boundary);
             }
         }
         if (!isset($boundary)) {
             parse_str($o->getBody(), $_POST);
         }
     }
     if (isset($headers['Cookie'])) {
         $c = new http\Cookie($headers['Cookie']);
         $_COOKIE = $c->getCookies();
     }
     $_REQUEST = array_merge($_GET, $_POST, $_COOKIE);
     return array('errno' => 0);
 }
<?php

$string = 'GET /2/trackcorrect/2/get_alltrps_byPid?pid=389&_=1436754087347 HTTP/1.1
Host: 10.183.0.90:8088
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://10.183.0.99
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36
Referer: http://10.183.0.99/src/trpjudge.html?pid=389&trpid=11783&sid=0
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8

 ';
$o = new http\Message($string);
$headers = $o->getHeaders();
print_r($o);