From bb18b29e07b22c1370ea2c8eeafd9ee9209b490c Mon Sep 17 00:00:00 2001 From: Davide Di Carlo Date: Wed, 1 Aug 2018 17:30:30 +0200 Subject: [PATCH] 0.3.0 --- .gitignore | 3 + CMakeLists.txt | 52 +- LICENSE | 35 +- README.md | 36 +- bench.log | 60 - bench.py | 14 - bench.sh | 16 - deps/alligator/alligator.c | 58 + deps/alligator/alligator.h | 69 + deps/alligator/alligator_config.h | 70 + deps/alligator/build.cmake | 7 + deps/alligator/package.json | 23 + deps/atom/atom.c | 208 +++ deps/atom/atom.h | 117 ++ deps/atom/build.cmake | 2 + deps/atom/package.json | 25 + deps/error/build.cmake | 6 + deps/error/error.c | 38 + deps/error/error.h | 98 ++ deps/error/package.json | 16 + deps/option/build.cmake | 7 + deps/option/option.c | 65 + deps/option/option.h | 144 ++ deps/option/package.json | 19 + deps/panic/build.cmake | 6 + deps/panic/package.json | 19 + deps/panic/panic.c | 65 + deps/panic/panic.h | 87 + deps/text/build.cmake | 7 + deps/text/package.json | 23 + deps/text/text.c | 544 ++++++ deps/text/text.h | 590 +++++++ deps/text/text_config.h | 40 + deps/traits-unit/build.cmake | 6 + deps/traits-unit/package.json | 24 + deps/traits-unit/traits-unit.c | 597 +++++++ deps/traits-unit/traits-unit.h | 200 +++ deps/traits/package.json | 15 + deps/traits/traits.h | 264 +++ examples/bench.c | 45 - examples/build.cmake | 2 + examples/example.c | 144 -- examples/main.c | 105 ++ ext/parson/README.md | 145 -- ext/parson/parson.c | 2007 ----------------------- ext/parson/parson.cmake | 6 - ext/parson/parson.h | 234 --- ext/picohttpparser/README.md | 116 -- ext/picohttpparser/picohttpparser.c | 620 ------- ext/picohttpparser/picohttpparser.cmake | 6 - ext/picohttpparser/picohttpparser.h | 89 - ext/sds/LICENSE | 24 - ext/sds/README.md | 892 ---------- ext/sds/sds.c | 1264 -------------- ext/sds/sds.cmake | 6 - ext/sds/sds.h | 264 --- ext/sds/sdsalloc.h | 41 - http.cmake | 7 - include/http/http.h | 45 - include/http/http_method.h | 56 - include/http/http_request.h | 58 - include/http/http_response.h | 44 - include/http/http_status.h | 82 - package.json | 42 +- sources/build.cmake | 10 + sources/http.c | 220 +++ sources/http.h | 77 + sources/http_error.c | 38 + sources/http_error.h | 52 + sources/http_fire_result.c | 65 + sources/http_fire_result.h | 114 ++ sources/http_maybe_text.c | 47 + sources/http_maybe_text.h | 78 + sources/http_method.c | 116 ++ sources/http_method.h | 80 + sources/http_request.c | 210 +++ sources/http_request.h | 291 ++++ sources/http_response.c | 164 ++ sources/http_response.h | 211 +++ sources/http_status.c | 215 +++ sources/http_status.h | 113 ++ src/http.c | 266 --- src/http_request.c | 38 - src/http_response.c | 19 - tests/unit/build.cmake | 14 + tests/unit/describe.c | 40 + tests/unit/features/http_fire_result.c | 98 ++ tests/unit/features/http_fire_result.h | 42 + tests/unit/features/http_maybe_text.c | 48 + tests/unit/features/http_maybe_text.h | 41 + tests/unit/fixtures.c | 42 + tests/unit/fixtures.h | 41 + valgrind.sh | 4 + 93 files changed, 6143 insertions(+), 6670 deletions(-) delete mode 100644 bench.log delete mode 100644 bench.py delete mode 100755 bench.sh create mode 100644 deps/alligator/alligator.c create mode 100644 deps/alligator/alligator.h create mode 100644 deps/alligator/alligator_config.h create mode 100644 deps/alligator/build.cmake create mode 100644 deps/alligator/package.json create mode 100644 deps/atom/atom.c create mode 100644 deps/atom/atom.h create mode 100644 deps/atom/build.cmake create mode 100644 deps/atom/package.json create mode 100644 deps/error/build.cmake create mode 100644 deps/error/error.c create mode 100644 deps/error/error.h create mode 100644 deps/error/package.json create mode 100644 deps/option/build.cmake create mode 100644 deps/option/option.c create mode 100644 deps/option/option.h create mode 100644 deps/option/package.json create mode 100644 deps/panic/build.cmake create mode 100644 deps/panic/package.json create mode 100644 deps/panic/panic.c create mode 100644 deps/panic/panic.h create mode 100644 deps/text/build.cmake create mode 100644 deps/text/package.json create mode 100644 deps/text/text.c create mode 100644 deps/text/text.h create mode 100644 deps/text/text_config.h create mode 100644 deps/traits-unit/build.cmake create mode 100644 deps/traits-unit/package.json create mode 100644 deps/traits-unit/traits-unit.c create mode 100644 deps/traits-unit/traits-unit.h create mode 100644 deps/traits/package.json create mode 100644 deps/traits/traits.h delete mode 100644 examples/bench.c create mode 100644 examples/build.cmake delete mode 100644 examples/example.c create mode 100644 examples/main.c delete mode 100644 ext/parson/README.md delete mode 100644 ext/parson/parson.c delete mode 100644 ext/parson/parson.cmake delete mode 100644 ext/parson/parson.h delete mode 100644 ext/picohttpparser/README.md delete mode 100644 ext/picohttpparser/picohttpparser.c delete mode 100644 ext/picohttpparser/picohttpparser.cmake delete mode 100644 ext/picohttpparser/picohttpparser.h delete mode 100644 ext/sds/LICENSE delete mode 100644 ext/sds/README.md delete mode 100644 ext/sds/sds.c delete mode 100644 ext/sds/sds.cmake delete mode 100644 ext/sds/sds.h delete mode 100644 ext/sds/sdsalloc.h delete mode 100644 http.cmake delete mode 100644 include/http/http.h delete mode 100644 include/http/http_method.h delete mode 100644 include/http/http_request.h delete mode 100644 include/http/http_response.h delete mode 100644 include/http/http_status.h create mode 100644 sources/build.cmake create mode 100644 sources/http.c create mode 100644 sources/http.h create mode 100644 sources/http_error.c create mode 100644 sources/http_error.h create mode 100644 sources/http_fire_result.c create mode 100644 sources/http_fire_result.h create mode 100644 sources/http_maybe_text.c create mode 100644 sources/http_maybe_text.h create mode 100644 sources/http_method.c create mode 100644 sources/http_method.h create mode 100644 sources/http_request.c create mode 100644 sources/http_request.h create mode 100644 sources/http_response.c create mode 100644 sources/http_response.h create mode 100644 sources/http_status.c create mode 100644 sources/http_status.h delete mode 100644 src/http.c delete mode 100644 src/http_request.c delete mode 100644 src/http_response.c create mode 100644 tests/unit/build.cmake create mode 100644 tests/unit/describe.c create mode 100644 tests/unit/features/http_fire_result.c create mode 100644 tests/unit/features/http_fire_result.h create mode 100644 tests/unit/features/http_maybe_text.c create mode 100644 tests/unit/features/http_maybe_text.h create mode 100644 tests/unit/fixtures.c create mode 100644 tests/unit/fixtures.h create mode 100755 valgrind.sh diff --git a/.gitignore b/.gitignore index 0adcbb5..9ccbfbc 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,6 @@ cmake-build-*/ # IDEs and editors *.idea *.swp + +# log files +*.log diff --git a/CMakeLists.txt b/CMakeLists.txt index bd72ce4..2f93345 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,37 +1,27 @@ -cmake_minimum_required(VERSION 3.1) -project(http) +cmake_minimum_required(VERSION 3.8) +project(http C) set(CMAKE_C_STANDARD 99) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror -pedantic") -if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") - # using GCC - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcast-align -Wbad-function-cast") -endif () +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror") -##### -# Archive -### -include_directories(${PROJECT_SOURCE_DIR}/include/http) -file(GLOB HEADER_FILES ${PROJECT_SOURCE_DIR}/include/http/*.h) -file(GLOB SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/*.c) -add_library(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES}) -target_link_libraries(${PROJECT_NAME} PRIVATE curl) +# dependencies +include_directories(deps) +find_package(CURL) +include(deps/atom/build.cmake) +include(deps/text/build.cmake) +include(deps/error/build.cmake) +include(deps/panic/build.cmake) +include(deps/option/build.cmake) +include(deps/alligator/build.cmake) +include(deps/traits-unit/build.cmake) -##### -# External -### -include(ext/sds/sds.cmake) -include(ext/parson/parson.cmake) -include(ext/picohttpparser/picohttpparser.cmake) +# archive +include_directories(sources) +include(sources/build.cmake) -##### -# Examples -### -set(EXAMPLE_PATH ${PROJECT_SOURCE_DIR}/examples) +# examples +include(examples/build.cmake) -add_executable(example ${EXAMPLE_PATH}/example.c) -target_include_directories(example PRIVATE ${PROJECT_SOURCE_DIR}/ext) -target_link_libraries(example PRIVATE ${PROJECT_NAME} sds parson picohttpparser) - -add_executable(bench ${EXAMPLE_PATH}/bench.c) -target_link_libraries(bench PRIVATE ${PROJECT_NAME}) +# tests +include_directories(tests) +include(tests/unit/build.cmake) diff --git a/LICENSE b/LICENSE index 7de9ab0..ca48049 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,24 @@ The MIT License (MIT) -Copyright (c) 2017-Present Davide Di Carlo +Copyright (c) 2018 Davide Di Carlo -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 2b3d8f4..881c715 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,34 @@ -# http +http +==== -HTTP requests made easier. -Depends on libcurl. +Http requests made easier, depends on libcurl. + +```C +#include +#include +#include + +int main() { + Http_initialize(); + + Atom url = Atom_fromLiteral("https://api.github.com/repos/daddinuz/http/issues"); + struct HttpRequestBuilder *requestBuilder = HttpRequestBuilder_new(HTTP_METHOD_GET, url); + HttpRequestBuilder_setTimeout(requestBuilder, 25); + HttpRequestBuilder_emplaceHeaders(requestBuilder, + "Authorization: token %s\n" + "Accept: application/vnd.github.VERSION.raw+json\n" + "Content-Type: application/json\n" + "User-Agent: daddinuz/http\n", + getenv("GITHUB_AUTH_TOKEN")); + + const struct HttpRequest *request = HttpRequestBuilder_build(&requestBuilder); + printRequest(request); + + const struct HttpResponse *response = Http_FireResult_unwrap(HttpRequest_fire(&request)); + printResponse(response); + + HttpResponse_delete(response); + Http_terminate(); + return 0; +} +``` diff --git a/bench.log b/bench.log deleted file mode 100644 index e1fff88..0000000 --- a/bench.log +++ /dev/null @@ -1,60 +0,0 @@ -==================== -Python -real 0.77 -user 0.30 -sys 0.00 - -C -real 0.82 -user 0.13 -sys 0.03 -==================== - -==================== -Python -real 0.81 -user 0.24 -sys 0.04 - -C -real 0.79 -user 0.15 -sys 0.02 -==================== - -==================== -Python -real 0.81 -user 0.28 -sys 0.01 - -C -real 0.81 -user 0.14 -sys 0.03 -==================== - -==================== -Python -real 0.82 -user 0.29 -sys 0.00 - -C -real 0.80 -user 0.09 -sys 0.04 -==================== - -==================== -Python -real 0.83 -user 0.25 -sys 0.03 - -C -real 0.84 -user 0.15 -sys 0.01 -==================== - diff --git a/bench.py b/bench.py deleted file mode 100644 index f9e52ef..0000000 --- a/bench.py +++ /dev/null @@ -1,14 +0,0 @@ -import requests - -status = 0 -url = 'https://api.github.com/repos/daddinuz/http/issues' -headers = { - 'Accept': 'application/vnd.github.v3+json', - 'Content-Type': 'application/json', - 'User-Agent': 'daddinuz/http' -} - -response = requests.get(url, headers=headers) -status = response.status_code - -exit(not (200 == status)) diff --git a/bench.sh b/bench.sh deleted file mode 100755 index 2364e1f..0000000 --- a/bench.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env bash - -set -e - -x=1 -while [ ${x} -le 5 ]; do - echo -e "====================\nPython" - \time -p python3 bench.py 2>&1 - echo - - echo "C" - \time -p ./cmake-build-release/bench 2>&1 - echo -e "====================\n" - - x=$(( ${x} + 1 )) -done diff --git a/deps/alligator/alligator.c b/deps/alligator/alligator.c new file mode 100644 index 0000000..9ece0f2 --- /dev/null +++ b/deps/alligator/alligator.c @@ -0,0 +1,58 @@ +/* + * Author: daddinuz + * email: daddinuz@gmail.com + * + * Copyright (c) 2018 Davide Di Carlo + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "alligator.h" +#include "alligator_config.h" + +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || (defined(__cplusplus) && __cplusplus >= 201103L) + +Option Alligator_aligned_alloc(size_t alignment, size_t size) { + void *memory = __Alligator_aligned_alloc(alignment, size); + return memory ? Option_some(memory) : None; +} + +#endif + +Option Alligator_malloc(const size_t size) { + void *memory = __Alligator_malloc(size); + return memory ? Option_some(memory) : None; +} + +Option Alligator_calloc(const size_t numberOfMembers, const size_t memberSize) { + void *memory = __Alligator_calloc(numberOfMembers, memberSize); + return memory ? Option_some(memory) : None; +} + +Option Alligator_realloc(void *ptr, size_t newSize) { + void *memory = __Alligator_realloc(ptr, newSize); + return memory ? Option_some(memory) : None; +} + +void Alligator_free(void *ptr) { + __Alligator_free(ptr); +} diff --git a/deps/alligator/alligator.h b/deps/alligator/alligator.h new file mode 100644 index 0000000..5603bf7 --- /dev/null +++ b/deps/alligator/alligator.h @@ -0,0 +1,69 @@ +/* + * Author: daddinuz + * email: daddinuz@gmail.com + * + * Copyright (c) 2018 Davide Di Carlo + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include