Commits

Alessio Caiazza  committed 32990a5

jsoninfo: HTTP headers with CORS (if requested)

The new "httpheaders" parameter prepends HTTP headers to the reply.
Cross-origin resource sharing headers (CORS) are included in reply
allowing the json retrieval by javascript applications not served
by olsrd itself.
This will allow to easily develop js applications running directly
into the browser.

  • Participants
  • Parent commits a8aa663
  • Branches jsoninfo-stable-6.5.2

Comments (0)

Files changed (5)

File lib/jsoninfo/README_JSONINFO

     # if you set it to 0.0.0.0, it will accept all connections
     #PlParam      "accept" "0.0.0.0"
 
+    # reply can include standard HTTP headers (default "no")
+    #PlParam      "httpheaders" "yes"
+
     # specify a UUID for this node to track it for debugging
     #PlParam      "UUIDFile" "/etc/olsrd/olsrd.uuid"
 }

File lib/jsoninfo/src/olsrd_jsoninfo.c

  *                     includes code by Andreas Lopatic
  *                     includes code by Sven-Ola Tuecke
  *                     includes code by Lorenz Schori
+ *                     includes code by Alessio Caiazza
  *                     includes bugs by Markus Kittenberger
  *                     includes bugs by Hans-Christoph Steiner
  * All rights reserved.
 
 static int ipc_socket;
 
+/**Response types */
+#define HTTP_200 "HTTP/1.1 200 OK"
+
 /* IPC initialization function */
 static int plugin_ipc_init(void);
 
 static void ipc_print_plugins(struct autobuf *);
 static void ipc_print_olsrd_conf(struct autobuf *abuf);
 
+static size_t build_http_header(const char *status, const char *mime, 
+  uint32_t msgsize, char *buf, uint32_t bufsize);
+
+
 #define TXT_IPC_BUFSIZE 256
 
 /* these provide all of the runtime status info */
 #define SIW_OLSRD_CONF 0x1000
 
 #define MAX_CLIENTS 3
