memgraph/speedy/http/httpserver.inl

80 lines
1.8 KiB
Plaintext
Raw Normal View History

#ifndef UVMACHINE_HTTP_HTTPSERVER_INL
#define UVMACHINE_HTTP_HTTPSERVER_INL
#include "httpserver.hpp"
namespace http
{
2015-10-09 07:24:12 +08:00
static BlockAllocator<65536> buffer_allocator;
template <class Req, class Res>
HttpServer<Req, Res>::HttpServer(uv::UvLoop& loop)
: loop(loop), stream(loop) {}
2015-10-09 07:24:12 +08:00
template <class Req, class Res>
void HttpServer<Req, Res>::listen(const Ipv4& ip, request_cb_t callback)
{
request_cb = callback;
stream.data(this);
uv_tcp_bind(stream, ip, 0);
2015-10-09 07:24:12 +08:00
uv_listen(stream, 128, server_t::on_connect);
}
2015-10-09 07:24:12 +08:00
template <class Req, class Res>
HttpServer<Req, Res>::operator uv_tcp_t*()
{
return stream;
}
2015-10-09 07:24:12 +08:00
template <class Req, class Res>
HttpServer<Req, Res>::operator uv_stream_t*()
{
return stream;
}
2015-10-09 07:24:12 +08:00
template <class Req, class Res>
void HttpServer<Req, Res>::on_connect(uv_stream_t* tcp_server, int)
{
2015-10-09 07:24:12 +08:00
server_t& server = *reinterpret_cast<server_t*>(tcp_server->data);
2015-10-09 07:24:12 +08:00
auto connection = new connection_t(server.loop, server);
uv_accept(server, connection->client);
2015-10-09 07:24:12 +08:00
uv_read_start(connection->client, server_t::on_alloc, server_t::on_read);
}
2015-10-09 07:24:12 +08:00
template <class Req, class Res>
void HttpServer<Req, Res>::
on_alloc(uv_handle_t*, size_t, uv_buf_t* buf)
{
2015-10-09 07:24:12 +08:00
buf->base = static_cast<char*>(buffer_allocator.acquire());
buf->len = buffer_allocator.size;
}
2015-10-09 07:24:12 +08:00
template <class Req, class Res>
void HttpServer<Req, Res>::
on_read(uv_stream_t* tcp_client, ssize_t nread, const uv_buf_t* buf)
{
2015-10-09 07:24:12 +08:00
connection_t& conn = *reinterpret_cast<connection_t*>(tcp_client->data);
if(nread >= 0)
conn.parser.execute(conn.server.settings, buf->base, nread);
else
conn.close();
2015-10-09 07:24:12 +08:00
buffer_allocator.release(buf->base);
}
2015-10-09 07:24:12 +08:00
template <class Req, class Res>
void HttpServer<Req, Res>::respond(connection_t& conn)
{
conn.server.request_cb(conn.request, conn.response);
}
}
#endif