Exemple #1
0
 /**
  * @param swoole_http_request $swooleRequet
  * @param swoole_http_response $swooleResponse
  */
 public function onRequest(swoole_http_request $swooleRequet, swoole_http_response $swooleResponse)
 {
     try {
         $swooleRequestServer = SwooleServerRequest::createFromSwoole($swooleRequet, $swooleResponse);
         if (!($response = $this->doRequest($swooleRequestServer)) instanceof Response) {
             throw new CannotResponseException();
         }
         if (!empty($sessionId = $swooleRequestServer->session->getSessionId())) {
             $swooleResponse->header(Session::SESSION_KEY, $sessionId);
         }
         foreach ($response->getHeaders() as $key => $header) {
             $swooleResponse->header($key, $response->getHeaderLine($key));
         }
         foreach ($swooleRequestServer->getCookieParams() as $cookieParam) {
             $swooleResponse->cookie($cookieParam->getName(), $cookieParam->getValue(), $cookieParam->getExpire(), $cookieParam->getPath(), $cookieParam->getDomain(), $cookieParam->isSecure(), $cookieParam->isHttpOnly());
         }
         $swooleResponse->gzip(static::GZIP_LEVEL);
         $swooleResponse->status($response->getStatusCode());
         $swooleResponse->end($response->getContent());
         unset($response, $swooleRequestServer);
     } catch (HttpException $e) {
         $swooleResponse->status($e->getStatusCode());
         $swooleResponse->end($this->isDebug() ? $e->getMessage() : static::SERVER_INTERVAL_ERROR);
     } catch (Exception $e) {
         $swooleResponse->status(500);
         $swooleResponse->end($this->isDebug() ? $e->getMessage() : static::SERVER_INTERVAL_ERROR);
     }
 }
Exemple #2
0
<?php

/**
 * @author    jan huang <*****@*****.**>
 * @copyright 2016
 *
 * @link      https://www.github.com/janhuang
 * @link      http://www.fast-d.cn/
 */
use FastD\Http\SwooleServerRequest;
include __DIR__ . '/../vendor/autoload.php';
$http = new swoole_http_server("127.0.0.1", 9501);
$http->on('request', function ($request, $response) {
    $server = SwooleServerRequest::createFromSwoole($request, $response);
    $server->session->set('name', 'jan');
    $response->end('hello world');
});
$http->start();