2022-08-11 23:40:13 +00:00
|
|
|
/*
|
2022-12-26 15:52:52 +00:00
|
|
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
2022-08-11 23:40:13 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#include <HttpServer.h>
|
2022-10-13 16:18:42 +00:00
|
|
|
#include <Memory.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-28 18:30:15 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2022-08-12 00:13:10 +00:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2022-10-15 14:08:52 +00:00
|
|
|
#include <fcntl.h>
|
2022-08-11 23:40:13 +00:00
|
|
|
|
2022-11-02 16:21:03 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2022-11-02 00:18:13 +00:00
|
|
|
static const char ENABLE = 1;
|
|
|
|
|
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;
|
2022-10-15 23:17:49 +00:00
|
|
|
HashMap *requestParams;
|
2022-08-26 15:07:54 +00:00
|
|
|
|
|
|
|
HashMap *responseHeaders;
|
|
|
|
HttpStatus responseStatus;
|
|
|
|
|
|
|
|
FILE *stream;
|
|
|
|
};
|
|
|
|
|
2022-11-08 01:05:28 +00:00
|
|
|
typedef struct HttpServerWorkerThreadArgs
|
|
|
|
{
|
|
|
|
HttpServer *server;
|
|
|
|
int id;
|
|
|
|
pthread_t thread;
|
|
|
|
} HttpServerWorkerThreadArgs;
|
|
|
|
|
2022-08-27 01:02:53 +00:00
|
|
|
static HttpServerContext *
|
|
|
|
HttpServerContextCreate(HttpRequestMethod requestMethod,
|
2022-10-15 23:17:49 +00:00
|
|
|
char *requestPath, HashMap * requestParams, FILE * stream)
|
2022-08-27 01:02:53 +00:00
|
|
|
{
|
2022-08-28 18:30:15 +00:00
|
|
|
HttpServerContext *c;
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
c = Malloc(sizeof(HttpServerContext));
|
2022-08-28 18:30:15 +00:00
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->requestHeaders = HashMapCreate();
|
|
|
|
if (!c->requestHeaders)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(c);
|
2022-08-28 18:30:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->responseHeaders = HashMapCreate();
|
|
|
|
if (!c->responseHeaders)
|
|
|
|
{
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(c->requestHeaders);
|
|
|
|
Free(c);
|
2022-08-28 18:30:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->requestMethod = requestMethod;
|
|
|
|
c->requestPath = requestPath;
|
2022-10-15 23:17:49 +00:00
|
|
|
c->requestParams = requestParams;
|
2022-08-28 18:30:15 +00:00
|
|
|
c->stream = stream;
|
2022-09-07 00:48:27 +00:00
|
|
|
c->responseStatus = HTTP_OK;
|
2022-08-28 18:30:15 +00:00
|
|
|
|
|
|
|
return c;
|
2022-08-27 01:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-08-28 18:30:15 +00:00
|
|
|
HttpServerContextFree(HttpServerContext * c)
|
2022-08-27 01:02:53 +00:00
|
|
|
{
|
2022-08-28 19:45:24 +00:00
|
|
|
char *key;
|
|
|
|
void *val;
|
|
|
|
|
2022-08-28 18:30:15 +00:00
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-08-27 01:02:53 +00:00
|
|
|
|
2022-08-28 19:45:24 +00:00
|
|
|
while (HashMapIterate(c->requestHeaders, &key, &val))
|
|
|
|
{
|
2022-10-31 23:52:37 +00:00
|
|
|
/*
|
|
|
|
* These are always parsed from the request, so they should
|
|
|
|
* always be on the heap.
|
|
|
|
*/
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(key);
|
|
|
|
Free(val);
|
2022-08-28 19:45:24 +00:00
|
|
|
}
|
2022-08-28 18:30:15 +00:00
|
|
|
HashMapFree(c->requestHeaders);
|
2022-08-28 19:45:24 +00:00
|
|
|
|
2022-10-13 16:34:17 +00:00
|
|
|
while (HashMapIterate(c->responseHeaders, &key, &val))
|
|
|
|
{
|
2022-10-31 23:52:37 +00:00
|
|
|
/*
|
|
|
|
* These are generated by code. As such, they may be either
|
|
|
|
* on the heap, or on the stack, depending on how they were
|
|
|
|
* added.
|
|
|
|
*
|
|
|
|
* Basically, if the memory API knows about a pointer, then
|
|
|
|
* it can be freed. If it doesn't know about a pointer, skip
|
|
|
|
* freeing it because it's probably a stack pointer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (MemoryInfoGet(key))
|
|
|
|
{
|
|
|
|
Free(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MemoryInfoGet(val))
|
|
|
|
{
|
|
|
|
Free(val);
|
|
|
|
}
|
2022-10-13 16:34:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 18:30:15 +00:00
|
|
|
HashMapFree(c->responseHeaders);
|
2022-08-28 19:45:24 +00:00
|
|
|
|
2022-10-15 23:17:49 +00:00
|
|
|
while (HashMapIterate(c->requestParams, &key, &val))
|
|
|
|
{
|
|
|
|
Free(key);
|
|
|
|
Free(val);
|
|
|
|
}
|
2022-10-15 20:28:32 +00:00
|
|
|
|
2022-10-15 23:17:49 +00:00
|
|
|
HashMapFree(c->requestParams);
|
2022-10-15 20:28:32 +00:00
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(c->requestPath);
|
2022-08-28 18:30:15 +00:00
|
|
|
fclose(c->stream);
|
2022-10-13 16:34:17 +00:00
|
|
|
|
|
|
|
Free(c);
|
2022-08-28 18:30:15 +00:00
|
|
|
}
|
2022-08-27 01:02:53 +00:00
|
|
|
|
2022-08-28 19:45:24 +00:00
|
|
|
HashMap *
|
|
|
|
HttpRequestHeaders(HttpServerContext * c)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c->requestHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpRequestMethod
|
|
|
|
HttpRequestMethodGet(HttpServerContext * c)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
return HTTP_METHOD_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c->requestMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
HttpRequestPath(HttpServerContext * c)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c->requestPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMap *
|
|
|
|
HttpRequestParams(HttpServerContext * c)
|
|
|
|
{
|
2022-10-15 23:17:49 +00:00
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-10-15 20:28:32 +00:00
|
|
|
|
|
|
|
return c->requestParams;
|
2022-08-28 19:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
HttpResponseHeader(HttpServerContext * c, char *key, char *val)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HashMapSet(c->responseHeaders, key, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpResponseStatus(HttpServerContext * c, HttpStatus status)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->responseStatus = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
HttpStream(HttpServerContext * c)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c->stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpSendHeaders(HttpServerContext * c)
|
|
|
|
{
|
|
|
|
FILE *fp = c->stream;
|
|
|
|
|
|
|
|
char *key;
|
|
|
|
char *val;
|
|
|
|
|
|
|
|
fprintf(fp, "HTTP/1.0 %d %s\n", c->responseStatus, HttpStatusToString(c->responseStatus));
|
|
|
|
|
|
|
|
while (HashMapIterate(c->responseHeaders, &key, (void **) &val))
|
|
|
|
{
|
|
|
|
fprintf(fp, "%s: %s\n", key, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
}
|
|
|
|
|
2022-08-24 14:06:14 +00:00
|
|
|
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-10-13 16:18:42 +00:00
|
|
|
server = Malloc(sizeof(HttpServer));
|
2022-08-11 23:40:13 +00:00
|
|
|
if (!server)
|
|
|
|
{
|
2022-08-24 23:31:28 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
memset(server, 0, sizeof(HttpServer));
|
|
|
|
|
2022-08-24 23:31:28 +00:00
|
|
|
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-10-15 14:08:52 +00:00
|
|
|
server->sd = socket(AF_INET, SOCK_STREAM, 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
|
|
|
}
|
|
|
|
|
2022-10-15 14:08:52 +00:00
|
|
|
if (fcntl(server->sd, F_SETFL, O_NONBLOCK) == -1)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2022-11-02 00:35:05 +00:00
|
|
|
if (setsockopt(server->sd, SOL_SOCKET, SO_REUSEADDR, &ENABLE, sizeof(int)) < 0)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
2022-11-02 00:18:13 +00:00
|
|
|
|
2022-11-02 15:20:37 +00:00
|
|
|
#ifdef SO_REUSEPORT
|
2022-11-02 00:35:05 +00:00
|
|
|
if (setsockopt(server->sd, SOL_SOCKET, SO_REUSEPORT, &ENABLE, sizeof(int)) < 0)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
2022-11-02 15:20:37 +00:00
|
|
|
#endif
|
2022-11-02 00:18:13 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-08-28 20:05:11 +00:00
|
|
|
pthread_mutex_destroy(&server->connQueueMutex);
|
2022-08-24 23:31:28 +00:00
|
|
|
|
|
|
|
if (server->threadPool)
|
|
|
|
{
|
|
|
|
ArrayFree(server->threadPool);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server->sd)
|
|
|
|
{
|
|
|
|
close(server->sd);
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(server);
|
2022-08-24 23:31:28 +00:00
|
|
|
}
|
|
|
|
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-10-13 16:18:42 +00:00
|
|
|
Free(server);
|
2022-08-11 23:40:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 23:31:28 +00:00
|
|
|
static void *
|
|
|
|
HttpServerWorkerThread(void *args)
|
|
|
|
{
|
2022-11-08 01:05:28 +00:00
|
|
|
HttpServerWorkerThreadArgs *wArgs = (HttpServerWorkerThreadArgs *) args;
|
|
|
|
HttpServer *server = wArgs->server;
|
2022-08-24 23:31:28 +00:00
|
|
|
|
|
|
|
while (!server->stop)
|
|
|
|
{
|
2022-11-08 01:05:28 +00:00
|
|
|
FILE *fp;
|
2022-08-28 18:30:15 +00:00
|
|
|
HttpServerContext *context;
|
|
|
|
|
|
|
|
char *line = NULL;
|
|
|
|
size_t lineSize = 0;
|
|
|
|
ssize_t lineLen = 0;
|
|
|
|
|
|
|
|
char *requestMethodPtr;
|
|
|
|
char *pathPtr;
|
|
|
|
char *requestPath;
|
|
|
|
char *requestProtocol;
|
|
|
|
|
2022-10-15 23:17:49 +00:00
|
|
|
HashMap *requestParams;
|
|
|
|
ssize_t requestPathLen;
|
2022-10-15 20:28:32 +00:00
|
|
|
|
2022-08-28 18:30:15 +00:00
|
|
|
ssize_t i = 0;
|
|
|
|
HttpRequestMethod requestMethod;
|
2022-08-24 23:31:28 +00:00
|
|
|
|
2022-11-08 01:05:28 +00:00
|
|
|
long firstRead;
|
|
|
|
|
|
|
|
fp = DequeueConnection(server);
|
|
|
|
|
2022-08-24 23:31:28 +00:00
|
|
|
if (!fp)
|
|
|
|
{
|
2022-10-15 20:28:32 +00:00
|
|
|
/* Block for 1 millisecond before continuing so we don't
|
2022-11-02 16:21:03 +00:00
|
|
|
* murder the CPU if the queue is empty. */
|
2022-08-24 23:31:28 +00:00
|
|
|
UtilSleepMillis(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:21:03 +00:00
|
|
|
/* Get the first line of the request.
|
|
|
|
*
|
|
|
|
* Every once in a while, we're too fast for the client. When this
|
2022-11-25 21:54:57 +00:00
|
|
|
* happens, UtilGetLine() sets errno to EAGAIN. If we get
|
2022-11-02 16:21:03 +00:00
|
|
|
* EAGAIN, then clear the error on the stream and try again
|
|
|
|
* after 1ms. This is typically more than enough time for the
|
|
|
|
* client to send data. */
|
2022-11-08 01:05:28 +00:00
|
|
|
firstRead = UtilServerTs();
|
2022-11-25 21:54:57 +00:00
|
|
|
while ((lineLen = UtilGetLine(&line, &lineSize, fp)) == -1
|
|
|
|
&& errno == EAGAIN)
|
2022-11-02 16:21:03 +00:00
|
|
|
{
|
|
|
|
clearerr(fp);
|
2022-11-08 01:05:28 +00:00
|
|
|
|
|
|
|
/* If the server is stopped, or it's been a while, just
|
|
|
|
* give up. */
|
|
|
|
if (server->stop || (UtilServerTs() - firstRead) > 1000 * 30)
|
|
|
|
{
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
UtilSleepMillis(5);
|
2022-11-02 16:21:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 18:30:15 +00:00
|
|
|
if (lineLen == -1)
|
|
|
|
{
|
|
|
|
goto bad_request;
|
|
|
|
}
|
|
|
|
|
|
|
|
requestMethodPtr = line;
|
|
|
|
for (i = 0; i < lineLen; i++)
|
|
|
|
{
|
|
|
|
if (line[i] == ' ')
|
|
|
|
{
|
|
|
|
line[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == lineLen)
|
|
|
|
{
|
|
|
|
goto bad_request;
|
|
|
|
}
|
|
|
|
|
|
|
|
requestMethod = HttpRequestMethodFromString(requestMethodPtr);
|
|
|
|
if (requestMethod == HTTP_METHOD_UNKNOWN)
|
|
|
|
{
|
|
|
|
goto bad_request;
|
|
|
|
}
|
|
|
|
|
|
|
|
pathPtr = line + i + 1;
|
|
|
|
|
|
|
|
for (i = 0; i < (line + lineLen) - pathPtr; i++)
|
|
|
|
{
|
|
|
|
if (pathPtr[i] == ' ')
|
|
|
|
{
|
|
|
|
pathPtr[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 23:17:49 +00:00
|
|
|
requestPathLen = i;
|
2022-10-15 20:28:32 +00:00
|
|
|
requestPath = Malloc(((requestPathLen + 1) * sizeof(char)));
|
2022-08-28 18:30:15 +00:00
|
|
|
strcpy(requestPath, pathPtr);
|
|
|
|
|
|
|
|
requestProtocol = &pathPtr[i + 1];
|
|
|
|
line[lineLen - 2] = '\0'; /* Get rid of \r and \n */
|
|
|
|
|
|
|
|
if (strcmp(requestProtocol, "HTTP/1.1") != 0 && strcmp(requestProtocol, "HTTP/1.0") != 0)
|
|
|
|
{
|
|
|
|
goto bad_request;
|
|
|
|
}
|
|
|
|
|
2022-10-15 23:17:49 +00:00
|
|
|
/* Find request params */
|
|
|
|
for (i = 0; i < requestPathLen; i++)
|
|
|
|
{
|
|
|
|
if (requestPath[i] == '?')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
requestPath[i] = '\0';
|
2023-01-09 15:48:56 +00:00
|
|
|
requestParams = (i == requestPathLen) ? NULL : HttpParamDecode(requestPath + i + 1);
|
2022-10-15 20:28:32 +00:00
|
|
|
|
|
|
|
context = HttpServerContextCreate(requestMethod, requestPath, requestParams, fp);
|
2022-08-28 18:30:15 +00:00
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
goto internal_error;
|
|
|
|
}
|
|
|
|
|
2022-11-25 21:54:57 +00:00
|
|
|
while ((lineLen = UtilGetLine(&line, &lineSize, fp)) != -1)
|
2022-08-28 18:30:15 +00:00
|
|
|
{
|
|
|
|
char *headerKey;
|
|
|
|
char *headerValue;
|
|
|
|
char *headerPtr;
|
|
|
|
|
|
|
|
if (strcmp(line, "\r\n") == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < lineLen; i++)
|
|
|
|
{
|
|
|
|
if (line[i] == ':')
|
|
|
|
{
|
|
|
|
line[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
line[i] = tolower(line[i]);
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:48:56 +00:00
|
|
|
headerKey = Malloc((i + 1) * sizeof(char));
|
2022-08-28 18:30:15 +00:00
|
|
|
if (!headerKey)
|
|
|
|
{
|
|
|
|
goto internal_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(headerKey, line);
|
|
|
|
|
|
|
|
headerPtr = line + i + 1;
|
|
|
|
|
2022-10-15 14:28:03 +00:00
|
|
|
while (isspace((unsigned char) *headerPtr))
|
2022-08-28 18:30:15 +00:00
|
|
|
{
|
|
|
|
headerPtr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = lineLen - 1; i > (line + lineLen) - headerPtr; i--)
|
|
|
|
{
|
2022-10-15 14:28:03 +00:00
|
|
|
if (!isspace((unsigned char) line[i]))
|
2022-08-28 18:30:15 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line[i] = '\0';
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:18:42 +00:00
|
|
|
headerValue = Malloc(strlen(headerPtr) + 1);
|
2022-08-28 18:30:15 +00:00
|
|
|
if (!headerValue)
|
|
|
|
{
|
|
|
|
goto internal_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(headerValue, headerPtr);
|
|
|
|
|
|
|
|
HashMapSet(context->requestHeaders, headerKey, headerValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
server->requestHandler(context, server->handlerArgs);
|
|
|
|
|
|
|
|
HttpServerContextFree(context);
|
2022-11-02 00:35:05 +00:00
|
|
|
fp = NULL; /* The above call will close this
|
|
|
|
* FILE */
|
2022-08-28 18:30:15 +00:00
|
|
|
goto finish;
|
|
|
|
|
|
|
|
internal_error:
|
|
|
|
fprintf(fp, "HTTP/1.0 500 Internal Server Error\n");
|
|
|
|
fprintf(fp, "Connection: close\n");
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
bad_request:
|
|
|
|
fprintf(fp, "HTTP/1.0 400 Bad Request\n");
|
|
|
|
fprintf(fp, "Connection: close\n");
|
|
|
|
goto finish;
|
2022-08-24 23:31:28 +00:00
|
|
|
|
2022-08-28 18:30:15 +00:00
|
|
|
finish:
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(line);
|
2022-11-02 00:35:05 +00:00
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2022-08-24 23:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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++)
|
|
|
|
{
|
2022-11-08 01:05:28 +00:00
|
|
|
HttpServerWorkerThreadArgs *workerThread = Malloc(sizeof(HttpServerWorkerThreadArgs));
|
2022-08-24 23:31:28 +00:00
|
|
|
|
|
|
|
if (!workerThread)
|
|
|
|
{
|
|
|
|
/* TODO: Make the event thread return an error to the main
|
|
|
|
* thread */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-08 01:05:28 +00:00
|
|
|
workerThread->server = server;
|
|
|
|
workerThread->id = i;
|
|
|
|
|
|
|
|
if (pthread_create(&workerThread->thread, NULL, HttpServerWorkerThread, workerThread) != 0)
|
2022-08-24 23:31:28 +00:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
|
2023-01-09 17:44:12 +00:00
|
|
|
|
2022-08-24 15:25:16 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-09 17:44:12 +00:00
|
|
|
pthread_mutex_lock(&server->connQueueMutex);
|
2022-08-12 01:19:52 +00:00
|
|
|
|
2023-01-09 17:44:12 +00:00
|
|
|
/* Don't even accept connections if the queue is full. */
|
|
|
|
if (!QueueFull(server->connQueue))
|
2022-08-12 01:19:52 +00:00
|
|
|
{
|
2023-01-09 17:44:12 +00:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
connFd = accept(server->sd, (struct sockaddr *) & addr, &addrLen);
|
2022-08-12 01:19:52 +00:00
|
|
|
|
2023-01-09 17:44:12 +00:00
|
|
|
if (connFd < 0)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&server->connQueueMutex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fdopen(connFd, "r+");
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&server->connQueueMutex);
|
|
|
|
close(connFd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueuePush(server->connQueue, fp);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&server->connQueueMutex);
|
2022-08-24 14:06:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 23:31:28 +00:00
|
|
|
for (i = 0; i < server->nThreads; i++)
|
|
|
|
{
|
2022-11-08 01:05:28 +00:00
|
|
|
HttpServerWorkerThreadArgs *workerThread = ArrayGet(server->threadPool, i);
|
2022-08-24 23:31:28 +00:00
|
|
|
|
2022-11-08 01:05:28 +00:00
|
|
|
pthread_join(workerThread->thread, NULL);
|
2022-10-13 16:18:42 +00:00
|
|
|
Free(workerThread);
|
2022-08-24 23:31:28 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|