function parseCookie($request) { $cookie = http_parse_cookie($request->head['Cookie']); if (isset($cookie->cookies)) { return $cookie->cookies; } else { return array(); } }
/** * Parsing the request (all the magic is here) */ public function emit($event, array $args = []) { if ($event === 'data') { $this->buffer .= $args[0]; if ($this->body === false) { // headers are not already parsed if (false !== strpos($this->buffer, "\r\n\r\n")) { $this->body = true; $this->buffer = explode("\r\n\r\n", $this->buffer, 2); $parts = new Message($this->buffer[0]); $this->method = $parts->getRequestMethod(); foreach ($parts->getHeaders() as $k => $v) { $this->headers[strtolower($k)] = $v; } $this->url = $parts->getRequestUrl(); $this->httpVersion = number_format($parts->getHttpVersion(), 1); $parts = explode('?', $this->url, 2); $this->path = $parts[0]; if (!empty($parts[1])) { parse_str($parts[1], $this->params); parse_str($parts[1], $this->get); } if (!empty($this->headers['cookie'])) { $parts = http_parse_cookie($this->headers['cookie']); $this->cookies = $parts['cookies']; } if (!$this->method !== 'GET' && !empty($this->headers['content-length'])) { $this->body = intval($this->headers['content-length']); if ($this->body === 0) { $this->emit('ready'); } else { $this->buffer = empty($this->buffer[1]) ? '' : $this->buffer[1]; } } else { $this->body = 0; $this->emit('ready'); } } } else { // parsing the request body if (strlen($this->buffer) === $this->body) { parse_str($this->buffer, $this->params); $this->emit('ready'); } } } return parent::emit($event, $args); }
/** * 返回 http_get() 函数所需的请求参数集合。(注: 此方法仅适用于 Pecl HTTP 扩展. cURL 方式请使用 getXXX() 获取参数值.) * * @return array */ function getOptions() { $opts = array('timeout' => $this->_timeout, 'connecttimeout' => $this->_connectTimeout, 'dns_cache_timeout' => $this->_dnsCacheTimeout, 'useragent' => $this->_userAgent, 'headers' => $this->_headers, 'redirect' => 0); if ($this->_cookies) { $opts['encodecookies'] = $this->_encodeCookies; $cks = http_parse_cookie($this->_cookies, 0); $opts['cookies'] = $cks->cookies; } if ($this->_proxyHost) { $opts['proxyhost'] = $this->_proxyHost; } if ($this->_onProgress) { $opts['onprogress'] = $this->_onProgress; } return $opts; }
/** * Create a new HTTP request based on the request object received from React * * @param ReactRequest $reactRequest * @param string $httpBody * * @return Request */ public static function createFromReact(ReactRequest $reactRequest, $httpBody) { $request = new Request(); $request->setReactRequest($reactRequest); $request->setQueryVariables(new Map($reactRequest->getQuery())); $request->setHeaders(new Map($reactRequest->getHeaders())); $parser = new BodyParser($httpBody, $reactRequest->getHeaders()); $request->setBodyParser($parser); $request->setBodyVariables(new Map($parser->getVariables())); $request->setFiles(new Map($parser->getFiles())); $request->setHttpVersion($reactRequest->getHttpVersion()); $request->setMethod(new Method(strtolower($reactRequest->getMethod()))); $request->setRemoteIp($reactRequest->remoteAddress); $request->setPath($reactRequest->getPath()); if ($request->getHeaders()->containsKey('Cookie')) { $cookieData = http_parse_cookie($request->getHeaders()->get('Cookie')->get(), HTTP_COOKIE_PARSE_RAW); $request->setCookies(new Map($cookieData->cookies)); } else { $request->setCookies(new Map()); } return $request; }
protected function getCookies(array $headers) { $cookies = []; if (isset($headers['Cookie'])) { if (function_exists('http_parse_cookie')) { $cookie_data = http_parse_cookie($headers['Cookie']); if ($cookie_data) { $cookies = $cookie_data->cookies; } } else { if (class_exists("\\Guzzle\\Parser\\Cookie\\CookieParser")) { $cookies = array_get(with(new \Guzzle\Parser\Cookie\CookieParser())->parseCookie($headers['Cookie']), 'cookies', []); } else { if (class_exists("\\GuzzleHttp\\Cookie\\SetCookie")) { foreach (\GuzzleHttp\Cookie\SetCookie::fromString($headers['Cookie'])->toArray() as $data) { $cookies[$data['Name']] = $data['Value']; } } } } } return $cookies; }
/** * Parse a cookie string. * * Automatically perform the signature check. * * @param $cookie Cookie string * @param $key Shared key for HMAC signature * @return array Valid cookies in associative array */ public static function parse_cookie($cookie, $key) { $c = \http_parse_cookie($cookie); $cookies = array(); foreach ($c->cookies as $name => $val) { if (strlen($val) > 0) { try { $cookies[$name] = \photon\crypto\Sign::loads($val, $key); } catch (\Exception $e) { // We simply ignore bad cookies. } } } return $cookies; }
<?php $_COOKIE = http_parse_cookie("PHPSESSID=_"); _filter_snapshot_globals(); error_reporting(0); session_start(); var_dump(session_id()); var_dump(basename(__FILE__) == $_POST[ini_get("session.upload_progress.name")]); var_dump($_FILES); var_dump($_SESSION["upload_progress_" . basename(__FILE__)]); session_destroy();
<?php $_COOKIE = http_parse_cookie("GET=10;POST=11;COOKIE=12;FILES=13;REQUEST=14"); _filter_snapshot_globals(); parse_str("GET=0&POST=1&COOKIE=2&FILES=3&REQUEST=4", $_GET); $_REQUEST = array_merge($_REQUEST, $_GET); _filter_snapshot_globals(); parse_str("GET=5&POST=6&COOKIE=7&FILES=8&REQUEST=9", $_POST); $_REQUEST = array_merge($_REQUEST, $_POST); _filter_snapshot_globals(); import_request_variables("gpc", "_"); var_dump($_GET, $_POST, $_COOKIE, $_FILES, $_REQUEST); echo "Done\n";
private final function doHandshake($client, $strHandshake) { $headers = $this->getHeaders($strHandshake); if (isset($headers['Sec-WebSocket-Version']) && $headers['Sec-WebSocket-Version'] == 13) { // Standards compliant (Chrome, Safari) $accept_key = $headers['Sec-WebSocket-Key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; $accept_key = sha1($accept_key, true); $accept_key = base64_encode($accept_key); $upgrade = 'HTTP/1.1 101 Switching Protocols' . "\r\n" . 'Upgrade: websocket' . "\r\n" . 'Connection: Upgrade' . "\r\n" . 'Sec-WebSocket-Accept: ' . $accept_key . "\r\n\r\n"; $client->upgraded = true; $client->version = $headers['Sec-WebSocket-Version']; } else { //hybi-00 (Opera, wtf guys?) $key = md5(pack('N', $this->_doStuffToObtainAnInt32($headers['Sec-WebSocket-Key1'])) . pack('N', $this->_doStuffToObtainAnInt32($headers['Sec-WebSocket-Key2'])) . base64_decode($headers['request_content']), true); $upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" . "Upgrade: WebSocket\r\n" . "Connection: Upgrade\r\n" . "Sec-WebSocket-Origin: " . $headers['Origin'] . "\r\n" . "Sec-WebSocket-Location: ws://" . $headers['Host'] . "/\r\n" . "\r\n" . $key; $client->upgraded = true; $client->version = 'hybi-00'; } $client->headers = $headers; $client->COOKIE = http_parse_cookie($headers['Cookie']); $client->COOKIE = $client->COOKIE->cookies; $client->SESSION = session_save_path() . '/sess_' . $client->COOKIE['PHPSESSID']; if (file_exists($client->SESSION)) { session_start(); session_decode(file_get_contents($client->SESSION)); $client->SESSION = $_SESSION; } socket_write($client->sock, $upgrade, strlen($upgrade)) or die("Could not write output(handshake)\n"); $this->onConnect($client); }
/** * 从文本文件读取 Cookie 会话信息并解析。 * * @param string $cookie_file * @return array */ function doParseCookieFile($cookie_file) { $fp = fopen($cookie_file, 'r'); if ($fp) { $cookie_str = fread($fp, 8192); fclose($fp); $c = http_parse_cookie($cookie_str); if ($c) { return $c->cookies; } } return false; }
// now we've got our request finished... (at first ;-) ) } catch (HttpException $ex) { echo "Urghs... We got an Exception: {$ex}"; return; } // Cookie handling while getting answer = // Header handling. Cookie could be rewritten.. if ($response->getHeader('Set-Cookie')) { $cookies = $response->getHeader('Set-Cookie'); // Wenn mehrere Cookies gegeben, wird es Array. // Sicherstellen, dass Array: if (!is_array($cookies)) { $cookies = array($cookies); } foreach ($cookies as $cookie) { $parsed = http_parse_cookie($cookie); foreach ($parsed->cookies as $name => $value) { #print "SET COOKIE $name to $value\n"; setcookie($name, $value, 0, '/'); } } } // check if we can skip rewriting because we have binary data if (stripos($response->getHeader('Content-Type'), 'image') !== false) { // we have an image -- send response to client... $response->send(); exit; } // Header handling foreach (array('Content-Type') as $name) { $content = $response->getHeader($name);
/** * Test Http functions. */ function test_functions() { http_cache_last_modified(); http_chunked_decode(); http_deflate(); http_inflate(); http_build_cookie(); http_date(); http_get_request_body_stream(); http_get_request_body(); http_get_request_headers(); http_match_etag(); http_match_modified(); http_match_request_header(); http_support(); http_negotiate_charset(); http_negotiate_content_type(); http_negotiate_language(); ob_deflatehandler(); ob_etaghandler(); ob_inflatehandler(); http_parse_cookie(); http_parse_headers(); http_parse_message(); http_parse_params(); http_persistent_handles_clean(); http_persistent_handles_count(); http_persistent_handles_ident(); http_get(); http_head(); http_post_data(); http_post_fields(); http_put_data(); http_put_file(); http_put_stream(); http_request_body_encode(); http_request_method_exists(); http_request_method_name(); http_request_method_register(); http_request_method_unregister(); http_request(); http_redirect(); http_send_content_disposition(); http_send_content_type(); http_send_data(); http_send_file(); http_send_last_modified(); http_send_status(); http_send_stream(); http_throttle(); http_build_str(); http_build_url(); }
function cookie_strings_to_array($cookie_strings) { $cookies = array(); foreach ($cookie_strings as $cookie) { $cookie_obj = http_parse_cookie($cookie); foreach ($cookie_obj->cookies as $k => $v) { $cookies[$k] = $v; } } return $cookies; }
/** * cookie string转数组 * @param $cookieStr * @return string * */ protected function cookieStr2Arr($cookieStr) { if (function_exists('http_parse_cookie')) { return http_parse_cookie($cookieStr); } else { if (!$cookieStr) { return []; } $cookieArr = explode(';', $cookieStr); $cookieData = []; foreach ($cookieArr as $cookie) { list($cKey, $cVal) = explode('=', $cookie); $cookieData[$cKey] = $cVal; } return $cookieData; } }
<?php $_COOKIE = http_parse_cookie("1=10;2=11;3=12;4=13;5=14"); _filter_snapshot_globals(); parse_str("1=0&2=1&3=2&4=3&5=4", $_GET); $_REQUEST = array_merge($_REQUEST, $_GET); _filter_snapshot_globals(); parse_str("1=5&2=6&3=7&4=8&5=9", $_POST); $_REQUEST = array_merge($_REQUEST, $_POST); _filter_snapshot_globals(); import_request_variables("gpc", "_"); var_dump($_1, $_2, $_3, $_4, $_5); echo "Done\n";
<?php $_COOKIE = http_parse_cookie("cookie1=val1 ; cookie2=val2%20; cookie3=val 3.; cookie 4= value 4 %3B; cookie1=bogus; %20cookie1=ignore;+cookie1=ignore;cookie1;cookie 5=%20 value; cookie%206=���;cookie+7=;{$cookie}.8;cookie-9=1;;;- & % {$cookie} 10=10"); _filter_snapshot_globals(); var_dump($_COOKIE);
/** * Executes an HTTP request to the specified URI, throws a CouchException if an error is detected in the response. * * @access protected * @param array $data The JSON data to be included in the request (default: null) * @param const $method The PECL_HTTP constant defining the HTTP Method (ie. POST, GET, etc.) to be used (default: HTTP_METH_GET) * @param array $options Array of additional options (including additional headers) to be sent with the HTTP request * @return array The json_decoded response received */ protected function _execute($method = HTTP_METH_GET, $data = null, $append_path = null, $append_query = null, $options = null, $raw_data = false, $json_response = true) { $info = array(); $defaults = array('cookies' => $_COOKIE); if ($json_response) { $defaults['headers'] = array('Content-Type' => 'application/json', 'Accept' => 'application/json'); } if (isset($this->_auth)) { $defaults['httpauth'] = $this->_auth; } $options = isset($options) ? array_merge($defaults, $options) : $defaults; $this->_setURI(); $uri = isset($append_path) || isset($append_query) ? $this->_buildURI($append_path, $append_query) : $this->_uri; $data = $raw_data ? http_build_query($data) : json_encode($data); $output = http_parse_message(http_request($method, $uri, $data, $options, $info)); if ($this->debug) { echo '<pre>'; echo 'Output: ' . htmlspecialchars(print_r($output, true)); echo 'HTTP Info: ' . htmlspecialchars(print_r($info, true)); echo 'Options: ' . htmlspecialchars(print_r($options, true)); echo 'Data: ' . htmlspecialchars(print_r($data, true)); echo '</pre>'; } if (isset($output->headers['Set-Cookie'])) { $oCookie = http_parse_cookie($output->headers['Set-Cookie']); setcookie('AuthSession', $oCookie->cookies['AuthSession'], time() + 600, $oCookie->path); } $output = $json_response ? json_decode($output->body, true) : $output->body; if (is_array($output) && isset($output['error'])) { throw new CouchException($output['error'], $output['reason'], $info['response_code'], $uri); } return $output; }
<?php $_COOKIE = http_parse_cookie("PHPSESSID=rfc1867-tests"); _filter_snapshot_globals(); parse_str("PHPSESSID=rfc1867-tests-get", $_GET); $_REQUEST = array_merge($_REQUEST, $_GET); _filter_snapshot_globals(); session_start(); var_dump(session_id()); var_dump($_FILES); var_dump($_SESSION["upload_progress_" . basename(__FILE__)]); session_destroy();
/** * getCookie * * @method getCookie * @param {boolean} $parse * @return {stdClass or false or string} */ public static function getCookie($parse = true) { return $parse ? http_parse_cookie($_SERVER['HTTP_COOKIE']) : $_SERVER['HTTP_COOKIE']; }