Skip to content

Commit 71bf2a2

Browse files
etrclaude
andcommitted
TASK-006: Replace #define constants with httpserver::constants
Move every value-form #define from public headers into inline constexpr declarations under httpserver::constants: - DEFAULT_WS_PORT -> std::uint16_t (9898) - DEFAULT_WS_TIMEOUT -> int (180 seconds) - DEFAULT_MASK_VALUE -> std::uint16_t (0xFFFF) - NOT_FOUND_ERROR -> std::string_view ("Not Found") - METHOD_ERROR -> std::string_view ("Method not Allowed") - NOT_METHOD_ERROR -> std::string_view ("Method not Acceptable") - GENERIC_ERROR -> std::string_view ("Internal Error") The new header src/httpserver/constants.hpp uses the established two-token gate (_HTTPSERVER_HPP_INSIDE_ + HTTPSERVER_COMPILATION), is re-exported from <httpserver.hpp>, and is registered in nobase_include_HEADERS so it ships in the install layout. Internal callers in webserver.cpp, http_utils.cpp, create_webserver.hpp, and http_utils.hpp are migrated to the namespaced names. The string_response call sites materialize a std::string from the string_view to satisfy the existing ctor signature. A new unit test (test/unit/constants_test.cpp) pins the values and types via static_assert, and uses #ifdef sentinels to witness that the v1 macro names no longer leak into consumer namespace after #include <httpserver.hpp>. NOT_METHOD_ERROR has no in-tree caller; retained for v1 API parity per the v2.0 mechanical-migration policy. Acceptance: - 23/23 tests pass (release + debug -Werror -Wall -Wextra) - Filtered grep on src/httpserver/*.hpp shows no leftover value-constant #defines (include guards, _WINDOWS, _WIN32_WINNT, and COMPARATOR are out of scope per plan §2) - Installed-header layout includes httpserver/constants.hpp Closes TASK-006. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent a3ec719 commit 71bf2a2

10 files changed

Lines changed: 254 additions & 20 deletions

File tree

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp fil
2424
# Detail headers (httpserver/details/*.hpp) live here so they cannot leak to
2525
# downstream consumers — the public surface comes in through <httpserver.hpp>.
2626
noinst_HEADERS = httpserver/string_utilities.hpp httpserver/details/modded_request.hpp httpserver/details/http_endpoint.hpp gettext.h
27-
nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/file_info.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/string_response.hpp httpserver/digest_auth_fail_response.hpp httpserver/deferred_response.hpp httpserver/file_response.hpp httpserver/pipe_response.hpp httpserver/empty_response.hpp httpserver/feature_unavailable.hpp httpserver/iovec_entry.hpp httpserver/iovec_response.hpp httpserver/http_arg_value.hpp httpserver/http_method.hpp
27+
nobase_include_HEADERS = httpserver.hpp httpserver/constants.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/file_info.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/string_response.hpp httpserver/digest_auth_fail_response.hpp httpserver/deferred_response.hpp httpserver/file_response.hpp httpserver/pipe_response.hpp httpserver/empty_response.hpp httpserver/feature_unavailable.hpp httpserver/iovec_entry.hpp httpserver/iovec_response.hpp httpserver/http_arg_value.hpp httpserver/http_method.hpp
2828

2929
if HAVE_BAUTH
3030
libhttpserver_la_SOURCES += basic_auth_fail_response.cpp

src/http_utils.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
USA
1919
*/
2020

21+
#include "httpserver/constants.hpp"
2122
#include "httpserver/http_utils.hpp"
2223

2324
#if defined(_WIN32) && !defined(__CYGWIN__)
@@ -373,12 +374,12 @@ ip_representation::ip_representation(const struct sockaddr* ip) {
373374
pieces[i] = (reinterpret_cast<const u_char*>(sin_addr6_pt))[i];
374375
}
375376
}
376-
mask = DEFAULT_MASK_VALUE;
377+
mask = constants::DEFAULT_MASK_VALUE;
377378
}
378379

