Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zonyitoo/httpsrv

HTTP Server in C++ 11
https://github.com/zonyitoo/httpsrv

Last synced: 3 months ago
JSON representation

HTTP Server in C++ 11

Awesome Lists containing this project

README

        

HTTP Server
-----------

A simple HTTP server in C++ 11.

Build
=====

* Install ``SCons`` and ``clang``.

* Write a ``SConstruct`` file to build the library

.. code:: python

env = Environment()
env["CXX"] = "clang++"

SConscript(['src/SConscript'])

env.Program('test', 'test.cpp',
LIBS=['srv', 'c++abi', 'c++'],
LIBPATH='src',
CPPFLAGS='-std=c++11 -stdlib=libc++ -g',
CPPPATH=['src'])

* Run ``scons``.

Note: GCC currently doesn't support C++11's ``regex``. We have to use ``clang`` and ``libc++``.

Usage
=====

.. code:: cpp

#include "server.h"
#include "ioloop.h"
#include "handler.h"

using namespace httpserver;

class HelloWorldHandler : public HttpHandler {
public:
void get_handler(const HttpRequest& req, HttpResponse& resp,
const std::vector& args) {

resp.body = "Hello World!!";
resp.headers["Content-Type"] = "text/plain";

}
};

int main(int argc, char **argv) {

EPollIOLoop ioloop(argc, argv);

HttpServer server(800, 5, ioloop);
server.register_handler("/", new HelloWorldHandler());
server.register_handler("/([^/]*)/", new HelloWorldHandler());

return ioloop.start();
}