Start working on adding TLS support to HttpServer.

This commit is contained in:
Jordan Bancino 2023-03-22 01:46:24 +00:00
parent 6561b5bae1
commit b58ca7d22e
8 changed files with 37 additions and 13 deletions

View file

@ -61,7 +61,7 @@ HttpRequest(HttpRequestMethod method, int flags, unsigned short port, char *host
}
#ifndef TLS_IMPL
if (flags & HTTP_TLS)
if (flags & HTTP_FLAG_TLS)
{
return NULL;
}
@ -69,7 +69,7 @@ HttpRequest(HttpRequestMethod method, int flags, unsigned short port, char *host
if (!port)
{
if (flags & HTTP_TLS)
if (flags & HTTP_FLAG_TLS)
{
strcpy(serv, "https");
}
@ -130,7 +130,7 @@ HttpRequest(HttpRequestMethod method, int flags, unsigned short port, char *host
freeaddrinfo(res0);
#ifdef TLS_IMPL
if (flags & HTTP_TLS)
if (flags & HTTP_FLAG_TLS)
{
context->stream = TlsClientStream(sd, host);
}

View file

@ -26,6 +26,7 @@
#include <Queue.h>
#include <Array.h>
#include <Util.h>
#include <Tls.h>
#include <pthread.h>
#include <stdio.h>
@ -51,6 +52,7 @@ struct HttpServer
unsigned int nThreads;
unsigned int maxConnections;
pthread_t socketThread;
int flags;
volatile unsigned int stop:1;
volatile unsigned int isRunning:1;
@ -276,7 +278,7 @@ DequeueConnection(HttpServer * server)
}
HttpServer *
HttpServerCreate(unsigned short port, unsigned int nThreads, unsigned int maxConnections,
HttpServerCreate(int flags, unsigned short port, unsigned int nThreads, unsigned int maxConnections,
HttpHandler * requestHandler, void *handlerArgs)
{
HttpServer *server;
@ -287,6 +289,13 @@ HttpServerCreate(unsigned short port, unsigned int nThreads, unsigned int maxCon
return NULL;
}
#ifndef TLS_IMPL
if (flags & HTTP_FLAG_TLS)
{
return NULL;
}
#endif
server = Malloc(sizeof(HttpServer));
if (!server)
{
@ -295,6 +304,8 @@ HttpServerCreate(unsigned short port, unsigned int nThreads, unsigned int maxCon
memset(server, 0, sizeof(HttpServer));
server->flags = flags;
server->threadPool = ArrayCreate();
if (!server->threadPool)
{
@ -632,7 +643,20 @@ HttpServerEventThread(void *args)
continue;
}
#ifdef TLS_IMPL
if (server->flags & HTTP_FLAG_TLS)
{
/* TODO: Get server cert and key in here */
fp = TlsServerStream(connFd, NULL, NULL);
}
else
{
fp = StreamFd(connFd);
}
#else
fp = StreamFd(connFd);
#endif
if (!fp)
{
pthread_mutex_unlock(&server->connQueueMutex);

View file

@ -274,8 +274,8 @@ main(int argc, char **argv)
matrixArgs.config = tConfig;
/* Bind the socket before possibly dropping permissions */
httpServer = HttpServerCreate(tConfig->listenPort, tConfig->threads, tConfig->maxConnections,
MatrixHttpHandler, &matrixArgs);
httpServer = HttpServerCreate(HTTP_FLAG_NONE, tConfig->listenPort, tConfig->threads,
tConfig->maxConnections, MatrixHttpHandler, &matrixArgs);
if (!httpServer)
{
Log(lc, LOG_ERR, "Unable to create HTTP server on port %d: %s",

View file

@ -29,6 +29,9 @@
#include <HashMap.h>
#include <Stream.h>
#define HTTP_FLAG_NONE 0
#define HTTP_FLAG_TLS (1 << 0)
typedef enum HttpRequestMethod
{
HTTP_METHOD_UNKNOWN,

View file

@ -29,9 +29,6 @@
#include <HashMap.h>
#include <Http.h>
#define HTTP_NONE 0
#define HTTP_TLS (1 << 0)
typedef struct HttpClientContext HttpClientContext;
extern HttpClientContext *

View file

@ -37,7 +37,7 @@ typedef struct HttpServerContext HttpServerContext;
typedef void (HttpHandler) (HttpServerContext *, void *);
extern HttpServer *
HttpServerCreate(unsigned short, unsigned int, unsigned int, HttpHandler *, void *);
HttpServerCreate(int, unsigned short, unsigned int, unsigned int, HttpHandler *, void *);
extern void
HttpServerFree(HttpServer *);

View file

@ -74,7 +74,7 @@ main(void)
{
struct sigaction sa;
server = HttpServerCreate(8008, 1, 1, HttpHandle, NULL);
server = HttpServerCreate(HTTP_FLAG_NONE, 8008, 1, 1, HttpHandle, NULL);
if (!HttpServerStart(server))
{
StreamPuts(StreamStderr(), "Unable to start HTTP server.\n");

View file

@ -57,7 +57,7 @@ main(int argc, char **argv)
char *val;
int flags = 0;
int requestFlags = HTTP_NONE;
int requestFlags = HTTP_FLAG_NONE;
int ch;
@ -138,7 +138,7 @@ main(int argc, char **argv)
if (strcmp(uri->proto, "https") == 0)
{
requestFlags |= HTTP_TLS;
requestFlags |= HTTP_FLAG_TLS;
}
cx = HttpRequest(method, requestFlags, uri->port, uri->host, uri->path);