+#define MAX_HTTPHEADER_SIZE 1024
 
 static char *outbuffer[MAX_CLIENTS];
 static size_t outbuffer_size[MAX_CLIENTS];
     ssize_t s = recv(ipc_connection, (void *)&requ, sizeof(requ), 0);   /* Win32 needs the cast here */
     if (0 < s) {
       requ[s] = 0;
+#ifndef NODEBUG
+      olsr_printf(2, "(JSONINFO) request: %s\n", requ);
+#endif /* NODEBUG */
       /* print out the requested tables */
       if (0 != strstr(requ, "/olsrd.conf"))
         send_what |= SIW_OLSRD_CONF;
 send_info(unsigned int send_what, int the_socket)
 {
   struct autobuf abuf;
+  size_t header_len = 0;
+  char header_buf[MAX_HTTPHEADER_SIZE];
+  const char *content_type = "application/json";
 
   /* global variables for tracking when to put a comma in for JSON */
   entrynumber[0] = 0;
   /* this outputs the olsrd.conf text directly, not JSON */
   if ((send_what & SIW_OLSRD_CONF) == SIW_OLSRD_CONF) {
     ipc_print_olsrd_conf(&abuf);
+    content_type = "text/plain";
+  }
+
+  if(http_headers) {
+    memset(header_buf, 0, sizeof(header_buf));
+    header_len = build_http_header(HTTP_200, content_type, abuf.len, header_buf, sizeof(header_buf));
   }
 
-  outbuffer[outbuffer_count] = olsr_malloc(abuf.len, "txt output buffer");
-  outbuffer_size[outbuffer_count] = abuf.len;
-  outbuffer_written[outbuffer_count] = 0;
-  outbuffer_socket[outbuffer_count] = the_socket;
+  if (header_len + abuf.len > 0) {
+    outbuffer[outbuffer_count] = olsr_malloc(header_len + abuf.len, "json output buffer");
+    outbuffer_size[outbuffer_count] = header_len + abuf.len;
+    outbuffer_written[outbuffer_count] = 0;
+    outbuffer_socket[outbuffer_count] = the_socket;
 
-  memcpy(outbuffer[outbuffer_count], abuf.buf, abuf.len);
-  outbuffer_count++;
+    memcpy(outbuffer[outbuffer_count], header_buf, header_len);
+    if (abuf.len > 0) {
+      memcpy((outbuffer[outbuffer_count]) + header_len, abuf.buf, abuf.len);
+    }
+    outbuffer_count++;
 
-  if (outbuffer_count == 1) {
-    writetimer_entry = olsr_start_timer(100,
+    if (outbuffer_count == 1) {
+      writetimer_entry = olsr_start_timer(100,
                                         0,
                                         OLSR_TIMER_PERIODIC,
                                         &jsoninfo_write_data,
                                         NULL,
                                         0);
+    }
   }
 
   abuf_free(&abuf);
 }
 
+static size_t
+build_http_header(const char *status, const char *mime, uint32_t msgsize,
+  char *buf, uint32_t bufsize)
+{
+  time_t currtime;
+  size_t size;
+
+  size = snprintf(buf, bufsize, "%s\r\n", status);
+
+  /* Date */
+  time(&currtime);
+  size += strftime(&buf[size], bufsize - size, "Date: %a, %d %b %Y %H:%M:%S GMT\r\n", localtime(&currtime));
+
+  /* Server version */
+  size += snprintf(&buf[size], bufsize - size, "Server: %s %s\r\n", PLUGIN_NAME, PLUGIN_VERSION);
+
+  /* connection-type */
+  size += snprintf(&buf[size], bufsize - size, "Connection: closed\r\n");
+
+  /* MIME type */
+  if(mime != NULL) {
+    size += snprintf(&buf[size], bufsize - size, "Content-type: %s\r\n", mime);
+  }
+
+  /* CORS data */
+  /**No needs to be strict here, access control is based on source IP*/
+  size += snprintf(&buf[size], bufsize - size, "Access-Control-Allow-Origin: *\r\n");
+  size += snprintf(&buf[size], bufsize - size, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
+  size += snprintf(&buf[size], bufsize - size, "Access-Control-Allow-Headers: Accept, Origin, X-Requested-With\r\n");
+  size += snprintf(&buf[size], bufsize - size, "Access-Control-Max-Age: 1728000\r\n");
+
+  /* Content length */
+  if (msgsize > 0) {
+    size += snprintf(&buf[size], bufsize - size, "Content-length: %i\r\n", msgsize);
+  }
+
+  /* Cache-control
+   * No caching dynamic pages
+   */
+  size += snprintf(&buf[size], bufsize - size, "Cache-Control: no-cache\r\n");
+
+  /* End header */
+  size += snprintf(&buf[size], bufsize - size, "\r\n");
+
+#ifndef NODEBUG
+  olsr_printf(1, "(JSONINFO) HEADER:\n%s", buf);
+#endif /* NODEBUG */
+
+  return size;
+}
+
 /*
  * Local Variables:
  * mode: c

File lib/jsoninfo/src/olsrd_jsoninfo.h

  * The olsr.org Optimized Link-State Routing daemon(olsrd)
  * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
  *                     includes code by Bruno Randolf
+ *                     includes code by Alessio Caiazza
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 extern union olsr_ip_addr jsoninfo_listen_ip;
 extern int ipc_port;
 extern int nompr;
+extern bool http_headers;
 
 int olsrd_plugin_interface_version(void);
 int olsrd_plugin_init(void);

File lib/jsoninfo/src/olsrd_plugin.c

 /*
  * The olsr.org Optimized Link-State Routing daemon(olsrd)
  * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
+ *                     includes code by Alessio Caiazza
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include "olsrd_jsoninfo.h"
 #include "defs.h"
 
-#define PLUGIN_NAME    "JSON info and dyn_gw plugin"
-#define PLUGIN_VERSION "0.0"
-#define PLUGIN_AUTHOR   "Hans-Christoph Steiner"
-#define MOD_DESC PLUGIN_NAME " " PLUGIN_VERSION " by " PLUGIN_AUTHOR
-#define PLUGIN_INTERFACE_VERSION 5
-
 union olsr_ip_addr jsoninfo_accept_ip;
 union olsr_ip_addr jsoninfo_listen_ip;
 int ipc_port;
 int nompr;
+bool http_headers;
 
 static void my_init(void) __attribute__ ((constructor));
 static void my_fini(void) __attribute__ ((destructor));
 
   /* defaults for parameters */
   ipc_port = 9090;
+  http_headers = false;
   if (olsr_cnf->ip_version == AF_INET) {
     jsoninfo_accept_ip.v4.s_addr = htonl(INADDR_LOOPBACK);
     jsoninfo_listen_ip.v4.s_addr = htonl(INADDR_ANY);
   return 0;
 }
 
+static int
+store_boolean(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
+{
+  bool *dest = data;
+  if(strcmp(value, "yes") == 0)
+    *dest = true;
+  else if (strcmp(value, "no") == 0)
+    *dest = false;
+  else
+    return 1; //error
+
+  return 0;
+}
+
 static const struct olsrd_plugin_parameters plugin_parameters[] = {
   {.name = "port",.set_plugin_parameter = &set_plugin_port,.data = &ipc_port},
   {.name = "accept",.set_plugin_parameter = &set_plugin_ipaddress,.data = &jsoninfo_accept_ip},
   {.name = "listen",.set_plugin_parameter = &set_plugin_ipaddress,.data = &jsoninfo_listen_ip},
   {.name = "uuidfile",.set_plugin_parameter = &store_string,.data = uuidfile},
+  {.name = "httpheaders",.set_plugin_parameter = &store_boolean,.data = &http_headers},
 };
 
 void

File lib/jsoninfo/src/olsrd_plugin.h

+/*
+ * The olsr.org Optimized Link-State Routing daemon(olsrd)
+ * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
+ *                     includes code by Alessio Caiazza
+ * 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.
+ *
+ */
+
+/*
+ * Dynamic linked library for the olsr.org olsr daemon
+ */
+
+#ifndef _OLSRD_JSON_INFO
+#define _OLSRD_JSON_INFO
+
+#include "olsrd_plugin.h"
+#include "plugin_util.h"
+
+#define PLUGIN_NAME    "JSON info and dyn_gw plugin"
+#define PLUGIN_VERSION "0.0"
+#define PLUGIN_AUTHOR   "Hans-Christoph Steiner"
+#define MOD_DESC PLUGIN_NAME " " PLUGIN_VERSION " by " PLUGIN_AUTHOR
+#define PLUGIN_INTERFACE_VERSION 5
+
+
+#endif /* _OLSRD_JSON_INFO */
+
+/*
+ * Local Variables:
+ * mode: c
+ * style: linux
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */