FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

update swoole_http_server support. · stonegithubs/framework@f3af8c3 · GitHub

Repository navigation

Commit f3af8c3

Browse files
committed
update swoole_http_server support.
1 parent f8e6ce1 commit f3af8c3

6 files changed

Lines changed: 106 additions & 60 deletions

File tree

‎libs/Swoole/Cookie.php‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@ static function get($key,$default = null)
1616

1717
static function set($key, $value, $expire = 0)
1818
{
19-
if($expire!=0) $expire=time()+$expire;
20-
if(defined('SWOOLE_SERVER'))
19+
if ($expire != 0)
2120
{
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);
2436
}
25-
else
26-
{
27-
setcookie($key, $value, $expire, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
28-
}
2937
}
3038

3139
static function delete($key)

‎libs/Swoole/Http/ExtServer.php‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Class Http_LAMP
88
* @package Swoole
99
*/
10-
class ExtServer implements \Swoole\IFace\Http
10+
class ExtServer implements Swoole\IFace\Http
1111
{
1212
/**
1313
* @var \swoole_http_request
@@ -63,6 +63,11 @@ function finish($content = null)
6363
throw new Swoole\ResponseException;
6464
}
6565

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+
6671
function setGlobal()
6772
{
6873
if (isset($this->request->get))
@@ -137,4 +142,68 @@ function doStatic(\swoole_http_request $req, \swoole_http_response $resp)
137142
$resp->header('Content-Type', $mime_type);
138143
$resp->end(file_get_contents($this->document_root . $req->server['request_uri']));
139144
}
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+
}
140209
}

‎libs/Swoole/Http/LAMP.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ function finish($content = null)
2828
{
2929
exit($content);
3030
}
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+
}
3136
}

‎libs/Swoole/Http/PWS.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ function finish($content = null)
3737
if($content) \Swoole::$php->response->body = $content;
3838
throw new Swoole\ResponseException;
3939
}
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+
}
4045
}

‎libs/Swoole/IFace/Http.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
interface Http
55
{
66
function header($k, $v);
7+
78
function status($code);
9+
810
function response($content);
11+
912
function redirect($url, $mode = 301);
13+
1014
function finish($content = null);
15+
16+
function setcookie($name, $value = null, $expire = null, $path = '/', $domain = null, $secure = null,
17+
$httponly = null);
1118
}

‎libs/Swoole/Swoole.php‎

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -431,63 +431,15 @@ function handlerServer(Swoole\Request $request)
431431

432432
function runHttpServer($host = '0.0.0.0', $port = 9501, $config = array())
433433
{
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();
436436
$server = new swoole_http_server($host, $port);
437437
$server->set($config);
438438
if (!empty($config['document_root']))
439439
{
440440
$this->ext_http_server->document_root = trim($config['document_root']);
441441
}
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']);
491443
$server->start();
492444
}
493445

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL