Skip to content

Commit 41eb63f

Browse files
Add C++ modules support
1 parent c795ad1 commit 41eb63f

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system cer
110110
option(HTTPLIB_USE_NON_BLOCKING_GETADDRINFO "Enables the non-blocking alternatives for getaddrinfo." ON)
111111
option(HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build." OFF)
112112
option(HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON)
113+
option(HTTPLIB_BUILD_MODULES "Build httplib modules (requires HTTPLIB_COMPILE to be ON)." OFF)
113114
# Defaults to static library but respects standard BUILD_SHARED_LIBS if set
114115
include(CMakeDependentOption)
115116
cmake_dependent_option(HTTPLIB_SHARED "Build the library as a shared library instead of static. Has no effect if using header-only."
@@ -365,3 +366,10 @@ if(HTTPLIB_TEST)
365366
include(CTest)
366367
add_subdirectory(test)
367368
endif()
369+
370+
if(HTTPLIB_BUILD_MODULES)
371+
if(NOT HTTPLIB_COMPILE)
372+
message(FATAL_ERROR "HTTPLIB_BUILD_MODULES requires HTTPLIB_COMPILE to be ON.")
373+
endif()
374+
add_subdirectory(modules)
375+
endif()

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,11 @@ $ ./split.py
12531253
Wrote out/httplib.h and out/httplib.cc
12541254
```
12551255

1256+
Build C++ Modules
1257+
-----------------
1258+
1259+
If using CMake, it is possible to build this as a C++20 module using the `HTTPLIB_BUILD_MODULES` option (which requires `HTTPLIB_COMPILE` to be enabled).
1260+
12561261
Dockerfile for Static HTTP Server
12571262
---------------------------------
12581263

modules/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
add_library(httplib_module)
2+
3+
target_sources(httplib_module
4+
PUBLIC
5+
FILE_SET CXX_MODULES FILES
6+
httplib.cppm
7+
)
8+
9+
target_compile_features(httplib_module PUBLIC cxx_std_20)
10+
11+
target_include_directories(httplib_module PUBLIC
12+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
13+
$<INSTALL_INTERFACE:include>
14+
)
15+
16+
add_library(httplib::module ALIAS httplib_module)
17+
18+
# Installation
19+
install(TARGETS httplib_module
20+
EXPORT ${PROJECT_NAME}Targets
21+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
22+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
23+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
24+
FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/httplib/modules
25+
)

modules/httplib.cppm

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//
2+
// httplib.h
3+
//
4+
// Copyright (c) 2025 Yuji Hirose. All rights reserved.
5+
// MIT License
6+
//
7+
8+
module;
9+
10+
#include "../httplib.h"
11+
12+
export module httplib;
13+
14+
export namespace httplib {
15+
using httplib::SSLVerifierResponse;
16+
using httplib::StatusCode;
17+
using httplib::Headers;
18+
using httplib::Params;
19+
using httplib::Match;
20+
using httplib::DownloadProgress;
21+
using httplib::UploadProgress;
22+
using httplib::Response;
23+
using httplib::ResponseHandler;
24+
using httplib::FormData;
25+
using httplib::FormField;
26+
using httplib::FormFields;
27+
using httplib::FormFiles;
28+
using httplib::MultipartFormData;
29+
using httplib::UploadFormData;
30+
using httplib::UploadFormDataItems;
31+
using httplib::DataSink;
32+
using httplib::ContentProvider;
33+
using httplib::ContentProviderWithoutLength;
34+
using httplib::ContentProviderResourceReleaser;
35+
using httplib::FormDataProvider;
36+
using httplib::FormDataProviderItems;
37+
using httplib::ContentReceiverWithProgress;
38+
using httplib::ContentReceiver;
39+
using httplib::FormDataHeader;
40+
using httplib::ContentReader;
41+
using httplib::Range;
42+
using httplib::Ranges;
43+
using httplib::Request;
44+
using httplib::Response;
45+
using httplib::Error;
46+
using httplib::to_string;
47+
using httplib::operator<<;
48+
using httplib::Stream;
49+
using httplib::TaskQueue;
50+
using httplib::ThreadPool;
51+
using httplib::Logger;
52+
using httplib::ErrorLogger;
53+
using httplib::SocketOptions;
54+
using httplib::default_socket_options;
55+
using httplib::status_message;
56+
using httplib::get_bearer_token_auth;
57+
using httplib::Server;
58+
using httplib::Result;
59+
using httplib::ClientConnection;
60+
using httplib::ClientImpl;
61+
using httplib::Client;
62+
63+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
64+
using httplib::SSLServer;
65+
using httplib::SSLClient;
66+
#endif
67+
68+
using httplib::hosted_at;
69+
using httplib::encode_uri_component;
70+
using httplib::encode_uri;
71+
using httplib::decode_uri_component;
72+
using httplib::decode_uri;
73+
using httplib::encode_path_component;
74+
using httplib::decode_path_component;
75+
using httplib::encode_query_component;
76+
using httplib::decode_query_component;
77+
using httplib::append_query_params;
78+
using httplib::make_range_header;
79+
using httplib::make_basic_authentication_header;
80+
81+
using httplib::get_client_ip;
82+
83+
namespace stream {
84+
using httplib::stream::Result;
85+
using httplib::stream::Get;
86+
using httplib::stream::Post;
87+
using httplib::stream::Put;
88+
using httplib::stream::Patch;
89+
using httplib::stream::Delete;
90+
using httplib::stream::Head;
91+
using httplib::stream::Options;
92+
}
93+
}

0 commit comments

Comments
 (0)