379380
ip_representation::ip_representation(const std::string& ip) {
380381
std::vector<std::string> parts;
381-
mask = DEFAULT_MASK_VALUE;
382+
mask = constants::DEFAULT_MASK_VALUE;
382383
std::fill(pieces, pieces + 16, 0);
383384
if (ip.find(':') != std::string::npos) { // IPV6
384385
ip_version = http_utils::IPV6;

src/httpserver.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#ifdef HAVE_BAUTH
3131
#include "httpserver/basic_auth_fail_response.hpp"
3232
#endif // HAVE_BAUTH
33+
#include "httpserver/constants.hpp"
3334
#include "httpserver/deferred_response.hpp"
3435
#ifdef HAVE_DAUTH
3536
#include "httpserver/digest_auth_fail_response.hpp"

src/httpserver/constants.hpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011-2019 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#if !defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION)
22+
#error "Only <httpserver.hpp> or <httpserverpp> can be included directly."
23+
#endif
24+
25+
#ifndef SRC_HTTPSERVER_CONSTANTS_HPP_
26+
#define SRC_HTTPSERVER_CONSTANTS_HPP_
27+
28+
#include <cstdint>
29+
#include <string_view>
30+
31+
// Public, namespaced replacements for the v1 #define wall. Each constant
32+
// here was previously a value-form macro in a public header (see PRD-CFG-
33+
// REQ-002 / architecture §4.9 for the rationale). The identifiers
34+
// preserve their v1 spellings so the migration is mechanical: only the
35+
// namespace qualifier changes at call sites.
36+
//
37+
// `inline constexpr` (C++17+, project floor is C++20 per TASK-001) gives
38+
// each symbol a single ODR-stable definition usable from any TU that
39+
// includes this header.
40+
namespace httpserver::constants {
41+
42+
// Default TCP port the webserver binds to when no `port()` is set on the
43+
// create_webserver builder. Replaces v1 `#define DEFAULT_WS_PORT 9898`.
44+
inline constexpr std::uint16_t DEFAULT_WS_PORT = 9898;
45+
46+
// Default per-connection timeout in seconds. Replaces v1
47+
// `#define DEFAULT_WS_TIMEOUT 180`. Type is `int` to match the
48+
// `create_webserver._connection_timeout` field exactly — no implicit
49+
// conversion at the assignment site, no -Wconversion noise. The value
50+
// is non-negative by construction.
51+
inline constexpr int DEFAULT_WS_TIMEOUT = 180;
52+
53+
// Bitmask sentinel used by ip_representation when no explicit CIDR mask
54+
// has been parsed (all 16 nibbles "present"). Replaces v1
55+
// `#define DEFAULT_MASK_VALUE 0xFFFF`.
56+
inline constexpr std::uint16_t DEFAULT_MASK_VALUE = 0xFFFFu;
57+
58+
// Default body for a 404 response when no not_found_resource is set on
59+
// the webserver. Replaces v1 `#define NOT_FOUND_ERROR "Not Found"`.
60+
// std::string_view keeps storage non-allocating; call sites materialize
61+
// a std::string via the string_response constructor.
62+
inline constexpr std::string_view NOT_FOUND_ERROR = "Not Found";
63+
64+
// Default body for a 405 response when no method_not_allowed_resource
65+
// is set. Replaces v1 `#define METHOD_ERROR "Method not Allowed"`.
66+
// The name is preserved (rather than renamed to METHOD_NOT_ALLOWED_ERROR)
67+
// to keep the migration mechanical — the namespacing is the API change,
68+
// not a rename.
69+
inline constexpr std::string_view METHOD_ERROR = "Method not Allowed";
70+
71+
// Default body for a 406 response. Replaces v1
72+
// `#define NOT_METHOD_ERROR "Method not Acceptable"`. Currently unused
73+
// by any in-tree caller (verified by grep across src/, test/, examples/);
74+
// retained for v1 API parity per the v2.0 mechanical-migration policy.
75+
inline constexpr std::string_view NOT_METHOD_ERROR = "Method not Acceptable";
76+
77+
// Default body for a 500 response when no internal_error_resource is
78+
// set. Replaces v1 `#define GENERIC_ERROR "Internal Error"`.
79+
inline constexpr std::string_view GENERIC_ERROR = "Internal Error";
80+
81+
} // namespace httpserver::constants
82+
83+
#endif // SRC_HTTPSERVER_CONSTANTS_HPP_

src/httpserver/create_webserver.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@
3333
#include <variant>
3434
#include <vector>
3535

36+
#include "httpserver/constants.hpp"
3637
#include "httpserver/http_response.hpp"
3738
#include "httpserver/http_utils.hpp"
3839

39-
#define DEFAULT_WS_TIMEOUT 180
40-
#define DEFAULT_WS_PORT 9898
41-
4240
namespace httpserver {
4341

4442
class webserver;
@@ -480,13 +478,13 @@ class create_webserver {
480478
}
481479

482480
private:
483-
uint16_t _port = DEFAULT_WS_PORT;
481+
uint16_t _port = constants::DEFAULT_WS_PORT;
484482
http::http_utils::start_method_T _start_method = http::http_utils::INTERNAL_SELECT;
485483
int _max_threads = 0;
486484
int _max_connections = 0;
487485
int _memory_limit = 0;
488486
size_t _content_size_limit = std::numeric_limits<size_t>::max();
489-
int _connection_timeout = DEFAULT_WS_TIMEOUT;
487+
int _connection_timeout = constants::DEFAULT_WS_TIMEOUT;
490488
int _per_IP_connection_limit = 0;
491489
log_access_ptr _log_access = nullptr;
492490
log_error_ptr _log_error = nullptr;

src/httpserver/http_utils.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@
5656
#include <string>
5757
#include <vector>
5858

59+
#include "httpserver/constants.hpp"
5960
#include "httpserver/http_arg_value.hpp"
6061

61-
#define DEFAULT_MASK_VALUE 0xFFFF
62-
6362

6463
namespace httpserver {
6564

@@ -370,7 +369,7 @@ struct ip_representation {
370369

371370
explicit ip_representation(http_utils::IP_version_T ip_version) :
372371
ip_version(ip_version) {
373-
mask = DEFAULT_MASK_VALUE;
372+
mask = constants::DEFAULT_MASK_VALUE;
374373
std::fill(pieces, pieces + 16, 0);
375374
}
376375

src/httpserver/webserver.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
#ifndef SRC_HTTPSERVER_WEBSERVER_HPP_
2626
#define SRC_HTTPSERVER_WEBSERVER_HPP_
2727

28-
#define NOT_FOUND_ERROR "Not Found"
29-
#define METHOD_ERROR "Method not Allowed"
30-
#define NOT_METHOD_ERROR "Method not Acceptable"
31-
#define GENERIC_ERROR "Internal Error"
32-
3328
#include <microhttpd.h>
3429
#include <pthread.h>
3530
#include <stdarg.h>
@@ -54,6 +49,7 @@
5449
#include <gnutls/gnutls.h>
5550
#endif // HAVE_GNUTLS
5651

52+
#include "httpserver/constants.hpp"
5753
#include "httpserver/http_utils.hpp"
5854
#include "httpserver/create_webserver.hpp"
5955
#include "httpserver/details/http_endpoint.hpp"

src/webserver.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include <utility>
5757
#include <vector>
5858

59+
#include "httpserver/constants.hpp"
5960
#include "httpserver/create_webserver.hpp"
6061
#include "httpserver/details/http_endpoint.hpp"
6162
#include "httpserver/details/modded_request.hpp"
@@ -1019,23 +1020,23 @@ std::shared_ptr<http_response> webserver::not_found_page(details::modded_request
10191020
if (not_found_resource != nullptr) {
10201021
return not_found_resource(*mr->dhr);
10211022
} else {
1022-
return std::make_shared<string_response>(NOT_FOUND_ERROR, http_utils::http_not_found);
1023+
return std::make_shared<string_response>(std::string{constants::NOT_FOUND_ERROR}, http_utils::http_not_found);
10231024
}
10241025
}
10251026

10261027
std::shared_ptr<http_response> webserver::method_not_allowed_page(details::modded_request* mr) const {
10271028
if (method_not_allowed_resource != nullptr) {
10281029
return method_not_allowed_resource(*mr->dhr);
10291030
} else {
1030-
return std::make_shared<string_response>(METHOD_ERROR, http_utils::http_method_not_allowed);
1031+
return std::make_shared<string_response>(std::string{constants::METHOD_ERROR}, http_utils::http_method_not_allowed);
10311032
}
10321033
}
10331034

10341035
std::shared_ptr<http_response> webserver::internal_error_page(details::modded_request* mr, bool force_our) const {
10351036
if (internal_error_resource != nullptr && !force_our) {
10361037
return internal_error_resource(*mr->dhr);
10371038
} else {
1038-
return std::make_shared<string_response>(GENERIC_ERROR, http_utils::http_internal_server_error);
1039+
return std::make_shared<string_response>(std::string{constants::GENERIC_ERROR}, http_utils::http_internal_server_error);
10391040
}
10401041
}
10411042

test/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ LDADD += -lcurl
2626

2727
AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/ -DHTTPSERVER_COMPILATION
2828
METASOURCES = AUTO
29-
check_PROGRAMS = basic file_upload http_utils threaded nodelay string_utilities http_endpoint ban_system ws_start_stop authentication deferred http_resource http_response create_webserver new_response_types daemon_info uri_log feature_unavailable header_hygiene_iovec iovec_entry iovec_response http_method
29+
check_PROGRAMS = basic file_upload http_utils threaded nodelay string_utilities http_endpoint ban_system ws_start_stop authentication deferred http_resource http_response create_webserver new_response_types daemon_info uri_log feature_unavailable header_hygiene_iovec iovec_entry iovec_response http_method constants
3030

3131
MOSTLYCLEANFILES = *.gcda *.gcno *.gcov
3232

@@ -56,6 +56,7 @@ header_hygiene_iovec_SOURCES = unit/header_hygiene_iovec_test.cpp
5656
iovec_entry_SOURCES = unit/iovec_entry_test.cpp
5757
iovec_response_SOURCES = unit/iovec_response_test.cpp
5858
http_method_SOURCES = unit/http_method_test.cpp
59+
constants_SOURCES = unit/constants_test.cpp
5960

6061
noinst_HEADERS = littletest.hpp
6162
AM_CXXFLAGS += -Wall -fPIC -Wno-overloaded-virtual

0 commit comments

Comments
 (0)