#!/opt/local/bin/php <?php require "nanoserv/handlers/HTTP/XML_RPC/Persistent_Server.php"; class dumb_xmlrpc { public $bar; public function getFoo($bar) { return array("foo" => "foo{$bar}", "bar" => $this->bar++); } } $o = new dumb_xmlrpc(); $l = \Nanoserv\Core::New_Listener("tcp://0.0.0.0:800", '\\Nanoserv\\HTTP\\XML_RPC\\Persistent_Server', \Nanoserv\Core::New_Shared_Object($o)); $l->Set_Forking(); $l->Activate(); $o->bar = 100; \Nanoserv\Core::Run();
#!/opt/local/bin/php <?php require "nanoserv/handlers/HTTP/JSON_RPC/Direct_Server.php"; class dumb_jsonrpc_httpd extends \Nanoserv\HTTP\JSON_RPC\Direct_Server { public function getFoo($bar) { return array("foo" => $bar); } } \Nanoserv\Core::New_Listener("tcp://0.0.0.0:800", "dumb_jsonrpc_httpd")->Activate(); \Nanoserv\Core::Run();
#!/opt/local/bin/php <?php require "nanoserv/handlers/HTTP/Server.php"; use Nanoserv\Core as Nanoserv; class dumb_httpd extends \Nanoserv\HTTP\Server { public function on_Request($url) { return "You asked for url : <b>{$url}</b>"; } } // Replace [::] below with 0.0.0.0 for IPv4-only operation $l = Nanoserv::New_Listener("tcp://[::]:800", "dumb_httpd"); $l->Set_Forking(); $l->Activate(); Nanoserv::Run();
#!/opt/local/bin/php <?php require "nanoserv/handlers/SMTP/Server.php"; class dumb_smtpd extends \Nanoserv\SMTP\Server { function on_Mail($from, $to, $data) { echo "mail from: {$from}\n"; echo "mail to : " . implode(", ", $to) . "\n"; echo "\n" . $data . "[EOM]\n"; return false; } } \Nanoserv\Core::New_Listener("tcp://0.0.0.0:250", "dumb_smtpd")->Activate(); \Nanoserv\Core::Run();
#!/opt/local/bin/php <?php require "nanoserv/handlers/HTTP/Server.php"; use Nanoserv\Core as Nanoserv; class dumb_httpd extends \Nanoserv\HTTP\Server { public function on_Request($url) { return "You asked for url : <b>{$url}</b>\n"; } } // Replace [::] below with 0.0.0.0 for IPv4-only operation Nanoserv::New_Listener("tcp://[::]:800", "dumb_httpd")->Activate(); Nanoserv::Run();
$newnick = trim(strtok("")); $msg = "* {$this->nick} is now known as {$newnick}"; $this->nick = $newnick; break; case "ME": $msg = "* {$this->nick} " . trim(strtok("")); break; } } else { if ($ts = trim($s)) { $msg = "<{$this->nick}> {$ts}"; } } if (isset($msg)) { $this->Pubmsg($msg); } } public function on_Disconnect() { $this->Pubmsg("* {$this->nick} has quit"); } protected function Pubmsg($s) { $msg = $s . "\r\n"; foreach (Nanoserv::Get_Connections() as $c) { $c->Write($msg); } } } Nanoserv::New_Listener("tcp://[::]:999", "simple_chat_server")->Activate(); Nanoserv::Run();
$cfn = realpath($this->docroot . DIRECTORY_SEPARATOR . $url); if (strpos($cfn, $this->docroot) !== 0 || !is_readable($cfn)) { $this->Set_Response_Status(404); return "<html><body><h1>Not Found</h1></body></html>"; } else { if (is_dir($cfn)) { $ret .= "<ul>"; foreach (scandir($cfn) as $dname) { if ($dname == ".") { continue; } $ret .= "<li><a href='" . addslashes($url . ($url[strlen($url) - 1] != "/" ? "/" : "") . $dname) . "'>" . $dname . "</a></li>"; } $ret .= "</ul><hr /><span style='font-size:10px;'>nanoserv/2.1.0</span>"; return "<html><body>" . $ret . "</body></html>"; } else { if (is_callable("mime_content_type") && ($ct = mime_content_type($cfn))) { $this->Set_Content_Type($ct); } return file_get_contents($cfn); } } } } $docroot = realpath($argv[1]) or die("usage: " . $argv[0] . " /path/to/document/root\n"); \Nanoserv\Core::New_Listener("tcp://0.0.0.0:800", "NS_httpd", $docroot)->Activate(); $ssl = \Nanoserv\Core::New_Listener("ssl://0.0.0.0:443", "NS_httpd", $docroot); $ssl->Set_Option("ssl", "local_cert", "/etc/ssl/certs/nanoweb.pem"); $ssl->Set_Forking(); $ssl->Activate() or die("unable to activate ssl listener"); \Nanoserv\Core::Run();
<? require "nanoserv/nanoserv.php"; class timer_test extends \Nanoserv\Connection_Handler { public $cd = 10; function on_Accept() { $this->Write("starting countdown in 5 seconds ... "); \Nanoserv\Core::New_Timer(5, array($this, "Countdown")); } function Countdown() { $this->Write($this->cd--." "); \Nanoserv\Core::New_Timer(0.5, array($this, $this->cd ? "Countdown" : "Disconnect")); } } \Nanoserv\Core::New_Listener("tcp://0.0.0.0:999", "timer_test")->Activate(); \Nanoserv\Core::Run(); ?>
#!/opt/local/bin/php <?php require "nanoserv/handlers/HTTP/Server.php"; class dumb_httpd extends \Nanoserv\HTTP\Server { public function on_Request($url) { echo date("Ymd:His") . " got hit by " . $this->socket->Get_Peer_Name() . " : '{$url}'\n"; return "You asked for url : <b>{$url}</b>"; } } $l = \Nanoserv\Core::New_Listener("ssl://0.0.0.0:443", "dumb_httpd"); $l->Set_Option("ssl", "local_cert", "/etc/ssl/certs/nanoweb.pem"); $l->Set_Forking(); $l->Activate(); \Nanoserv\Core::Run();
#!/opt/local/bin/php <?php require "nanoserv/handlers/HTTP/Server.php"; class dumb_httpd extends \Nanoserv\HTTP\Server { private $shared_obj; public function __construct($o) { $this->shared_obj = $o; } public function on_Request($url) { $val = $this->shared_obj->foo(); return "[" . posix_getpid() . "] Content of shared value : <b>{$val}</b>"; } } class foo_inc { public $value = 0; public function foo() { return ++$this->value; } } $o = \Nanoserv\Core::New_Shared_Object(new foo_inc()); $l = \Nanoserv\Core::New_Listener("tcp://0.0.0.0:800", "dumb_httpd", $o); $l->Set_Forking(); $l->Activate(); $o->value = 100; \Nanoserv\Core::Run();
#!/opt/local/bin/php <?php // Simple time server with tcp and udp support (RFC868 compliant) require "nanoserv/nanoserv.php"; use Nanoserv\Core as Nanoserv; function rfc868_time() { return pack("N", time() + 2208988800.0); } class time_server_tcp extends \Nanoserv\Connection_Handler { public function on_Accept() { $this->Write(rfc868_time(), array($this, "Disconnect")); } } class time_server_udp extends \Nanoserv\Datagram_Handler { public function on_Read($from, $data) { $this->Write($from, rfc868_time()); } } Nanoserv::New_Listener("tcp://0.0.0.0:37", "time_server_tcp")->Activate(); Nanoserv::New_Listener("udp://0.0.0.0:37", "time_server_udp")->Activate(); Nanoserv::Run();
#!/opt/local/bin/php <?php require "nanoserv/handlers/Syslog/Server.php"; class dumb_syslogger extends \Nanoserv\Syslog\Server { public function on_Event($host, $facility, $severity, $message) { echo "--- Syslog event --------------------------------------------------------------\n"; echo "Datetime : " . date("Y-m-d H:i:s") . "\n"; echo "Host : {$host}\n"; echo "Facility : " . self::Code_To_Facility($facility) . "\n"; echo "Severity : {$severity}\n"; echo "Message : " . trim($message) . "\n"; } } \Nanoserv\Core::New_Listener("udp://0.0.0.0:514", "dumb_syslogger")->Activate(); \Nanoserv\Core::Run();