olsrd: jsoninfo: HTTP headers with CORS (if requested)
[feed/routing.git] / olsrd / patches / 004-jsonplugin-http-headers.patch
1 commit b342385531c18b8afb42db64c7a38d7879668566
2 Author: Alessio Caiazza <nolith@abisso.org>
3 Date: Fri May 16 12:53:15 2014 +0200
4
5 jsoninfo: HTTP headers with CORS (if requested)
6
7 The new "httpheaders" parameter prepends HTTP headers to the reply.
8 If not set it will default to "no" and have the same behaviour as before.
9 Cross-origin resource sharing headers (CORS) are included in reply allowing the
10 json retrieval by javascript applications not served by olsrd itself.
11 This will allow to easily develop js applications running directly in the
12 browser.
13
14 Reviewed-by: Ferry Huberts <ferry.huberts@pelagic.nl>
15
16 diff --git a/lib/jsoninfo/README_JSONINFO b/lib/jsoninfo/README_JSONINFO
17 index 709c975..8311ade 100644
18 --- a/lib/jsoninfo/README_JSONINFO
19 +++ b/lib/jsoninfo/README_JSONINFO
20 @@ -73,6 +73,14 @@ LoadPlugin "olsrd_jsoninfo.so.0.0"
21 # if you set it to 0.0.0.0, it will accept all connections
22 #PlParam "accept" "0.0.0.0"
23
24 + # The "httpheaders" parameter prepends HTTP headers to the reply.
25 + # If not set it will default to "no" and have the same behaviour as before.
26 + # Among with a minimal set of headers also Cross-origin resource sharing
27 + # headers (CORS) are included in reply allowing the json retrieval by
28 + # javascript applications not served by olsrd itself.
29 + # You can enable it uncommenting the following line:
30 + #PlParam "httpheaders" "yes"
31 +
32 # specify a UUID for this node to track it for debugging
33 #PlParam "UUIDFile" "/etc/olsrd/olsrd.uuid"
34 }
35 diff --git a/lib/jsoninfo/src/olsrd_jsoninfo.c b/lib/jsoninfo/src/olsrd_jsoninfo.c
36 index 817c64a..f29a37c 100644
37 --- a/lib/jsoninfo/src/olsrd_jsoninfo.c
38 +++ b/lib/jsoninfo/src/olsrd_jsoninfo.c
39 @@ -96,6 +96,9 @@
40
41 static int ipc_socket;
42
43 +/* Response types */
44 +#define HTTP_200 "HTTP/1.1 200 OK"
45 +
46 /* IPC initialization function */
47 static int plugin_ipc_init(void);
48
49 @@ -126,6 +129,18 @@ static void ipc_print_interfaces(struct autobuf *);
50 static void ipc_print_plugins(struct autobuf *);
51 static void ipc_print_olsrd_conf(struct autobuf *abuf);
52
53 +static size_t build_http_header(const char *status, const char *mime,
54 + uint32_t msgsize, char *buf, uint32_t bufsize);
55 +
56 +/*
57 + * this is the size of the buffer used for build_http_header
58 + * the amount of data written into the buffer will be less than
59 + * 400 bytes approximatively.
60 + * The size may vary because the Content-Length header contains
61 + * the length of the json data
62 + */
63 +#define MAX_HTTPHEADER_SIZE 512
64 +
65 #define TXT_IPC_BUFSIZE 256
66
67 /* these provide all of the runtime status info */
68 @@ -1282,6 +1297,9 @@ static void
69 send_info(unsigned int send_what, int the_socket)
70 {
71 struct autobuf abuf;
72 + size_t header_len = 0;
73 + char header_buf[MAX_HTTPHEADER_SIZE];
74 + const char *content_type = "application/json";
75
76 /* global variables for tracking when to put a comma in for JSON */
77 entrynumber[0] = 0;
78 @@ -1320,12 +1338,17 @@ send_info(unsigned int send_what, int the_socket)
79 ipc_print_olsrd_conf(&abuf);
80 }
81
82 - outbuffer[outbuffer_count] = olsr_malloc(abuf.len, "txt output buffer");
83 - outbuffer_size[outbuffer_count] = abuf.len;
84 + if(http_headers) {
85 + header_len = build_http_header(HTTP_200, content_type, abuf.len, header_buf, sizeof(header_buf));
86 + }
87 +
88 + outbuffer[outbuffer_count] = olsr_malloc(header_len + abuf.len, "json output buffer");
89 + outbuffer_size[outbuffer_count] = header_len + abuf.len;
90 outbuffer_written[outbuffer_count] = 0;
91 outbuffer_socket[outbuffer_count] = the_socket;
92
93 - memcpy(outbuffer[outbuffer_count], abuf.buf, abuf.len);
94 + memcpy(outbuffer[outbuffer_count], header_buf, header_len);
95 + memcpy((outbuffer[outbuffer_count]) + header_len, abuf.buf, abuf.len);
96 outbuffer_count++;
97
98 if (outbuffer_count == 1) {
99 @@ -1340,6 +1363,53 @@ send_info(unsigned int send_what, int the_socket)
100 abuf_free(&abuf);
101 }
102
103 +static size_t
104 +build_http_header(const char *status, const char *mime, uint32_t msgsize,
105 + char *buf, uint32_t bufsize)
106 +{
107 + time_t currtime;
108 + size_t size;
109 +
110 + size = snprintf(buf, bufsize, "%s\r\n", status);
111 +
112 + /* Date */
113 + time(&currtime);
114 + size += strftime(&buf[size], bufsize - size, "Date: %a, %d %b %Y %H:%M:%S GMT\r\n", localtime(&currtime));
115 +
116 + /* Server version */
117 + size += snprintf(&buf[size], bufsize - size, "Server: OLSRD JSONInfo plugin\r\n");
118 +
119 + /* connection-type */
120 + size += snprintf(&buf[size], bufsize - size, "Connection: closed\r\n");
121 +
122 + /* MIME type */
123 + if(mime != NULL) {
124 + size += snprintf(&buf[size], bufsize - size, "Content-type: %s\r\n", mime);
125 + }
126 +
127 + /* CORS data */
128 + /**No needs to be strict here, access control is based on source IP*/
129 + size += snprintf(&buf[size], bufsize - size, "Access-Control-Allow-Origin: *\r\n");
130 + size += snprintf(&buf[size], bufsize - size, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
131 + size += snprintf(&buf[size], bufsize - size, "Access-Control-Allow-Headers: Accept, Origin, X-Requested-With\r\n");
132 + size += snprintf(&buf[size], bufsize - size, "Access-Control-Max-Age: 1728000\r\n");
133 +
134 + /* Content length */
135 + if (msgsize > 0) {
136 + size += snprintf(&buf[size], bufsize - size, "Content-length: %i\r\n", msgsize);
137 + }
138 +
139 + /* Cache-control
140 + * No caching dynamic pages
141 + */
142 + size += snprintf(&buf[size], bufsize - size, "Cache-Control: no-cache\r\n");
143 +
144 + /* End header */
145 + size += snprintf(&buf[size], bufsize - size, "\r\n");
146 +
147 + return size;
148 +}
149 +
150 /*
151 * Local Variables:
152 * mode: c
153 diff --git a/lib/jsoninfo/src/olsrd_jsoninfo.h b/lib/jsoninfo/src/olsrd_jsoninfo.h
154 index 8478f62..56acb70 100644
155 --- a/lib/jsoninfo/src/olsrd_jsoninfo.h
156 +++ b/lib/jsoninfo/src/olsrd_jsoninfo.h
157 @@ -62,6 +62,7 @@ extern union olsr_ip_addr jsoninfo_accept_ip;
158 extern union olsr_ip_addr jsoninfo_listen_ip;
159 extern int ipc_port;
160 extern int nompr;
161 +extern bool http_headers;
162
163 int olsrd_plugin_interface_version(void);
164 int olsrd_plugin_init(void);
165 diff --git a/lib/jsoninfo/src/olsrd_plugin.c b/lib/jsoninfo/src/olsrd_plugin.c
166 index 36550a8..03aa45f 100644
167 --- a/lib/jsoninfo/src/olsrd_plugin.c
168 +++ b/lib/jsoninfo/src/olsrd_plugin.c
169 @@ -64,6 +64,7 @@ union olsr_ip_addr jsoninfo_accept_ip;
170 union olsr_ip_addr jsoninfo_listen_ip;
171 int ipc_port;
172 int nompr;
173 +bool http_headers;
174
175 static void my_init(void) __attribute__ ((constructor));
176 static void my_fini(void) __attribute__ ((destructor));
177 @@ -79,6 +80,7 @@ my_init(void)
178
179 /* defaults for parameters */
180 ipc_port = 9090;
181 + http_headers = false;
182 if (olsr_cnf->ip_version == AF_INET) {
183 jsoninfo_accept_ip.v4.s_addr = htonl(INADDR_LOOPBACK);
184 jsoninfo_listen_ip.v4.s_addr = htonl(INADDR_ANY);
185 @@ -120,11 +122,26 @@ store_string(const char *value, void *data, set_plugin_parameter_addon addon __a
186 return 0;
187 }
188
189 +static int
190 +store_boolean(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
191 +{
192 + bool *dest = data;
193 + if(strcmp(value, "yes") == 0)
194 + *dest = true;
195 + else if (strcmp(value, "no") == 0)
196 + *dest = false;
197 + else
198 + return 1; //error
199 +
200 + return 0;
201 +}
202 +
203 static const struct olsrd_plugin_parameters plugin_parameters[] = {
204 {.name = "port",.set_plugin_parameter = &set_plugin_port,.data = &ipc_port},
205 {.name = "accept",.set_plugin_parameter = &set_plugin_ipaddress,.data = &jsoninfo_accept_ip},
206 {.name = "listen",.set_plugin_parameter = &set_plugin_ipaddress,.data = &jsoninfo_listen_ip},
207 {.name = "uuidfile",.set_plugin_parameter = &store_string,.data = uuidfile},
208 + {.name = "httpheaders",.set_plugin_parameter = &store_boolean,.data = &http_headers},
209 };
210
211 void