Ejemplo n.º 1
0
function no_chunk(swoole_http_request $request, swoole_http_response $response)
{
    //	try
    //	{
    //		if (rand(1, 99) % 2 == 1)
    //		{
    //			throw new Exception("just for fun.");
    //		}
    //		$response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
    //	}
    //	catch(Exception $e)
    //	{
    //		$response->end("<h1>Exceptiom</h1><div>".$e->getMessage()."</div>");
    //	}
    //    var_dump($request);
    //var_dump($request->files);
    //    var_dump($request->post);
    //    var_dump($request->cookie);
    var_dump($request->rawContent());
    $response->end("<h1>Hello Swoole.</h1>");
    return;
    //var_dump($request);
    //    var_dump($_GET);
    //var_dump($_POST);
    //var_dump($_COOKIE);
    //$response->status(301);
    //$response->header("Location", "http://www.baidu.com/");
    //$response->cookie("hello", "world", time() + 3600);
    //    $response->header("Content-Type", "text/html; charset=utf-8");
    //var_dump($request->post);
    //    var_dump($request->get);
    //    echo strlen(gzdeflate("<h1>Hello Swoole.</h1>"));
    //    $response->end("<h1>Hello Swoole.</h1>");
    //$response->end("<h1>Hello Swoole. #".str_repeat('A', rand(100, 999))."</h1>");
    //global $http;
    //$http->task("hello world");
    $file = realpath(__DIR__ . '/../' . $request->server['request_uri']);
    if (is_file($file)) {
        echo "http get file={$file}\n";
        if (substr($file, -4) == '.php') {
            $response->gzip();
        } else {
            $response->header('Content-Type', 'image/jpeg');
        }
        $content = file_get_contents($file);
        echo "response size = " . strlen($content) . "\n";
        //        $response->write($content);
        //        $response->end();
        $response->end($content);
    } else {
        $response->end("<h1>Hello Swoole.</h1>");
    }
}
Ejemplo n.º 2
0
 public function handle()
 {
     if (empty($this->_dispatcher)) {
         throw new EasyWorkException('[error] dispatcher is not exist!');
     }
     $method = $this->_sw->server['request_method'];
     $uri = $this->_sw->server['request_uri'];
     $_routeInfo = $this->_dispatcher->dispatch($method, $uri);
     $routeInfo['r_code'] = $_routeInfo[0];
     $routeInfo['r_handle'] = isset($_routeInfo[1]) ? $_routeInfo[1] : [];
     $routeInfo['r_var'] = isset($_routeInfo[2]) ? $_routeInfo[2] : [];
     $routeInfo['request'] = ['uri' => $uri, 'header' => $this->_sw->header, 'server' => $this->_sw->server, 'get' => array_merge(isset($this->_sw->get) ? (array) $this->_sw->get : [], $routeInfo['r_var']), 'post' => isset($this->_sw->post) ? $this->_sw->post : [], 'files' => isset($this->_sw->files) ? $this->_sw->files : [], 'cookie' => isset($this->_sw->cookie) ? $this->_sw->cookie : [], 'rawcontent' => $this->_sw->rawContent(), 'method' => $method, 'fd' => $this->_sw->fd];
     return $routeInfo;
 }
Ejemplo n.º 3
0
 function onRequest(\swoole_http_request $req, \swoole_http_response $resp)
 {
     $path = trim($req->server['request_uri'], '/');
     if ($path == 'app/stats') {
         if ($req->server['request_method'] != 'POST') {
             $resp->status(403);
             $resp->end("<h1>No POST Data</h1>");
         } else {
             $data = $req->rawContent();
             $stats = gzdecode($data);
             if ($stats) {
                 $this->insertToDb($stats);
             } else {
                 $this->log("gzdecode failed, fd={$req->fd}, length={$req->header['content-length']}");
             }
             $resp->end('{"code": 1}');
         }
     } else {
         $resp->status(404);
         $resp->end("<h1>Page Not Found</h1>");
     }
 }
Ejemplo n.º 4
0
 function getRequestBody()
 {
     return $this->request->rawContent();
 }
Ejemplo n.º 5
0
 /**
  * Gets HTTP raws request body
  *
  * @return string
  */
 public function getRawBody()
 {
     return $this->request->rawContent();
 }
 /**
  * @param \swoole_http_request $swooleRequest
  *
  * @return StreamInterface
  */
 private function transformBody(\swoole_http_request $swooleRequest)
 {
     $body = $this->psr7Factory->createStream();
     if (isset($swooleRequest->fd) && false !== ($rawContent = $swooleRequest->rawContent())) {
         $body->write($rawContent);
         $body->rewind();
         unset($rawContent);
     }
     return $body;
 }
