Created
March 4, 2016 06:42
-
-
Save ikariiin/9c8066e7dc897745245b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/******************************************************************************************** | |
* @author Gourab Nag | |
* License: MIT. Get it here https://github.com/gourabNagDev/router/blob/master/MIT | |
* Supporting ONLY text/html | |
* Error Fallback-s only for 404 | |
* Only Static Pages | |
* Routing information is got from the router-parser (https://gihub.com/gourabNagDev/router) | |
* A rules.router file must be there following the schema given in (https://github.com/gourabNagDev/router/blob/master/example.router) | |
* Currently I would term it is in the version even far off from alpha. | |
* Error Handling is not done properly in parser. | |
********************************************************************************************/ | |
require "router_parser.php"; | |
class Client extends Thread { | |
private $socket; | |
private $conf; | |
public function __construct($socket){ | |
$this->socket = $socket; | |
$conf = ""; | |
try{ | |
$router = new Router_Parser(); | |
if(!$router->process()) { | |
echo $router->process(); | |
} else { | |
$conf = $router->getConf(); | |
} | |
} catch(Exception $ex) { | |
$ex->getMessage(); | |
} | |
$this->conf = $conf; | |
$this->start(); | |
} | |
public function run(){ | |
$client = $this->socket; | |
$conf = $this->conf; | |
if ($client) { | |
$header = 0; | |
while(($chars = socket_read($client, 1024, PHP_NORMAL_READ))) { | |
$head[$header]=trim($chars); | |
if ($header>0) { | |
if (!$head[$header] && !$head[$header-1]) | |
break; | |
} | |
$header++; | |
} | |
/** @noinspection PhpUndefinedVariableInspection */ | |
foreach($head as $header) { | |
if ($header) { | |
$headers[]=$header; | |
} | |
} | |
$response = array( | |
"head" => array( | |
"HTTP/1.0 200 OK", | |
"Content-Type: text/html", | |
"X-Powered-By: Gourabs-Socket-Server" | |
), | |
"body" => array() | |
); | |
/** @noinspection PhpUndefinedVariableInspection */ | |
var_dump($headers); | |
/* TESTED ONLY FOR Chrome/48.0.2564.116 . IF OTHER VERSION OR OTHER BROWSERS. THINGS SHOULD BREAK. */ | |
/* Handle Routing Here */ | |
foreach($headers as $request) { | |
// We would support only GET at this moment. | |
$request_main = $request[0]; | |
$request_main = explode(" ", $request_main); | |
if($request_main[0] != "GET") { | |
$response["head"] = "HTTP/1.0 405" | |
// NO COMPLETED YET, WORKING ON IT ATM | |
} | |
} | |
/* End */ | |
$response["body"][] = "<html><head><style>body {font-family: sans-serif;}</style></head>"; | |
$response["body"][] = "<body>"; | |
$response["body"][] = "<h1>TESTING</h1>"; | |
$response["body"][] = "</body>"; | |
$response["body"][] = "</html>"; | |
$response["body"] = implode("\r\n", $response["body"]); | |
$response["head"][] = sprintf("Content-Length: %d", strlen($response["body"])); | |
$response["head"] = implode("\r\n", $response["head"]); | |
/*socket_getpeername($client, $address, $port); | |
$response["body"][]="<html>"; | |
$response["body"][]="<head>"; | |
$response["body"][]="<title>Multithread Sockets PHP ({$address}:{$port})</title>"; | |
$response["body"][]="</head>"; | |
$response["body"][]="<body>"; | |
$response["body"][]="<pre>"; | |
foreach($headers as $header) | |
$response["body"][]="{$header}"; | |
$response["body"][]="</pre>"; | |
$response["body"][]="</body>"; | |
$response["body"][]="</html>"; | |
$response["body"] = implode("\r\n", $response["body"]); | |
$response["head"][] = sprintf("Content-Length: %d", strlen($response["body"])); | |
$response["head"] = implode("\r\n", $response["head"]); | |
*/ | |
socket_write($client, $response["head"]); | |
socket_write($client, "\r\n\r\n"); | |
socket_write($client, $response["body"]); | |
socket_close($client); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment