| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_defines.h" |
|---|
| 34 |
#include "eventer/eventer.h" |
|---|
| 35 |
#include "noit_conf.h" |
|---|
| 36 |
#include "utils/noit_hash.h" |
|---|
| 37 |
#include "utils/noit_log.h" |
|---|
| 38 |
#include "utils/noit_str.h" |
|---|
| 39 |
#include "jlog/jlog.h" |
|---|
| 40 |
#include "noit_jlog_listener.h" |
|---|
| 41 |
#include "noit_listener.h" |
|---|
| 42 |
#include "noit_http.h" |
|---|
| 43 |
#include "noit_rest.h" |
|---|
| 44 |
#include "noit_livestream_listener.h" |
|---|
| 45 |
#include "stratcon_realtime_http.h" |
|---|
| 46 |
#include "stratcon_jlog_streamer.h" |
|---|
| 47 |
#include "stratcon_datastore.h" |
|---|
| 48 |
|
|---|
| 49 |
#include <ctype.h> |
|---|
| 50 |
#include <unistd.h> |
|---|
| 51 |
#include <assert.h> |
|---|
| 52 |
#include <errno.h> |
|---|
| 53 |
#include <sys/types.h> |
|---|
| 54 |
#include <sys/socket.h> |
|---|
| 55 |
#ifdef HAVE_SYS_FILIO_H |
|---|
| 56 |
#include <sys/filio.h> |
|---|
| 57 |
#endif |
|---|
| 58 |
#include <netinet/in.h> |
|---|
| 59 |
#include <sys/un.h> |
|---|
| 60 |
#include <arpa/inet.h> |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
typedef struct realtime_recv_ctx_t { |
|---|
| 64 |
int bytes_expected; |
|---|
| 65 |
int bytes_read; |
|---|
| 66 |
int bytes_written; |
|---|
| 67 |
int body_len; |
|---|
| 68 |
char *buffer; /* These guys are for doing partial reads */ |
|---|
| 69 |
|
|---|
| 70 |
enum { |
|---|
| 71 |
REALTIME_HTTP_WANT_INITIATE = 0, |
|---|
| 72 |
REALTIME_HTTP_WANT_SEND_INTERVAL = 1, |
|---|
| 73 |
REALTIME_HTTP_WANT_SEND_UUID = 2, |
|---|
| 74 |
REALTIME_HTTP_WANT_HEADER = 3, |
|---|
| 75 |
REALTIME_HTTP_WANT_BODY = 4, |
|---|
| 76 |
} state; |
|---|
| 77 |
int count; /* Number of jlog messages we need to read */ |
|---|
| 78 |
u_int32_t hack_inc_id; |
|---|
| 79 |
noit_http_session_ctx *ctx; |
|---|
| 80 |
struct realtime_tracker *rt; |
|---|
| 81 |
} realtime_recv_ctx_t; |
|---|
| 82 |
|
|---|
| 83 |
typedef struct realtime_context { |
|---|
| 84 |
enum { RC_INITIAL = 0, RC_REQ_RECV, RC_INTERESTS_RESOLVED, RC_FEEDING } setup; |
|---|
| 85 |
struct realtime_tracker *checklist; |
|---|
| 86 |
char *document_domain; |
|---|
| 87 |
} realtime_context; |
|---|
| 88 |
|
|---|
| 89 |
static realtime_context *alloc_realtime_context(const char *domain) { |
|---|
| 90 |
realtime_context *ctx; |
|---|
| 91 |
ctx = calloc(sizeof(*ctx), 1); |
|---|
| 92 |
ctx->document_domain = strdup(domain); |
|---|
| 93 |
return ctx; |
|---|
| 94 |
} |
|---|
| 95 |
static void free_realtime_tracker(struct realtime_tracker *rt) { |
|---|
| 96 |
if(rt->noit) free(rt->noit); |
|---|
| 97 |
free(rt); |
|---|
| 98 |
} |
|---|
| 99 |
static void clear_realtime_context(realtime_context *rc) { |
|---|
| 100 |
rc->setup = RC_INITIAL; |
|---|
| 101 |
while(rc->checklist) { |
|---|
| 102 |
struct realtime_tracker *tofree; |
|---|
| 103 |
tofree = rc->checklist; |
|---|
| 104 |
rc->checklist = tofree->next; |
|---|
| 105 |
free_realtime_tracker(tofree); |
|---|
| 106 |
} |
|---|
| 107 |
if(rc->document_domain) free(rc->document_domain); |
|---|
| 108 |
rc->document_domain = NULL; |
|---|
| 109 |
} |
|---|
| 110 |
int |
|---|
| 111 |
stratcon_line_to_javascript(noit_http_session_ctx *ctx, char *buff, |
|---|
| 112 |
u_int32_t inc_id) { |
|---|
| 113 |
char buffer[1024]; |
|---|
| 114 |
char *scp, *ecp, *token; |
|---|
| 115 |
int len; |
|---|
| 116 |
void *vcb; |
|---|
| 117 |
const char *v, *cb = NULL; |
|---|
| 118 |
noit_hash_table json = NOIT_HASH_EMPTY; |
|---|
| 119 |
char s_inc_id[42]; |
|---|
| 120 |
|
|---|
| 121 |
snprintf(s_inc_id, sizeof(s_inc_id), "script-%08x", inc_id); |
|---|
| 122 |
if(noit_hash_retrieve(&ctx->req.querystring, "cb", strlen("cb"), &vcb)) |
|---|
| 123 |
cb = vcb; |
|---|
| 124 |
for(v = cb; v && *v; v++) |
|---|
| 125 |
if(!((*v >= '0' && *v <= '9') || |
|---|
| 126 |
(*v >= 'a' && *v <= 'z') || |
|---|
| 127 |
(*v >= 'A' && *v <= 'Z') || |
|---|
| 128 |
(*v == '_') || (*v == '.'))) { |
|---|
| 129 |
cb = NULL; |
|---|
| 130 |
break; |
|---|
| 131 |
} |
|---|
| 132 |
if(!cb) cb = "window.parent.plot_iframe_data"; |
|---|
| 133 |
|
|---|
| 134 |
#define BAIL_HTTP_WRITE do { \ |
|---|
| 135 |
noit_hash_destroy(&json, NULL, free); \ |
|---|
| 136 |
noitL(noit_error, "javascript emit failed: %s:%s:%d\n", \ |
|---|
| 137 |
__FILE__, __FUNCTION__, __LINE__); \ |
|---|
| 138 |
return -1; \ |
|---|
| 139 |
} while(0) |
|---|
| 140 |
|
|---|
| 141 |
#define PROCESS_NEXT_FIELD(t,l) do { \ |
|---|
| 142 |
if(!*scp) goto bad_row; \ |
|---|
| 143 |
ecp = strchr(scp, '\t'); \ |
|---|
| 144 |
if(!ecp) goto bad_row; \ |
|---|
| 145 |
t = scp; \ |
|---|
| 146 |
l = (ecp-scp); \ |
|---|
| 147 |
scp = ecp + 1; \ |
|---|
| 148 |
} while(0) |
|---|
| 149 |
#define PROCESS_LAST_FIELD(t,l) do { \ |
|---|
| 150 |
if(!*scp) ecp = scp; \ |
|---|
| 151 |
else { \ |
|---|
| 152 |
ecp = scp + strlen(scp); /* Puts us at the '\0' */ \ |
|---|
| 153 |
if(*(ecp-1) == '\n') ecp--; /* We back up on letter if we ended in \n */ \ |
|---|
| 154 |
} \ |
|---|
| 155 |
t = scp; \ |
|---|
| 156 |
l = (ecp-scp); \ |
|---|
| 157 |
} while(0) |
|---|
| 158 |
|
|---|
| 159 |
scp = buff; |
|---|
| 160 |
PROCESS_NEXT_FIELD(token,len); /* Skip the leader */ |
|---|
| 161 |
if(buff[0] == 'M') { |
|---|
| 162 |
noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| 163 |
const char *key; |
|---|
| 164 |
int klen, i=0; |
|---|
| 165 |
void *vval; |
|---|
| 166 |
|
|---|
| 167 |
#define ra_write(a,b) if(noit_http_response_append(ctx, a, b) == noit_false) BAIL_HTTP_WRITE |
|---|
| 168 |
|
|---|
| 169 |
snprintf(buffer, sizeof(buffer), "<script id=\"%s\">%s({", s_inc_id, cb); |
|---|
| 170 |
ra_write(buffer, strlen(buffer)); |
|---|
| 171 |
|
|---|
| 172 |
while(noit_hash_next(&ctx->req.querystring, &iter, &key, &klen, &vval)) { |
|---|
| 173 |
if(!strcmp(key, "cb")) continue; |
|---|
| 174 |
noit_hash_store(&json, key, klen, strdup(vval ?(char *)vval : "true")); |
|---|
| 175 |
} |
|---|
| 176 |
/* Time */ |
|---|
| 177 |
noit_hash_store(&json, "script_id", 9, strdup(s_inc_id)); |
|---|
| 178 |
noit_hash_store(&json, "type", 4, strdup("M")); |
|---|
| 179 |
PROCESS_NEXT_FIELD(token,len); |
|---|
| 180 |
noit_hash_store(&json, "time", 4, noit__strndup(token, len)); |
|---|
| 181 |
/* UUID */ |
|---|
| 182 |
PROCESS_NEXT_FIELD(token,len); |
|---|
| 183 |
noit_hash_store(&json, "id", 2, noit__strndup(token, len)); |
|---|
| 184 |
/* name */ |
|---|
| 185 |
PROCESS_NEXT_FIELD(token,len); |
|---|
| 186 |
noit_hash_store(&json, "metric_name", 11, noit__strndup(token, len)); |
|---|
| 187 |
/* type */ |
|---|
| 188 |
PROCESS_NEXT_FIELD(token,len); |
|---|
| 189 |
noit_hash_store(&json, "metric_type", 11, noit__strndup(token, len)); |
|---|
| 190 |
/* value */ |
|---|
| 191 |
PROCESS_LAST_FIELD(token,len); /* value */ |
|---|
| 192 |
noit_hash_store(&json, "value", 5, noit__strndup(token, len)); |
|---|
| 193 |
|
|---|
| 194 |
memset(&iter, 0, sizeof(iter)); |
|---|
| 195 |
while(noit_hash_next(&json, &iter, &key, &klen, &vval)) { |
|---|
| 196 |
char *val = (char *)vval; |
|---|
| 197 |
if(i++) ra_write(",", 1); |
|---|
| 198 |
ra_write("'", 1); |
|---|
| 199 |
ra_write(key, klen); |
|---|
| 200 |
ra_write("':\"", 3); |
|---|
| 201 |
while(*val) { |
|---|
| 202 |
if(*val == '\"' || *val == '\\') { |
|---|
| 203 |
ra_write((char *)"\\", 1); |
|---|
| 204 |
} |
|---|
| 205 |
if(isprint(*val)) { |
|---|
| 206 |
ra_write((char *)val, 1); |
|---|
| 207 |
} |
|---|
| 208 |
else { |
|---|
| 209 |
char od[5]; |
|---|
| 210 |
snprintf(od, sizeof(od), "\\%03o", *((unsigned char *)val)); |
|---|
| 211 |
ra_write(od, strlen(od)); |
|---|
| 212 |
} |
|---|
| 213 |
val++; |
|---|
| 214 |
} |
|---|
| 215 |
ra_write("\"", 1); |
|---|
| 216 |
} |
|---|
| 217 |
snprintf(buffer, sizeof(buffer), "});</script>\n"); |
|---|
| 218 |
ra_write(buffer, strlen(buffer)); |
|---|
| 219 |
|
|---|
| 220 |
if(noit_http_response_flush(ctx, noit_false) == noit_false) BAIL_HTTP_WRITE; |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
noit_hash_destroy(&json, NULL, free); |
|---|
| 224 |
return 0; |
|---|
| 225 |
|
|---|
| 226 |
bad_row: |
|---|
| 227 |
BAIL_HTTP_WRITE; |
|---|
| 228 |
} |
|---|
| 229 |
int |
|---|
| 230 |
stratcon_realtime_uri_parse(realtime_context *rc, char *uri) { |
|---|
| 231 |
int len, cnt = 0; |
|---|
| 232 |
char *cp, *copy, *interest, *brk; |
|---|
| 233 |
if(strncmp(uri, "/data/", 6)) return 0; |
|---|
| 234 |
cp = uri + 6; |
|---|
| 235 |
len = strlen(cp); |
|---|
| 236 |
copy = alloca(len + 1); |
|---|
| 237 |
if(!copy) return 0; |
|---|
| 238 |
memcpy(copy, cp, len); |
|---|
| 239 |
copy[len] = '\0'; |
|---|
| 240 |
|
|---|
| 241 |
for (interest = strtok_r(copy, "/", &brk); |
|---|
| 242 |
interest; |
|---|
| 243 |
interest = strtok_r(NULL, "/", &brk)) { |
|---|
| 244 |
uuid_t in_uuid; |
|---|
| 245 |
struct realtime_tracker *node; |
|---|
| 246 |
char *interval; |
|---|
| 247 |
|
|---|
| 248 |
interval = strchr(interest, '@'); |
|---|
| 249 |
if(!interval) |
|---|
| 250 |
interval = "5000"; |
|---|
| 251 |
else |
|---|
| 252 |
*interval++ = '\0'; |
|---|
| 253 |
if(uuid_parse(interest, in_uuid)) continue; |
|---|
| 254 |
node = calloc(1, sizeof(*node)); |
|---|
| 255 |
node->rc = rc; |
|---|
| 256 |
uuid_copy(node->checkid, in_uuid); |
|---|
| 257 |
node->interval = atoi(interval); |
|---|
| 258 |
node->next = rc->checklist; |
|---|
| 259 |
rc->checklist = node; |
|---|
| 260 |
cnt++; |
|---|
| 261 |
} |
|---|
| 262 |
return cnt; |
|---|
| 263 |
} |
|---|
| 264 |
static void |
|---|
| 265 |
free_realtime_recv_ctx(void *vctx) { |
|---|
| 266 |
realtime_recv_ctx_t *rrctx = vctx; |
|---|
| 267 |
noit_atomic_dec32(&rrctx->ctx->ref_cnt); |
|---|
| 268 |
free(rrctx); |
|---|
| 269 |
} |
|---|
| 270 |
#define Eread(a,b) e->opset->read(e->fd, (a), (b), &mask, e) |
|---|
| 271 |
static int |
|---|
| 272 |
__read_on_ctx(eventer_t e, realtime_recv_ctx_t *ctx, int *newmask) { |
|---|
| 273 |
int len, mask; |
|---|
| 274 |
while(ctx->bytes_read < ctx->bytes_expected) { |
|---|
| 275 |
len = Eread(ctx->buffer + ctx->bytes_read, |
|---|
| 276 |
ctx->bytes_expected - ctx->bytes_read); |
|---|
| 277 |
if(len < 0) { |
|---|
| 278 |
*newmask = mask; |
|---|
| 279 |
return -1; |
|---|
| 280 |
} |
|---|
| 281 |
/* if we get 0 inside SSL, and there was a real error, we |
|---|
| 282 |
* will actually get a -1 here. |
|---|
| 283 |
* if(len == 0) return ctx->bytes_read; |
|---|
| 284 |
*/ |
|---|
| 285 |
ctx->bytes_read += len; |
|---|
| 286 |
} |
|---|
| 287 |
assert(ctx->bytes_read == ctx->bytes_expected); |
|---|
| 288 |
return ctx->bytes_read; |
|---|
| 289 |
} |
|---|
| 290 |
#define FULLREAD(e,ctx,size) do { \ |
|---|
| 291 |
int mask, len; \ |
|---|
| 292 |
if(!ctx->bytes_expected) { \ |
|---|
| 293 |
ctx->bytes_expected = size; \ |
|---|
| 294 |
if(ctx->buffer) free(ctx->buffer); \ |
|---|
| 295 |
ctx->buffer = malloc(size + 1); \ |
|---|
| 296 |
if(ctx->buffer == NULL) { \ |
|---|
| 297 |
noitL(noit_error, "malloc(%lu) failed.\n", (unsigned long)size + 1); \ |
|---|
| 298 |
goto socket_error; \ |
|---|
| 299 |
} \ |
|---|
| 300 |
ctx->buffer[size] = '\0'; \ |
|---|
| 301 |
} \ |
|---|
| 302 |
len = __read_on_ctx(e, ctx, &mask); \ |
|---|
| 303 |
if(len < 0) { \ |
|---|
| 304 |
if(errno == EAGAIN) return mask | EVENTER_EXCEPTION; \ |
|---|
| 305 |
noitL(noit_error, "SSL read error: %s\n", strerror(errno)); \ |
|---|
| 306 |
goto socket_error; \ |
|---|
| 307 |
} \ |
|---|
| 308 |
ctx->bytes_read = 0; \ |
|---|
| 309 |
ctx->bytes_expected = 0; \ |
|---|
| 310 |
if(len != size) { \ |
|---|
| 311 |
noitL(noit_error, "SSL short read [%d] (%d/%lu). Reseting connection.\n", \ |
|---|
| 312 |
ctx->state, len, (unsigned long)size); \ |
|---|
| 313 |
goto socket_error; \ |
|---|
| 314 |
} \ |
|---|
| 315 |
} while(0) |
|---|
| 316 |
|
|---|
| 317 |
int |
|---|
| 318 |
stratcon_realtime_recv_handler(eventer_t e, int mask, void *closure, |
|---|
| 319 |
struct timeval *now) { |
|---|
| 320 |
static u_int32_t livestream_cmd = 0; |
|---|
| 321 |
noit_connection_ctx_t *nctx = closure; |
|---|
| 322 |
realtime_recv_ctx_t *ctx = nctx->consumer_ctx; |
|---|
| 323 |
int len; |
|---|
| 324 |
u_int32_t nint; |
|---|
| 325 |
char uuid_str[37]; |
|---|
| 326 |
|
|---|
| 327 |
if(!livestream_cmd) livestream_cmd = htonl(NOIT_LIVESTREAM_DATA_FEED); |
|---|
| 328 |
|
|---|
| 329 |
if(mask & EVENTER_EXCEPTION || nctx->wants_shutdown) { |
|---|
| 330 |
socket_error: |
|---|
| 331 |
ctx->state = REALTIME_HTTP_WANT_INITIATE; |
|---|
| 332 |
ctx->count = 0; |
|---|
| 333 |
ctx->bytes_read = 0; |
|---|
| 334 |
ctx->bytes_written = 0; |
|---|
| 335 |
ctx->bytes_expected = 0; |
|---|
| 336 |
if(ctx->buffer) free(ctx->buffer); |
|---|
| 337 |
ctx->buffer = NULL; |
|---|
| 338 |
noit_connection_ctx_dealloc(nctx); |
|---|
| 339 |
eventer_remove_fd(e->fd); |
|---|
| 340 |
e->opset->close(e->fd, &mask, e); |
|---|
| 341 |
return 0; |
|---|
| 342 |
} |
|---|
| 343 |
|
|---|
| 344 |
#define full_nb_write(data, wlen) do { \ |
|---|
| 345 |
if(!ctx->bytes_expected) { \ |
|---|
| 346 |
ctx->bytes_written = 0; \ |
|---|
| 347 |
ctx->bytes_expected = wlen; \ |
|---|
| 348 |
} \ |
|---|
| 349 |
while(ctx->bytes_written < ctx->bytes_expected) { \ |
|---|
| 350 |
while(-1 == (len = e->opset->write(e->fd, ((char *)data) + ctx->bytes_written, \ |
|---|
| 351 |
ctx->bytes_expected - ctx->bytes_written, \ |
|---|
| 352 |
&mask, e)) && errno == EINTR); \ |
|---|
| 353 |
if(len < 0) { \ |
|---|
| 354 |
if(errno == EAGAIN) return mask | EVENTER_EXCEPTION; \ |
|---|
| 355 |
goto socket_error; \ |
|---|
| 356 |
} \ |
|---|
| 357 |
ctx->bytes_written += len; \ |
|---|
| 358 |
} \ |
|---|
| 359 |
if(ctx->bytes_written != ctx->bytes_expected) { \ |
|---|
| 360 |
noitL(noit_error, "short write on initiating stream [%d != %d].\n", \ |
|---|
| 361 |
ctx->bytes_written, ctx->bytes_expected); \ |
|---|
| 362 |
goto socket_error; \ |
|---|
| 363 |
} \ |
|---|
| 364 |
ctx->bytes_expected = 0; \ |
|---|
| 365 |
} while(0) |
|---|
| 366 |
|
|---|
| 367 |
noit_connection_update_timeout(nctx); |
|---|
| 368 |
while(1) { |
|---|
| 369 |
u_int32_t net_body_len; |
|---|
| 370 |
|
|---|
| 371 |
switch(ctx->state) { |
|---|
| 372 |
case REALTIME_HTTP_WANT_INITIATE: |
|---|
| 373 |
full_nb_write(&livestream_cmd, sizeof(livestream_cmd)); |
|---|
| 374 |
ctx->state = REALTIME_HTTP_WANT_SEND_INTERVAL; |
|---|
| 375 |
case REALTIME_HTTP_WANT_SEND_INTERVAL: |
|---|
| 376 |
nint = htonl(ctx->rt->interval); |
|---|
| 377 |
full_nb_write(&nint, sizeof(nint)); |
|---|
| 378 |
ctx->state = REALTIME_HTTP_WANT_SEND_UUID; |
|---|
| 379 |
case REALTIME_HTTP_WANT_SEND_UUID: |
|---|
| 380 |
uuid_unparse_lower(ctx->rt->checkid, uuid_str); |
|---|
| 381 |
full_nb_write(uuid_str, 36); |
|---|
| 382 |
ctx->state = REALTIME_HTTP_WANT_HEADER; |
|---|
| 383 |
case REALTIME_HTTP_WANT_HEADER: |
|---|
| 384 |
FULLREAD(e, ctx, sizeof(u_int32_t)); |
|---|
| 385 |
memcpy(&net_body_len, ctx->buffer, sizeof(u_int32_t)); |
|---|
| 386 |
ctx->body_len = ntohl(net_body_len); |
|---|
| 387 |
free(ctx->buffer); ctx->buffer = NULL; |
|---|
| 388 |
ctx->state = REALTIME_HTTP_WANT_BODY; |
|---|
| 389 |
break; |
|---|
| 390 |
case REALTIME_HTTP_WANT_BODY: |
|---|
| 391 |
FULLREAD(e, ctx, ctx->body_len); |
|---|
| 392 |
if(stratcon_line_to_javascript(ctx->ctx, ctx->buffer, ctx->hack_inc_id++)) goto socket_error; |
|---|
| 393 |
free(ctx->buffer); ctx->buffer = NULL; |
|---|
| 394 |
ctx->state = REALTIME_HTTP_WANT_HEADER; |
|---|
| 395 |
break; |
|---|
| 396 |
} |
|---|
| 397 |
} |
|---|
| 398 |
|
|---|
| 399 |
} |
|---|
| 400 |
|
|---|
| 401 |
int |
|---|
| 402 |
stratcon_realtime_http_postresolve(eventer_t e, int mask, void *closure, |
|---|
| 403 |
struct timeval *now) { |
|---|
| 404 |
noit_http_session_ctx *ctx = closure; |
|---|
| 405 |
realtime_context *rc = ctx->dispatcher_closure; |
|---|
| 406 |
struct realtime_tracker *node; |
|---|
| 407 |
|
|---|
| 408 |
for(node = rc->checklist; node; node = node->next) { |
|---|
| 409 |
if(node->noit) { |
|---|
| 410 |
realtime_recv_ctx_t *rrctx; |
|---|
| 411 |
rrctx = calloc(1, sizeof(*rrctx)); |
|---|
| 412 |
rrctx->ctx = ctx; |
|---|
| 413 |
rrctx->rt = node; |
|---|
| 414 |
stratcon_streamer_connection(NULL, node->noit, |
|---|
| 415 |
stratcon_realtime_recv_handler, |
|---|
| 416 |
NULL, rrctx, |
|---|
| 417 |
free_realtime_recv_ctx); |
|---|
| 418 |
} |
|---|
| 419 |
else |
|---|
| 420 |
noit_atomic_dec32(&ctx->ref_cnt); |
|---|
| 421 |
} |
|---|
| 422 |
if(ctx->ref_cnt == 1) { |
|---|
| 423 |
noit_http_response_end(ctx); |
|---|
| 424 |
clear_realtime_context(rc); |
|---|
| 425 |
if(ctx->conn.e) eventer_trigger(ctx->conn.e, EVENTER_WRITE); |
|---|
| 426 |
} |
|---|
| 427 |
return 0; |
|---|
| 428 |
} |
|---|
| 429 |
int |
|---|
| 430 |
stratcon_request_dispatcher(noit_http_session_ctx *ctx) { |
|---|
| 431 |
const char *key, *value; |
|---|
| 432 |
realtime_context *rc = ctx->dispatcher_closure; |
|---|
| 433 |
int klen; |
|---|
| 434 |
noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| 435 |
noit_http_request *req = &ctx->req; |
|---|
| 436 |
|
|---|
| 437 |
if(rc->setup == RC_INITIAL) { |
|---|
| 438 |
eventer_t completion; |
|---|
| 439 |
struct realtime_tracker *node; |
|---|
| 440 |
char c[1024]; |
|---|
| 441 |
int num_interests; |
|---|
| 442 |
|
|---|
| 443 |
num_interests = stratcon_realtime_uri_parse(rc, ctx->req.uri_str); |
|---|
| 444 |
if(num_interests == 0) { |
|---|
| 445 |
noit_http_response_status_set(ctx, 404, "OK"); |
|---|
| 446 |
noit_http_response_option_set(ctx, NOIT_HTTP_CLOSE); |
|---|
| 447 |
noit_http_response_end(ctx); |
|---|
| 448 |
return 0; |
|---|
| 449 |
} |
|---|
| 450 |
|
|---|
| 451 |
noitL(noit_error, "http: %s %s %s\n", |
|---|
| 452 |
req->method_str, req->uri_str, req->protocol_str); |
|---|
| 453 |
while(noit_hash_next_str(&req->headers, &iter, &key, &klen, &value)) { |
|---|
| 454 |
noitL(noit_error, "http: [%s: %s]\n", key, value); |
|---|
| 455 |
} |
|---|
| 456 |
noit_http_response_status_set(ctx, 200, "OK"); |
|---|
| 457 |
noit_http_response_option_set(ctx, NOIT_HTTP_CHUNKED); |
|---|
| 458 |
/*noit_http_response_option_set(ctx, NOIT_HTTP_GZIP);*/ |
|---|
| 459 |
/*noit_http_response_option_set(ctx, NOIT_HTTP_DEFLATE);*/ |
|---|
| 460 |
noit_http_response_header_set(ctx, "Content-Type", "text/html"); |
|---|
| 461 |
|
|---|
| 462 |
snprintf(c, sizeof(c), |
|---|
| 463 |
"<html><head><script>document.domain='%s';</script></head><body>\n", |
|---|
| 464 |
rc->document_domain); |
|---|
| 465 |
noit_http_response_append(ctx, c, strlen(c)); |
|---|
| 466 |
|
|---|
| 467 |
/* this dumb crap is to make some browsers happy (Safari) */ |
|---|
| 468 |
memset(c, ' ', sizeof(c)); |
|---|
| 469 |
noit_http_response_append(ctx, c, sizeof(c)); |
|---|
| 470 |
noit_http_response_flush(ctx, noit_false); |
|---|
| 471 |
|
|---|
| 472 |
rc->setup = RC_REQ_RECV; |
|---|
| 473 |
/* Each interest references the ctx */ |
|---|
| 474 |
for(node = rc->checklist; node; node = node->next) { |
|---|
| 475 |
char uuid_str[UUID_STR_LEN+1]; |
|---|
| 476 |
noit_atomic_inc32(&ctx->ref_cnt); |
|---|
| 477 |
uuid_unparse_lower(node->checkid, uuid_str); |
|---|
| 478 |
noitL(noit_error, "Resolving uuid: %s\n", uuid_str); |
|---|
| 479 |
} |
|---|
| 480 |
completion = eventer_alloc(); |
|---|
| 481 |
completion->mask = EVENTER_TIMER; |
|---|
| 482 |
completion->callback = stratcon_realtime_http_postresolve; |
|---|
| 483 |
completion->closure = ctx; |
|---|
| 484 |
gettimeofday(&completion->whence, NULL); |
|---|
| 485 |
stratcon_datastore_push(DS_OP_FIND_COMPLETE, NULL, NULL, |
|---|
| 486 |
rc->checklist, completion); |
|---|
| 487 |
} |
|---|
| 488 |
return EVENTER_EXCEPTION; |
|---|
| 489 |
} |
|---|
| 490 |
int |
|---|
| 491 |
stratcon_realtime_http_handler(eventer_t e, int mask, void *closure, |
|---|
| 492 |
struct timeval *now) { |
|---|
| 493 |
acceptor_closure_t *ac = closure; |
|---|
| 494 |
noit_http_session_ctx *http_ctx = ac->service_ctx; |
|---|
| 495 |
return http_ctx->drive(e, mask, http_ctx, now); |
|---|
| 496 |
} |
|---|
| 497 |
static int |
|---|
| 498 |
rest_stream_data(noit_http_rest_closure_t *restc, |
|---|
| 499 |
int npats, char **pats) { |
|---|
| 500 |
/* We're here and want to subvert the rest system */ |
|---|
| 501 |
const char *document_domain = NULL; |
|---|
| 502 |
noit_http_session_ctx *ctx = restc->http_ctx; |
|---|
| 503 |
|
|---|
| 504 |
/* Rewire the handler */ |
|---|
| 505 |
restc->ac->service_ctx = ctx; |
|---|
| 506 |
|
|---|
| 507 |
if(!noit_hash_retr_str(restc->ac->config, |
|---|
| 508 |
"document_domain", strlen("document_domain"), |
|---|
| 509 |
&document_domain)) { |
|---|
| 510 |
noitL(noit_error, "Document domain not set! Realtime streaming will be broken\n"); |
|---|
| 511 |
document_domain = ""; |
|---|
| 512 |
} |
|---|
| 513 |
noit_http_rest_closure_free(restc); |
|---|
| 514 |
|
|---|
| 515 |
noit_http_process_querystring(&ctx->req); |
|---|
| 516 |
/* Rewire the http context */ |
|---|
| 517 |
ctx->conn.e->callback = stratcon_realtime_http_handler; |
|---|
| 518 |
ctx->dispatcher = stratcon_request_dispatcher; |
|---|
| 519 |
ctx->dispatcher_closure = alloc_realtime_context(document_domain); |
|---|
| 520 |
//ctx->drive = stratcon_realtime_http_handler; |
|---|
| 521 |
return stratcon_request_dispatcher(ctx); |
|---|
| 522 |
} |
|---|
| 523 |
|
|---|
| 524 |
void |
|---|
| 525 |
stratcon_realtime_http_init(const char *toplevel) { |
|---|
| 526 |
eventer_name_callback("stratcon_realtime_http", |
|---|
| 527 |
stratcon_realtime_http_handler); |
|---|
| 528 |
assert(noit_http_rest_register_auth( |
|---|
| 529 |
"GET", "/data/", |
|---|
| 530 |
"^((?:" UUID_REGEX "(?:@\\d+)?)(?:/" UUID_REGEX "(?:@\\d+)?)*)$", |
|---|
| 531 |
rest_stream_data, noit_http_rest_access |
|---|
| 532 |
) == 0); |
|---|
| 533 |
assert(noit_http_rest_register_auth( |
|---|
| 534 |
"GET", "/", "^(.*)$", noit_rest_simple_file_handler, noit_http_rest_access |
|---|
| 535 |
) == 0); |
|---|
| 536 |
} |
|---|