add compile option to disable custom links

This commit is contained in:
swirl 2021-09-25 18:14:16 -04:00
parent 4f164ddc67
commit 0ec0db6ccf
3 changed files with 13 additions and 3 deletions

View File

@ -4,8 +4,9 @@ systemd_dir=${DESTDIR}${confdir}/systemd/system
nginx_dir=${DESTDIR}${confdir}/nginx
bindir=${DESTDIR}${prefix}/bin
CC := gcc
CFLAGS := -O2
DISABLE_CUSTOM_LINKS ?= 0
CC ?= gcc
CFLAGS := -O2 -DDISABLE_CUSTOM_LINKS=${DISABLE_CUSTOM_LINKS} ${CFLAGS}
BIN := pacebin

View File

@ -32,7 +32,7 @@ To build this project, you'll need a libc implementation (only tested with glibc
git clone https://git.swurl.xyz/swirl/pacebin && cd pacebin
```
2. Compile:
2. Now, you need to compile. When compiling, you can optionally choose to disable the ability to use custom links for pastes. This can easily be done by setting `DISABLE_CUSTOM_LINKS` to 1; i.e. `make DISABLE_CUSTOM_LINKS=1`. Defaults to 0, enabling them.
```bash
make
```

9
main.c
View File

@ -104,6 +104,10 @@ void trim(char *str) {
memmove(str, _str, len + 1);
}
#if DISABLE_CUSTOM_LINKS == 1
void handle_post(struct mg_connection *nc, char *content, char *host) {
char *short_link = gen_random_link();
#else
void handle_post(struct mg_connection *nc, char *content, char *host, char *link) {
char *short_link;
if (strlen(link) == 0) {
@ -117,6 +121,7 @@ void handle_post(struct mg_connection *nc, char *content, char *host, char *link
if (paste_exists(short_link)) {
return mg_http_reply(nc, 500, "", "a paste named %s already exists", short_link);
}
#endif
FILE *url = get_paste_file(short_link, "w+");
fputs(content, url);
@ -165,7 +170,11 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p, void *f) {
char *body = strdup(hm->body.ptr);
if (strncmp(hm->method.ptr, "POST", hm->method.len) == 0) {
#if DISABLE_CUSTOM_LINKS == 1
handle_post(nc, body, host); // FIXME: return 400 on bad Content-Type
#else
handle_post(nc, body, host, uri); // FIXME: return 400 on bad Content-Type
#endif
} else if (strncmp(hm->method.ptr, "DELETE", hm->method.len) == 0) {
handle_delete(nc, uri, body);
} else if (strncmp(hm->method.ptr, "GET", hm->method.len) == 0) {