commit 5d8306e6db301aea703186bfa11f06d4562b5e6e
parent 938b62fe33703ba83aed1708aad842964fb6dca3
Author: finwo <finwo@pm.me>
Date: Wed, 22 Feb 2017 11:18:24 +0100
Basic http handling
Diffstat:
| A | Aptfile | | | 3 | +++ |
| A | Procfile | | | 1 | + |
| A | init.php | | | 116 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | run.sh | | | 10 | ++++++++++ |
4 files changed, 130 insertions(+), 0 deletions(-)
diff --git a/Aptfile b/Aptfile
@@ -0,0 +1,3 @@
+bsdmainutils
+crossroads
+netcat
diff --git a/Procfile b/Procfile
@@ -0,0 +1 @@
+web: ./run.sh
diff --git a/init.php b/init.php
@@ -0,0 +1,116 @@
+<?php
+$f = fopen('php://stdin', 'r');
+$line = str_replace("\r",'',str_replace("\r\n", "\n", fgets($f)));
+$tokens = explode(' ', $line);
+
+$_SERVER['REQUEST_METHOD'] = array_shift($tokens);
+$_SERVER['REQUEST_URI'] = array_shift($tokens);
+
+// Keep it simple
+$statusCodes = array(
+ 200 => 'OK',
+ 400 => 'Bad Request',
+ 404 => 'Not Found',
+ 500 => 'Internal Server Error',
+);
+$mimeTypes = array(
+ 'css' => 'text/css',
+ 'htm' => 'text/html',
+ 'html' => 'text/html',
+ 'js' => 'text/javascript',
+);
+
+$status = 200;
+$headers = array();
+ob_start(function( $buffer ) {
+ global $status;
+ global $statusCodes;
+ global $headers;
+ $extra = 'HTTP/1.0 '.$status.' '.$statusCodes[$status].PHP_EOL;
+ $extra .= 'Content-Length: '.strlen($buffer) . PHP_EOL;
+ foreach ($headers as $header) $extra .= $header . PHP_EOL;
+ $extra .= PHP_EOL;
+ return $extra . $buffer;
+});
+
+// Make sure we support this
+if(!in_array($_SERVER['REQUEST_METHOD'],array('GET'))) {
+ $status = 501;
+ die('The requested method has not (yet) been implemented'.PHP_EOL);
+}
+
+// Some security
+if (strpos($_SERVER['REQUEST_URI'], '..')!==false) {
+ $status = 400;
+ die('You\'ve send something we don\'t understand or allow'.PHP_EOL);
+}
+
+// Read headers
+while(($line=str_replace("\r",'',str_replace("\r\n", "\n", fgets($f))))!="\n") {
+ while(substr(rtrim($line), -1)=="\\") {
+ $line = rtrim($line);
+ $line = substr($line, 0, strlen($line)-1);
+ $line .= str_replace("\r",'',str_replace("\r\n", "\n", fgets($f)));
+ }
+ list($key, $value) = array_map('trim', explode(':', $line, 2));
+ $_SERVER['HTTP_'.strtoupper(str_replace('-','_',$key))] = $value;
+}
+
+$params = explode('?', $_SERVER['REQUEST_URI'], 2);
+$path = rtrim(__DIR__,'/').'/'.trim(array_shift($params),'/');
+$ext = @array_pop(explode('.',$path));
+
+function set_deep($path, &$dataHolder = array(), $value = null) {
+ $keys = explode('.', $path);
+ while (count($keys)) {
+ $dataHolder = &$dataHolder[array_shift($keys)];
+ }
+ $dataHolder = $value;
+}
+
+if(count($params)) {
+ $params = array_shift($params);
+ $_GET = array();
+ $variables = explode('&', $params);
+ foreach ($variables as $variable) {
+ $components = explode('=', $variable);
+ $key = str_replace(array( '[', ']' ), array( '.', '' ), urldecode(array_shift($components)));
+ $value = urldecode(array_shift($components));
+ set_deep($key, $_GET, $value);
+ }
+}
+
+if(is_dir($path)) {
+ foreach(
+ array(
+ $path . '/index.php',
+ $path . '/index.html',
+ $path . '/index.htm',
+ ) as $file
+ ) {
+ if(is_file($file)) {
+ $path = $file;
+ break;
+ }
+ }
+}
+
+if(is_file($path)) {
+ switch($ext) {
+ case 'php':
+ include $path;
+ exit(0);
+ default:
+ if(isset($mimeTypes[$ext])) $headers[]='Content-Type: '.$mimeTypes[$ext];
+ $headers[]='Expires:'.date('c',time()+300);
+ readfile($path);
+ exit(0);
+ }
+}
+if(is_file(rtrim(__DIR__,'/').'/app.php')) {
+ include $path;
+ exit(0);
+}
+
+$status = 404;
+die('We could not find the page you\'re looking for.'.PHP_EOL);
diff --git a/run.sh b/run.sh
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+
+XRARGS= -x -v -s http:0.0.0.0:$PORT
+
+for i in {0..9} ; do
+ XRARGS="$XARGS -b 127.0.0.1:800$i"
+ while true ; do FIFO=$(hexdump -n 16 -v -e '/1 "%02X"' -e '/16 "\n"' /dev/urandom) ; mkfifo fifo.$FIFO ; nc -l 800$i < fifo.$FIFO | php sgi.php > fifo.$FIFO ; rm fifo.$FIFO ; done &
+done;
+
+xr $XRARGS