Skip to content

Instantly share code, notes, and snippets.

@bsergean
Last active June 27, 2021 06:01
Show Gist options
  • Save bsergean/20322145cc4fcb3de4aa2f366621fc62 to your computer and use it in GitHub Desktop.
Save bsergean/20322145cc4fcb3de4aa2f366621fc62 to your computer and use it in GitHub Desktop.
CMakeLists.txt
llhttp_sample$ mkdir build
llhttp_sample$ cd build
build$ cmake ..
make
-- The C compiler identification is AppleClang 12.0.5.12050022
-- The ASM compiler identification is Clang
-- Found assembler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- The CXX compiler identification is AppleClang 12.0.5.12050022
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/benjaminsergeant/tmp/llhttp_sample/build
build$ make
[ 16%] Building C object _deps/llhttp-build/CMakeFiles/llhttp.dir/src/llhttp.c.o
[ 33%] Building C object _deps/llhttp-build/CMakeFiles/llhttp.dir/src/http.c.o
[ 50%] Building C object _deps/llhttp-build/CMakeFiles/llhttp.dir/src/api.c.o
[ 66%] Linking C static library libllhttp.a
[ 66%] Built target llhttp
[ 83%] Building C object CMakeFiles/llhttp_example_program.dir/llhttp_example_program.c.o
[100%] Linking C executable llhttp_example_program
[100%] Built target llhttp_example_program
build$ ./llhttp_example_program 
Successfully parsed!
cmake_minimum_required(VERSION 3.14)
include(FetchContent)
FetchContent_Declare(llhttp
URL "https://github.com/bsergean/llhttp/releases/download/v6.0.3/llhttp-release-v6.0.3.tar.gz")
FetchContent_MakeAvailable(llhttp)
project(llhttp_example_program)
add_executable(llhttp_example_program "")
target_sources(llhttp_example_program PRIVATE llhttp_example_program.c)
target_link_libraries(llhttp_example_program llhttp)
#include "llhttp.h"
#include <stdio.h>
#include <strings.h>
int main()
{
llhttp_t parser;
llhttp_settings_t settings;
/* Initialize user callbacks and settings */
llhttp_settings_init(&settings);
/* Set user callback */
#if 0
settings.on_message_complete = handle_on_message_complete;
#endif
/* Initialize the parser in HTTP_BOTH mode, meaning that it will select between
* HTTP_REQUEST and HTTP_RESPONSE parsing automatically while reading the first
* input.
*/
llhttp_init(&parser, HTTP_BOTH, &settings);
/* Parse request! */
const char* request = "GET / HTTP/1.1\r\n\r\n";
int request_len = strlen(request);
enum llhttp_errno err = llhttp_execute(&parser, request, request_len);
if (err == HPE_OK) {
/* Successfully parsed! */
fprintf(stdout, "Successfully parsed!\n");
} else {
fprintf(stderr, "Parse error: %s %s\n", llhttp_errno_name(err),
parser.reason);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment