root/src/stratcon_realtime_http.c

Revision 2d89607d671aa2191518fe131eb3f8bcee49127a, 12.6 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

fixes... holy crap, it works. This things leaks and persists when it shouldn't... but we have data flow! refs #71

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7 #include "eventer/eventer.h"
8 #include "noit_conf.h"
9 #include "utils/noit_hash.h"
10 #include "utils/noit_log.h"
11 #include "jlog/jlog.h"
12 #include "noit_jlog_listener.h"
13 #include "noit_listener.h"
14 #include "noit_http.h"
15 #include "noit_livestream_listener.h"
16 #include "stratcon_realtime_http.h"
17 #include "stratcon_jlog_streamer.h"
18 #include "stratcon_datastore.h"
19
20 #include <unistd.h>
21 #include <assert.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #ifdef HAVE_SYS_FILIO_H
26 #include <sys/filio.h>
27 #endif
28 #include <netinet/in.h>
29 #include <sys/un.h>
30 #include <arpa/inet.h>
31
32
33 typedef struct realtime_recv_ctx_t {
34   int bytes_expected;
35   int bytes_read;
36   int bytes_written;
37   int body_len;
38   char *buffer;         /* These guys are for doing partial reads */
39
40   enum {
41     WANT_INITIATE = 0,
42     WANT_SEND_INTERVAL = 1,
43     WANT_SEND_UUID = 2,
44     WANT_HEADER = 3,
45     WANT_BODY = 4,
46   } state;
47   int count;            /* Number of jlog messages we need to read */
48   noit_http_session_ctx *ctx;
49   struct realtime_tracker *rt;
50 } realtime_recv_ctx_t;
51
52 typedef struct realtime_context {
53   enum { RC_INITIAL = 0, RC_REQ_RECV, RC_INTERESTS_RESOLVED, RC_FEEDING } setup;
54   struct realtime_tracker *checklist;
55 } realtime_context;
56
57 static realtime_context *alloc_realtime_context() {
58   realtime_context *ctx;
59   return calloc(sizeof(*ctx), 1);
60 }
61 static void free_realtime_tracker(struct realtime_tracker *rt) {
62   if(rt->noit) free(rt->noit);
63   free(rt);
64 }
65 static void clear_realtime_context(realtime_context *rc) {
66   while(rc->checklist) {
67     struct realtime_tracker *tofree;
68     tofree = rc->checklist;
69     rc->checklist = tofree->next;
70     free_realtime_tracker(tofree);
71   }
72 }
73 int
74 stratcon_line_to_javascript(noit_http_session_ctx *ctx, char *buff) {
75   char buffer[1024];
76   char *scp, *ecp, *token;
77   int len;
78
79 #define PROCESS_NEXT_FIELD(t,l) do { \
80   if(!*scp) goto bad_row; \
81   ecp = strchr(scp, '\t'); \
82   if(!ecp) goto bad_row; \
83   t = scp; \
84   l = (ecp-scp); \
85   scp = ecp + 1; \
86 } while(0)
87 #define PROCESS_LAST_FIELD(t,l) do { \
88   if(!*scp) ecp = scp; \
89   else { \
90     ecp = scp + strlen(scp); /* Puts us at the '\0' */ \
91     if(*(ecp-1) == '\n') ecp--; /* We back up on letter if we ended in \n */ \
92   } \
93   t = scp; \
94   l = (ecp-scp); \
95 } while(0)
96
97   scp = buff;
98   PROCESS_NEXT_FIELD(token,len); /* Skip the leader */
99   if(buff[0] == 'M') {
100     snprintf(buffer, sizeof(buffer), "<script>window.parent.plot_iframe_data('");
101     if(noit_http_response_append(ctx, buffer, strlen(buffer)) == noit_false) return -1;
102
103     /* Time */
104     PROCESS_NEXT_FIELD(token,len);
105     if(noit_http_response_append(ctx, token, len) == noit_false) return -1;
106
107     snprintf(buffer, sizeof(buffer), "', '");
108     if(noit_http_response_append(ctx, buffer, strlen(buffer)) == noit_false) return -1;
109
110     /* UUID */
111     PROCESS_NEXT_FIELD(token,len);
112     if(noit_http_response_append(ctx, token, len) == noit_false) return -1;
113
114     snprintf(buffer, sizeof(buffer), "', '");
115     if(noit_http_response_append(ctx, buffer, strlen(buffer)) == noit_false) return -1;
116
117     /* name */
118     PROCESS_NEXT_FIELD(token,len);
119     if(noit_http_response_append(ctx, token, len) == noit_false) return -1;
120
121     snprintf(buffer, sizeof(buffer), "', '");
122     if(noit_http_response_append(ctx, buffer, strlen(buffer)) == noit_false) return -1;
123
124     PROCESS_NEXT_FIELD(token,len); /* skip type */
125     PROCESS_LAST_FIELD(token,len); /* value */
126     if(noit_http_response_append(ctx, token, len) == noit_false) return -1;
127
128     snprintf(buffer, sizeof(buffer), "');</script>\n");
129     if(noit_http_response_append(ctx, buffer, strlen(buffer)) == noit_false) return -1;
130
131     if(noit_http_response_flush(ctx, noit_false) == noit_false) return -1;
132   }
133
134   return 0;
135
136  bad_row:
137   return -1;
138   if(0) {
139     noit_http_response_end(ctx);
140     memset(ctx->dispatcher_closure, 0, sizeof(realtime_context));
141     if(ctx->conn.e) eventer_trigger(ctx->conn.e, EVENTER_WRITE);
142     return 0;
143   }
144 }
145 int
146 stratcon_realtime_uri_parse(realtime_context *rc, char *uri) {
147   int len, cnt = 0;
148   char *cp, *copy, *interest, *brk;
149   if(strncmp(uri, "/data/", 6)) return 0;
150   cp = uri + 6;
151   len = strlen(cp);
152   copy = alloca(len + 1);
153   if(!copy) return 0;
154   memcpy(copy, cp, len);
155   copy[len] = '\0';
156
157   for (interest = strtok_r(copy, "/", &brk);
158        interest;
159        interest = strtok_r(NULL, "/", &brk)) {
160     struct realtime_tracker *node;
161     char *interval;
162
163     interval = strchr(interest, '@');
164     if(!interval) return 0;
165     *interval++ = '\0';
166     node = calloc(1, sizeof(*node));
167     node->rc = rc;
168     node->sid = atoi(interest);
169     node->interval = atoi(interval);
170     node->next = rc->checklist;
171     rc->checklist = node;
172     cnt++;
173   }
174   return cnt;
175 }
176 static void
177 free_realtime_recv_ctx(void *vctx) {
178   realtime_recv_ctx_t *rrctx = vctx;
179   noit_atomic_dec32(&rrctx->ctx->ref_cnt);
180   free(rrctx);
181 }
182 #define Eread(a,b) e->opset->read(e->fd, (a), (b), &mask, e)
183 static int
184 __read_on_ctx(eventer_t e, realtime_recv_ctx_t *ctx, int *newmask) {
185   int len, mask;
186   while(ctx->bytes_read < ctx->bytes_expected) {
187     len = Eread(ctx->buffer + ctx->bytes_read,
188                 ctx->bytes_expected - ctx->bytes_read);
189     if(len < 0) {
190       *newmask = mask;
191       return -1;
192     }
193     /* if we get 0 inside SSL, and there was a real error, we
194      * will actually get a -1 here.
195      * if(len == 0) return ctx->bytes_read;
196      */
197     ctx->bytes_read += len;
198   }
199   assert(ctx->bytes_read == ctx->bytes_expected);
200   return ctx->bytes_read;
201 }
202 #define FULLREAD(e,ctx,size) do { \
203   int mask, len; \
204   if(!ctx->bytes_expected) { \
205     ctx->bytes_expected = size; \
206     if(ctx->buffer) free(ctx->buffer); \
207     ctx->buffer = malloc(size + 1); \
208     if(ctx->buffer == NULL) { \
209       noitL(noit_error, "malloc(%lu) failed.\n", (unsigned long)size + 1); \
210       goto socket_error; \
211     } \
212     ctx->buffer[size] = '\0'; \
213   } \
214   len = __read_on_ctx(e, ctx, &mask); \
215   if(len < 0) { \
216     if(errno == EAGAIN) return mask | EVENTER_EXCEPTION; \
217     noitL(noit_error, "SSL read error: %s\n", strerror(errno)); \
218     goto socket_error; \
219   } \
220   ctx->bytes_read = 0; \
221   ctx->bytes_expected = 0; \
222   if(len != size) { \
223     noitL(noit_error, "SSL short read [%d] (%d/%lu).  Reseting connection.\n", \
224           ctx->state, len, (unsigned long)size); \
225     goto socket_error; \
226   } \
227 } while(0)
228
229 int
230 stratcon_realtime_recv_handler(eventer_t e, int mask, void *closure,
231                                struct timeval *now) {
232   static u_int32_t livestream_cmd = 0;
233   noit_connection_ctx_t *nctx = closure;
234   realtime_recv_ctx_t *ctx = nctx->consumer_ctx;
235   int len;
236   u_int32_t nint;
237   char uuid_str[37];
238
239   if(!livestream_cmd) livestream_cmd = htonl(NOIT_LIVESTREAM_DATA_FEED);
240
241   if(mask & EVENTER_EXCEPTION || nctx->wants_shutdown) {
242  socket_error:
243     ctx->state = WANT_INITIATE;
244     ctx->count = 0;
245     ctx->bytes_read = 0;
246     ctx->bytes_written = 0;
247     ctx->bytes_expected = 0;
248     if(ctx->buffer) free(ctx->buffer);
249     ctx->buffer = NULL;
250     free_realtime_recv_ctx(ctx);
251     eventer_remove_fd(e->fd);
252     e->opset->close(e->fd, &mask, e);
253     return 0;
254   }
255
256 #define full_nb_write(data, wlen) do { \
257   if(!ctx->bytes_expected) { \
258     ctx->bytes_written = 0; \
259     ctx->bytes_expected = wlen; \
260   } \
261   while(ctx->bytes_written < ctx->bytes_expected) { \
262     while(-1 == (len = e->opset->write(e->fd, ((char *)data) + ctx->bytes_written, \
263                                        ctx->bytes_expected - ctx->bytes_written, \
264                                        &mask, e)) && errno == EINTR); \
265     if(len < 0) { \
266       if(errno == EAGAIN) return mask | EVENTER_EXCEPTION; \
267       goto socket_error; \
268     } \
269     ctx->bytes_written += len; \
270   } \
271   if(ctx->bytes_written != ctx->bytes_expected) { \
272     noitL(noit_error, "short write on initiating stream [%d != %d].\n", \
273           ctx->bytes_written, ctx->bytes_expected); \
274     goto socket_error; \
275   } \
276   ctx->bytes_expected = 0; \
277 } while(0)
278
279   while(1) {
280     u_int32_t net_body_len;
281
282     switch(ctx->state) {
283       case WANT_INITIATE:
284         full_nb_write(&livestream_cmd, sizeof(livestream_cmd));
285         ctx->state = WANT_SEND_INTERVAL;
286       case WANT_SEND_INTERVAL:
287         nint = htonl(ctx->rt->interval);
288         full_nb_write(&nint, sizeof(nint));
289         ctx->state = WANT_SEND_UUID;
290       case WANT_SEND_UUID:
291         uuid_unparse_lower(ctx->rt->checkid, uuid_str);
292         full_nb_write(uuid_str, 36);
293         ctx->state = WANT_HEADER;
294       case WANT_HEADER:
295         FULLREAD(e, ctx, sizeof(u_int32_t));
296         memcpy(&net_body_len, ctx->buffer, sizeof(u_int32_t));
297         ctx->body_len = ntohl(net_body_len);
298         free(ctx->buffer); ctx->buffer = NULL;
299         ctx->state = WANT_BODY;
300         break;
301       case WANT_BODY:
302         FULLREAD(e, ctx, ctx->body_len);
303         noitL(noit_error, "Read: '%s'\n", ctx->buffer);
304         if(stratcon_line_to_javascript(ctx->ctx, ctx->buffer)) goto socket_error;
305         free(ctx->buffer); ctx->buffer = NULL;
306         ctx->state = WANT_HEADER;
307         break;
308     }
309   }
310
311 }
312
313 int
314 stratcon_realtime_http_postresolve(eventer_t e, int mask, void *closure,
315                                    struct timeval *now) {
316   noit_http_session_ctx *ctx = closure;
317   realtime_context *rc = ctx->dispatcher_closure;
318   struct realtime_tracker *node;
319
320   for(node = rc->checklist; node; node = node->next) {
321     if(node->noit) {
322       realtime_recv_ctx_t *rrctx;
323       rrctx = calloc(1, sizeof(*rrctx));
324       rrctx->ctx = ctx;
325       rrctx->rt = node;
326       stratcon_streamer_connection(NULL, node->noit,
327                                    stratcon_realtime_recv_handler,
328                                    NULL, rrctx,
329                                    free_realtime_recv_ctx);
330     }
331     else
332       noit_atomic_dec32(&ctx->ref_cnt);
333   }
334   if(ctx->ref_cnt == 1) {
335     noit_http_response_end(ctx);
336     clear_realtime_context(rc);
337     memset(ctx->dispatcher_closure, 0, sizeof(realtime_context));
338     if(ctx->conn.e) eventer_trigger(ctx->conn.e, EVENTER_WRITE);
339   }
340   return 0;
341 }
342 int
343 stratcon_request_dispatcher(noit_http_session_ctx *ctx) {
344   const char *key, *value;
345   realtime_context *rc = ctx->dispatcher_closure;
346   int klen;
347   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
348   noit_http_request *req = &ctx->req;
349
350   if(rc->setup == RC_INITIAL) {
351     eventer_t completion;
352     struct realtime_tracker *node;
353     char c[1024];
354     int num_interests;
355
356     num_interests = stratcon_realtime_uri_parse(rc, ctx->req.uri_str);
357     if(num_interests == 0) {
358       noit_http_response_status_set(ctx, 404, "OK");
359       noit_http_response_option_set(ctx, NOIT_HTTP_CLOSE);
360       noit_http_response_end(ctx);
361       return 0;
362     }
363
364     noitL(noit_error, "http: %s %s %s\n",
365           req->method_str, req->uri_str, req->protocol_str);
366     while(noit_hash_next(&req->headers, &iter, &key, &klen, (void **)&value)) {
367       noitL(noit_error, "http: [%s: %s]\n", key, value);
368     }
369     noit_http_response_status_set(ctx, 200, "OK");
370     noit_http_response_option_set(ctx, NOIT_HTTP_CHUNKED);
371     noit_http_response_option_set(ctx, NOIT_HTTP_DEFLATE);
372     noit_http_response_header_set(ctx, "Content-Type", "text/html");
373
374     snprintf(c, sizeof(c), "<html><head><script>document.domain='omniti.com';</script></head><body>\n");
375     noit_http_response_append(ctx, c, strlen(c));
376
377     /* this dumb crap is to make some browsers happy (Safari) */
378     memset(c, ' ', sizeof(c));
379     noit_http_response_append(ctx, c, sizeof(c));
380     noit_http_response_flush(ctx, noit_false);
381
382     rc->setup = RC_REQ_RECV;
383     /* Each interest references the ctx */
384     for(node = rc->checklist; node; node = node->next) {
385       noit_atomic_inc32(&ctx->ref_cnt);
386       stratcon_datastore_push(DS_OP_FIND, NULL, node);
387       noitL(noit_error, "Resolving sid: %d\n", node->sid);
388     }
389     completion = eventer_alloc();
390     completion->mask = EVENTER_TIMER;
391     completion->callback = stratcon_realtime_http_postresolve;
392     completion->closure = ctx;
393     gettimeofday(&completion->whence, NULL);
394     stratcon_datastore_push(DS_OP_FIND_COMPLETE, NULL, completion);
395   }
396   return EVENTER_EXCEPTION;
397 }
398
399 int
400 stratcon_realtime_http_handler(eventer_t e, int mask, void *closure,
401                                struct timeval *now) {
402   acceptor_closure_t *ac = closure;
403   noit_http_session_ctx *http_ctx = ac->service_ctx;
404   if(!http_ctx) {
405     http_ctx = ac->service_ctx =
406       noit_http_session_ctx_new(stratcon_request_dispatcher,
407                                 alloc_realtime_context(),
408                                 e);
409   }
410   return http_ctx->drive(e, mask, http_ctx, now);
411 }
412
413 void
414 stratcon_realtime_http_init(const char *toplevel) {
415   eventer_name_callback("stratcon_realtime_http",
416                         stratcon_realtime_http_handler);
417 }
Note: See TracBrowser for help on using the browser.