[Olsr-dev] [PATCH v1 015/115] txtinfo: move http header related functions into common code

Ferry Huberts (spam-protected)
Wed Dec 2 21:27:48 CET 2015


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 lib/info/Makefile                           | 17 +++++
 lib/info/README_INFO                        |  4 ++
 lib/info/http_headers.c                     | 99 +++++++++++++++++++++++++++++
 src/hysteresis.h => lib/info/http_headers.h | 28 +++-----
 lib/txtinfo/Makefile                        |  3 +
 lib/txtinfo/src/olsrd_txtinfo.c             | 63 +-----------------
 6 files changed, 134 insertions(+), 80 deletions(-)
 create mode 100644 lib/info/Makefile
 create mode 100644 lib/info/README_INFO
 create mode 100644 lib/info/http_headers.c
 copy src/hysteresis.h => lib/info/http_headers.h (78%)

diff --git a/lib/info/Makefile b/lib/info/Makefile
new file mode 100644
index 0000000..9605c23
--- /dev/null
+++ b/lib/info/Makefile
@@ -0,0 +1,17 @@
+OLSRD_PLUGIN =	false
+PLUGIN_NAME =	olsrd_info
+PLUGIN_VER =	0.0
+
+TOPDIR=../..
+include $(TOPDIR)/Makefile.inc
+
+default_target: $(PLUGIN_FULLNAME)
+
+$(PLUGIN_FULLNAME): $(OBJS)
+
+install:
+
+uninstall:
+
+clean:
+	rm -f $(OBJS) $(SRCS:%.c=%.d) $(PLUGIN_FULLNAME)
diff --git a/lib/info/README_INFO b/lib/info/README_INFO
new file mode 100644
index 0000000..14288c4
--- /dev/null
+++ b/lib/info/README_INFO
@@ -0,0 +1,4 @@
+
+Common code for the jsoninfo and txtinfo plugins.
+
+This is NOT a plugin
\ No newline at end of file
diff --git a/lib/info/http_headers.c b/lib/info/http_headers.c
new file mode 100644
index 0000000..9a6d5a6
--- /dev/null
+++ b/lib/info/http_headers.c
@@ -0,0 +1,99 @@
+/*
+ * The olsr.org Optimized Link-State Routing daemon(olsrd)
+ * Copyright (c) 2004
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in
+ *   the documentation and/or other materials provided with the
+ *   distribution.
+ * * Neither the name of olsr.org, olsrd nor the names of its
+ *   contributors may be used to endorse or promote products derived
+ *   from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Visit http://www.olsr.org for more information.
+ *
+ * If you find this software useful feel free to make a donation
+ * to the project. For more information see the website or contact
+ * the copyright holders.
+ *
+ */
+
+#include "http_headers.h"
+
+void build_http_header(const char *plugin_name, const char *status, const char *mime, struct autobuf *abuf, int *contentLengthPlaceholderStart) {
+  /* Status */
+  abuf_appendf(abuf, "%s\r\n", status);
+
+  /* Date */
+  {
+    time_t currtime;
+    char buf[128];
+
+    time(&currtime);
+    if (strftime(buf, sizeof(buf), "Date: %a, %d %b %Y %H:%M:%S GMT\r\n", gmtime(&currtime))) {
+      abuf_puts(abuf, buf);
+    }
+  }
+
+  /* Server version */
+  abuf_appendf(abuf, "Server: OLSRD %s\r\n", plugin_name);
+
+  /* connection-type */
+  abuf_puts(abuf, "Connection: close\r\n");
+
+  /* MIME type */
+  if (mime != NULL) {
+    abuf_appendf(abuf, "Content-Type: %s\r\n", mime);
+  }
+
+  /* CORS data */
+  /* No needs to be strict here, access control is based on source IP */
+  abuf_puts(abuf, "Access-Control-Allow-Origin: *\r\n");
+  abuf_puts(abuf, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
+  abuf_puts(abuf, "Access-Control-Allow-Headers: Accept, Origin, X-Requested-With\r\n");
+  abuf_puts(abuf, "Access-Control-Max-Age: 1728000\r\n");
+
+  /* Content length */
+  abuf_puts(abuf, "Content-Length: ");
+  *contentLengthPlaceholderStart = abuf->len;
+  abuf_puts(abuf, "            "); /* 12 spaces reserved for the length (max. 1TB-1), to be filled at the end */
+  abuf_puts(abuf, "\r\n");
+
+  /* Cache-control
+   * No caching dynamic pages
+   */
+  abuf_puts(abuf, "Cache-Control: no-cache\r\n");
+
+  /* End header */
+  abuf_puts(abuf, "\r\n");
+}
+
+void http_header_adjust_content_length(struct autobuf *abuf, int contentLengthPlaceholderStart, int contentLength) {
+  char buf[12 + 1]; /* size must match to number of spaces used (+1 for the terminating byte) */
+
+  memset(buf, 0, sizeof(buf));
+  snprintf(buf, sizeof(buf), "%d", contentLength);
+  buf[sizeof(buf) - 1] = '\0';
+  memcpy(&abuf->buf[contentLengthPlaceholderStart], buf, strlen(buf));
+}
diff --git a/src/hysteresis.h b/lib/info/http_headers.h
similarity index 78%
copy from src/hysteresis.h
copy to lib/info/http_headers.h
index 8d61032..a5c96ab 100644
--- a/src/hysteresis.h
+++ b/lib/info/http_headers.h
@@ -1,7 +1,7 @@
-
 /*
  * The olsr.org Optimized Link-State Routing daemon(olsrd)
  * Copyright (c) 2004, Andreas Tonnesen((spam-protected))
+ *                     includes code by Bruno Randolf
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -38,27 +38,17 @@
  * the copyright holders.
  *
  */
-#ifndef _OLSR_HYSTERESIS
-#define _OLSR_HYSTERESIS
-
-#include "link_set.h"
-#include "mantissa.h"
-
-float olsr_hyst_calc_stability(float);
 
-int olsr_process_hysteresis(struct link_entry *);
+#ifndef _OLSRD_INFO_COMMON_HTTP_HEADERS
+#define _OLSRD_INFO_COMMON_HTTP_HEADERS
 
-float olsr_hyst_calc_instability(float);
+#include "common/autobuf.h"
 
-void olsr_update_hysteresis_hello(struct link_entry *, olsr_reltime);
+/* Response types */
+#define HTTP_200 "HTTP/1.1 200 OK"
 
-void update_hysteresis_incoming(union olsr_ip_addr *, struct interface_olsr *, uint16_t);
+void build_http_header(const char * plugin_name, const char *status, const char *mime, struct autobuf *abuf, int *contentLengthPlaceholderStart);
 
-#endif /* _OLSR_HYSTERESIS */
+void http_header_adjust_content_length(struct autobuf *abuf, int contentLengthPlaceholderStart, int contentLength);
 
-/*
- * Local Variables:
- * c-basic-offset: 2
- * indent-tabs-mode: nil
- * End:
- */
+#endif /* _OLSRD_INFO_COMMON_HTTP_HEADERS */
diff --git a/lib/txtinfo/Makefile b/lib/txtinfo/Makefile
index a2fa16a..c1eab2b 100644
--- a/lib/txtinfo/Makefile
+++ b/lib/txtinfo/Makefile
@@ -43,6 +43,9 @@ PLUGIN_VER =	0.1
 TOPDIR =	../..
 include $(TOPDIR)/Makefile.inc
 
+COMMONINFO = $(wildcard ../info/*.c)
+OBJS += $(COMMONINFO:%.c=%.o)
+
 default_target: $(PLUGIN_FULLNAME)
 
 $(PLUGIN_FULLNAME): $(OBJS) version-script.txt
diff --git a/lib/txtinfo/src/olsrd_txtinfo.c b/lib/txtinfo/src/olsrd_txtinfo.c
index 8b4742b..5203764 100644
--- a/lib/txtinfo/src/olsrd_txtinfo.c
+++ b/lib/txtinfo/src/olsrd_txtinfo.c
@@ -75,6 +75,7 @@
 #include "lq_plugin.h"
 #include "common/autobuf.h"
 #include "gateway.h"
+#include "../../info/http_headers.h"
 
 #include "olsrd_txtinfo.h"
 #include "olsrd_plugin.h"
@@ -94,9 +95,6 @@
 
 static int ipc_socket;
 
-/* Response types */
-#define HTTP_200 "HTTP/1.1 200 OK"
-
 /* IPC initialization function */
 static int plugin_ipc_init(void);
 
@@ -141,63 +139,6 @@ static int outbuffer_count = 0;
 
 static struct timer_entry *writetimer_entry;
 
-static void build_http_header(const char *status, const char *mime, struct autobuf *abuf, int *contentLengthPlaceholderStart) {
-  /* Status */
-  abuf_appendf(abuf, "%s\r\n", status);
-
-  /* Date */
-  {
-    time_t currtime;
-    char buf[128];
-
-    time(&currtime);
-    if (strftime(buf, sizeof(buf), "Date: %a, %d %b %Y %H:%M:%S GMT\r\n", gmtime(&currtime))) {
-      abuf_puts(abuf, buf);
-    }
-  }
-
-  /* Server version */
-  abuf_puts(abuf, "Server: OLSRD "PLUGIN_NAME"\r\n");
-
-  /* connection-type */
-  abuf_puts(abuf, "Connection: close\r\n");
-
-  /* MIME type */
-  if (mime != NULL) {
-    abuf_appendf(abuf, "Content-Type: %s\r\n", mime);
-  }
-
-  /* CORS data */
-  /* No needs to be strict here, access control is based on source IP */
-  abuf_puts(abuf, "Access-Control-Allow-Origin: *\r\n");
-  abuf_puts(abuf, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
-  abuf_puts(abuf, "Access-Control-Allow-Headers: Accept, Origin, X-Requested-With\r\n");
-  abuf_puts(abuf, "Access-Control-Max-Age: 1728000\r\n");
-
-  /* Content length */
-  abuf_puts(abuf, "Content-Length: ");
-  *contentLengthPlaceholderStart = abuf->len;
-  abuf_puts(abuf, "            "); /* 12 spaces reserved for the length (max. 1TB-1), to be filled at the end */
-  abuf_puts(abuf, "\r\n");
-
-  /* Cache-control
-   * No caching dynamic pages
-   */
-  abuf_puts(abuf, "Cache-Control: no-cache\r\n");
-
-  /* End header */
-  abuf_puts(abuf, "\r\n");
-}
-
-static void http_header_adjust_content_length(struct autobuf *abuf, int contentLengthPlaceholderStart, int contentLength) {
-  char buf[12 + 1]; /* size must match to number of spaces used (+1 for the terminating byte) */
-
-  memset(buf, 0, sizeof(buf));
-  snprintf(buf, sizeof(buf), "%d", contentLength);
-  buf[sizeof(buf) - 1] = '\0';
-  memcpy(&abuf->buf[contentLengthPlaceholderStart], buf, strlen(buf));
-}
-
 /**
  *Do initialization here
  *
@@ -874,7 +815,7 @@ static void send_info(unsigned int send_what, int the_socket) {
   abuf_init(&abuf, 2 * 4096);
 
   if (http_headers) {
-    build_http_header(HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
+    build_http_header(PLUGIN_NAME, HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
     headerLength = abuf.len;
   }
 
-- 
2.5.0




More information about the Olsr-dev mailing list