From 67094237a6176bf0a684fb89445f0543b29c09d6 Mon Sep 17 00:00:00 2001 From: nekobit Date: Thu, 2 Jun 2022 05:45:40 +0000 Subject: [PATCH] Update send_result function FossilOrigin-Name: 070284b06db0c672bf2e0c63a49f80611909cc531ea12d732de346cfbea5abb9 --- src/base_page.c | 8 +++++--- src/base_page.h | 7 ++++--- src/notifications.c | 4 ++-- src/status.c | 4 ++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/base_page.c b/src/base_page.c index ac594f4..02ad90b 100644 --- a/src/base_page.c +++ b/src/base_page.c @@ -157,7 +157,7 @@ void render_base_page(struct base_page* page, struct session* ssn, mastodont_t* goto cleanup; } - render_html("text/html", data, len); + send_result(NULL, "text/html", data, len); // Cleanup /* cleanup_all: */ @@ -170,11 +170,13 @@ cleanup: free(instance_str); } -void render_html(char* content_type, char* data, size_t data_len) +void send_result(char* status, char* content_type, char* data, size_t data_len) { if (data_len == 0) data_len = strlen(data); - printf("Content-type: %s\r\n" + printf("Status: %s\r\n" + "Content-type: %s\r\n" "Content-Length: %d\r\n\r\n", + status ? status : "200 OK", content_type ? content_type : "text/html", data_len + 1); puts(data); diff --git a/src/base_page.h b/src/base_page.h index 540ace5..b210282 100644 --- a/src/base_page.h +++ b/src/base_page.h @@ -50,10 +50,11 @@ void render_base_page(struct base_page* page, struct session* ssn, mastodont_t* /** * Outputs HTML in format for CGI. This can only be called once! * - * @param content_type The Content-Type to display, if NULL, assume "text/html" + * @param status The full HTTP status. if NULL, then status is "200 OK" + * @param content_type The Content-Type to display. if NULL, assume "text/html" * @param data HTML content - * @param data_len Length of data. + * @param data_len Length of data. If 0, calls strlen(data) */ -void render_html(char* content_type, char* data, size_t data_len); +void send_result(char* status, char* content_type, char* data, size_t data_len); #endif // BASE_PAGE_H diff --git a/src/notifications.c b/src/notifications.c index 5d408fe..d0e99e3 100644 --- a/src/notifications.c +++ b/src/notifications.c @@ -273,7 +273,7 @@ void content_notifications_compact(struct session* ssn, mastodont_t* api, char** page = tmpl_gen_notifications_embed(&tdata, &len); - render_html(NULL, page, len); + send_result(NULL, NULL, page, len); if (notif_html) free(notif_html); if (navigation_box) free(navigation_box); @@ -282,5 +282,5 @@ void content_notifications_compact(struct session* ssn, mastodont_t* api, char** void api_notifications(struct session* ssn, mastodont_t* api, char** data) { - render_html("application/json", "{\"status\":0}", 0); + send_result(NULL, "application/json", "{\"status\":0}", 0); } diff --git a/src/status.c b/src/status.c index c14798e..8edbead 100644 --- a/src/status.c +++ b/src/status.c @@ -674,10 +674,10 @@ void api_status_interact(struct session* ssn, mastodont_t* api, char** data) { if (try_interact_status(ssn, api, ssn->post.id)) { - render_html("application/json", "{\"status\":\"Success\"}", 0); + send_result(NULL, "application/json", "{\"status\":\"Success\"}", 0); } else - render_html("application/json", "{\"status\":\"Couldn't load status\"}", 0); + send_result(NULL, "application/json", "{\"status\":\"Couldn't load status\"}", 0); } void status_view(struct session* ssn, mastodont_t* api, char** data)