Skip to content

Commit

Permalink
Migrate from deprecated http_parser to llhttp library
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulh committed Jan 16, 2024
1 parent 566fb33 commit 4ec4c6e
Show file tree
Hide file tree
Showing 32 changed files with 12,445 additions and 8,059 deletions.
1 change: 1 addition & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"${workspaceFolder}/**",
"/usr/include/sigc++-2.0",
"/usr/lib/x86_64-linux-gnu/sigc++-2.0/include",
"${workspaceFolder}/src/lib/llhttp/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"cinttypes": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp"
"variant": "cpp",
"any": "cpp"
}
}
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
}
},
"dependsOn": [
"clean"
"create_build_folder"
],
"group": "build",
},
Expand All @@ -62,7 +62,7 @@
"cwd": "${workspaceFolder}/_build",
},
"dependsOn": [
"clean"
"create_build_folder"
],
"group": "build"
}
Expand Down
12 changes: 6 additions & 6 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

CALAOS_VERSION
AC_INIT([calaos], [calaos_version], [[email protected]])
AC_PREREQ([2.59])
AC_INIT([calaos],[calaos_version],[[email protected]])
AC_PREREQ([2.71])
AC_CONFIG_SRCDIR([configure.ac])
AC_CONFIG_MACRO_DIR([m4])

Expand All @@ -10,7 +10,7 @@ AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([tar-ustar 1.6 dist-xz subdir-objects foreign])
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])

AC_GNU_SOURCE
AC_USE_SYSTEM_EXTENSIONS
AC_SYS_LARGEFILE

LT_PREREQ([2.2])
Expand Down Expand Up @@ -85,7 +85,7 @@ AC_CHECK_HEADERS([owcapi.h], [have_owcapi="yes"])
AM_CONDITIONAL([HAVE_OWCAPI], [test "x${have_owcapi}" = "xyes"])
if test "x${have_owcapi}" == "xyes"
then
AC_CHECK_LIB([owcapi], [OW_init],, AC_ERROR([Missing owfs library]))
AC_CHECK_LIB([owcapi], [OW_init],, AC_MSG_ERROR(Missing owfs library))
fi

have_libusb="no"
Expand All @@ -109,7 +109,7 @@ AC_CHECK_HEADERS([eibclient.h], [have_libknx="yes"])
AM_CONDITIONAL([HAVE_LIBKNX], [test "x${have_libknx}" = "xyes"])
if test "x${have_libknx}" == "xyes"
then
AC_CHECK_LIB([eibclient], [EIBSocketLocal],, AC_ERROR([Missing eibclient library]))
AC_CHECK_LIB([eibclient], [EIBSocketLocal],, AC_MSG_ERROR(Missing eibclient library))
AC_DEFINE([HAVE_LIBKNX], [1], [libknx])
fi

Expand All @@ -134,7 +134,7 @@ AC_CHECK_HEADERS([mosquitto.h], [have_mosquitto="yes"])
AM_CONDITIONAL([HAVE_LIBMOSQUITTO], [test "x${have_mosquitto}" = "xyes"])
if test "x${have_mosquitto}" == "xyes"
then
AC_CHECK_LIB([mosquitto], [mosquitto_lib_init],, AC_ERROR([Missing mosquitto library]))
AC_CHECK_LIB([mosquitto], [mosquitto_lib_init],, AC_MSG_ERROR(Missing mosquitto library))
AC_DEFINE([HAVE_LIBMOSQUITTO], [1], [libmosquitto])
fi

Expand Down
29 changes: 16 additions & 13 deletions src/bin/calaos_server/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using namespace Calaos;
index++)
#endif

int _parser_begin(http_parser *parser)
int _parser_begin(llhttp_t *parser)
{
HttpClient *client = reinterpret_cast<HttpClient *>(parser->data);

Expand All @@ -51,7 +51,7 @@ int _parser_begin(http_parser *parser)
return 0;
}

int _parser_header_field(http_parser *parser, const char *at, size_t length)
int _parser_header_field(llhttp_t *parser, const char *at, size_t length)
{
HttpClient *client = reinterpret_cast<HttpClient *>(parser->data);

Expand All @@ -72,7 +72,7 @@ int _parser_header_field(http_parser *parser, const char *at, size_t length)
return 0;
}

int _parser_header_value(http_parser *parser, const char *at, size_t length)
int _parser_header_value(llhttp_t *parser, const char *at, size_t length)
{
HttpClient *client = reinterpret_cast<HttpClient *>(parser->data);

Expand All @@ -84,7 +84,7 @@ int _parser_header_value(http_parser *parser, const char *at, size_t length)
return 0;
}

int _parser_headers_complete(http_parser *parser)
int _parser_headers_complete(llhttp_t *parser)
{
HttpClient *client = reinterpret_cast<HttpClient *>(parser->data);

Expand All @@ -100,7 +100,7 @@ int _parser_headers_complete(http_parser *parser)
return 0;
}

int _parser_url(http_parser *parser, const char *at, size_t length)
int _parser_url(llhttp_t *parser, const char *at, size_t length)
{
HttpClient *client = reinterpret_cast<HttpClient *>(parser->data);

Expand All @@ -109,7 +109,7 @@ int _parser_url(http_parser *parser, const char *at, size_t length)
return 0;
}

int _parser_message_complete(http_parser *parser)
int _parser_message_complete(llhttp_t *parser)
{
HttpClient *client = reinterpret_cast<HttpClient *>(parser->data);

Expand All @@ -119,7 +119,7 @@ int _parser_message_complete(http_parser *parser)
return 0;
}

int _parser_body_complete(http_parser* parser, const char *at, size_t length)
int _parser_body_complete(llhttp_t* parser, const char *at, size_t length)
{
HttpClient *client = reinterpret_cast<HttpClient *>(parser->data);

Expand All @@ -131,6 +131,8 @@ int _parser_body_complete(http_parser* parser, const char *at, size_t length)
HttpClient::HttpClient(const std::shared_ptr<uvw::TcpHandle> &client):
client_conn(client)
{
llhttp_settings_init(&parser_settings);

//set up callbacks for the parser
parser_settings.on_message_begin = _parser_begin;
parser_settings.on_url = _parser_url;
Expand All @@ -140,8 +142,8 @@ HttpClient::HttpClient(const std::shared_ptr<uvw::TcpHandle> &client):
parser_settings.on_body = _parser_body_complete;
parser_settings.on_message_complete = _parser_message_complete;

parser = (http_parser *)calloc(1, sizeof(http_parser));
http_parser_init(parser, HTTP_REQUEST);
parser = (llhttp_t *)calloc(1, sizeof(llhttp_t));
llhttp_init(parser, HTTP_REQUEST, &parser_settings);
parser->data = this;

cDebugDom("network") << this;
Expand All @@ -166,15 +168,16 @@ HttpClient::~HttpClient()

int HttpClient::processHeaders(const string &request)
{
size_t nparsed;

nparsed = http_parser_execute(parser, &parser_settings, request.c_str(), request.size());
enum llhttp_errno err = llhttp_execute(parser, request.c_str(), request.size());

if (nparsed != request.size())
if (err != HPE_OK)
{
/* Handle error. Usually just close the connection. */
CloseConnection();

cDebugDom("network") << "Error parsing HTTP request: " << llhttp_errno_name(err)
<< " (" << parser->reason << ")";

return HTTP_PROCESS_DONE;
}

Expand Down
20 changes: 10 additions & 10 deletions src/bin/calaos_server/HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define S_HttpClient_H

#include "Calaos.h"
#include "http_parser.h"
#include "llhttp.h"
#include <unordered_map>
#include "JsonApiHandlerHttp.h"
#include "JsonApiHandlerWS.h"
Expand All @@ -42,8 +42,8 @@ class HttpClient: public sigc::trackable

std::shared_ptr<uvw::TcpHandle> client_conn;

http_parser_settings parser_settings;
http_parser *parser;
llhttp_settings_t parser_settings;
llhttp_t *parser;

bool parse_done = false;
unsigned char request_method;
Expand Down Expand Up @@ -95,13 +95,13 @@ class HttpClient: public sigc::trackable

string getMimeType(const string &file_ext);

friend int _parser_begin(http_parser *parser);
friend int _parser_header_field(http_parser *parser, const char *at, size_t length);
friend int _parser_header_value(http_parser *parser, const char *at, size_t length);
friend int _parser_headers_complete(http_parser *parser);
friend int _parser_message_complete(http_parser *parser);
friend int _parser_url(http_parser *parser, const char *at, size_t length);
friend int _parser_body_complete(http_parser* parser, const char *at, size_t length);
friend int _parser_begin(llhttp_t *parser);
friend int _parser_header_field(llhttp_t *parser, const char *at, size_t length);
friend int _parser_header_value(llhttp_t *parser, const char *at, size_t length);
friend int _parser_headers_complete(llhttp_t *parser);
friend int _parser_message_complete(llhttp_t *parser);
friend int _parser_url(llhttp_t *parser, const char *at, size_t length);
friend int _parser_body_complete(llhttp_t* parser, const char *at, size_t length);

public:
HttpClient(const std::shared_ptr<uvw::TcpHandle> &client);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/calaos_server/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ AM_CPPFLAGS = \
-I$(top_srcdir)/src/lib/LuaScript \
-I$(top_srcdir)/src/lib/TinyXML \
-I$(top_srcdir)/src/lib/cJSON \
-I$(top_srcdir)/src/lib/http-parser \
-I$(top_srcdir)/src/lib/llhttp/include \
-I$(top_srcdir)/src/lib/libquickmail \
-I$(top_srcdir)/src/lib/uri_parser \
-I$(top_srcdir)/src/lib/sqlite_modern_cpp/hdr \
Expand Down
2 changes: 1 addition & 1 deletion src/bin/calaos_server/WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void WebSocket::ProcessData(string data)
if (parse_done)
{
//init parser again
http_parser_init(parser, HTTP_REQUEST);
llhttp_init(parser, HTTP_REQUEST, &parser_settings);
parser->data = this;

handleJsonRequest();
Expand Down
10 changes: 6 additions & 4 deletions src/lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AM_CXXFLAGS = \
-I$(top_srcdir)/src/bin/calaos_server/IPCam \
-I$(top_srcdir)/src/bin/calaos_server/Rules \
-I$(top_srcdir)/src/lib \
-I$(top_srcdir)/src/lib/http-parser \
-I$(top_srcdir)/src/lib/llhttp/include \
-I$(top_srcdir)/src/lib/libquickmail \
-I$(top_srcdir)/src/lib/uri_parser \
-I. \
Expand All @@ -36,7 +36,7 @@ AM_CFLAGS = \
-I$(top_srcdir)/src/bin/calaos_server/Rules \
-I$(top_srcdir)/src/lib \
-I$(top_srcdir)/src/lib/LuaScript \
-I$(top_srcdir)/src/lib/http-parser \
-I$(top_srcdir)/src/lib/llhttp/include \
-I$(top_srcdir)/src/lib/libquickmail \
-I$(top_srcdir)/src/lib/uri_parser \
-I. \
Expand Down Expand Up @@ -106,8 +106,10 @@ libcalaos_common_la_SOURCES = \
WebSocketFrame.h \
base64.cpp \
base64.h \
http-parser/http_parser.c \
http-parser/http_parser.h \
llhttp/src/api.c \
llhttp/src/http.c \
llhttp/src/llhttp.c \
llhttp/include/llhttp.h \
libquickmail/quickmail.c \
libquickmail/quickmail.h \
sunset.c \
Expand Down
68 changes: 0 additions & 68 deletions src/lib/http-parser/AUTHORS

This file was deleted.

4 changes: 0 additions & 4 deletions src/lib/http-parser/CONTRIBUTIONS

This file was deleted.

23 changes: 0 additions & 23 deletions src/lib/http-parser/LICENSE-MIT

This file was deleted.

Loading

0 comments on commit 4ec4c6e

Please sign in to comment.