[Olsr-dev] [PATCH v1 8/8] txtinfo: bring over proper http header support from jsoninfo
Ferry Huberts
(spam-protected)
Fri Nov 27 17:26:17 CET 2015
From: Ferry Huberts <(spam-protected)>
Signed-off-by: Ferry Huberts <(spam-protected)>
---
lib/txtinfo/src/olsrd_txtinfo.c | 73 +++++++++++++++++++++++++++++++++++++++--
1 file changed, 70 insertions(+), 3 deletions(-)
diff --git a/lib/txtinfo/src/olsrd_txtinfo.c b/lib/txtinfo/src/olsrd_txtinfo.c
index 1c3e860..833a012 100644
--- a/lib/txtinfo/src/olsrd_txtinfo.c
+++ b/lib/txtinfo/src/olsrd_txtinfo.c
@@ -94,6 +94,9 @@
static int ipc_socket;
+/* Response types */
+#define HTTP_200 "HTTP/1.1 200 OK"
+
/* IPC initialization function */
static int plugin_ipc_init(void);
@@ -136,6 +139,63 @@ 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
*
@@ -794,12 +854,15 @@ static void info_write_data(void *foo __attribute__ ((unused))) {
static void send_info(unsigned int send_what, int the_socket) {
struct autobuf abuf;
+ const char *content_type = "text/plain; charset=utf-8";
+ int contentLengthPlaceholderStart = 0;
+ int headerLength = 0;
+
abuf_init(&abuf, 2 * 4096);
- /* Print minimal http header */
if (http_headers) {
- abuf_puts(&abuf, "HTTP/1.1 200 OK\r\n");
- abuf_puts(&abuf, "Content-Type: text/plain; charset=utf-8\r\n\r\n");
+ build_http_header(HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
+ headerLength = abuf.len;
}
/* Print tables to IPC socket */
@@ -831,6 +894,10 @@ static void send_info(unsigned int send_what, int the_socket) {
if (send_what & SIW_OLSRD_CONF)
ipc_print_olsrd_conf(&abuf);
+ if (http_headers) {
+ http_header_adjust_content_length(&abuf, contentLengthPlaceholderStart, abuf.len - headerLength);
+ }
+
/* avoid a memcpy: just move the abuf.buf pointer and clear abuf */
outbuffer[outbuffer_count] = abuf.buf;
outbuffer_size[outbuffer_count] = abuf.len;
--
2.5.0
More information about the Olsr-dev
mailing list