Created
June 20, 2010 01:42
-
-
Save eleclerc/445471 to your computer and use it in GitHub Desktop.
web server in php to serve markdown documents as html
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 | |
/** | |
* The web server for Markdown document. | |
* | |
* The useless server developed as a learning exercise. | |
* | |
* Will serve every `*.markdown` file in current directory as html file. | |
* note: uses a modified markdown for syntax highlighting: http://gist.github.com/113004 | |
*/ | |
require 'Markdown/markdown.php'; | |
class Server | |
{ | |
private $stream; | |
private $socket; | |
private $socketErrno; | |
private $socketErrstr; | |
private $options = array( | |
'host' => '127.0.0.1', | |
'port' => 8080, | |
); | |
public function __construct($options=array()) { | |
$this->options = array_merge($this->options, $options); | |
} | |
public function run() | |
{ | |
set_time_limit(0); | |
$localSocket = 'tcp://' . $this->options['host'] . ':' . $this->options['port']; | |
$this->stream = stream_socket_server( | |
$localSocket, | |
$this->socketErrno, | |
$this->socketErrstr | |
); | |
if (false === $this->stream) { | |
throw new Exception('Cannot create socket stream: ' . $this->socketErrstr); | |
} | |
echo 'Server listening on ' . $localSocket . PHP_EOL; | |
stream_set_blocking($this->stream, 0); | |
while(true) { | |
$this->socket = stream_socket_accept($this->stream); | |
// we assume url will not be longer than 512 chars | |
$request = stream_get_line($this->socket, 512, "\n"); | |
$matches = array(); | |
preg_match('/(\w*)\s([^\s]*)\s(\w*)\/(\d\.\d)/', $request, $matches); | |
$verb = $matches[1]; | |
$document = $matches[2]; | |
$protocol = $matches[3]; | |
$protocolVersion = $matches[4]; | |
$response = "HTTP/1.1 200 OK\n" | |
. "Server: BasicHttpServer\n" | |
. "Content-Type: text/html; charset=utf-8\n" | |
. "\n"; | |
if ($document == '/') { | |
$response .= $this->getIndex(); | |
} elseif ((strtolower($document) == '/favicon.ico')){ | |
continue; | |
} else { | |
$response .= '<p><a href="/">back to index</a><p>' . PHP_EOL; | |
$filename = basename($document); | |
$response .= Markdown(file_get_contents($filename)); | |
} | |
fwrite($this->socket, $response, strlen($response)); | |
fclose($this->socket); | |
} | |
} | |
private function getHeader() | |
{ | |
return '<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Markdown Server</title> | |
</head> | |
<body>'; | |
} | |
private function getFooter() | |
{ | |
return '</body></html>'; | |
} | |
private function getIndex() | |
{ | |
$page = $this->getHeader() . PHP_EOL . '<ul>'; | |
foreach(glob(dirname(__FILE__) . '/*.markdown') as $filename) { | |
$filename = basename($filename); | |
$page .= '<li><a href="/'.$filename.'">' . str_replace('.markdown', '', $filename) . '</a></li>'; | |
} | |
$page .= '</ul>'; | |
return $page; | |
} | |
} | |
$server = new Server(); | |
$server->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment