diff --git a/Makefile.toml b/Makefile.toml index 2ebfebb..d3dfd07 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -74,12 +74,7 @@ "-style=file:clang-format.yml", "-i", "benchmarks/cpp/src/main.cc", - "references/cpp/src/output.h", - "references/cpp/src/output.cc", - "references/cpp/src/utils.h", - "references/cpp/src/utils.cc", - "references/cpp/src/reference.cc", - "references/cpp/src/readme.cc", + "references/cpp/src/main.cc", ] [tasks."format:js"] diff --git a/references/cpp/Makefile.toml b/references/cpp/Makefile.toml index 98bf576..0663138 100644 --- a/references/cpp/Makefile.toml +++ b/references/cpp/Makefile.toml @@ -5,7 +5,7 @@ dependencies = ["test"] [tasks.build] - dependencies = ["prepare", "milo", "release", "debug", "readme"] + dependencies = ["prepare", "milo", "release", "debug"] [tasks.test] env = { VARIANT = "cpp" } @@ -30,9 +30,7 @@ "../../dist/cpp/release", "-o", "../../tmp/references/cpp/release", - "src/output.cc", - "src/utils.cc", - "src/reference.cc", + "src/main.cc", "../../dist/cpp/release/libmilo.a", ] @@ -44,26 +42,10 @@ "../../dist/cpp/debug", "-o", "../../tmp/references/cpp/debug", - "src/output.cc", - "src/utils.cc", - "src/reference.cc", + "src/main.cc", "../../dist/cpp/debug/libmilo.a", ] -[tasks.readme] - command = "clang++" - args = [ - "-std=c++11", - "-I", - "../../dist/cpp/release", - "-o", - "../../tmp/references/cpp/readme", - "src/output.cc", - "src/utils.cc", - "src/readme.cc", - "../../dist/cpp/release/libmilo.a", - ] - [tasks."milo:headers"] cwd = "../../parser" command = "makers" diff --git a/references/cpp/src/reference.cc b/references/cpp/src/main.cc similarity index 83% rename from references/cpp/src/reference.cc rename to references/cpp/src/main.cc index e1e9ab0..efb03da 100644 --- a/references/cpp/src/reference.cc +++ b/references/cpp/src/main.cc @@ -1,9 +1,92 @@ #include "milo.h" #include "stdio.h" #include "string.h" +#include +#include +#include + +#define MAX_FORMAT 1000 + +typedef uintptr_t usize_t; +typedef unsigned char uchar_t; + +struct context_t { + uchar_t* input; + uchar_t* method; + uchar_t* url; + uchar_t* protocol; + uchar_t* version; +}; + +void clear_context(context_t* context) { + if (context->input != NULL) { + free(context->input); + context->input = NULL; + } + + if (context->method != NULL) { + free(context->method); + context->method = NULL; + } + + if (context->url != NULL) { + free(context->url); + context->url = NULL; + } + + if (context->protocol != NULL) { + free(context->protocol); + context->protocol = NULL; + } -#include "output.h" -#include "utils.h" + if (context->version != NULL) { + free(context->version); + context->version = NULL; + } +} + +uchar_t* create_string() { + return reinterpret_cast(calloc(MAX_FORMAT, sizeof(uchar_t))); +} + +void append_output(const milo_parser::Parser* parser, uchar_t* message, const uchar_t* data, usize_t from, + usize_t size) { + if (data == NULL) { + printf("{ %s, \"data\": null }\n", message); + } else { + uchar_t* string_data = create_string(); + strncpy(reinterpret_cast(string_data), reinterpret_cast(data), size); + printf("{ %s, \"data\": \"%s\" }\n", message, string_data); + } + + free(message); +} + +void event(const milo_parser::Parser* parser, const char* name, const uchar_t* data, usize_t from, usize_t size) { + auto message = create_string(); + snprintf((char*) message, MAX_FORMAT, "\"pos\": %lu, \"event\": \"%s\"", from, name); + append_output(parser, message, data, from, size); +} + +void show_span(const milo_parser::Parser* parser, const char* name, const uchar_t* data, usize_t from, usize_t size) { + auto context = reinterpret_cast(parser->context); + uchar_t* string_data = create_string(); + strncpy(reinterpret_cast(string_data), reinterpret_cast(data), size); + + if (strcmp(name, "method") == 0) { + context->method = string_data; + } else if (strcmp(name, "url") == 0) { + context->url = string_data; + } else if (strcmp(name, "protocol") == 0) { + context->protocol = string_data; + } else if (strcmp(name, "version") == 0) { + context->version = string_data; + } else { + free(string_data); + } + + event(parser, name, data, from, size); +} #define EXTRACT_PAYLOAD(name, parser, from, size) \ auto name = size > 0 ? reinterpret_cast(parser->context)->input + from : NULL; diff --git a/references/cpp/src/output.cc b/references/cpp/src/output.cc deleted file mode 100644 index d39a079..0000000 --- a/references/cpp/src/output.cc +++ /dev/null @@ -1,41 +0,0 @@ -#include "output.h" -#include "milo.h" - -void append_output(const milo_parser::Parser* parser, uchar_t* message, const uchar_t* data, usize_t from, - usize_t size) { - if (data == NULL) { - printf("{ %s, \"data\": null }\n", message); - } else { - uchar_t* string_data = create_string(); - strncpy(reinterpret_cast(string_data), reinterpret_cast(data), size); - printf("{ %s, \"data\": \"%s\" }\n", message, string_data); - } - - free(message); -} - -void event(const milo_parser::Parser* parser, const char* name, const uchar_t* data, usize_t from, usize_t size) { - auto message = create_string(); - snprintf((char*) message, MAX_FORMAT, "\"pos\": %lu, \"event\": \"%s\"", from, name); - append_output(parser, message, data, from, size); -} - -void show_span(const milo_parser::Parser* parser, const char* name, const uchar_t* data, usize_t from, usize_t size) { - auto context = reinterpret_cast(parser->context); - uchar_t* string_data = create_string(); - strncpy(reinterpret_cast(string_data), reinterpret_cast(data), size); - - if (strcmp(name, "method") == 0) { - context->method = string_data; - } else if (strcmp(name, "url") == 0) { - context->url = string_data; - } else if (strcmp(name, "protocol") == 0) { - context->protocol = string_data; - } else if (strcmp(name, "version") == 0) { - context->version = string_data; - } else { - free(string_data); - } - - event(parser, name, data, from, size); -} diff --git a/references/cpp/src/output.h b/references/cpp/src/output.h deleted file mode 100644 index 8c6a319..0000000 --- a/references/cpp/src/output.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef MILO_OUTPUT_H -#define MILO_OUTPUT_H - -#include "milo.h" -#include "stdio.h" -#include "string.h" - -#include "utils.h" - -void append_output(const milo_parser::Parser* parser, uchar_t* message, const uchar_t* data, usize_t from, - usize_t size); -void event(const milo_parser::Parser* parser, const char* name, const uchar_t* data, usize_t from, usize_t size); -void show_span(const milo_parser::Parser* parser, const char* name, const uchar_t* data, usize_t from, usize_t size); -#endif // MILO_OUTPUT_H diff --git a/references/cpp/src/readme.cc b/references/cpp/src/readme.cc deleted file mode 100644 index 637d07c..0000000 --- a/references/cpp/src/readme.cc +++ /dev/null @@ -1,42 +0,0 @@ -#include "milo.h" -#include "stdio.h" -#include "string.h" -#include - -int main() { - // Create the parser. - milo_parser::Parser* parser = milo_parser::milo_create(); - - // Prepare a message to parse. - const char* message = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nabc"; - - parser->context = (char*) message; - - /* - Milo works using callbacks. - - All callbacks have the same signature, which characterizes the payload: - - * p: The current parser. - * at: The payload offset. - * len: The payload length. - - The payload parameters above are relative to the last data sent to the milo_parse method. - - If the current callback has no payload, both values are set to 0. - */ - parser->callbacks.on_data = [](milo_parser::Parser* p, uintptr_t from, uintptr_t size) { - char* payload = reinterpret_cast(malloc(sizeof(char) * size)); - strncpy(payload, reinterpret_cast(p->context) + from, size); - - printf("Pos=%" PRIuPTR " Body: %.*s\n", from, static_cast(size), payload); - free(payload); - }; - parser->active_callbacks |= milo_parser::CALLBACK_ACTIVE_ON_DATA; - - // Now perform the main parsing using milo.parse. The method returns the number of consumed characters. - milo_parser::milo_parse(parser, reinterpret_cast(message), strlen(message)); - - // Cleanup used resources. - milo_parser::milo_destroy(parser); -} diff --git a/references/cpp/src/utils.cc b/references/cpp/src/utils.cc deleted file mode 100644 index 06add00..0000000 --- a/references/cpp/src/utils.cc +++ /dev/null @@ -1,32 +0,0 @@ -#include "utils.h" - -void clear_context(context_t* context) { - if (context->input != NULL) { - free(context->input); - context->input = NULL; - } - - if (context->method != NULL) { - free(context->method); - context->method = NULL; - } - - if (context->url != NULL) { - free(context->url); - context->url = NULL; - } - - if (context->protocol != NULL) { - free(context->protocol); - context->protocol = NULL; - } - - if (context->version != NULL) { - free(context->version); - context->version = NULL; - } -} - -uchar_t* create_string() { - return reinterpret_cast(calloc(MAX_FORMAT, sizeof(uchar_t))); -} diff --git a/references/cpp/src/utils.h b/references/cpp/src/utils.h deleted file mode 100644 index cfd2b37..0000000 --- a/references/cpp/src/utils.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef MILO_UTILS_H -#define MILO_UTILS_H - -#include "stdio.h" -#include -#include -#define MAX_FORMAT 1000 - -typedef uintptr_t usize_t; -typedef unsigned char uchar_t; - -struct context_t { - uchar_t* input; - uchar_t* method; - uchar_t* url; - uchar_t* protocol; - uchar_t* version; -}; - -void clear_context(context_t* context); -uchar_t* create_string(); -#endif // MILO_UTILS_H \ No newline at end of file