129 lines
3.6 KiB
C
129 lines
3.6 KiB
C
|
#include "mongoose.h"
|
||
|
#include "index.h"
|
||
|
|
||
|
#include <string.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <ctype.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
#include <crypt.h>
|
||
|
#include <time.h>
|
||
|
|
||
|
char *port = "33368"; // sneed, backwards, in phone number, with the last number changed to 8 cuz sneedmc.org is already on 33367
|
||
|
|
||
|
static struct mg_http_serve_opts s_http_server_opts;
|
||
|
|
||
|
void trim(char *str) {
|
||
|
char *_str = str;
|
||
|
int len = strlen(_str);
|
||
|
|
||
|
while(*_str && *_str == '/') ++_str, --len;
|
||
|
|
||
|
memmove(str, _str, len + 1);
|
||
|
}
|
||
|
|
||
|
void handle_url_req(struct mg_connection *nc, char *host, char *link) {
|
||
|
if (strlen(link) == 0) { // FIXME: real index/html here
|
||
|
return mg_http_reply(nc, 200, "Content-Type: text/html\r\n", INDEX_HTML,
|
||
|
host, host, host, host, host, host, host, host, host, host, host); // FIXME: need better solution
|
||
|
} else {
|
||
|
if (strncmp(link, "favicon.ico", 12) == 0) {
|
||
|
mg_http_reply(nc, 404, "", "Not Found"); // FIXME: sneed cube.
|
||
|
} else {
|
||
|
// TODO: stub
|
||
|
// FIXME: Subdomain handling here
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void ev_handler(struct mg_connection *nc, int ev, void *p, void *f) {
|
||
|
if (ev == MG_EV_HTTP_MSG) {
|
||
|
struct mg_http_message *hm = (struct mg_http_message *) p;
|
||
|
char *uri = malloc(hm->uri.len + 1);
|
||
|
|
||
|
snprintf(uri, hm->uri.len + 1, "%s", hm->uri.ptr);
|
||
|
trim(uri);
|
||
|
|
||
|
// Host parsing here.
|
||
|
struct mg_str *pmhost = mg_http_get_header(hm, "Host");
|
||
|
struct mg_str mhost;
|
||
|
if (pmhost == NULL) {
|
||
|
fprintf(stderr, "request sent with no Host header");
|
||
|
free(uri);
|
||
|
mg_http_reply(nc, 400, "", "");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
mhost = *pmhost;
|
||
|
char *host = malloc(mhost.len + 1);
|
||
|
snprintf(host, mhost.len + 1, "%s", mhost.ptr);
|
||
|
|
||
|
if (strncmp(hm->method.ptr, "GET", hm->method.len) == 0){
|
||
|
handle_url_req(nc, host, uri);
|
||
|
} else {
|
||
|
mg_http_reply(nc, 405, "Allow: GET\r\n", "");
|
||
|
}
|
||
|
free(uri);
|
||
|
free(host);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
int index;
|
||
|
int c;
|
||
|
|
||
|
opterr = 0;
|
||
|
setvbuf(stdout, NULL, _IONBF, 0);
|
||
|
|
||
|
while ((c = getopt (argc, argv, "p:h")) != -1) {
|
||
|
switch (c) {
|
||
|
case 'p':
|
||
|
port = optarg;
|
||
|
break;
|
||
|
case 'h':
|
||
|
printf("gsl: host the GSL licenses\n");
|
||
|
printf("usage: %s [-p port]\n\n", argv[0]);
|
||
|
printf("options:\n");
|
||
|
printf("-p <port>\t\tport to use (default 33368)\n");
|
||
|
printf("source: https://git.sneedmc.org/sneederix/gsl");
|
||
|
return 0;
|
||
|
case '?':
|
||
|
if (optopt == 'p') {
|
||
|
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
|
||
|
}
|
||
|
else if (isprint (optopt)) {
|
||
|
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
|
||
|
}
|
||
|
else {
|
||
|
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
|
||
|
}
|
||
|
return 1;
|
||
|
default:
|
||
|
abort();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for (index = optind; index < argc; index++) {
|
||
|
printf ("Non-option argument %s\n", argv[index]);
|
||
|
}
|
||
|
|
||
|
struct mg_mgr mgr;
|
||
|
struct mg_connection *nc;
|
||
|
|
||
|
mg_mgr_init(&mgr);
|
||
|
printf("Starting web server on port %s\n", port);
|
||
|
char *str_port = malloc(20);
|
||
|
sprintf(str_port, "http://0.0.0.0:%s", port);
|
||
|
nc = mg_http_listen(&mgr, str_port, ev_handler, &mgr);
|
||
|
if (nc == NULL) {
|
||
|
printf("Failed to create listener\n");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
for (;;) { mg_mgr_poll(&mgr, 1000); }
|
||
|
mg_mgr_free(&mgr);
|
||
|
return 0;
|
||
|
}
|
||
|
|