Skip to content

Commit

Permalink
Added new function "sg_httpsrv_tls_listen3()"
Browse files Browse the repository at this point in the history
  • Loading branch information
silvioprog committed Dec 19, 2021
1 parent 21a27b1 commit 2aff171
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
28 changes: 28 additions & 0 deletions include/sagui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,34 @@ SG_EXTERN void sg_httpsrv_free(struct sg_httpsrv *srv);

#ifdef SG_HTTPS_SUPPORT

/**
* Starts the HTTPS server.
* \param[in] srv Server handle.
* \param[in] key Memory pointer for the private key (key.pem) to be used by
* the HTTPS server.
* \param[in] pwd Password for the private key.
* \param[in] cert Memory pointer for the certificate (cert.pem) to be used by
* the HTTPS server.
* \param[in] trust Memory pointer for the certificate (ca.pem) to be used by
* the HTTPS server for client authentication.
* \param[in] dhparams Memory pointer for the Diffie Hellman parameters (dh.pem)
* to be used by the HTTPS server for key exchange.
* \param[in] priorities Memory pointer specifying the cipher algorithm.
* Default: `"NORMAL"`.
* \param[in] port Port for listening to connections.
* \param[in] threaded Enables/disables the threaded mode. If `true`, the
* server creates one thread per connection.
* \retval true If the server is started, `false` otherwise. If \pr{srv} is
* null, set the `errno` to `EINVAL`.
* \note If port is `0`, the operating system will assign an unused port
* randomly.
*/
SG_EXTERN bool sg_httpsrv_tls_listen3(struct sg_httpsrv *srv, const char *key,
const char *pwd, const char *cert,
const char *trust, const char *dhparams,
const char *priorities, uint16_t port,
bool threaded);

/**
* Starts the HTTPS server.
* \param[in] srv Server handle.
Expand Down
28 changes: 23 additions & 5 deletions src/sg_httpsrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (C) 2016-2020 Silvio Clecio <[email protected]>
* Copyright (C) 2016-2021 Silvio Clecio <[email protected]>
*
* Sagui library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -144,7 +144,8 @@ static void sg__httpsrv_addopt(struct MHD_OptionItem ops[8], unsigned char *pos,
static bool sg__httpsrv_listen(struct sg_httpsrv *srv, const char *key,
const char *pwd, const char *cert,
const char *trust, const char *dhparams,
uint16_t port, bool threaded) {
const char *priorities, uint16_t port,
bool threaded) {
struct MHD_OptionItem ops[8];
unsigned int flags;
unsigned char pos = 0;
Expand Down Expand Up @@ -186,6 +187,9 @@ static bool sg__httpsrv_listen(struct sg_httpsrv *srv, const char *key,
if (dhparams)
sg__httpsrv_addopt(ops, &pos, MHD_OPTION_HTTPS_MEM_DHPARAMS, 0,
(void *) dhparams);
if (priorities)
sg__httpsrv_addopt(ops, &pos, MHD_OPTION_HTTPS_PRIORITIES, 0,
(void *) priorities);
}
sg__httpsrv_addopt(ops, &pos, MHD_OPTION_END, 0, NULL);
srv->handle = MHD_start_daemon(flags, port, NULL, NULL, sg__httpsrv_ahc, srv,
Expand Down Expand Up @@ -303,12 +307,24 @@ void sg_httpsrv_free(struct sg_httpsrv *srv) {

#ifdef SG_HTTPS_SUPPORT

bool sg_httpsrv_tls_listen3(struct sg_httpsrv *srv, const char *key,
const char *pwd, const char *cert,
const char *trust, const char *dhparams,
const char *priorities, uint16_t port,
bool threaded) {
if (key && cert)
return sg__httpsrv_listen(srv, key, pwd, cert, trust, dhparams, priorities,
port, threaded);
errno = EINVAL;
return false;
}

bool sg_httpsrv_tls_listen2(struct sg_httpsrv *srv, const char *key,
const char *pwd, const char *cert,
const char *trust, const char *dhparams,
uint16_t port, bool threaded) {
if (key && cert)
return sg__httpsrv_listen(srv, key, pwd, cert, trust, dhparams, port,
return sg__httpsrv_listen(srv, key, pwd, cert, trust, dhparams, NULL, port,
threaded);
errno = EINVAL;
return false;
Expand All @@ -317,15 +333,17 @@ bool sg_httpsrv_tls_listen2(struct sg_httpsrv *srv, const char *key,
bool sg_httpsrv_tls_listen(struct sg_httpsrv *srv, const char *key,
const char *cert, uint16_t port, bool threaded) {
if (key && cert)
return sg__httpsrv_listen(srv, key, NULL, cert, NULL, NULL, port, threaded);
return sg__httpsrv_listen(srv, key, NULL, cert, NULL, NULL, NULL, port,
threaded);
errno = EINVAL;
return false;
}

#endif /* SG_HTTPS_SUPPORT */

bool sg_httpsrv_listen(struct sg_httpsrv *srv, uint16_t port, bool threaded) {
return sg__httpsrv_listen(srv, NULL, NULL, NULL, NULL, NULL, port, threaded);
return sg__httpsrv_listen(srv, NULL, NULL, NULL, NULL, NULL, NULL, port,
threaded);
}

int sg_httpsrv_shutdown(struct sg_httpsrv *srv) {
Expand Down
14 changes: 13 additions & 1 deletion test/test_httpsrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Cross-platform library which helps to develop web servers or frameworks.
*
* Copyright (C) 2016-2020 Silvio Clecio <[email protected]>
* Copyright (C) 2016-2021 Silvio Clecio <[email protected]>
*
* Sagui library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -523,6 +523,17 @@ static void test_httpsrv_tls_listen2(struct sg_httpsrv *srv) {
ASSERT(errno == EINVAL);
}

static void test_httpsrv_tls_listen3(struct sg_httpsrv *srv) {
errno = 0;
ASSERT(!sg_httpsrv_tls_listen3(srv, NULL, "", "", "", "", "",
TEST_HTTPSRV_PORT, false));
ASSERT(errno == EINVAL);
errno = 0;
ASSERT(!sg_httpsrv_tls_listen3(srv, "", "", NULL, "", "", "",
TEST_HTTPSRV_PORT, false));
ASSERT(errno == EINVAL);
}

#endif /* SG_HTTPS_SUPPORT */

static void test_httpsrv_shutdown(struct sg_httpsrv *srv) {
Expand Down Expand Up @@ -782,6 +793,7 @@ int main(void) {
#ifdef SG_HTTPS_SUPPORT
test_httpsrv_tls_listen(srv);
test_httpsrv_tls_listen2(srv);
test_httpsrv_tls_listen3(srv);
#endif /* SG_HTTPS_SUPPORT */
test_httpsrv_shutdown(srv);
test_httpsrv_port(srv);
Expand Down

0 comments on commit 2aff171

Please sign in to comment.