65536) { fclose($client); continue 2; } $request .= $chunk; } list($headers, $body) = explode("\r\n\r\n", $request, 2); $length = preg_match('/^Content-Length:\s*(\d+)/mi', $headers, $lengthMatch) ? (int)$lengthMatch[1] : 0; if (stripos($headers, 'Expect: 100-continue') !== false) { fwrite($client, "HTTP/1.1 100 Continue\r\n\r\n"); } while (strlen($body) < $length) { $chunk = fread($client, min(4096, $length - strlen($body))); if ($chunk === false || $chunk === '') { fclose($client); continue 2; } $body .= $chunk; } // 简单解析请求头 $headerLines = explode("\r\n", $headers); $requestLine = array_shift($headerLines); preg_match('/\btimeout:\s*true\b/i', $headers, $timeoutMatch); preg_match('/\b(bodytype):[ ]*([\w\d]+)\b/i', $headers, $bodyTypeMatch); $bodyType = isset($bodyTypeMatch[2]) ? $bodyTypeMatch[2] : null; preg_match('/^(GET|POST|PUT|DELETE)\s(\/\S*)\sHTTP\/1.1/', $requestLine, $matches); if (count($matches) < 3) { fclose($client); continue; } $method = $matches[1]; $path = $matches[2]; $headerAssoc = []; foreach ($headerLines as $header) { $pair = explode(':', $header, 2); if (count($pair) === 2) { $headerAssoc[strtolower($pair[0])] = trim($pair[1]); } } if (substr($path, 0, 4) === '/sse') { $responseHeaders = "HTTP/1.1 200 OK\r\n" . "Content-Type: text/event-stream;charset=UTF-8\r\n" . "Cache-Control: no-cache\r\n" . "Connection: close\r\n"; foreach ($headerAssoc as $name => $value) { if (in_array($name, array('content-type', 'content-length', 'transfer-encoding', 'connection', 'expect'), true)) { continue; } $responseHeaders .= $name . ": " . $value . "\r\n"; } $responseHeaders .= "\r\n"; fwrite($client, $responseHeaders); // 刷新缓冲区,确保头部被立刻发送 fflush($client); // 模拟发送事件流 $count = 0; while ($count < 5) { $data = [ "count" => $count ]; $sseData = "id: sse-test\n"; $sseData .= "event: flow\n"; $sseData .= "data: " . json_encode($data) . "\n"; $sseData .= "retry: 3000\n\n"; // 重试时间可选 fwrite($client, $sseData); // 再次刷新缓冲区,以确保数据被发送 fflush($client); // 等待100毫秒 usleep(100000); $count++; } fclose($client); continue; } if ($timeoutMatch) { // 模拟超时 sleep(5); $responseHeaders = "HTTP/1.1 500 Internal Server Error\r\n" . "Content-Type: text/plain\r\n" . "Connection: close\r\n\r\n"; fwrite($client, $responseHeaders . "Server Timeout"); } else { // 获取路径和请求方法 preg_match('/^(GET|POST|PUT|DELETE)\s(\/\S*)\sHTTP\/1.1/', $requestLine, $matches); $method = $matches[1]; $path = $matches[2]; // 构建响应头 $responseHeaders = "HTTP/1.1 200 OK\r\n" . "Connection: close\r\n" . "Content-Type: application/json\r\n" . "x-acs-request-id: A45EE076-334D-5012-9746-A8F828D20FD4\r\n" . "http-method: $method\r\n" . "pathname: $path\r\n" . "raw-body: $body\r\n"; // 构建响应体 $responseBody = ""; echo $bodyType . "\n"; if ($path === '/empty') { $responseHeaders = "HTTP/1.1 204 No Content\r\n" . "Connection: close\r\n" . "x-acs-request-id: A45EE076-334D-5012-9746-A8F828D20FD4\r\n" . "http-method: $method\r\n" . "pathname: $path\r\n\r\n"; fwrite($client, $responseHeaders); fclose($client); continue; } switch ($bodyType) { case 'array': $responseBody = json_encode(["AppId", "ClassId", "UserId"]); break; case 'xml': $responseHeaders = "HTTP/1.1 400 Bad Request\r\n" . "Connection: close\r\n" . "Content-Type: text/xml;charset=utf-8\r\n" . "x-acs-request-id: A45EE076-334D-5012-9746-A8F828D20FD4\r\n" . "http-method: $method\r\n" . "pathname: $path\r\n"; $responseBody = "InvalidAccessKeyIdxml errorxml-request-id"; break; case 'serverError': $responseHeaders = "HTTP/1.1 500 Internal Server Error\r\n" . "Connection: close\r\n" . "Content-Type: application/json\r\n" . "x-acs-request-id: A45EE076-334D-5012-9746-A8F828D20FD4\r\n" . "http-method: $method\r\n" . "pathname: $path\r\n"; $responseBody = json_encode([ "Code" => "InternalError", "Message" => "server boom", "RequestId" => "A45EE076-334D-5012-9746-A8F828D20FD4", "Description" => "desc", "Detail" => "detail" ]); break; case 'error': $responseHeaders = "HTTP/1.1 400 Bad Request\r\n" . "Connection: close\r\n" . "Content-Type: application/json\r\n" . "x-acs-request-id: A45EE076-334D-5012-9746-A8F828D20FD4\r\n" . "http-method: $method\r\n" . "pathname: $path\r\n"; $responseBody = json_encode([ "Code" => "error code", "Message" => "error message", "RequestId" => "A45EE076-334D-5012-9746-A8F828D20FD4", "Description" => "error description", "AccessDeniedDetail" => new stdClass() ]); break; case 'error1': $responseHeaders = "HTTP/1.1 400 Bad Request\r\n" . "Connection: close\r\n" . "Content-Type: application/json\r\n" . "x-acs-request-id: A45EE076-334D-5012-9746-A8F828D20FD4\r\n" . "http-method: $method\r\n" . "pathname: $path\r\n"; $responseBody = json_encode([ "Code" => "error code", "Message" => "error message", "RequestId" => "A45EE076-334D-5012-9746-A8F828D20FD4", "Description" => "error description", "AccessDeniedDetail" => new stdClass(), "accessDeniedDetail" => ["test" => 0] ]); break; case 'error2': $responseHeaders = "HTTP/1.1 400 Bad Request\r\n" . "Connection: close\r\n" . "Content-Type: application/json\r\n" . "x-acs-request-id: A45EE076-334D-5012-9746-A8F828D20FD4\r\n" . "http-method: $method\r\n" . "pathname: $path\r\n"; $responseBody = json_encode([ "Code" => "error code", "Message" => "error message", "RequestId" => "A45EE076-334D-5012-9746-A8F828D20FD4", "Description" => "error description", "accessDeniedDetail" => ["test" => 0] ]); break; default: $responseBody = json_encode([ "AppId" => "test", "ClassId" => "test", "UserId" => 123 ]); } fwrite($client, $responseHeaders . "\r\n" . $responseBody); } fclose($client); continue; } fclose($server);