From 395f80bdc40115087e8e4eb8fdbed8520df54863 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 16 Oct 2022 19:00:17 +0200 Subject: [PATCH] Added support for HEAD methods. Mastodon uses them when it founds an attachment. --- activitypub.c | 2 +- httpd.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/activitypub.c b/activitypub.c index fb790d5..7ac4c5f 100644 --- a/activitypub.c +++ b/activitypub.c @@ -880,7 +880,7 @@ int activitypub_get_handler(d_char *req, char *q_path, xs *msg = NULL; if (accept == NULL) - return 400; + return 0; if (xs_str_in(accept, "application/activity+json") == -1 && xs_str_in(accept, "application/ld+json") == -1) diff --git a/httpd.c b/httpd.c index d30d05b..4233057 100644 --- a/httpd.c +++ b/httpd.c @@ -7,6 +7,7 @@ #include "xs_json.h" #include "xs_socket.h" #include "xs_httpd.h" +#include "xs_mime.h" #include "snac.h" @@ -30,7 +31,7 @@ int server_get_handler(d_char *req, char *q_path, char *acpt = xs_dict_get(req, "accept"); if (acpt == NULL) - return 400; + return 0; /* is it the server root? */ if (*q_path == '\0') { @@ -126,7 +127,7 @@ void httpd_connection(FILE *f) if (xs_startswith(q_path, p)) q_path = xs_crop(q_path, strlen(p), 0); - if (strcmp(method, "GET") == 0) { + if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0) { /* cascade through */ if (status == 0) status = server_get_handler(req, q_path, &body, &b_size, &ctype); @@ -181,6 +182,12 @@ void httpd_connection(FILE *f) if (b_size == 0 && body != NULL) b_size = strlen(body); + /* if it was a HEAD, no body will be sent */ + if (strcmp(method, "HEAD") == 0) { + free(body); + body = NULL; + } + xs_httpd_response(f, status, headers, body, b_size); fclose(f);