Skip to content

Commit

Permalink
protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
李亮 authored and 李亮 committed Jun 5, 2017
1 parent 7e776d3 commit 1c4e11d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
37 changes: 27 additions & 10 deletions Protocols/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function input($recv_buffer, TcpConnection $connection)
*/
protected static function getRequestSize($header, $method)
{
if($method=='GET') {
if($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD') {
return strlen($header) + 4;
}
$match = array();
Expand Down Expand Up @@ -108,6 +108,7 @@ public static function decode($recv_buffer, TcpConnection $connection)
'HTTP_CONNECTION' => '',
'REMOTE_ADDR' => '',
'REMOTE_PORT' => '0',
'REQUEST_TIME' => time()
);

// Parse headers.
Expand Down Expand Up @@ -158,21 +159,30 @@ public static function decode($recv_buffer, TcpConnection $connection)

// Parse $_POST.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] === 'multipart/form-data') {
self::parseUploadFiles($http_body, $http_post_boundary);
if (isset($_SERVER['CONTENT_TYPE'])) {
switch ($_SERVER['CONTENT_TYPE']) {
case 'multipart/form-data':
self::parseUploadFiles($http_body, $http_post_boundary);
break;
case 'application/x-www-form-urlencoded':
parse_str($http_body, $_POST);
break;
default:
// $GLOBALS['HTTP_RAW_POST_DATA']
$GLOBALS['HTTP_RAW_REQUEST_DATA'] = $GLOBALS['HTTP_RAW_POST_DATA'] = $http_body;
}
} else {
parse_str($http_body, $_POST);
// $GLOBALS['HTTP_RAW_POST_DATA']
$GLOBALS['HTTP_RAW_REQUEST_DATA'] = $GLOBALS['HTTP_RAW_POST_DATA'] = $http_body;
}
}

if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$GLOBALS['HTTP_RAW_REQUEST_DATA'] = $http_body;
$GLOBALS['HTTP_RAW_REQUEST_DATA'] = $http_body;
}

if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
$GLOBALS['HTTP_RAW_REQUEST_DATA'] = $http_body;
$GLOBALS['HTTP_RAW_REQUEST_DATA'] = $http_body;
}

// QUERY_STRING
Expand Down Expand Up @@ -437,16 +447,19 @@ protected static function parseUploadFiles($http_body, $http_post_boundary)
list($boundary_header_buffer, $boundary_value) = explode("\r\n\r\n", $boundary_data_buffer, 2);
// Remove \r\n from the end of buffer.
$boundary_value = substr($boundary_value, 0, -2);
$key = -1;
foreach (explode("\r\n", $boundary_header_buffer) as $item) {
list($header_key, $header_value) = explode(": ", $item);
$header_key = strtolower($header_key);
switch ($header_key) {
case "content-disposition":
$key ++;
// Is file data.
if (preg_match('/name=".*?"; filename="(.*?)"$/', $header_value, $match)) {
if (preg_match('/name="(.*?)"; filename="(.*?)"$/', $header_value, $match)) {
// Parse $_FILES.
$_FILES[] = array(
'file_name' => $match[1],
$_FILES[$key] = array(
'name' => $match[1],
'file_name' => $match[2],
'file_data' => $boundary_value,
'file_size' => strlen($boundary_value),
);
Expand All @@ -459,6 +472,10 @@ protected static function parseUploadFiles($http_body, $http_post_boundary)
}
}
break;
case "content-type":
// add file_type
$_FILES[$key]['file_type'] = trim($header_value);
break;
}
}
}
Expand Down Expand Up @@ -553,7 +570,7 @@ class HttpCache
public static function init()
{
self::$sessionName = ini_get('session.name');
self::$sessionPath = session_save_path();
self::$sessionPath = @session_save_path();
if (!self::$sessionPath || strpos(self::$sessionPath, 'tcp://') === 0) {
self::$sessionPath = sys_get_temp_dir();
}
Expand Down
2 changes: 1 addition & 1 deletion Protocols/Ws.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public static function encode($payload, $connection)
*/
public static function decode($bytes, $connection)
{
$masked = $bytes[1] >> 7;
$masked = ord($bytes[1]) >> 7;
$data_length = $masked ? ord($bytes[1]) & 127 : ord($bytes[1]);
$decoded_data = '';
if ($masked === true) {
Expand Down

0 comments on commit 1c4e11d

Please sign in to comment.