Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement custom host name support #66

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,39 +166,6 @@ if(UNIX AND ((NOT APPLE) AND (NOT ANDROID)))
endif()
endif()

if(MINGW)
message(STATUS "Looking for inet_ntop")
set(_chk_inet_ntop_src "${CMAKE_BINARY_DIR}/check_inet_ntop.c")
file(
WRITE ${_chk_inet_ntop_src}
"#include <stdio.h>\n#include <ws2tcpip.h>\nint main(void){inet_ntop(0,NULL,NULL,0);return 0;}"
)
try_compile(HAVE_INET_NTOP ${CMAKE_BINARY_DIR} ${_chk_inet_ntop_src}
LINK_LIBRARIES ws2_32)
file(REMOVE ${_chk_inet_ntop_src})
unset(_chk_inet_ntop_src)
if(${HAVE_INET_NTOP})
message(STATUS "Looking for inet_ntop - found")
else()
message(STATUS "Looking for inet_ntop - not found")
endif()
else()
include(CheckIncludeFile)
check_include_file(arpa/inet.h HAVE_ARPA_INET_H)
if(HAVE_ARPA_INET_H)
add_definitions(-DHAVE_ARPA_INET_H=1)
endif()
include(CheckFunctionExists)
check_function_exists(inet_ntop HAVE_INET_NTOP)
if(NOT HAVE_INET_NTOP)
include(CheckSymbolExists)
check_symbol_exists(inet_ntop "arpa/inet.h" HAVE_INET_NTOP)
endif()
endif()
if(${HAVE_INET_NTOP})
add_definitions(-DHAVE_INET_NTOP=1)
endif()

include_directories(${SG_INCLUDE_DIR})
include_directories(${MHD_INCLUDE_DIR})
if(SG_HTTP_COMPRESSION)
Expand Down
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# SPDX-License-Identifier: MIT
######################################################################

# podman build --platform linux/amd64 -t hello_sagui .
# podman run --platform linux/amd64 --rm -it -p 8080:8080 hello_sagui
# podman build -t hello_sagui .
# podman run --rm -p 8080:8080 -it hello_sagui

FROM alpine:3.19.1 AS builder

Expand All @@ -15,11 +15,8 @@ RUN apk add --no-cache \
automake \
clang \
cmake

WORKDIR /app

COPY . /app/

RUN mkdir build && \
cd build/ && \
cmake -DBUILD_SHARED_LIBS=OFF .. && \
Expand Down
9 changes: 1 addition & 8 deletions doxygen/Doxyfile.in
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-2021 Silvio Clecio <[email protected]>
# Copyright (C) 2016-2024 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 @@ -1163,13 +1163,6 @@ CLANG_DATABASE_PATH =

ALPHABETICAL_INDEX = YES

# The COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns in
# which the alphabetical index list will be split.
# Minimum value: 1, maximum value: 20, default value: 5.
# This tag requires that the tag ALPHABETICAL_INDEX is set to YES.

COLS_IN_ALPHA_INDEX = 5

# In case all classes in a project start with a common prefix, all classes will
# be put under the same header in the alphabetical index. The IGNORE_PREFIX tag
# can be used to specify a prefix (or a list of prefixes) that should be ignored
Expand Down
14 changes: 13 additions & 1 deletion examples/example_httpsrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <sagui.h>

/* NOTE: Error checking has been omitted to make it clear. */

static bool terminated = false;

static void sig_handler(__SG_UNUSED int signum) {
terminated = true;
}

static void req_cb(__SG_UNUSED void *cls, __SG_UNUSED struct sg_httpreq *req,
struct sg_httpres *res) {
sg_httpres_send(res,
Expand All @@ -46,6 +54,8 @@ int main(int argc, const char *argv[]) {
printf("%s <PORT>\n", argv[0]);
return EXIT_FAILURE;
}
signal(SIGTERM, sig_handler);
signal(SIGINT, sig_handler);
port = strtol(argv[1], NULL, 10);
srv = sg_httpsrv_new(req_cb, NULL);
if (!sg_httpsrv_listen(srv, port, false)) {
Expand All @@ -55,7 +65,9 @@ int main(int argc, const char *argv[]) {
fprintf(stdout, "Server running at http://localhost:%d\n",
sg_httpsrv_port(srv));
fflush(stdout);
getchar();
while (!terminated) {
usleep(100 * 1000);
}
sg_httpsrv_free(srv);
return EXIT_SUCCESS;
}
54 changes: 52 additions & 2 deletions include/sagui.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ extern "C" {
#endif /* __SG_FORMAT */

#define SG_VERSION_MAJOR 3
#define SG_VERSION_MINOR 4
#define SG_VERSION_PATCH 4
#define SG_VERSION_MINOR 5
#define SG_VERSION_PATCH 0
#define SG_VERSION_HEX \
((SG_VERSION_MAJOR << 16) | (SG_VERSION_MINOR << 8) | (SG_VERSION_PATCH))

Expand Down Expand Up @@ -1424,6 +1424,38 @@ 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] hostname Host name for listening to connections.
* \param[in] port Port for listening to connections.
* \param[in] backlog Maximum length of the queue of pending connections.
* Default: `511`.
* \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_listen4(struct sg_httpsrv *srv, const char *key,
const char *pwd, const char *cert,
const char *trust, const char *dhparams,
const char *priorities,
const char *hostname, uint16_t port,
uint32_t backlog, bool threaded);

/**
* Starts the HTTPS server.
* \param[in] srv Server handle.
Expand Down Expand Up @@ -1498,6 +1530,24 @@ SG_EXTERN bool sg_httpsrv_tls_listen(struct sg_httpsrv *srv, const char *key,

#endif /* SG_HTTPS_SUPPORT */

/**
* Starts the HTTP server.
* \param[in] srv Server handle.
* \param[in] hostname Host name for listening to connections.
* \param[in] port Port for listening to connections.
* \param[in] backlog Maximum length of the queue of pending connections.
* Default: `511`.
* \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 randomly assign an unused
* port.
*/
SG_EXTERN bool sg_httpsrv_listen2(struct sg_httpsrv *srv, const char *hostname,
uint16_t port, uint32_t backlog,
bool threaded);

/**
* Starts the HTTP server.
* \param[in] srv Server handle.
Expand Down
5 changes: 1 addition & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#
# Cross-platform library which helps to develop web servers or frameworks.
#
# Copyright (C) 2016-2020 Silvio Clecio <[email protected]>
# Copyright (C) 2016-2024 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 @@ -65,9 +65,6 @@ list(
${SG_SOURCE_DIR}/sg_httpreq.c
${SG_SOURCE_DIR}/sg_httpres.c
${SG_SOURCE_DIR}/sg_httpsrv.c)
if(NOT HAVE_INET_NTOP)
list(APPEND SG_C_SOURCE ${SG_SOURCE_DIR}/inet_ntop.c)
endif()
if(SG_PATH_ROUTING)
list(APPEND SG_C_SOURCE ${SG_SOURCE_DIR}/sg_entrypoint.c
${SG_SOURCE_DIR}/sg_entrypoints.c ${SG_SOURCE_DIR}/sg_routes.c
Expand Down
38 changes: 0 additions & 38 deletions src/inet.h

This file was deleted.

Loading