| 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 |
|
|---|
| 16 |
typedef struct { |
|---|
| 17 |
int setup; |
|---|
| 18 |
} realtime_context; |
|---|
| 19 |
|
|---|
| 20 |
static realtime_context *alloc_realtime_context() { |
|---|
| 21 |
realtime_context *ctx; |
|---|
| 22 |
return calloc(sizeof(*ctx), 1); |
|---|
| 23 |
} |
|---|
| 24 |
int |
|---|
| 25 |
stratcon_realtime_ticker(eventer_t old, int mask, void *closure, |
|---|
| 26 |
struct timeval *now) { |
|---|
| 27 |
char buffer[100]; |
|---|
| 28 |
noit_http_session_ctx *ctx = closure; |
|---|
| 29 |
|
|---|
| 30 |
if(0) { |
|---|
| 31 |
noit_http_response_end(ctx); |
|---|
| 32 |
memset(ctx->dispatcher_closure, 0, sizeof(realtime_context)); |
|---|
| 33 |
if(ctx->conn.e) eventer_trigger(ctx->conn.e, EVENTER_WRITE); |
|---|
| 34 |
return 0; |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
eventer_t e = eventer_alloc(); |
|---|
| 38 |
gettimeofday(&e->whence, NULL); |
|---|
| 39 |
snprintf(buffer, sizeof(buffer), "<script>reconnoiter_realtime_feed(%lu,'%s','%s','%0.3f');</script>\n", |
|---|
| 40 |
e->whence.tv_sec * 1000 + e->whence.tv_usec / 1000, "A-UUID", "metric-name", (float)(rand() % 100000) / 1000.0); |
|---|
| 41 |
noit_http_response_append(ctx, buffer, strlen(buffer)); |
|---|
| 42 |
noit_http_response_flush(ctx, noit_false); |
|---|
| 43 |
|
|---|
| 44 |
e->mask = EVENTER_TIMER; |
|---|
| 45 |
e->whence.tv_sec += 0; |
|---|
| 46 |
e->whence.tv_usec += 500; |
|---|
| 47 |
e->callback = stratcon_realtime_ticker; |
|---|
| 48 |
e->closure = closure; |
|---|
| 49 |
eventer_add(e); |
|---|
| 50 |
return 0; |
|---|
| 51 |
} |
|---|
| 52 |
int |
|---|
| 53 |
stratcon_request_dispatcher(noit_http_session_ctx *ctx) { |
|---|
| 54 |
const char *key, *value; |
|---|
| 55 |
realtime_context *rc = ctx->dispatcher_closure; |
|---|
| 56 |
int klen; |
|---|
| 57 |
noit_hash_iter iter = NOIT_HASH_ITER_ZERO; |
|---|
| 58 |
noit_http_request *req = &ctx->req; |
|---|
| 59 |
|
|---|
| 60 |
if(strcmp(ctx->req.uri_str, "/data")) { |
|---|
| 61 |
noit_http_response_status_set(ctx, 404, "OK"); |
|---|
| 62 |
noit_http_response_option_set(ctx, NOIT_HTTP_CLOSE); |
|---|
| 63 |
noit_http_response_end(ctx); |
|---|
| 64 |
return 0; |
|---|
| 65 |
} |
|---|
| 66 |
if(!rc->setup) { |
|---|
| 67 |
const char *c = "<html><body>\n"; |
|---|
| 68 |
noitL(noit_error, "http: %s %s %s\n", |
|---|
| 69 |
req->method_str, req->uri_str, req->protocol_str); |
|---|
| 70 |
while(noit_hash_next(&req->headers, &iter, &key, &klen, (void **)&value)) { |
|---|
| 71 |
noitL(noit_error, "http: [%s: %s]\n", key, value); |
|---|
| 72 |
} |
|---|
| 73 |
noit_http_response_status_set(ctx, 200, "OK"); |
|---|
| 74 |
noit_http_response_option_set(ctx, NOIT_HTTP_CHUNKED); |
|---|
| 75 |
noit_http_response_option_set(ctx, NOIT_HTTP_DEFLATE); |
|---|
| 76 |
noit_http_response_header_set(ctx, "Content-Type", "text/html"); |
|---|
| 77 |
noit_http_response_append(ctx, c, strlen(c)); |
|---|
| 78 |
noit_http_response_flush(ctx, noit_false); |
|---|
| 79 |
stratcon_realtime_ticker(NULL, 0, ctx, NULL); |
|---|
| 80 |
rc->setup = 1; |
|---|
| 81 |
} |
|---|
| 82 |
return EVENTER_EXCEPTION; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
int |
|---|
| 86 |
stratcon_realtime_http_handler(eventer_t e, int mask, void *closure, |
|---|
| 87 |
struct timeval *now) { |
|---|
| 88 |
acceptor_closure_t *ac = closure; |
|---|
| 89 |
noit_http_session_ctx *http_ctx = ac->service_ctx; |
|---|
| 90 |
if(!http_ctx) { |
|---|
| 91 |
http_ctx = ac->service_ctx = |
|---|
| 92 |
noit_http_session_ctx_new(stratcon_request_dispatcher, |
|---|
| 93 |
alloc_realtime_context(), |
|---|
| 94 |
e); |
|---|
| 95 |
} |
|---|
| 96 |
return http_ctx->drive(e, mask, http_ctx, now); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
void |
|---|
| 100 |
stratcon_realtime_http_init(const char *toplevel) { |
|---|
| 101 |
eventer_name_callback("stratcon_realtime_http", |
|---|
| 102 |
stratcon_realtime_http_handler); |
|---|
| 103 |
} |
|---|