| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,16 +16,24 @@ static function get($key,$default = null) | |||
| 16 | 16 | ||
| 17 | 17 | static function set($key, $value, $expire = 0) | |
| 18 | 18 | { | |
| 19 | - if($expire!=0) $expire=time()+$expire; | ||
| 20 | - if(defined('SWOOLE_SERVER')) | ||
| 19 | + if ($expire != 0) | ||
| 21 | 20 | { | |
| 22 | - global $php; | ||
| 23 | - $php->response->setcookie($key,$value,$expire,Cookie::$path,Cookie::$domain,Cookie::$secure,Cookie::$httponly); | ||
| 21 | + $expire = time() + $expire; | ||
| 22 | + } | ||
| 23 | + if (defined('SWOOLE_SERVER')) | ||
| 24 | + { | ||
| 25 | + \Swoole::$php->http->setcookie($key, | ||
| 26 | + $value, | ||
| 27 | + $expire, | ||
| 28 | + Cookie::$path, | ||
| 29 | + Cookie::$domain, | ||
| 30 | + Cookie::$secure, | ||
| 31 | + Cookie::$httponly); | ||
| 32 | + } | ||
| 33 | + else | ||
| 34 | + { | ||
| 35 | + setcookie($key, $value, $expire, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly); | ||
| 24 | 36 | } | |
| 25 | - else | ||
| 26 | - { | ||
| 27 | - setcookie($key, $value, $expire, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly); | ||
| 28 | - } | ||
| 29 | 37 | } | |
| 30 | 38 | ||
| 31 | 39 | static function delete($key) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ | |||
| 7 | 7 | * Class Http_LAMP | |
| 8 | 8 | * @package Swoole | |
| 9 | 9 | */ | |
| 10 | - class ExtServer implements \Swoole\IFace\Http | ||
| 10 | + class ExtServer implements Swoole\IFace\Http | ||
| 11 | 11 | { | |
| 12 | 12 | /** | |
| 13 | 13 | * @var \swoole_http_request | |
@@ -63,6 +63,11 @@ function finish($content = null) | |||
| 63 | 63 | throw new Swoole\ResponseException; | |
| 64 | 64 | } | |
| 65 | 65 | ||
| 66 | + function setcookie($name, $value = null, $expire = null, $path = '/', $domain = null, $secure = null, $httponly = null) | ||
| 67 | + { | ||
| 68 | + $this->response->cookie($name, $value, $expire, $path, $domain, $secure, $httponly); | ||
| 69 | + } | ||
| 70 | + | ||
| 66 | 71 | function setGlobal() | |
| 67 | 72 | { | |
| 68 | 73 | if (isset($this->request->get)) | |
@@ -137,4 +142,68 @@ function doStatic(\swoole_http_request $req, \swoole_http_response $resp) | |||
| 137 | 142 | $resp->header('Content-Type', $mime_type); | |
| 138 | 143 | $resp->end(file_get_contents($this->document_root . $req->server['request_uri'])); | |
| 139 | 144 | } | |
| 145 | + | ||
| 146 | + function onRequest(\swoole_http_request $req, \swoole_http_response $resp) | ||
| 147 | + { | ||
| 148 | + if ($this->document_root and is_file($this->document_root . $req->server['request_uri'])) | ||
| 149 | + { | ||
| 150 | + $this->doStatic($req, $resp); | ||
| 151 | + return; | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + $this->request = $req; | ||
| 155 | + $this->response = $resp; | ||
| 156 | + $this->setGlobal(); | ||
| 157 | + | ||
| 158 | + $php = Swoole::getInstance(); | ||
| 159 | + try | ||
| 160 | + { | ||
| 161 | + try | ||
| 162 | + { | ||
| 163 | + ob_start(); | ||
| 164 | + /*---------------------处理MVC----------------------*/ | ||
| 165 | + $body = $php->runMVC(); | ||
| 166 | + $echo_output = ob_get_contents(); | ||
| 167 | + if ($echo_output) | ||
| 168 | + { | ||
| 169 | + $resp->write($echo_output); | ||
| 170 | + } | ||
| 171 | + if ($body) | ||
| 172 | + { | ||
| 173 | + $resp->write($body); | ||
| 174 | + } | ||
| 175 | + ob_end_clean(); | ||
| 176 | + $resp->end(); | ||
| 177 | + } | ||
| 178 | + catch (Swoole\ResponseException $e) | ||
| 179 | + { | ||
| 180 | + if ($this->finish != 1) | ||
| 181 | + { | ||
| 182 | + $resp->status(500); | ||
| 183 | + $resp->end($e->getMessage()); | ||
| 184 | + } | ||
| 185 | + } | ||
| 186 | + } | ||
| 187 | + catch (\Exception $e) | ||
| 188 | + { | ||
| 189 | + $resp->status(500); | ||
| 190 | + $resp->end($e->getMessage() . "<hr />" . nl2br($e->getTraceAsString())); | ||
| 191 | + } | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + function __clean() | ||
| 195 | + { | ||
| 196 | + $php = Swoole::getInstance(); | ||
| 197 | + //模板初始化 | ||
| 198 | + if (!empty($php->tpl)) | ||
| 199 | + { | ||
| 200 | + $php->tpl->clear_all_assign(); | ||
| 201 | + } | ||
| 202 | + //还原session | ||
| 203 | + if (!empty($php->session)) | ||
| 204 | + { | ||
| 205 | + $php->session->open = false; | ||
| 206 | + $php->session->readonly = false; | ||
| 207 | + } | ||
| 208 | + } | ||
| 140 | 209 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,4 +28,9 @@ function finish($content = null) | |||
| 28 | 28 | { | |
| 29 | 29 | exit($content); | |
| 30 | 30 | } | |
| 31 | + | ||
| 32 | + function setcookie($name, $value = null, $expire = null, $path = '/', $domain = null, $secure = null, $httponly = null) | ||
| 33 | + { | ||
| 34 | + setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); | ||
| 35 | + } | ||
| 31 | 36 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,4 +37,9 @@ function finish($content = null) | |||
| 37 | 37 | if($content) \Swoole::$php->response->body = $content; | |
| 38 | 38 | throw new Swoole\ResponseException; | |
| 39 | 39 | } | |
| 40 | + | ||
| 41 | + function setcookie($name, $value = null, $expire = null, $path = '/', $domain = null, $secure = null, $httponly = null) | ||
| 42 | + { | ||
| 43 | + \Swoole::$php->response->setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); | ||
| 44 | + } | ||
| 40 | 45 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,8 +4,15 @@ | |||
| 4 | 4 | interface Http | |
| 5 | 5 | { | |
| 6 | 6 | function header($k, $v); | |
| 7 | + | ||
| 7 | 8 | function status($code); | |
| 9 | + | ||
| 8 | 10 | function response($content); | |
| 11 | + | ||
| 9 | 12 | function redirect($url, $mode = 301); | |
| 13 | + | ||
| 10 | 14 | function finish($content = null); | |
| 15 | + | ||
| 16 | + function setcookie($name, $value = null, $expire = null, $path = '/', $domain = null, $secure = null, | ||
| 17 | + $httponly = null); | ||
| 11 | 18 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -431,63 +431,15 @@ function handlerServer(Swoole\Request $request) | |||
| 431 | 431 | ||
| 432 | 432 | function runHttpServer($host = '0.0.0.0', $port = 9501, $config = array()) | |
| 433 | 433 | { | |
| 434 | - define('SWOOLE_HTTP_SERVER', true); | ||
| 435 | - $this->ext_http_server = $this->loadModule('http'); | ||
| 434 | + define('SWOOLE_SERVER', true); | ||
| 435 | + $this->ext_http_server = $this->http = new Swoole\Http\ExtServer(); | ||
| 436 | 436 | $server = new swoole_http_server($host, $port); | |
| 437 | 437 | $server->set($config); | |
| 438 | 438 | if (!empty($config['document_root'])) | |
| 439 | 439 | { | |
| 440 | 440 | $this->ext_http_server->document_root = trim($config['document_root']); | |
| 441 | 441 | } | |
| 442 | - | ||
| 443 | - $server->on('Request', function(swoole_http_request $req, swoole_http_response $resp) { | ||
| 444 | - | ||
| 445 | - $http = $this->ext_http_server; | ||
| 446 | - if ($http->document_root and is_file($http->document_root . $req->server['request_uri'])) | ||
| 447 | - { | ||
| 448 | - $http->doStatic($req, $resp); | ||
| 449 | - return; | ||
| 450 | - } | ||
| 451 | - | ||
| 452 | - $http->request = $req; | ||
| 453 | - $http->response = $resp; | ||
| 454 | - $http->setGlobal(); | ||
| 455 | - | ||
| 456 | - $php = Swoole::getInstance(); | ||
| 457 | - try | ||
| 458 | - { | ||
| 459 | - try | ||
| 460 | - { | ||
| 461 | - ob_start(); | ||
| 462 | - /*---------------------处理MVC----------------------*/ | ||
| 463 | - $body = $php->runMVC(); | ||
| 464 | - $echo_output = ob_get_contents(); | ||
| 465 | - if ($echo_output) | ||
| 466 | - { | ||
| 467 | - $resp->write($echo_output); | ||
| 468 | - } | ||
| 469 | - if ($body) | ||
| 470 | - { | ||
| 471 | - $resp->write($body); | ||
| 472 | - } | ||
| 473 | - ob_end_clean(); | ||
| 474 | - $resp->end(); | ||
| 475 | - } | ||
| 476 | - catch (Swoole\ResponseException $e) | ||
| 477 | - { | ||
| 478 | - if ($http->finish != 1) | ||
| 479 | - { | ||
| 480 | - $resp->status(500); | ||
| 481 | - $resp->end($e->getMessage()); | ||
| 482 | - } | ||
| 483 | - } | ||
| 484 | - } | ||
| 485 | - catch (\Exception $e) | ||
| 486 | - { | ||
| 487 | - $resp->status(500); | ||
| 488 | - $resp->end( $e->getMessage()."<hr />".nl2br($e->getTraceAsString())); | ||
| 489 | - } | ||
| 490 | - }); | ||
| 442 | + $server->on('Request', [$this->http, 'onRequest']); | ||
| 491 | 443 | $server->start(); | |
| 492 | 444 | } | |
| 493 | 445 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments