Skip to content

Commit c5cf5ea

Browse files
authored
Add a getter to allow access to the gnutls_session_t. (#233)
* Add a getter to allow access to the gnutls_session_t. * Placate lint. * Placate lint, some more. * Document get_tls_session. * Add http_request::has_tls_session to allow checking if http_request::get_tls_session is safe to call. * Spelling and grammer.
1 parent d5a5f88 commit c5cf5ea

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ The `http_request` class has a set of methods you will have access to when imple
563563
* _**const std::string** get_pass() **const**:_ Returns the `password` as self-identified through basic authentication. The content of the password header will be parsed only if basic authentication is enabled on the server (enabled by default).
564564
* _**const std::string** get_digested_user() **const**:_ Returns the `digested user` as self-identified through digest authentication. The content of the user header will be parsed only if digest authentication is enabled on the server (enabled by default).
565565
* _**bool** check_digest_auth(**const std::string&** realm, **const std::string&** password, **int** nonce_timeout, **bool*** reload_nonce) **const**:_ Allows to check the validity of the authentication token sent through digest authentication (if the provided values in the WWW-Authenticate header are valid and sound according to RFC2716). Takes in input the `realm` of validity of the authentication, the `password` as known to the server to compare against, the `nonce_timeout` to indicate how long the nonce is valid and `reload_nonce` a boolean that will be set by the method to indicate a nonce being reloaded. The method returns `true` if the authentication is valid, `false` otherwise.
566+
* _**gnutls_session_t** get_tls_session() **const**:_ Tests if there is am underlying TLS state of the current request.
567+
* _**gnutls_session_t** get_tls_session() **const**:_ Returns the underlying TLS state of the current request for inspection. (It is an error to call this if the state does not exist.)
566568

567569
#### Example of handler reading arguments from a request
568570
#include <httpserver.hpp>

src/http_request.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ const std::string http_request::get_digested_user() const {
212212
return digested_user;
213213
}
214214

215+
#ifdef HAVE_GNUTLS
216+
bool http_request::has_tls_session() const {
217+
const MHD_ConnectionInfo * conninfo = MHD_get_connection_info(underlying_connection, MHD_CONNECTION_INFO_GNUTLS_SESSION);
218+
return (conninfo != nullptr);
219+
}
220+
221+
gnutls_session_t http_request::get_tls_session() const {
222+
const MHD_ConnectionInfo * conninfo = MHD_get_connection_info(underlying_connection, MHD_CONNECTION_INFO_GNUTLS_SESSION);
223+
224+
return static_cast<gnutls_session_t>(conninfo->tls_session);
225+
}
226+
#endif // HAVE_GNUTLS
227+
215228
const std::string http_request::get_requestor() const {
216229
const MHD_ConnectionInfo * conninfo = MHD_get_connection_info(underlying_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
217230

src/httpserver/http_request.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727

2828
#include <microhttpd.h>
2929

30+
#ifdef HAVE_GNUTLS
31+
#include <gnutls/gnutls.h>
32+
#endif // HAVE_GNUTLS
33+
3034
#include <stddef.h>
3135
#include <algorithm>
3236
#include <iosfwd>
@@ -183,6 +187,20 @@ class http_request {
183187
return version;
184188
}
185189

190+
#ifdef HAVE_GNUTLS
191+
/**
192+
* Method used to check if there is a TLS session.
193+
* @return the TLS session
194+
**/
195+
bool has_tls_session() const;
196+
197+
/**
198+
* Method used to get the TLS session.
199+
* @return the TLS session
200+
**/
201+
gnutls_session_t get_tls_session() const;
202+
#endif // HAVE_GNUTLS
203+
186204
/**
187205
* Method used to get the requestor.
188206
* @return the requestor

0 commit comments

Comments
 (0)