protected function logRequest($requestInfo) { // Get profiling info. $profiling = $requestInfo['profiling']; $receiveTime = $processTime = $sendTime = 0; if (isset($profiling['receive.start']) and isset($profiling['receive.end'])) { $receiveTime = ($profiling['receive.end'] - $profiling['receive.start']) * 1000.0; } if (isset($profiling['process.start']) and isset($profiling['process.end'])) { $processTime = ($profiling['process.end'] - $profiling['process.start']) * 1000.0; } if (isset($profiling['send.start']) and isset($profiling['send.end'])) { $sendTime = ($profiling['send.end'] - $profiling['send.start']) * 1000.0; } $totalTime = ceil($receiveTime + $processTime + $sendTime); // Do the logging. $request = $requestInfo['request']; $response = $requestInfo['response']; if ($request and $response) { $clientInfo = $this->handler->getClientInfo($requestInfo['socket']); $statusName = StupidHttp_WebServer::getHttpStatusHeader($response->getStatus()); $replacements = array('%date%' => date(self::REQUEST_DATE_FORMAT), '%client_ip%' => $clientInfo['address'], '%client_port%' => $clientInfo['port'], '%method%' => $request->getMethod(), '%uri%' => $request->getUri(), '%path%' => $request->getUriPath(), '%status%' => $response->getStatus(), '%status_name%' => $statusName, '%time%' => $totalTime); $this->log->info(str_replace(array_keys($replacements), array_values($replacements), self::REQUEST_LOG_FORMAT)); } }
print_r($r->getBody()); echo '</pre>'; echo '<h1>Forms</h1>'; echo '<h2>GET</h2>'; echo '<p>Here is a GET form to see what kind of request it will create.</p>'; echo '<form name="input_get" action="/" method="get">'; echo 'First Name: <input type="text" name="first_name" /><br/>'; echo 'Last Name: <input type="text" name="last_name" /><br/>'; echo 'Are You Awesome? <input type="checkbox" name="is_awesome" /><br/>'; echo 'Your Credit Card Number: <input type="password" name="password" /><br/>'; echo '<input type="submit" value="Send!" name="submit_btn" />'; echo '</form>'; echo '<h2>POST</h2>'; echo '<p>Here is a POST form to see what kind of request it will create.</p>'; echo '<form name="input_post" action="/" method="post">'; echo 'First Name: <input type="text" name="first_name" /><br/>'; echo 'Last Name: <input type="text" name="last_name" /><br/>'; echo 'Are You Awesome? <input type="checkbox" name="is_awesome" /><br/>'; echo 'Your Credit Card Number: <input type="password" name="password" /><br/>'; echo '<input type="submit" value="Send!" name="submit_btn" />'; echo '</form>'; echo '</body></html>'; } $server = new StupidHttp_WebServer(); $server->onPattern('GET', '.*')->call(function ($c) { print_request($c->getRequest()); }); $server->onPattern('POST', '.*')->call(function ($c) { print_request($c->getRequest()); }); $server->run(array('run_browser' => true));
<?php require '../lib/StupidHttp/Autoloader.php'; StupidHttp_Autoloader::register(); $server = new StupidHttp_WebServer(); $server->on('GET', '/')->call(function ($r) { echo '<html><body>'; echo '<p>Hello from StupidHttp!</p>'; echo '<form name="input" action="/hello/" method="post">'; echo 'What\'s your name?: <input type="text" name="name" />'; echo '</form>'; echo '<p>Also: say hello to <a href="/hello/Bob">Bob</a>!</p>'; echo '</body></html>'; }); $server->onPattern('GET', '/hello/(.+)')->call(function ($c, $m) { if ($m[1]) { echo '<html><body>'; echo 'Hello, ' . $m[1] . '<br/>'; echo '(brought to you via GET)'; echo '</body></html>'; } }); $server->onPattern('POST', '/hello')->call(function ($c) { $data = $c->getRequest()->getFormData(); echo '<html><body>'; echo 'Hello, ' . $data['name'] . '<br/>'; echo '(brought to you via POST)'; echo '</body></html>'; }); $server->run(array('run_browser' => true));
<?php require '../lib/StupidHttp/Autoloader.php'; StupidHttp_Autoloader::register(); $server = new StupidHttp_WebServer(); $server->on('GET', '/')->call(function ($c) { echo '<html><body>'; echo 'Go to <code>/xxx</code> where <code>xxx</code> is an <a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes">HTTP status code</a>, and see how your browser renders it.'; echo '</body></html>'; }); $server->onPattern('GET', '/(\\d{3})')->call(function ($c, $m) { $c->getResponse()->setStatus(intval($m[1])); }); $server->run(array('run_browser' => true));
<?php require '../lib/StupidHttp/Autoloader.php'; StupidHttp_Autoloader::register(); $documentRoot = getcwd(); $argv = $_SERVER['argv']; if (count($argv) == 2) { $documentRoot = $argv[1]; } $server = new StupidHttp_WebServer($documentRoot); $server->run(array('run_browser' => true));
protected function buildRawResponse($response, &$responseInfo) { $statusName = StupidHttp_WebServer::getHttpStatusHeader($response->getStatus()); $responseStr = "HTTP/1.1 " . $statusName . PHP_EOL; $responseStr .= "Server: StupidHttp" . PHP_EOL; $responseStr .= "Date: " . date("D, d M Y H:i:s T") . PHP_EOL; foreach ($response->getFormattedHeaders() as $header) { $responseStr .= $header . PHP_EOL; } $responseStr .= PHP_EOL; $responseInfo['header_length'] = strlen($responseStr); if ($response->getBody() != null) { $responseStr .= $response->getBody(); } return $responseStr; }
/** * Logs a request. */ public function logRequest($request, $requestInfo, $response) { $statusName = StupidHttp_WebServer::getHttpStatusHeader($response->getStatus()); $replacements = array('%date%' => date($this->dateFormat), '%client_ip%' => $requestInfo['address'], '%client_port%' => $requestInfo['port'], '%method%' => $request->getMethod(), '%uri%' => $request->getUri(), '%path%' => $request->getUriPath(), '%status%' => $response->getStatus(), '%status_name%' => $statusName, '%time%' => $requestInfo['time']); $this->info(str_replace(array_keys($replacements), array_values($replacements), $this->requestFormat)); }