[Olsr-dev] [PATCH v1 08/10] info: respond with "no content" (204) on empty output

Ferry Huberts (spam-protected)
Sun Feb 21 18:56:21 CET 2016


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 lib/info/http_headers.h           |  4 ++++
 lib/info/olsrd_info.c             | 22 +++++++++++++++++++---
 lib/jsoninfo/src/olsrd_jsoninfo.c |  4 ++++
 lib/txtinfo/src/olsrd_txtinfo.c   |  5 +++++
 4 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/lib/info/http_headers.h b/lib/info/http_headers.h
index 2a54031..beabf98 100644
--- a/lib/info/http_headers.h
+++ b/lib/info/http_headers.h
@@ -48,6 +48,7 @@
 
 /* Response types */
 #define INFO_HTTP_OK             (200)
+#define INFO_HTTP_NOCONTENT      (204)
 #define INFO_HTTP_NOTFOUND       (404)
 
 void http_header_build(const char * plugin_name, unsigned int status, const char *mime, struct autobuf *abuf, int *contentLengthIndex);
@@ -56,6 +57,9 @@ void http_header_adjust_content_length(struct autobuf *abuf, int contentLengthIn
 
 static INLINE const char * httpStatusToReply(unsigned int status) {
   switch (status) {
+    case INFO_HTTP_NOCONTENT:
+      return INFO_HTTP_VERSION " 204 No Content";
+
     case INFO_HTTP_NOTFOUND:
       return INFO_HTTP_VERSION " 404 Not Found";
 
diff --git a/lib/info/olsrd_info.c b/lib/info/olsrd_info.c
index acb728a..862368c 100644
--- a/lib/info/olsrd_info.c
+++ b/lib/info/olsrd_info.c
@@ -189,6 +189,7 @@ static void write_data(void *foo __attribute__ ((unused))) {
 
 static void send_info(const char * req, unsigned int send_what, int the_socket, unsigned int status) {
   struct autobuf abuf;
+  unsigned int outputLength = 0;
 
   const char *content_type = functions->determine_mime_type ? functions->determine_mime_type(send_what) : "text/plain; charset=utf-8";
   int contentLengthIndex = 0;
@@ -229,11 +230,14 @@ static void send_info(const char * req, unsigned int send_what, int the_socket,
         };
 
       unsigned int i;
+      unsigned int preLength;
 
       if (functions->output_start) {
         functions->output_start(&abuf);
       }
 
+      preLength = abuf.len;
+
       for (i = 0; i < ARRAY_SIZE(funcs); i++) {
         if (send_what & funcs[i].siw) {
           printer_generic func = funcs[i].func;
@@ -243,23 +247,35 @@ static void send_info(const char * req, unsigned int send_what, int the_socket,
         }
       }
 
+      outputLength = abuf.len - preLength;
+
       if (functions->output_end) {
         functions->output_end(&abuf);
       }
     } else if ((send_what & SIW_OLSRD_CONF) && functions->olsrd_conf) {
       /* this outputs the olsrd.conf text directly, not normal format */
+      unsigned int preLength = abuf.len;
       functions->olsrd_conf(&abuf);
+      outputLength = abuf.len - preLength;
     }
 
-    if (!abuf.len) {
-      /* wget can't handle output of zero length */
-      abuf_puts(&abuf, "\n");
+    if (!abuf.len || !outputLength) {
+      status = INFO_HTTP_NOCONTENT;
+      abuf.buf[0] = '\0';
+      abuf.len = 0;
+      if (config->http_headers) {
+        http_header_build(name, status, content_type, &abuf, &contentLengthIndex);
+        headerLength = abuf.len;
+      }
     }
   }
 
   if (status != INFO_HTTP_OK) {
     if (functions->output_error) {
       functions->output_error(&abuf, status, req, config->http_headers);
+    } else if (status == INFO_HTTP_NOCONTENT) {
+      /* wget can't handle output of zero length */
+      abuf_puts(&abuf, "\n");
     }
   }
 
diff --git a/lib/jsoninfo/src/olsrd_jsoninfo.c b/lib/jsoninfo/src/olsrd_jsoninfo.c
index 0e4d1f1..8aae4ca 100644
--- a/lib/jsoninfo/src/olsrd_jsoninfo.c
+++ b/lib/jsoninfo/src/olsrd_jsoninfo.c
@@ -190,6 +190,10 @@ void output_error(struct autobuf *abuf, unsigned int status, const char * req, b
       abuf_json_string(abuf, "error", buf);
       break;
 
+    case INFO_HTTP_NOCONTENT:
+      abuf_json_string(abuf, "error", "no content");
+      break;
+
     default:
       break;
   }
diff --git a/lib/txtinfo/src/olsrd_txtinfo.c b/lib/txtinfo/src/olsrd_txtinfo.c
index a62ad88..4b28265 100644
--- a/lib/txtinfo/src/olsrd_txtinfo.c
+++ b/lib/txtinfo/src/olsrd_txtinfo.c
@@ -192,6 +192,11 @@ void output_error(struct autobuf *abuf, unsigned int status, const char * req, b
       abuf_appendf(abuf, "error: Invalid request '%s'\n", req);
       break;
 
+    case INFO_HTTP_NOCONTENT:
+      /* wget can't handle output of zero length */
+      abuf_puts(abuf, "\n");
+      break;
+
     case INFO_HTTP_OK:
     default:
       return;
-- 
2.5.0




More information about the Olsr-dev mailing list