Ejemplo n.º 7
0
 /**
  * Method  initRequestParam
  * @desc   将请求信息放入全局注册器中
  * @author WenJun <*****@*****.**>
  * @param swoole_http_request $request
  * @return bool
  */
 private function initRequestParam(swoole_http_request $request)
 {
     //将请求的一些环境参数放入全局变量桶中
     $server = isset($request->server) ? $request->server : array();
     $header = isset($request->header) ? $request->header : array();
     $get = isset($request->get) ? $request->get : array();
     $post = isset($request->post) ? $request->post : array();
     $cookie = isset($request->cookie) ? $request->cookie : array();
     $files = isset($request->files) ? $request->files : array();
     Yaf_Registry::set('REQUEST_SERVER', $server);
     Yaf_Registry::set('REQUEST_HEADER', $header);
     Yaf_Registry::set('REQUEST_GET', $get);
     Yaf_Registry::set('REQUEST_POST', $post);
     Yaf_Registry::set('REQUEST_COOKIE', $cookie);
     Yaf_Registry::set('REQUEST_FILES', $files);
     Yaf_Registry::set('REQUEST_RAW_CONTENT', $request->rawContent());
     return true;
 }
Ejemplo n.º 8
0
 /**
  * Crea una isntancia de petición usando como base la petición de Swoole.
  *
  * @param \swoole_http_request $req
  *
  * @return self
  */
 public static function crearDesdeSwoole(\swoole_http_request $req)
 {
     // Preparamos Metodo
     $metodo = $req->server['request_method'];
     // Preparamos URL
     $url = 'http';
     if (!empty($req->server['https']) && $req->server['https'] !== 'off') {
         $url .= 's';
     } elseif ($req->server['server_port'] == 443) {
         $url .= 's';
     }
     $url .= '://' . $req->header['host'];
     $url .= $req->server['request_uri'];
     if (isset($req->server['query_string'])) {
         $url .= '?' . $req->server['query_string'];
     }
     // Preparamos parámetros según método
     $parametros = [];
     if (!empty($req->post)) {
         $parametros = $req->post;
     }
     if ($metodo != 'GET' && isset($req->header['content-type'])) {
         $parametros = self::procesarParametros($req->rawContent(), $req->header['content-type'], $parametros);
     }
     // Preparamos cabeceras
     $cabeceras = [];
     foreach ($req->header as $nombre => $valor) {
         $nombre = str_replace(' ', '-', ucwords(str_replace('-', ' ', $nombre)));
         if (isset($cabeceras[$nombre])) {
             if (is_array($cabeceras[$nombre])) {
                 $cabeceras[$nombre][] = $valor;
             } else {
                 $cabeceras[$nombre] = [$cabeceras[$nombre], $valor];
             }
         } else {
             $cabeceras[$nombre] = $valor;
         }
     }
     // Preparamos galletas
     $galletas = [];
     if (!empty($req->cookie)) {
         $galletas = $req->cookie;
     }
     // Preparamos arhivos
     $archivos = [];
     if (!empty($req->files)) {
         $archivos = $req->files;
     }
     // Iniciamos instancia de petición con los datos ya preparados
     $peticion = new Peticion($url, $metodo, $parametros, $cabeceras, $galletas, $archivos);
     // Preparamos IP del cliente
     $peticion->cliente_ip = $req->server['remote_addr'];
     if (isset($req->server['x-client-ip'])) {
         $peticion->cliente_ip = $req->server['x-client-ip'];
     } elseif (isset($req->server['x-real-ip'])) {
         $peticion->cliente_ip = $req->server['x-real-ip'];
     } elseif (isset($_SERVER['x-forwarded-for'])) {
         $peticion->cliente_ip = trim(explode(',', $_SERVER['x-forwarded-for'])[0]);
     }
     // Retornamos instancia iniciada
     return $peticion;
 }
Ejemplo n.º 9
0
Archivo: swoole.php Proyecto: loder/asf
 function my_onRequest(swoole_http_request $request, swoole_http_response $response)
 {
     //var_dump($request);
     $method = $request->server['request_method'];
     $uri = $request->server['request_uri'];
     $content = $request->rawContent();
     Log::prn_log(NOTICE, "REQUEST {$method} {$uri} {$content}");
     //        if ( $request->server['request_method'] <> 'POST' ) {
     //            return $this->response($response, 405, 'Method Not Allowed, ' . $request->server['request_method']);
     //        }
     //        if ( !preg_match('#^/(\w+)/(\w+)$#', $uri, $match) ) {
     //            return $this->response($response, 404, "'$uri' is not found!");
     //        }
     //        $class = $match[1].'_controller';
     //        $fun = $match[2];
     $route_info = $this->route->handel_route($method, $uri);
     if ($route_info === 405) {
         return $this->response($response, 405, 'Method Not Allowed, ' . $request->server['request_method']);
     }
     if ($route_info === 404) {
         return $this->response($response, 404, "'{$uri}' is not found!");
     }
     //log::prn_log(DEBUG, json_encode($route_info));
     $class = $route_info['class'] . '_controller';
     $fun = $route_info['fun'];
     $param = isset($route_info['param']) ? $route_info['param'] : [];
     //判断类是否存在
     if (!class_exists($class) || !method_exists($class, $fun)) {
         return $this->response($response, 404, " class or fun not found class == {$class} fun == {$fun}");
     }
     if ($content === false) {
         $content = '';
     }
     if ($method === 'POST' and $content === '') {
         Log::prn_log(ERROR, $content);
         return $this->response($response, 415, 'post content is empty!');
     }
     $obj = new $class($this, $request, $param);
     return $this->response($response, 200, $obj->{$fun}(), array('Content-Type' => 'application/json'));
 }