Skip to content

Commit 0908e48

Browse files
committed
FEATURE-bauth-conditional-compile: Implementation complete
Add HAVE_BAUTH conditional compilation for basic authentication, mirroring the existing HAVE_DAUTH pattern. This allows libhttpserver to build against libmicrohttpd installations that lack basic auth support. Changes: - configure.ac: Auto-detect MHD_queue_basic_auth_fail_response, define HAVE_BAUTH flag and AM_CONDITIONAL, add to summary output - src/httpserver/basic_auth_fail_response.hpp: Guard with #ifdef HAVE_BAUTH - src/basic_auth_fail_response.cpp: Guard with #ifdef HAVE_BAUTH - src/httpserver.hpp: Conditionally include basic_auth_fail_response.hpp - src/httpserver/http_request.hpp: Guard get_user(), get_pass(), fetch_user_pass() declarations and username/password cache fields - src/http_request.cpp: Guard fetch_user_pass(), get_user(), get_pass() implementations and basic auth output in operator<< - src/httpserver/create_webserver.hpp: Guard basic_auth()/no_basic_auth() methods and _basic_auth_enabled member - src/httpserver/webserver.hpp: Guard basic_auth_enabled member - src/webserver.cpp: Guard basic_auth_enabled initialization - src/Makefile.am: Make basic_auth_fail_response conditional on HAVE_BAUTH - examples/Makefile.am: Guard basic_authentication and centralized_authentication examples behind HAVE_BAUTH - test/integ/authentication.cpp: Guard basic auth tests with HAVE_BAUTH - test/unit/create_webserver_test.cpp: Guard basic_auth builder test
1 parent 1a26dfc commit 0908e48

13 files changed

+71
-6
lines changed

configure.ac

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ fi
149149
AM_CONDITIONAL([COND_CROSS_COMPILE],[test x"$cond_cross_compile" = x"yes"])
150150
AC_SUBST(COND_CROSS_COMPILE)
151151

152+
# Check for basic auth support in libmicrohttpd
153+
AC_CHECK_LIB([microhttpd], [MHD_queue_basic_auth_fail_response],
154+
[have_bauth="yes"],
155+
[have_bauth="no"; AC_MSG_WARN("libmicrohttpd basic auth support not found. Basic auth will be disabled")])
156+
152157
# Check for digest auth support in libmicrohttpd
153158
AC_CHECK_LIB([microhttpd], [MHD_queue_auth_fail_response],
154159
[have_dauth="yes"],
@@ -264,6 +269,13 @@ fi
264269

265270
AM_CONDITIONAL([HAVE_GNUTLS],[test x"$have_gnutls" = x"yes"])
266271

272+
if test x"$have_bauth" = x"yes"; then
273+
AM_CXXFLAGS="$AM_CXXFLAGS -DHAVE_BAUTH"
274+
AM_CFLAGS="$AM_CXXFLAGS -DHAVE_BAUTH"
275+
fi
276+
277+
AM_CONDITIONAL([HAVE_BAUTH],[test x"$have_bauth" = x"yes"])
278+
267279
if test x"$have_dauth" = x"yes"; then
268280
AM_CXXFLAGS="$AM_CXXFLAGS -DHAVE_DAUTH"
269281
AM_CFLAGS="$AM_CXXFLAGS -DHAVE_DAUTH"
@@ -327,6 +339,7 @@ AC_MSG_NOTICE([Configuration Summary:
327339
License : LGPL only
328340
Debug : ${debugit}
329341
TLS Enabled : ${have_gnutls}
342+
Basic Auth : ${have_bauth}
330343
Digest Auth : ${have_dauth}
331344
TCP_FASTOPEN : ${is_fastopen_supported}
332345
Static : ${static}

examples/Makefile.am

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
LDADD = $(top_builddir)/src/libhttpserver.la
2020
AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/
2121
METASOURCES = AUTO
22-
noinst_PROGRAMS = hello_world service minimal_hello_world custom_error allowing_disallowing_methods handlers hello_with_get_arg args_processing setting_headers custom_access_log basic_authentication minimal_https minimal_file_response minimal_deferred url_registration minimal_ip_ban benchmark_select benchmark_threads benchmark_nodelay deferred_with_accumulator file_upload file_upload_with_callback
22+
noinst_PROGRAMS = hello_world service minimal_hello_world custom_error allowing_disallowing_methods handlers hello_with_get_arg args_processing setting_headers custom_access_log minimal_https minimal_file_response minimal_deferred url_registration minimal_ip_ban benchmark_select benchmark_threads benchmark_nodelay deferred_with_accumulator file_upload file_upload_with_callback
2323

2424
hello_world_SOURCES = hello_world.cpp
2525
service_SOURCES = service.cpp
@@ -31,7 +31,6 @@ hello_with_get_arg_SOURCES = hello_with_get_arg.cpp
3131
args_processing_SOURCES = args_processing.cpp
3232
setting_headers_SOURCES = setting_headers.cpp
3333
custom_access_log_SOURCES = custom_access_log.cpp
34-
basic_authentication_SOURCES = basic_authentication.cpp
3534
minimal_https_SOURCES = minimal_https.cpp
3635
minimal_file_response_SOURCES = minimal_file_response.cpp
3736
minimal_deferred_SOURCES = minimal_deferred.cpp
@@ -44,6 +43,12 @@ benchmark_nodelay_SOURCES = benchmark_nodelay.cpp
4443
file_upload_SOURCES = file_upload.cpp
4544
file_upload_with_callback_SOURCES = file_upload_with_callback.cpp
4645

46+
if HAVE_BAUTH
47+
noinst_PROGRAMS += basic_authentication centralized_authentication
48+
basic_authentication_SOURCES = basic_authentication.cpp
49+
centralized_authentication_SOURCES = centralized_authentication.cpp
50+
endif
51+
4752
if HAVE_GNUTLS
4853
LDADD += -lgnutls
4954
noinst_PROGRAMS += minimal_https_psk

src/Makefile.am

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@
1919
AM_CPPFLAGS = -I../ -I$(srcdir)/httpserver/
2020
METASOURCES = AUTO
2121
lib_LTLIBRARIES = libhttpserver.la
22-
libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp file_info.cpp http_request.cpp http_response.cpp string_response.cpp basic_auth_fail_response.cpp digest_auth_fail_response.cpp deferred_response.cpp file_response.cpp http_resource.cpp create_webserver.cpp details/http_endpoint.cpp
22+
libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp file_info.cpp http_request.cpp http_response.cpp string_response.cpp digest_auth_fail_response.cpp deferred_response.cpp file_response.cpp http_resource.cpp create_webserver.cpp details/http_endpoint.cpp
2323
noinst_HEADERS = httpserver/string_utilities.hpp httpserver/details/modded_request.hpp gettext.h
24-
nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/file_info.hpp httpserver/details/http_endpoint.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/string_response.hpp httpserver/basic_auth_fail_response.hpp httpserver/digest_auth_fail_response.hpp httpserver/deferred_response.hpp httpserver/file_response.hpp httpserver/http_arg_value.hpp
24+
nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/file_info.hpp httpserver/details/http_endpoint.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/http_arg_value.hpp
25+
26+
if HAVE_BAUTH
27+
libhttpserver_la_SOURCES += basic_auth_fail_response.cpp
28+
nobase_include_HEADERS += httpserver/basic_auth_fail_response.hpp
29+
endif
2530

2631
AM_CXXFLAGS += -fPIC -Wall
2732

src/basic_auth_fail_response.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
USA
1919
*/
2020

21+
#ifdef HAVE_BAUTH
22+
2123
#include "httpserver/basic_auth_fail_response.hpp"
2224
#include <microhttpd.h>
2325
#include <iosfwd>
@@ -32,3 +34,5 @@ int basic_auth_fail_response::enqueue_response(MHD_Connection* connection, MHD_R
3234
}
3335

3436
} // namespace httpserver
37+
38+
#endif // HAVE_BAUTH

src/http_request.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ MHD_Result http_request::build_request_querystring(void *cls, enum MHD_ValueKind
310310
return MHD_YES;
311311
}
312312

313+
#ifdef HAVE_BAUTH
313314
void http_request::fetch_user_pass() const {
314315
char* password = nullptr;
315316
auto* username = MHD_basic_auth_get_username_password(underlying_connection, &password);
@@ -339,6 +340,7 @@ std::string_view http_request::get_pass() const {
339340
fetch_user_pass();
340341
return cache->password;
341342
}
343+
#endif // HAVE_BAUTH
342344

343345
#ifdef HAVE_DAUTH
344346
std::string_view http_request::get_digested_user() const {
@@ -557,8 +559,11 @@ uint16_t http_request::get_requestor_port() const {
557559
}
558560

559561
std::ostream &operator<< (std::ostream &os, const http_request &r) {
560-
os << r.get_method() << " Request [user:\"" << r.get_user() << "\" pass:\"" << r.get_pass() << "\"] path:\""
561-
<< r.get_path() << "\"" << std::endl;
562+
os << r.get_method() << " Request [";
563+
#ifdef HAVE_BAUTH
564+
os << "user:\"" << r.get_user() << "\" pass:\"" << r.get_pass() << "\"";
565+
#endif // HAVE_BAUTH
566+
os << "] path:\"" << r.get_path() << "\"" << std::endl;
562567

563568
http::dump_header_map(os, "Headers", r.get_headers());
564569
http::dump_header_map(os, "Footers", r.get_footers());

src/httpserver.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828
#define _HTTPSERVER_HPP_INSIDE_
2929

30+
#ifdef HAVE_BAUTH
3031
#include "httpserver/basic_auth_fail_response.hpp"
32+
#endif // HAVE_BAUTH
3133
#include "httpserver/deferred_response.hpp"
3234
#ifdef HAVE_DAUTH
3335
#include "httpserver/digest_auth_fail_response.hpp"

src/httpserver/basic_auth_fail_response.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#ifndef SRC_HTTPSERVER_BASIC_AUTH_FAIL_RESPONSE_HPP_
2626
#define SRC_HTTPSERVER_BASIC_AUTH_FAIL_RESPONSE_HPP_
2727

28+
#ifdef HAVE_BAUTH
29+
2830
#include <string>
2931
#include "httpserver/http_utils.hpp"
3032
#include "httpserver/string_response.hpp"
@@ -60,4 +62,7 @@ class basic_auth_fail_response : public string_response {
6062
};
6163

6264
} // namespace httpserver
65+
66+
#endif // HAVE_BAUTH
67+
6368
#endif // SRC_HTTPSERVER_BASIC_AUTH_FAIL_RESPONSE_HPP_

src/httpserver/create_webserver.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ class create_webserver {
261261
return *this;
262262
}
263263

264+
#ifdef HAVE_BAUTH
264265
create_webserver& basic_auth() {
265266
_basic_auth_enabled = true;
266267
return *this;
@@ -270,6 +271,7 @@ class create_webserver {
270271
_basic_auth_enabled = false;
271272
return *this;
272273
}
274+
#endif // HAVE_BAUTH
273275

274276
create_webserver& digest_auth() {
275277
_digest_auth_enabled = true;
@@ -438,7 +440,9 @@ class create_webserver {
438440
std::string _digest_auth_random = "";
439441
int _nonce_nc_size = 0;
440442
http::http_utils::policy_T _default_policy = http::http_utils::ACCEPT;
443+
#ifdef HAVE_BAUTH
441444
bool _basic_auth_enabled = true;
445+
#endif // HAVE_BAUTH
442446
bool _digest_auth_enabled = true;
443447
bool _regex_checking = true;
444448
bool _ban_system_enabled = true;

src/httpserver/http_request.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ class http_request {
6060
public:
6161
static const char EMPTY[];
6262

63+
#ifdef HAVE_BAUTH
6364
/**
6465
* Method used to get the username eventually passed through basic authentication.
6566
* @return string representation of the username.
6667
**/
6768
std::string_view get_user() const;
69+
#endif // HAVE_BAUTH
6870

6971
#ifdef HAVE_DAUTH
7072
/**
@@ -74,11 +76,13 @@ class http_request {
7476
std::string_view get_digested_user() const;
7577
#endif // HAVE_DAUTH
7678

79+
#ifdef HAVE_BAUTH
7780
/**
7881
* Method used to get the password eventually passed through basic authentication.
7982
* @return string representation of the password.
8083
**/
8184
std::string_view get_pass() const;
85+
#endif // HAVE_BAUTH
8286

8387
/**
8488
* Method used to get the path requested
@@ -380,7 +384,9 @@ class http_request {
380384

381385
static MHD_Result build_request_querystring(void *cls, enum MHD_ValueKind kind, const char *key, const char *value);
382386

387+
#ifdef HAVE_BAUTH
383388
void fetch_user_pass() const;
389+
#endif // HAVE_BAUTH
384390

385391
/**
386392
* Method used to set an argument value by key.
@@ -485,8 +491,10 @@ class http_request {
485491
// Others (username, password, digested_user) MHD returns as char* that we need
486492
// to make a copy of and free anyway.
487493
struct http_request_data_cache {
494+
#ifdef HAVE_BAUTH
488495
std::string username;
489496
std::string password;
497+
#endif // HAVE_BAUTH
490498
std::string querystring;
491499
std::string requestor_ip;
492500
#ifdef HAVE_DAUTH

src/httpserver/webserver.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ class webserver {
168168
const int nonce_nc_size;
169169
bool running;
170170
const http::http_utils::policy_T default_policy;
171+
#ifdef HAVE_BAUTH
171172
const bool basic_auth_enabled;
173+
#endif // HAVE_BAUTH
172174
const bool digest_auth_enabled;
173175
const bool regex_checking;
174176
const bool ban_system_enabled;

0 commit comments

Comments
 (0)