Skip to content

Commit

Permalink
Adding static test page to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shevron committed Mar 21, 2009
1 parent 7d102a1 commit f55214f
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 36 deletions.
4 changes: 3 additions & 1 deletion examples/server.php
Expand Up @@ -6,5 +6,7 @@
);

require_once 'Aspamia/Http/Server.php';
$server = new Aspamia_Http_Server();
$server = new Aspamia_Http_Server(array(
'handler' => 'Aspamia_Http_Server_Handler_Static'
));
$server->run();
Binary file added examples/static/aspamia-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions examples/static/index.html
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Aspamia Test Page</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>
<h1>
Welcome to Aspamia!
</h1>
<p>
<img src="aspamia-logo.png" alt="Aspamia Web Server" />
This file is served by the Aspamia Web Server running on this server.
</p>
<p>
Aspamia is a simplistic, lightweight web server implemented entirely in PHP.
It is designed to be used for development and testing purposes. It is not
designed for serving real web sites.
</p>
<p>
You can learn more about Aspamia here: <a href="http://aspamia.org/">http://aspamia.org</a>
</p>
<p class="footer">
<span>Aspamia is &copy; 2009 Shahar Evron, all rights reserved</span>
</p>
</body>

</html>
29 changes: 29 additions & 0 deletions examples/static/style.css
@@ -0,0 +1,29 @@
body {
font-family: Georgia, serif;
padding: 0px;
margin: 0px;
}

h1 {
margin: 0px;
padding: 30px 10px;
background-color: #8B6AB1;
color: #522D7D;
}

p {
padding: 0px 25px;
}

p.footer {
margin-top: 10em;
text-align: center;
font-size: 9pt;
color: #B0D97B;
}

img {
position: absolute;
top: 10px;
right: 15px;
}
13 changes: 12 additions & 1 deletion library/Aspamia/Http/Response.php
Expand Up @@ -179,14 +179,25 @@ protected function _getStartLine()
* @param integer $code HTTP statis code
* @return string | null
*/
public static function getHttpReasonPhrase($code)
static public function getHttpReasonPhrase($code)
{
if (isset(self::$_messages[$code])) {
return self::$_messages[$code];
} else {
return null;
}
}

/**
* Create an HTTP response object from a string
*
* @param string $message
* @return Aspamia_Http_Response
*/
static public function fromString($message)
{
// TODO: Implement me!
}

// /**
// * Extract the response code from a response string
Expand Down
16 changes: 13 additions & 3 deletions library/Aspamia/Http/Server.php
Expand Up @@ -6,6 +6,8 @@

class Aspamia_Http_Server
{
const ASPAMIA_VERSION = '0.0.1';

const DEFAULT_ADDR = '127.0.0.1';
const DEFAULT_PORT = 8000;

Expand Down Expand Up @@ -51,12 +53,12 @@ public function __construct($config = array())

public function setConfig($config)
{
if ($config instanceof Aspamia_Config) {
if ($config instanceof Zend_Config) {
$config = $config->toArray();
}

if (! is_array($config)) {
throw new ErrorException("\$config is expected to be an array or a Aspamia_Config object, got " . gettype($config));
throw new ErrorException("\$config is expected to be an array or a Zend_Config object, got " . gettype($config));
}

foreach($config as $k => $v) {
Expand Down Expand Up @@ -105,7 +107,15 @@ protected function _handle($connection)
$request = $this->_readRequest($connection);
$response = $this->_handler->handle($request);

// TEST
$serverSignature = 'Aspamia/' . self::ASPAMIA_VERSION . ' ' .
'PHP/' . PHP_VERSION;

$response->setHeader(array(
'Server' => $serverSignature,
'Date' => date(DATE_RFC1123)
));

// TODO: Right now only 'close' is working, make keep-alive work too.
$response->setHeader('connection', 'close');

fwrite($connection, (string) $response);
Expand Down
33 changes: 30 additions & 3 deletions library/Aspamia/Http/Server/Handler/Abstract.php
Expand Up @@ -2,28 +2,55 @@

abstract class Aspamia_Http_Server_Handler_Abstract
{
/**
* Configuration data
*
* @var array
*/
protected $_config = array();

public function __construct($config = array())
{
$this->setConfig($config);
}

/**
* Set the handler configuration
*
* @param Zend_Config | array $config
*/
public function setConfig($config)
{
if ($config instanceof Aspamia_Config) {
if ($config instanceof Zend_Config) {
$config = $config->toArray();
}

if (! is_array($config)) {
require_once 'Aspamia/Http/Server/Handler/Exception.php';
throw new Aspamia_Http_Server_Handler_Exception("Configuration is expected to be an array or a Aspamia_Config object, got " . gettype($config));
throw new Aspamia_Http_Server_Handler_Exception("Configuration is expected to be an array or a Zend_Config object, got " . gettype($config));
}

foreach ($config as $k => $v) {
$this->_config[$k] = $v;
}
$this->_config = array();
}

/**
* Create an error HTTP response message based on code and message
*
* @param integer $code
* @param string $message
* @return Aspamia_Http_Response
*/
static protected function _errorResponse($code, $message)
{
$headers = array(
'content-type' => 'text/plain',
'content-length' => strlen($message),
'x-powered-by' => 'Aspamia_Http_Server/StaticHandler'
);

return new Aspamia_Http_Response($code, $headers, $message);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion library/Aspamia/Http/Server/Handler/Mock.php
Expand Up @@ -8,7 +8,11 @@ class Aspamia_Http_Server_Handler_Mock extends Aspamia_Http_Server_Handler_Abstr

public function setResponse($response)
{
// Set the response from either a string or a response object
if ($response instanceof Aspamia_Http_Response) {
$this->_response = $response;
} else {
$this->_response = Aspamia_Http_Message::fromString($response);
}
}

public function handle(Aspamia_Http_Request $request)
Expand Down

0 comments on commit f55214f

Please sign in to comment.