14.swoole_http_server.md


Table of Contents


1.swoole_http_server

从swoole-1.7.7-stable版本开始,swoole提供内置的http服务器swoole_http_server。swoole_http_server继承自swoole_server,是一个完整的http服务器实现。
示例:

  1. $http = new swoole_http_server("127.0.0.1", 9501);
  2. $http->on('request', function ($request, $response) {
  3. $html = "<h1>Hello Swoole.</h1>";
  4. $response->end($html);
  5. });
  6. $http->start();

1.1.work_mode

swoole_http_server支持同步和异步两种模式。
同步模式: 这种模式类似于nginx+php-fpm/apache,它需要设置大量worker进程来完成并发请求处理。Worker进程内可以使用同步阻塞IO,编程方式与普通PHP Web程序完全一致。
与php-fpm/apache不同的是,客户端连接并不会独占进程,每一个Worker进程都会常驻内存,服务器依然可以应对大量并发连接。

异步模式: 在这种模式下,Worker进程内不允许使用任何阻塞式API,例如MySQL、redis、http_client、file_get_contents、sleep等。需要使用swoole提供的各种异步API来实现,如异步swoole_client,swoole_event_add,swoole_timer,swoole_get_mysqli_sock等API。

1.2.callback

注册事件回调函数,与swoole_server回调函数基本相同,不同之处是:swoole_http_server不接受onConnect/onReceive/onClose回调设置, 同时swoole_http_server额外接受2种新的事件类型onRequest/onMessage

onRequest 描述:收到完整的http请求时的回调。
函数原型:

  1. function onRequest(swoole_http_request $request, swoole_http_response $response);

参数说明:

参数 描述
swoole_http_request $request Http请求信息对象,包含了header/get/post/cookie等相关信息
swoole_http_response $response Http响应对象,支持cookie/header/status等Http操作

说明:
在收到请求时,通过$request对象获取请求内容,然后通过$response发送响应。

onMessage 描述:收到一个WebSocket数据请求时的回调。
函数原型:

  1. function onMessage(swoole_http_request $request, swoole_http_response $response);

参数说明:

参数 描述
swoole_http_request $request WebSocket数据对象,存放在message)属性中
swoole_http_response $response WebSocket响应对象,支持message方法发送数据

说明:
WebSocket协议中,$request$response对象会常驻内存,message方法调用后并不会销毁这2个对象
(PS: 1.7.7版本还不支持WebSocket,因此这个回调无法正常工作。)

2.swoole_http_request

swoole_http_request是Http请求信息对象,包含了header/get/post/cookie等相关信息,在WebSocket模式下包含了message数据。

2.1.header

描述:
Http请求的头部信息。 内容:

key 描述

说明: 类型为数组,所有key均为小写。

2.2.server

Http请求相关的服务器信息,相当于PHP的$_SERVER数组。
内容:

key 描述

说明:
包含了Http请求的方法,URL路径,客户端IP等信息。数组的key全部为小写,并且与PHP的$_SERVER数组保持一致。

2.3.get

描述:
Http请求的GET参数。 说明:
相当于PHP中的$_GET,格式为数组。

1.7.7版本暂时不支持PHP的GET参数合并,比如hello[]=1&hello[]=2这样的参数
为防止HASH攻击,GET参数最大不允许超过128个

2.4.post

描述:
Http请求的POST参数。 说明:
相当于PHP中的$_POST,格式为数组。

POST与Header加起来的尺寸不得超过package_max_length的设置,否则会认为是恶意请求
POST参数的个数最大不超过128个

2.5.cookie

描述:
HTTP请求携带的COOKIE信息。 说明:
相当于PHP中的$_COOKIE,格式为数组。

2.6.message

描述:
WebSocket发送来的数据。 说明:无

3.swoole_http_response

3.1.header

描述:设置HTTP响应的Header信息。
函数原型:

  1. public function swoole_http_response::header(string $key, string $value);

参数说明:

参数 描述
string $key Header的名称
string $value Header的值

说明:
header设置必须在end方法之前。
示例:

  1. $response->header("content-type", "text/html");

3.2.cookie

描述:设置HTTP响应的Cookie信息。
函数原型:

  1. public function swoole_http_response::cookie(string $key, string $value = '', int $expire = 0 , string $path = '/', string $domain = '', bool $secure = false , bool $httponly = false);

参数说明:

参数 描述

说明:
cookie设置必须在end方法之前。
示例:

  1. $response->cookie("content-type", "text/html");

3.3.status

描述:发送Http状态码。
函数原型:

  1. public function swoole_http_response::status(int $http_status_code);

参数说明:

参数 描述
int $http_status_code 合法的HttpCode,如200,404等

说明:
cookie设置必须在end方法之前。
$http_status_code必须为合法的HttpCode,如200, 502, 301, 404等,否则会报错
示例:

  1. $response->status(200);

3.4.message

描述:通过WebSocket发送数据。
函数原型:

  1. public function swoole_http_response::message(string $message, bool $binary = false);

参数说明:

参数 描述
string $message 待发送的数据
bool $binary 是否为二进制数据,默认为false

说明:
该函数只能用在WebSocket握手成功的Http客户端。
message方法不同于end,它并不会销毁request/response对象。
(PS: ) 示例:

  1. $response->message("Hello World");

3.5.end

描述:发送Http响应体,并结束请求处理。
函数原型:

  1. public function swoole_http_response::end(string $html);

参数说明:

参数 描述
string $html 待发送的数据

说明:
end操作后将向客户端浏览器发送HTML内容,并销毁$request/$response对象
如果开启了KeepAlive,连接将会保持,服务器会等待下一次请求
未开启KeepAlive,服务器将会切断连接
示例:

  1. $response->end("<h1>Hello Swoole.</h1>");