This commit is contained in:
sneedium 2022-02-21 12:58:43 -05:00
commit 46c0be23e8
8 changed files with 6367 additions and 0 deletions

35
Makefile Normal file
View File

@ -0,0 +1,35 @@
prefix=/usr/local
confdir=/etc
systemd_dir=${DESTDIR}${confdir}/systemd/system
nginx_dir=${DESTDIR}${confdir}/nginx
bindir=${DESTDIR}${prefix}/bin
CC := gcc
CFLAGS := -O2 ${CFLAGS}
BIN := sneedmc
SOURCE := main.c mongoose.c
OBJ := mongoose.o main.o
DEPS := mongoose.h
LIBS := -lcrypt
all: $(BIN)
clean:
rm -f $(OBJ)
$(BIN): $(OBJ)
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
install-nginx:
@install -Dm644 doc/sneedmc.nginx ${nginx_dir}/sites-available/sneedmc
install-systemd:
@install -Dm644 doc/sneedmc.service ${systemd_dir}/sneedmc.service
@install -Dm644 doc/sneedmc.conf ${DESTDIR}/${confdir}/sneedmc.conf
install-bin:
@install -Dm755 ${BIN} ${bindir}/${BIN}
install: install-bin install-nginx install-systemd

1
index.html Normal file
View File

@ -0,0 +1 @@
a:

84
main.c Normal file
View File

@ -0,0 +1,84 @@
#include "mongoose.h"
char *port = "6333";
void trim(char *str) {
char *_str = str;
int len = strlen(_str);
while(*_str && *_str == '/') ++_str, --len;
memmove(str, _str, len + 1);
}
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);
char *query = NULL;
struct mg_str hquery = hm->query;
if (hquery.len > 0) {
char *base_query = malloc(hquery.len + 1);
snprintf(base_query, hquery.len + 1, "%s", hquery.ptr);
query = malloc(256);
mg_url_decode(base_query, hquery.len + 1, query, 256, 0);
free(base_query);
} else {
query = malloc(1);
sprintf(query, "");
}
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");
mhost = mg_str("<UNKNOWN DOMAIN>");
} else {
mhost = *pmhost;
}
char *host = malloc(mhost.len + 1);
snprintf(host, mhost.len + 1, "%s", mhost.ptr);
char *body = strdup(hm->body.ptr);
if (strncmp(hm->method.ptr, "POST", hm->method.len) == 0) {
} else if (strncmp(hm->method.ptr, "GET", hm->method.len) == 0){
struct mg_http_serve_opts opts = {.root_dir = "."}; // Serve local dir
mg_http_serve_dir(nc, p, &opts);
} else {
mg_http_reply(nc, 405, "Allow: GET, POST\r\n", "");
}
free(uri);
free(host);
free(body);
free(query);
}
}
int main(int argc, char *argv[]) {
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;
}

BIN
main.o Normal file

Binary file not shown.

5105
mongoose.c Normal file

File diff suppressed because it is too large Load Diff

1142
mongoose.h Normal file

File diff suppressed because it is too large Load Diff

BIN
mongoose.o Normal file

Binary file not shown.

BIN
sneedmc Executable file

Binary file not shown.