2015-08-30 07:12:46 +08:00
|
|
|
#ifndef MEMGRAPH_SERVER_HTTP_HTTPSERVER_INL
|
|
|
|
#define MEMGRAPH_SERVER_HTTP_HTTPSERVER_INL
|
|
|
|
|
|
|
|
#include "httpparser.hpp"
|
|
|
|
#include "httpserver.hpp"
|
|
|
|
#include "httpconnection.hpp"
|
|
|
|
#include "httpparsersettings.hpp"
|
|
|
|
|
|
|
|
namespace http
|
|
|
|
{
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
|
|
|
HttpParser<Req, Res>::HttpParser()
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
|
|
|
http_parser_init(&parser, HTTP_REQUEST);
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
|
|
|
size_t HttpParser<Req, Res>::
|
|
|
|
execute(settings_t& settings, const char* data, size_t size)
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
|
|
|
return http_parser_execute(&parser, settings, data, size);
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
2015-08-30 07:12:46 +08:00
|
|
|
template <typename T>
|
2015-10-09 07:24:12 +08:00
|
|
|
T* HttpParser<Req, Res>::data()
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
|
|
|
return reinterpret_cast<T*>(parser.data);
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
2015-08-30 07:12:46 +08:00
|
|
|
template <typename T>
|
2015-10-09 07:24:12 +08:00
|
|
|
void HttpParser<Req, Res>::data(T* value)
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
|
|
|
parser.data = reinterpret_cast<void*>(value);
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
|
|
|
int HttpParser<Req, Res>::on_message_begin(http_parser*)
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
|
|
|
int HttpParser<Req, Res>::
|
|
|
|
on_url(http_parser* parser, const char* at, size_t length)
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
2015-10-09 07:24:12 +08:00
|
|
|
connection_t& conn = *reinterpret_cast<connection_t*>(parser->data);
|
2015-08-30 07:12:46 +08:00
|
|
|
conn.request.url = std::string(at, length);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
|
|
|
int HttpParser<Req, Res>::
|
|
|
|
on_status_complete(http_parser*, const char*, size_t)
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
|
|
|
int HttpParser<Req, Res>::
|
|
|
|
on_header_field(http_parser* parser, const char* at, size_t length)
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
2015-10-09 07:24:12 +08:00
|
|
|
connection_t& conn = *reinterpret_cast<connection_t*>(parser->data);
|
2015-08-30 07:12:46 +08:00
|
|
|
conn.parser.last_field = std::string(at, length);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
|
|
|
int HttpParser<Req, Res>::
|
|
|
|
on_header_value(http_parser* parser, const char* at, size_t length)
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
2015-10-09 07:24:12 +08:00
|
|
|
connection_t& conn = *reinterpret_cast<connection_t*>(parser->data);
|
2015-08-30 07:12:46 +08:00
|
|
|
conn.request.headers[conn.parser.last_field] = std::string(at, length);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
|
|
|
int HttpParser<Req, Res>::on_headers_complete(http_parser* parser)
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
2015-10-09 07:24:12 +08:00
|
|
|
connection_t& conn = *reinterpret_cast<connection_t*>(parser->data);
|
2015-08-30 07:12:46 +08:00
|
|
|
|
|
|
|
conn.request.version.major = parser->http_major;
|
|
|
|
conn.request.version.minor = parser->http_minor;
|
|
|
|
|
|
|
|
conn.request.method = static_cast<Method>(parser->method);
|
|
|
|
conn.keep_alive = http_should_keep_alive(parser) == true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
|
|
|
int HttpParser<Req, Res>::
|
|
|
|
on_body(http_parser* parser, const char* at, size_t length)
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
|
|
|
if(length == 0)
|
|
|
|
return 0;
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
connection_t& conn = *reinterpret_cast<connection_t*>(parser->data);
|
2015-08-30 07:12:46 +08:00
|
|
|
conn.request.body.append(at, length);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-09 07:24:12 +08:00
|
|
|
template <class Req, class Res>
|
|
|
|
int HttpParser<Req, Res>::on_message_complete(http_parser* parser)
|
2015-08-30 07:12:46 +08:00
|
|
|
{
|
2015-10-09 07:24:12 +08:00
|
|
|
connection_t& conn = *reinterpret_cast<connection_t*>(parser->data);
|
2015-08-30 07:12:46 +08:00
|
|
|
conn.server.respond(conn);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|