2022-08-11 23:40:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2022-08-13 01:30:03 +00:00
|
|
|
#include <NonPosix.h>
|
|
|
|
|
2022-08-11 23:40:13 +00:00
|
|
|
#include <HttpServer.h>
|
2022-08-24 14:06:14 +00:00
|
|
|
#include <Queue.h>
|
2022-08-24 23:31:28 +00:00
|
|
|
#include <Array.h>
|
|
|
|
#include <Util.h>
|
2022-08-11 23:40:13 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
2022-08-24 14:06:14 +00:00
|
|
|
#include <stdio.h>
|
2022-08-11 23:40:13 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2022-08-12 01:19:52 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <poll.h>
|
2022-08-12 00:13:10 +00:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2022-08-11 23:40:13 +00:00
|
|
|
|
|
|
|
struct HttpServer
|
|
|
|
{
|
|
|
|
int sd;
|
|
|
|
unsigned int nThreads;
|
2022-08-21 16:35:16 +00:00
|
|
|
unsigned int maxConnections;
|
2022-08-11 23:40:13 +00:00
|
|
|
pthread_t socketThread;
|
|
|
|
|
|
|
|
volatile unsigned int stop:1;
|
|
|
|
volatile unsigned int isRunning:1;
|
2022-08-12 01:19:52 +00:00
|
|
|
|
|
|
|
HttpHandler *requestHandler;
|
|
|
|
void *handlerArgs;
|
2022-08-24 14:06:14 +00:00
|
|
|
|
|
|
|
Queue *connQueue;
|
|
|
|
pthread_mutex_t connQueueMutex;
|
2022-08-24 23:31:28 +00:00
|
|
|
|
|
|
|
Array *threadPool;
|
2022-08-11 23:40:13 +00:00
|
|
|
};
|
|
|
|
|
2022-08-26 15:07:54 +00:00
|
|
|
struct HttpServerContext
|
|
|
|
{
|
|
|
|
HashMap *requestHeaders;
|
|
|
|
HttpRequestMethod requestMethod;
|
|
|
|
char *requestPath;
|
|
|
|
|
|
|
|
HashMap *responseHeaders;
|
|
|
|
HttpStatus responseStatus;
|
|
|
|
|
|
|
|
FILE *stream;
|
|
|
|
};
|
|
|
|
|
2022-08-24 14:06:14 +00:00
|
|
|
static int
|
|
|
|
QueueConnection(HttpServer * server, int fd)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (!server)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-24 20:28:50 +00:00
|
|
|
fp = fdopen(fd, "r+");
|
2022-08-24 14:06:14 +00:00
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&server->connQueueMutex);
|
|
|
|
result = QueuePush(server->connQueue, fp);
|
|
|
|
pthread_mutex_unlock(&server->connQueueMutex);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *
|
|
|
|
DequeueConnection(HttpServer * server)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (!server)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&server->connQueueMutex);
|
|
|
|
fp = QueuePop(server->connQueue);
|
|
|
|
pthread_mutex_unlock(&server->connQueueMutex);
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
2022-08-11 23:40:13 +00:00
|
|
|
HttpServer *
|
2022-08-21 16:35:16 +00:00
|
|
|
HttpServerCreate(unsigned short port, unsigned int nThreads, unsigned int maxConnections,
|
2022-08-12 01:19:52 +00:00
|
|
|
HttpHandler * requestHandler, void *handlerArgs)
|
2022-08-11 23:40:13 +00:00
|
|
|
{
|
2022-08-12 01:19:52 +00:00
|
|
|
HttpServer *server;
|
2022-08-12 00:13:10 +00:00
|
|
|
struct sockaddr_in sa = {0};
|
2022-08-11 23:40:13 +00:00
|
|
|
|
2022-08-12 01:19:52 +00:00
|
|
|
if (!requestHandler)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-08-24 23:31:28 +00:00
|
|
|
server = calloc(1, sizeof(HttpServer));
|
2022-08-11 23:40:13 +00:00
|
|
|
if (!server)
|
|
|
|
{
|
2022-08-24 23:31:28 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->threadPool = ArrayCreate();
|
|
|
|
if (!server->threadPool)
|
|
|
|
{
|
|
|
|
goto error;
|
2022-08-11 23:40:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 14:06:14 +00:00
|
|
|
server->connQueue = QueueCreate(maxConnections);
|
|
|
|
if (!server->connQueue)
|
|
|
|
{
|
2022-08-24 23:31:28 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_mutex_init(&server->connQueueMutex, NULL) != 0)
|
|
|
|
{
|
|
|
|
goto error;
|
2022-08-24 14:06:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 01:30:03 +00:00
|
|
|
server->sd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
2022-08-12 00:13:10 +00:00
|
|
|
|
|
|
|
if (server->sd < 0)
|
|
|
|
{
|
2022-08-24 23:31:28 +00:00
|
|
|
goto error;
|
2022-08-12 00:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sa.sin_family = AF_INET;
|
2022-08-24 18:37:32 +00:00
|
|
|
sa.sin_port = htons(port);
|
2022-08-12 00:13:10 +00:00
|
|
|
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
if (bind(server->sd, (struct sockaddr *) & sa, sizeof(sa)) < 0)
|
|
|
|
{
|
2022-08-24 23:31:28 +00:00
|
|
|
goto error;
|
2022-08-12 00:13:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 16:35:16 +00:00
|
|
|
if (listen(server->sd, maxConnections) < 0)
|
2022-08-12 00:13:10 +00:00
|
|
|
{
|
2022-08-24 23:31:28 +00:00
|
|
|
goto error;
|
2022-08-12 00:13:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 23:40:13 +00:00
|
|
|
server->nThreads = nThreads;
|
2022-08-21 16:35:16 +00:00
|
|
|
server->maxConnections = maxConnections;
|
2022-08-11 23:40:13 +00:00
|
|
|
server->requestHandler = requestHandler;
|
|
|
|
server->handlerArgs = handlerArgs;
|
|
|
|
server->stop = 0;
|
|
|
|
server->isRunning = 0;
|
|
|
|
|
|
|
|
return server;
|
2022-08-24 23:31:28 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (server)
|
|
|
|
{
|
|
|
|
if (server->connQueue)
|
|
|
|
{
|
|
|
|
QueueFree(server->connQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server->connQueueMutex)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&server->connQueueMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server->threadPool)
|
|
|
|
{
|
|
|
|
ArrayFree(server->threadPool);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server->sd)
|
|
|
|
{
|
|
|
|
close(server->sd);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(server);
|
|
|
|
}
|
|
|
|
return NULL;
|
2022-08-11 23:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpServerFree(HttpServer * server)
|
|
|
|
{
|
2022-08-12 00:13:10 +00:00
|
|
|
if (!server)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(server->sd);
|
2022-08-24 15:25:16 +00:00
|
|
|
QueueFree(server->connQueue);
|
|
|
|
pthread_mutex_destroy(&server->connQueueMutex);
|
2022-08-24 23:31:28 +00:00
|
|
|
ArrayFree(server->threadPool);
|
2022-08-11 23:40:13 +00:00
|
|
|
free(server);
|
|
|
|
}
|
|
|
|
|
2022-08-24 23:31:28 +00:00
|
|
|
static void *
|
|
|
|
HttpServerWorkerThread(void *args)
|
|
|
|
{
|
|
|
|
HttpServer *server = (HttpServer *) args;
|
|
|
|
|
|
|
|
while (!server->stop)
|
|
|
|
{
|
|
|
|
FILE *fp = DequeueConnection(server);
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
/* Block for 1 millisecond before continuting so we don't
|
|
|
|
* murder the CPU */
|
|
|
|
UtilSleepMillis(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "HTTP/1.1 500 Internal Server Error\n");
|
|
|
|
fprintf(fp, "Server: Telodendria v" TELODENDRIA_VERSION "\n");
|
|
|
|
fprintf(fp, "Content-Type: application/json\n");
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fprintf(fp, "{}\n");
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-08-11 23:40:13 +00:00
|
|
|
static void *
|
|
|
|
HttpServerEventThread(void *args)
|
|
|
|
{
|
|
|
|
HttpServer *server = (HttpServer *) args;
|
2022-08-12 01:19:52 +00:00
|
|
|
struct pollfd pollFds[1];
|
2022-08-24 14:06:14 +00:00
|
|
|
FILE *fp;
|
2022-08-24 23:31:28 +00:00
|
|
|
size_t i;
|
2022-08-11 23:40:13 +00:00
|
|
|
|
|
|
|
server->isRunning = 1;
|
|
|
|
server->stop = 0;
|
|
|
|
|
2022-08-12 01:19:52 +00:00
|
|
|
pollFds[0].fd = server->sd;
|
|
|
|
pollFds[0].events = POLLIN;
|
|
|
|
|
2022-08-24 23:31:28 +00:00
|
|
|
for (i = 0; i < server->nThreads; i++)
|
|
|
|
{
|
|
|
|
pthread_t *workerThread = malloc(sizeof(pthread_t));
|
|
|
|
|
|
|
|
if (!workerThread)
|
|
|
|
{
|
|
|
|
/* TODO: Make the event thread return an error to the main
|
|
|
|
* thread */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_create(workerThread, NULL, HttpServerWorkerThread, server) != 0)
|
|
|
|
{
|
|
|
|
/* TODO: Make the event thread return an error to the main
|
|
|
|
* thread */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayAdd(server->threadPool, workerThread);
|
|
|
|
}
|
|
|
|
|
2022-08-11 23:40:13 +00:00
|
|
|
while (!server->stop)
|
|
|
|
{
|
2022-08-12 01:19:52 +00:00
|
|
|
struct sockaddr_storage addr;
|
|
|
|
socklen_t addrLen = sizeof(addr);
|
|
|
|
int connFd;
|
2022-08-24 15:25:16 +00:00
|
|
|
int pollResult;
|
|
|
|
|
|
|
|
pollResult = poll(pollFds, 1, 500);
|
2022-08-12 01:19:52 +00:00
|
|
|
|
|
|
|
if (pollResult < 0)
|
|
|
|
{
|
|
|
|
/* The poll either timed out, or was interrupted. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
connFd = accept(server->sd, (struct sockaddr *) & addr, &addrLen);
|
|
|
|
|
|
|
|
if (connFd < 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-24 14:06:14 +00:00
|
|
|
QueueConnection(server, connFd);
|
|
|
|
}
|
|
|
|
|
2022-08-24 23:31:28 +00:00
|
|
|
for (i = 0; i < server->nThreads; i++)
|
|
|
|
{
|
|
|
|
pthread_t *workerThread = ArrayGet(server->threadPool, i);
|
|
|
|
|
|
|
|
pthread_join(*workerThread, NULL);
|
|
|
|
free(workerThread);
|
|
|
|
}
|
2022-08-24 14:06:14 +00:00
|
|
|
|
|
|
|
while ((fp = DequeueConnection(server)))
|
|
|
|
{
|
|
|
|
fclose(fp);
|
2022-08-11 23:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
server->isRunning = 0;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
HttpServerStart(HttpServer * server)
|
|
|
|
{
|
|
|
|
if (!server)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server->isRunning)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_create(&server->socketThread, NULL, HttpServerEventThread, server) != 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpServerJoin(HttpServer * server)
|
|
|
|
{
|
|
|
|
if (!server)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_join(server->socketThread, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpServerStop(HttpServer * server)
|
|
|
|
{
|
|
|
|
if (!server)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->stop = 1;
|
|
|
|
}
|