Created
September 2, 2016 11:08
-
-
Save Moln/3e2b08f58cd0ba706436cd55bdf09598 to your computer and use it in GitHub Desktop.
swoole-to-psr7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Zend\Diactoros\ServerRequest; | |
| use Zend\Diactoros\ServerRequestFactory; | |
| use Zend\Http\Request; | |
| $http = new swoole_http_server("0.0.0.0", 9501); | |
| $rootServerConfigs = array( | |
| 'DOCUMENT_ROOT' => __DIR__, | |
| ); | |
| $threadServerConfig = array( | |
| 'REQUEST_TIME_FLOAT' => microtime(true), | |
| 'REQUEST_TIME' => time(), | |
| ); | |
| $cliServer = array ( | |
| 'HTTP_HOST' => 'localhost', | |
| 'HTTP_CONNECTION' => 'keep-alive', | |
| 'HTTP_CACHE_CONTROL' => 'max-age=0', | |
| 'HTTP_UPGRADE_INSECURE_REQUESTS' => '1', | |
| 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36', | |
| 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', | |
| 'HTTP_REFERER' => 'http://localhost/page-cache-middleware/', | |
| 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, sdch', | |
| 'HTTP_ACCEPT_LANGUAGE' => 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,lv;q=0.2', | |
| 'HTTP_COOKIE' => 'PHPSESSID=ik9ev0u5bb99o2op4n24d75213; _ga=GA1.1.1680623679.1468396079', | |
| 'PATH' => 'C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\TortoiseSVN\\bin;D:\\servers\\php-7;D:\\servers\\apache-2.4\\bin;D:\\servers\\mysql-5.6\\bin;C:\\Program Files (x86)\\GtkSharp\\2.12\\bin;D:\\Program Files\\Git\\cmd;D:\\Program Files\\Git\\mingw64\\bin;D:\\Program Files\\Git\\usr\\bin;D:\\Program Files\\TortoiseGit\\bin;C:\\ProgramData\\ComposerSetup\\bin;D:\\Program Files\\nodejs\\;C:\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\;D:\\Python34;D:\\Python34\\Scripts;C:\\Program Files\\Java\\jdk1.8.0_91\\bin;D:\\Go\\bin;', | |
| 'SystemRoot' => 'C:\\Windows', | |
| 'COMSPEC' => 'C:\\Windows\\system32\\cmd.exe', | |
| 'PATHEXT' => '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY', | |
| 'WINDIR' => 'C:\\Windows', | |
| 'SERVER_SIGNATURE' => '', | |
| 'SERVER_SOFTWARE' => 'Apache/2.4.17 (Win64) OpenSSL/1.0.2d PHP/7.0.7', | |
| 'SERVER_NAME' => 'localhost', | |
| 'SERVER_ADDR' => '::1', | |
| 'SERVER_PORT' => '80', | |
| 'REMOTE_ADDR' => '::1', | |
| 'REQUEST_SCHEME' => 'http', | |
| 'CONTEXT_PREFIX' => '', | |
| 'CONTEXT_DOCUMENT_ROOT' => 'D:/www/', | |
| 'SERVER_ADMIN' => 'admin@example.com', | |
| 'REMOTE_PORT' => '61898', | |
| 'GATEWAY_INTERFACE' => 'CGI/1.1', | |
| 'SERVER_PROTOCOL' => 'HTTP/1.1', | |
| 'REQUEST_METHOD' => 'GET', | |
| 'QUERY_STRING' => '', | |
| 'REQUEST_URI' => '/page-cache-middleware/examples/', | |
| // 'REQUEST_TIME_FLOAT' => 1472811538.326, | |
| // 'REQUEST_TIME' => 1472811538, | |
| 'PHP_SELF' => '-', | |
| 'SCRIPT_NAME' => '-', | |
| 'SCRIPT_FILENAME' => '', | |
| 'PATH_TRANSLATED' => '', | |
| 'REQUEST_TIME_FLOAT' => 1472811591.1053269, | |
| 'REQUEST_TIME' => 1472811591, | |
| ); | |
| $server = | |
| array ( | |
| 'HTTP_HOST' => 'localhost', | |
| 'HTTP_CONNECTION' => 'keep-alive', | |
| 'HTTP_CACHE_CONTROL' => 'max-age=0', | |
| 'HTTP_UPGRADE_INSECURE_REQUESTS' => '1', | |
| 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36', | |
| 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', | |
| 'HTTP_REFERER' => 'http://localhost/page-cache-middleware/', | |
| 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, sdch', | |
| 'HTTP_ACCEPT_LANGUAGE' => 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,lv;q=0.2', | |
| 'HTTP_COOKIE' => 'PHPSESSID=ik9ev0u5bb99o2op4n24d75213; _ga=GA1.1.1680623679.1468396079', | |
| 'PATH' => 'C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\TortoiseSVN\\bin;D:\\servers\\php-7;D:\\servers\\apache-2.4\\bin;D:\\servers\\mysql-5.6\\bin;C:\\Program Files (x86)\\GtkSharp\\2.12\\bin;D:\\Program Files\\Git\\cmd;D:\\Program Files\\Git\\mingw64\\bin;D:\\Program Files\\Git\\usr\\bin;D:\\Program Files\\TortoiseGit\\bin;C:\\ProgramData\\ComposerSetup\\bin;D:\\Program Files\\nodejs\\;C:\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\;D:\\Python34;D:\\Python34\\Scripts;C:\\Program Files\\Java\\jdk1.8.0_91\\bin;D:\\Go\\bin;', | |
| 'SystemRoot' => 'C:\\Windows', | |
| 'COMSPEC' => 'C:\\Windows\\system32\\cmd.exe', | |
| 'PATHEXT' => '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY', | |
| 'WINDIR' => 'C:\\Windows', | |
| 'SERVER_SIGNATURE' => '', | |
| 'SERVER_SOFTWARE' => 'Apache/2.4.17 (Win64) OpenSSL/1.0.2d PHP/7.0.7', | |
| 'SERVER_NAME' => 'localhost', | |
| 'SERVER_ADDR' => '::1', | |
| 'SERVER_PORT' => '80', | |
| 'REMOTE_ADDR' => '::1', | |
| 'DOCUMENT_ROOT' => 'D:/www/', | |
| 'REQUEST_SCHEME' => 'http', | |
| 'CONTEXT_PREFIX' => '', | |
| 'CONTEXT_DOCUMENT_ROOT' => 'D:/www/', | |
| 'SERVER_ADMIN' => 'admin@example.com', | |
| 'SCRIPT_FILENAME' => 'D:/www/page-cache-middleware/examples/index.php', | |
| 'REMOTE_PORT' => '61898', | |
| 'GATEWAY_INTERFACE' => 'CGI/1.1', | |
| 'SERVER_PROTOCOL' => 'HTTP/1.1', | |
| 'REQUEST_METHOD' => 'GET', | |
| 'QUERY_STRING' => '', | |
| 'REQUEST_URI' => '/page-cache-middleware/examples/', | |
| 'SCRIPT_NAME' => '/page-cache-middleware/examples/index.php', | |
| 'PHP_SELF' => '/page-cache-middleware/examples/index.php', | |
| 'REQUEST_TIME_FLOAT' => 1472811538.326, | |
| 'REQUEST_TIME' => 1472811538, | |
| ); | |
| $http->on('request', function (swoole_http_request $request, swoole_http_response $response) { | |
| var_dump($request, $response); | |
| $zendRequest = Request::fromString($request->data); | |
| $psrRequest = \Zend\Psr7Bridge\Psr7ServerRequest::fromZend($zendRequest); | |
| $response->header("Content-Type", "text/html; charset=utf-8"); | |
| $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>"); | |
| }); | |
| function convert2Psr7Request(swoole_http_request $request) { | |
| $headers = $request->header; | |
| $cookies = $request->cookie; | |
| $servers = $request->server; | |
| $query = isset($request->get) ? $request->get : []; | |
| $body = isset($request->post) ? $request->post : []; | |
| $request = new ServerRequest( | |
| $servers, | |
| [], | |
| ServerRequestFactory::marshalUriFromServer($servers, $headers), | |
| $request['server']['request_method'], | |
| 'php://input', | |
| $headers, | |
| $cookies ?: $_COOKIE, | |
| $query, | |
| $body, | |
| explode('/', $servers['server_protocol'])[1] | |
| ); | |
| return $request; | |
| } | |
| function mkServerParams(swoole_http_request $request) { | |
| $request | |
| } | |
| $http->start(); | |
| echo 1; | |
| echo 2; | |
| exit; | |
| ?> | |
| object(swoole_http_request)#4 (6) { | |
| ["fd"]=> | |
| int(2) | |
| ["header"]=> | |
| array(7) { | |
| ["host"]=> | |
| string(18) "192.168.39.18:9501" | |
| ["connection"]=> | |
| string(10) "keep-alive" | |
| ["upgrade-insecure-requests"]=> | |
| string(1) "1" | |
| ["user-agent"]=> | |
| string(109) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36" | |
| ["accept"]=> | |
| string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" | |
| ["accept-encoding"]=> | |
| string(19) "gzip, deflate, sdch" | |
| ["accept-language"]=> | |
| string(44) "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,lv;q=0.2" | |
| } | |
| ["server"]=> | |
| array(11) { | |
| ["query_string"]=> | |
| string(7) "a=1&b=2" | |
| ["request_method"]=> | |
| string(3) "GET" | |
| ["request_uri"]=> | |
| string(8) "/test.js" | |
| ["path_info"]=> | |
| string(8) "/test.js" | |
| ["request_time"]=> | |
| int(1472811403) | |
| ["request_time_float"]=> | |
| float(1472811403.6455) | |
| ["server_port"]=> | |
| int(9501) | |
| ["remote_port"]=> | |
| int(62576) | |
| ["remote_addr"]=> | |
| string(15) "192.168.124.206" | |
| ["server_protocol"]=> | |
| string(8) "HTTP/1.1" | |
| ["server_software"]=> | |
| string(18) "swoole-http-server" | |
| } | |
| ["get"]=> | |
| array(2) { | |
| ["a"]=> | |
| string(1) "1" | |
| ["b"]=> | |
| string(1) "2" | |
| } | |
| ["cookie"]=> | |
| array(3) { | |
| ["_gitlab_session"]=> | |
| string(32) "579bf3e3efdb65bdef329741e9d7644f" | |
| ["request_method"]=> | |
| string(3) "GET" | |
| ["_redmine_session"]=> | |
| string(594) "BAh7DkkiDHVzZXJfaWQGOgZFRmkMSSIKY3RpbWUGOwBGbCsH6O5sV0kiCmF0aW1lBjsARmwrB5wGyVdJIg9zZXNzaW9uX2lkBjsAVEkiJTA4OTUyY2I2NDAyNmFhMzA1ZTA0YWJiYzBhMTIxZDA2BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTNRNzg3MmhpWWpTejdhRFRZTWFoRmZHWmlGZjhLdHdDdFUzK29uaWhBQ0E9BjsARkkiFXVzZXJzX2luZGV4X3NvcnQGOwBGSSIKbG9naW4GOwBUSSIKcXVlcnkGOwBGewk6D3Byb2plY3RfaWRpQjoMZmlsdGVyc3sGSSIOc3RhdHVzX2lkBjsAVHsHOg1vcGVyYXRvckkiBm8GOwBUOgt2YWx1ZXNbBkkiAAY7AFQ6DWdyb3VwX2J5MDoRY29sdW1uX25hbWVzMEkiFmlzc3Vlc19pbmRleF9zb3J0BjsARkkiDGlkOmRlc2MGOwBUSSIVZmlsZXNfaW5kZXhfc29ydAY7AEZJIg1maWxlbmFtZQY7AFQ=--ce27b90ed04d872b3e87b2e580f0247269a37109" | |
| } | |
| ["data"]=> | |
| string(1114) "GET /test.js?a=1&b=2 HTTP/1.1 | |
| Host: 192.168.39.18:9501 | |
| Connection: keep-alive | |
| Upgrade-Insecure-Requests: 1 | |
| User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 | |
| Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 | |
| Accept-Encoding: gzip, deflate, sdch | |
| Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,lv;q=0.2 | |
| Cookie: _gitlab_session=579bf3e3efdb65bdef329741e9d7644f; request_method=GET; _redmine_session=BAh7DkkiDHVzZXJfaWQGOgZFRmkMSSIKY3RpbWUGOwBGbCsH6O5sV0kiCmF0aW1lBjsARmwrB5wGyVdJIg9zZXNzaW9uX2lkBjsAVEkiJTA4OTUyY2I2NDAyNmFhMzA1ZTA0YWJiYzBhMTIxZDA2BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTNRNzg3MmhpWWpTejdhRFRZTWFoRmZHWmlGZjhLdHdDdFUzK29uaWhBQ0E9BjsARkkiFXVzZXJzX2luZGV4X3NvcnQGOwBGSSIKbG9naW4GOwBUSSIKcXVlcnkGOwBGewk6D3Byb2plY3RfaWRpQjoMZmlsdGVyc3sGSSIOc3RhdHVzX2lkBjsAVHsHOg1vcGVyYXRvckkiBm8GOwBUOgt2YWx1ZXNbBkkiAAY7AFQ6DWdyb3VwX2J5MDoRY29sdW1uX25hbWVzMEkiFmlzc3Vlc19pbmRleF9zb3J0BjsARkkiDGlkOmRlc2MGOwBUSSIVZmlsZXNfaW5kZXhfc29ydAY7AEZJIg1maWxlbmFtZQY7AFQ%3D--ce27b90ed04d872b3e87b2e580f0247269a37109 | |
| " | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment