Last active
March 19, 2021 00:20
-
-
Save karlazz/a2a3f7802ec843373ce8550c790395c9 to your computer and use it in GitHub Desktop.
For a pair programming session, a server to accept and return key value pairs
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 | |
set_time_limit(0); | |
/* works with GET or POST, one key pair only :-( | |
curl -d '{"path":"/set","query":{"color":"green"}}' -X POST http://localhost:4000 | |
curl -d '{"path":"/get","query":{"key":"color"}}' -X POST http://localhost:4000 | |
localhost:4000/set?color=green | |
localhost:4000/get?key=color | |
you can set with GET and retrieve with POST as long as the server stays on | |
or you can set with POSt and retrieve with GET etc. | |
*/ | |
$address = '127.0.0.1'; | |
$port = 4000; | |
$sock = socket_create(AF_INET, SOCK_STREAM, 0); | |
$bound = false; | |
$i=0; | |
$virtualDB = []; // virtual database for key pairs | |
while (!$bound) { | |
$bound = @socket_bind($sock, $address, $port); | |
$i++; | |
if ($i>10) die('Could not bind to port'."\n"); | |
if ($bound) break; | |
echo 'retrying ...' . "\n"; | |
sleep(10); | |
} | |
echo "\n Listening on port $port For Connection... \n\n"; | |
while(1) { | |
socket_listen($sock); | |
$client = socket_accept($sock); | |
$input = socket_read($client, 1024); | |
echo 'read it'. "\n"; | |
$incoming = array(); | |
$incoming = explode("\r\n", $input); | |
$fetchArray = array(); | |
$fetchArray = explode(" ", $incoming[0]); | |
$reqType = $fetchArray[0]; | |
if (strtolower($reqType) == "post") { | |
$route = $incoming[7]; | |
$url_parts = json_decode($route, true); | |
$query=$url_parts['query']; | |
} | |
else { | |
$route = $fetchArray[1]; | |
$url_parts = parse_url($route); | |
$url_parts = parse_url($route); | |
$query = []; | |
if (isset($url_parts['query']) ) parse_str($url_parts['query'], $query); | |
} | |
// /?key= if value then save key and value | |
// else return value or error | |
$output = 'no action indicated'; | |
switch ($url_parts['path']) { | |
case "/set": | |
$keys = array_keys($query); | |
if ($keys) { | |
$virtualDB[$keys[0]] = $query[$keys[0]]; | |
$output = "key pair stored {$keys[0]} = {$virtualDB[$keys[0]]}"; | |
} else $output = "need a key pair!"; | |
break; | |
case "/get": | |
$key = $query['key']; | |
if ($key && array_key_exists($key, $virtualDB) ) | |
$output = $key . ' = ' . $virtualDB[$key] . ' retrieved from storage'; | |
else $output = 'no key found'; | |
break; | |
default: | |
$output = 'no action indicated'; | |
} | |
echo $reqType . " Request " . $route . "\n"; | |
$Header = "HTTP/1.1 200 OK \r\n" . | |
"Date: Fri, 31 Dec 1999 23:59:59 GMT \r\n" . | |
"Content-Type: text/html \r\n\r\n"; | |
if (strpos($route,"favicon.ico") === false) { | |
$Content = str_replace("\n","<br>",$output); | |
$output = $Header . $Content ; | |
socket_write($client,$output,strlen($output)); | |
socket_close($client); | |
} | |
} | |
/* not working ... :-( */ | |
socket_shutdown($sock, 2); | |
socket_close($sock); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment