- cf_session_t (opaque), cf_session_options_t, cf_turn_options_t,
cf_turn_event_t, cf_turn_result_t, cf_session_state_t, cf_session_list_t
- cf_session_new / _turn / _close (idempotent) / _free, _state_get,
_list_get, plus cf_session_id / _agent accessors
- cf_turn_result_text concatenates type=="text" events into a single
string, lazily cached, OWNED by the result (caller must not free)
- Session-id allow-list ([A-Za-z0-9_-]+) validated client-side everywhere
it lands in the URL path — same pattern as cf_admin_revoke_token, no
reverse-proxy traversal foothold
- Bearer hygiene preserved: regression test covers cf_session_new /
state_get / list_get and asserts the bearer never lands in
cf_error_t.message
- v0.1 surface unchanged. Existing types (cf_client_t, cf_run_*, etc.)
are byte-identical. cJSON 1.7.18 preserved.
Tests (21 → 34, +13):
- session_turn_round_trip — full create + turn + close, event parsing
- session_close_idempotent — close × 3, DELETE on the wire ONCE
- session_turn_after_close_returns_error — CF_ERR_USAGE, no HTTP
- session_cross_token_returns_404 — CF_ERR_API + http_status==404
- session_validates_id_traversal — "a/../healthz" rejected pre-network
- session_list_get — 2-row mock parsed; null last_turn_at → -1
- session_state_get — full state shape parsed
- turn_result_text_concatenates — skips non-text + empty content
- session_free_idempotent — NULL-safe across all v0.2 freers
- turn_result_memory_clean — valgrind regression guard
- session_turn_with_files — files + timeout_secs serialised
- session_bearer_never_leaks — 3 fallible paths, bearer stays out
- session_null_arg_defenses — every entry point rejects NULLs
Verification:
- cmake --build build (Release, -Werror -Wall -Wextra -Wpedantic
-Wshadow): clean
- ctest --test-dir build: 34/34 pass
- valgrind --leak-check=full: 12,678 allocs == 12,678 frees, 0 errors,
0 leaks
- ASan build: 34/34 pass clean
- UBSan build (-fsanitize=undefined -fno-sanitize-recover=all): 34/34
pass clean
README adds "Multi-turn / Sessions (v0.2)" section: lifecycle example,
idempotent-close note, event/text ownership rules, state + listing,
memory ownership table, what's not in v0.2.
Spec: memory/spec-clawdforge-v0.2.md
Server core: 940861f
150 lines
6 KiB
CMake
150 lines
6 KiB
CMake
# clawdforge — C SDK
|
|
#
|
|
# MIT License
|
|
#
|
|
# Copyright (c) 2026 clawdforge contributors
|
|
#
|
|
# 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.
|
|
|
|
cmake_minimum_required(VERSION 3.16)
|
|
project(clawdforge
|
|
VERSION 0.2.0
|
|
DESCRIPTION "C SDK for the clawdforge HTTP service"
|
|
LANGUAGES C)
|
|
|
|
option(CLAWDFORGE_BUILD_TESTS "Build the test suite" ON)
|
|
option(CLAWDFORGE_BUILD_EXAMPLES "Build example programs" ON)
|
|
option(CLAWDFORGE_ASAN "Build with AddressSanitizer" OFF)
|
|
|
|
set(CMAKE_C_STANDARD 11)
|
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
|
set(CMAKE_C_EXTENSIONS OFF)
|
|
|
|
include(GNUInstallDirs)
|
|
|
|
# ----------------------------------------------------------------------------
|
|
# Dependencies
|
|
# ----------------------------------------------------------------------------
|
|
find_package(CURL REQUIRED)
|
|
|
|
# Common warning flags. Tests bump to -Werror.
|
|
set(CF_WARN_FLAGS -Wall -Wextra -Wpedantic -Wshadow)
|
|
|
|
if(CLAWDFORGE_ASAN)
|
|
list(APPEND CF_WARN_FLAGS -fsanitize=address -fno-omit-frame-pointer -g)
|
|
endif()
|
|
|
|
# ----------------------------------------------------------------------------
|
|
# Sources
|
|
# ----------------------------------------------------------------------------
|
|
set(CF_SOURCES
|
|
src/client.c
|
|
src/http.c
|
|
src/json.c
|
|
src/session.c
|
|
src/cjson/cJSON.c)
|
|
|
|
# ----------------------------------------------------------------------------
|
|
# Static library
|
|
# ----------------------------------------------------------------------------
|
|
add_library(clawdforge_static STATIC ${CF_SOURCES})
|
|
set_target_properties(clawdforge_static PROPERTIES
|
|
OUTPUT_NAME clawdforge
|
|
POSITION_INDEPENDENT_CODE ON)
|
|
target_include_directories(clawdforge_static
|
|
PUBLIC
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
|
PRIVATE
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/cjson)
|
|
target_link_libraries(clawdforge_static PUBLIC CURL::libcurl)
|
|
target_compile_options(clawdforge_static PRIVATE ${CF_WARN_FLAGS})
|
|
target_compile_definitions(clawdforge_static PRIVATE CLAWDFORGE_BUILDING_LIB)
|
|
if(CLAWDFORGE_ASAN)
|
|
target_link_options(clawdforge_static PUBLIC -fsanitize=address)
|
|
endif()
|
|
|
|
# ----------------------------------------------------------------------------
|
|
# Shared library
|
|
# ----------------------------------------------------------------------------
|
|
add_library(clawdforge_shared SHARED ${CF_SOURCES})
|
|
set_target_properties(clawdforge_shared PROPERTIES
|
|
OUTPUT_NAME clawdforge
|
|
VERSION ${PROJECT_VERSION}
|
|
SOVERSION ${PROJECT_VERSION_MAJOR}
|
|
POSITION_INDEPENDENT_CODE ON
|
|
C_VISIBILITY_PRESET hidden)
|
|
target_include_directories(clawdforge_shared
|
|
PUBLIC
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
|
PRIVATE
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/cjson)
|
|
target_link_libraries(clawdforge_shared PUBLIC CURL::libcurl)
|
|
target_compile_options(clawdforge_shared PRIVATE ${CF_WARN_FLAGS})
|
|
target_compile_definitions(clawdforge_shared PRIVATE CLAWDFORGE_BUILDING_LIB)
|
|
if(CLAWDFORGE_ASAN)
|
|
target_link_options(clawdforge_shared PUBLIC -fsanitize=address)
|
|
endif()
|
|
|
|
# Convenience alias used by examples + tests.
|
|
add_library(clawdforge::clawdforge ALIAS clawdforge_static)
|
|
|
|
# ----------------------------------------------------------------------------
|
|
# Install + pkg-config
|
|
# ----------------------------------------------------------------------------
|
|
install(TARGETS clawdforge_static clawdforge_shared
|
|
EXPORT clawdforgeTargets
|
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
|
|
|
install(FILES include/clawdforge.h
|
|
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
|
|
|
configure_file(pkgconfig/clawdforge.pc.in
|
|
${CMAKE_CURRENT_BINARY_DIR}/clawdforge.pc @ONLY)
|
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/clawdforge.pc
|
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
|
|
|
|
# ----------------------------------------------------------------------------
|
|
# Examples
|
|
# ----------------------------------------------------------------------------
|
|
if(CLAWDFORGE_BUILD_EXAMPLES)
|
|
add_executable(clawdforge_example_basic examples/basic.c)
|
|
target_link_libraries(clawdforge_example_basic PRIVATE clawdforge_static)
|
|
target_compile_options(clawdforge_example_basic PRIVATE ${CF_WARN_FLAGS})
|
|
endif()
|
|
|
|
# ----------------------------------------------------------------------------
|
|
# Tests
|
|
# ----------------------------------------------------------------------------
|
|
if(CLAWDFORGE_BUILD_TESTS)
|
|
enable_testing()
|
|
add_executable(clawdforge_tests tests/test_client.c)
|
|
target_link_libraries(clawdforge_tests PRIVATE clawdforge_static)
|
|
target_include_directories(clawdforge_tests PRIVATE
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/cjson)
|
|
target_compile_options(clawdforge_tests PRIVATE
|
|
${CF_WARN_FLAGS} -Werror)
|
|
add_test(NAME clawdforge_tests COMMAND clawdforge_tests)
|
|
set_tests_properties(clawdforge_tests PROPERTIES TIMEOUT 60)
|
|
endif()
|