Skip to content

Commit

Permalink
Merge branch 'master' of github.com:youngj/httpserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Young committed Sep 17, 2011
2 parents af07a68 + 95a38ab commit 41dd2b7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
12 changes: 8 additions & 4 deletions cgistream.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,26 @@ function stream_read($count)

if (isset($headers['Status']))
{
$status = (int) $headers['Status'];
$status_arr = explode(' ', $headers['Status'], 2);
$status = (int) $status_arr[0];
$status_msg = trim($status_arr[1]);
unset($headers['Status']);
}
else
{
$status = 200;
}

$status_msg = null;
}

$content = substr($buffer, $end_response_headers + 4);
$response = $this->server->response($status, $content, $headers);
$response = $this->server->response($status, $content, $headers, $status_msg);
}

// set status and headers on the server's HTTPResponse object.
// these aren't actually sent to the client,
// but they could be referenced by HTTPServer::get_log_line
$this->response->status = $response->status;
$this->response->status_msg = $response->status_msg;
$this->response->headers = $response->headers;

$this->cur_state = static::BUFFERED;
Expand Down
5 changes: 5 additions & 0 deletions examples/example_www/brew_coffee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("HTTP/1.1 418 I'm a teapot");

echo "<a href='http://tools.ietf.org/html/rfc2324#page-5'>short and stout</a>";
21 changes: 14 additions & 7 deletions httpresponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

class HTTPResponse
{
public $status; // HTTP status code
public $status; // HTTP status code
public $status_msg; // HTTP status message
public $headers; // associative array of HTTP headers

public $content = ''; // response body, as string (optional)
Expand All @@ -20,10 +21,11 @@ class HTTPResponse
public $buffer = ''; // buffer of HTTP response waiting to be written to client socket
public $bytes_written = 0; // count of bytes written to client socket

function __construct($status = 200, $content = '', $headers = null)
function __construct($status = 200, $content = '', $headers = null, $status_msg = null)
{
$this->status = $status;

$this->status_msg = $status_msg;

if (is_resource($content))
{
$this->stream = $content;
Expand All @@ -45,9 +47,14 @@ function stream_eof()
return !$this->stream || feof($this->stream);
}

static function render_status($status)
{
$status_msg = static::$status_messages[$status];
static function render_status($status, $status_msg = null)
{
// Per RFC2616 6.1.1 we pass on a status message from the provider if
// provided, otherwise we use the standard message for that code.
if (empty($status_msg))
{
$status_msg = static::$status_messages[$status];
}
return "HTTP/1.1 $status $status_msg\r\n";
}

Expand All @@ -71,7 +78,7 @@ function render()
$headers['Content-Length'] = $this->get_content_length();
}

return static::render_status($this->status).
return static::render_status($this->status, $this->status_msg).
static::render_headers($headers).
$this->content;
}
Expand Down
38 changes: 28 additions & 10 deletions httpserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ function route_request($request)
{
return $this->text_response(500, "HTTPServer::route_request not implemented");
}

/*
* Subclasses can override to get started event
*/
function listening()
{
$port = $this->port;
echo "HTTP server listening on {$this->addr}:$port (see http://localhost:$port/)...\n";
}

/*
* Subclasses could override to disallow other characters in path names
Expand All @@ -77,6 +86,14 @@ function get_log_line($request)
// http://www.w3.org/Daemon/User/Config/Logging.html#common-logfile-format
return "{$request->remote_addr} - - [$time] \"{$request->request_line}\" {$response->status} {$response->bytes_written}\n";
}

/*
* Subclasses could override for logging or other other post-request events
*/
function request_done($request)
{
echo $this->get_log_line($request);
}

function bind_error($errno, $errstr)
{
Expand Down Expand Up @@ -105,14 +122,15 @@ function run_forever()
$this->bind_error($errno, $errstr);
return;
}

echo "HTTP server listening on $addr_port (see http://localhost:{$this->port}/)...\n";

stream_set_blocking($sock, 0);

$requests =& $this->requests;
$responses =& $this->responses;


// send startup event
$this->listening();

while (true)
{
$read = array();
Expand Down Expand Up @@ -178,7 +196,7 @@ function write_socket($client)
$response = $request->response;
$response_buf =& $response->buffer;

$len = @fwrite($client, $response_buf);
$len = @fwrite($client, $response_buf);
if ($len === false)
{
$this->end_request($request);
Expand All @@ -189,9 +207,9 @@ function write_socket($client)
$response_buf = substr($response_buf, $len);

if ($response->eof())
{
echo $this->get_log_line($request);
{
$this->request_done($request);

if ($request->get_header('Connection') == 'close' || $request->http_version != 'HTTP/1.1')
{
$this->end_request($request);
Expand All @@ -203,7 +221,7 @@ function write_socket($client)
$this->requests[(int)$client] = new HTTPRequest($client);
}
}
}
}
}

function read_response($stream)
Expand Down Expand Up @@ -298,9 +316,9 @@ function end_response($response)
/*
* Returns a generic HTTPResponse object for this server.
*/
function response($status = 200, $content = '', $headers = null)
function response($status = 200, $content = '', $headers = null, $status_msg = null)
{
$response = new HTTPResponse($status, $content, $headers);
$response = new HTTPResponse($status, $content, $headers, $status_msg);
$response->headers['Server'] = $this->server_id;
return $response;
}
Expand Down

0 comments on commit 41dd2b7

Please sign in to comment.