Skip to content

Commit

Permalink
web server proto: unfold namespaces. run forever.
Browse files Browse the repository at this point in the history
  • Loading branch information
aalex committed Feb 14, 2012
1 parent d9a284f commit 619f005
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -58,3 +58,4 @@ tests/check_it_works
tests/stats
xml/
!prototypes/web-server/Makefile
prototypes/web-server/main
39 changes: 16 additions & 23 deletions prototypes/web-server/main.cpp
Expand Up @@ -6,35 +6,27 @@
#include <pion/net/HTTPRequest.hpp>
#include <pion/net/HTTPResponseWriter.hpp>

using namespace std;
using namespace pion;
using namespace pion::net;

void handleRootURI(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn)
void handleRootURI(pion::net::HTTPRequestPtr& http_request, pion::net::TCPConnectionPtr& tcp_conn)
{
static const std::string kHTMLStart("<html><body>\n");
static const std::string kHTMLEnd("</body></html>\n");
static const std::string html_start("<html><body>\n");
static const std::string html_end("</body></html>\n");

HTTPResponseWriterPtr writer(
HTTPResponseWriter::create(
tcp_conn,
*http_request,
boost::bind(
&TCPConnection::finish,
tcp_conn)));
HTTPResponse& r = writer->getResponse();
r.setStatusCode(HTTPTypes::RESPONSE_CODE_OK);
r.setStatusMessage(HTTPTypes::RESPONSE_MESSAGE_OK);
HTTPTypes::QueryParams& params = http_request->getQueryParams();
pion::net::HTTPResponseWriterPtr writer(
pion::net::HTTPResponseWriter::create(tcp_conn, *http_request,
boost::bind(&pion::net::TCPConnection::finish, tcp_conn)));
pion::net::HTTPResponse& r = writer->getResponse();
r.setStatusCode(pion::net::HTTPTypes::RESPONSE_CODE_OK);
r.setStatusMessage(pion::net::HTTPTypes::RESPONSE_MESSAGE_OK);
pion::net::HTTPTypes::QueryParams& params = http_request->getQueryParams();

writer->writeNoCopy(kHTMLStart);
writer->writeNoCopy(html_start);
writer->write("The query about resource ");
writer->write(http_request->getResource());

if (params.size() > 0)
{
writer->write(" has the following parameters: <br>");
for (HTTPTypes::QueryParams::const_iterator i = params.begin();
for (pion::net::HTTPTypes::QueryParams::const_iterator i = params.begin();
i != params.end(); ++i)
{
writer->write(i->first);
Expand All @@ -47,7 +39,7 @@ void handleRootURI(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn)
{
writer->write(" has no parameter.");
}
writer->writeNoCopy(kHTMLEnd);
writer->writeNoCopy(html_end);
writer->send();
}

Expand All @@ -56,13 +48,14 @@ int main(int argc, char *argv[])
static const unsigned int DEFAULT_PORT = 8080;
try
{
HTTPServerPtr hello_server(new HTTPServer(DEFAULT_PORT));
pion::net::HTTPServerPtr hello_server(new pion::net::HTTPServer(DEFAULT_PORT));
hello_server->addResource("/", &handleRootURI);
hello_server->start(); // Spawn a thead to run the HTTP
// service, and the main thread can
// focus on the main work now.
std::cout << "Hello, the server is running now ...\n";
sleep(10);
while (true)
sleep(1);
}
catch (std::exception& e)
{
Expand Down

0 comments on commit 619f005

Please sign in to comment.