root/src/stratcon_realtime_http.c

Revision 49e329e9ec43712076e6d8fa719ed3cebd9f66e7, 3.1 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

fix off-by-one in hex output for chunked encoding, support for deflate, deflate and non-deflate works with curl, but only non-deflate (regular chunked) works with firefox. refs #64

  • 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"c
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   int f;
28   char buffer[100];
29   noit_http_session_ctx *ctx = closure;
30   f = rand() % 10;
31
32   if(!f) {
33     noit_http_response_end(ctx);
34     memset(ctx->dispatcher_closure, 0, sizeof(realtime_context));
35     if(ctx->conn.e) eventer_trigger(ctx->conn.e, EVENTER_WRITE);
36     return 0;
37   }
38
39   eventer_t e = eventer_alloc();
40   gettimeofday(&e->whence, NULL);
41   snprintf(buffer, sizeof(buffer), "[%lu] Ticker...<br />\n", e->whence.tv_sec);
42   noit_http_response_append(ctx, buffer, strlen(buffer));
43   noit_http_response_flush(ctx, false);
44
45   fprintf(stderr, " Next tick in %d seconds\n", f);
46   e->mask = EVENTER_TIMER;
47   e->whence.tv_sec += f;
48   e->callback = stratcon_realtime_ticker;
49   e->closure = closure;
50   eventer_add(e);
51   return 0;
52 }
53 int
54 stratcon_request_dispatcher(noit_http_session_ctx *ctx) {
55   const char *key, *value;
56   realtime_context *rc = ctx->dispatcher_closure;
57   int klen;
58   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
59   noit_http_request *req = &ctx->req;
60
61   if(strcmp(ctx->req.uri_str, "/data")) {
62     noit_http_response_status_set(ctx, 404, "OK");
63     noit_http_response_option_set(ctx, NOIT_HTTP_CLOSE);
64     noit_http_response_end(ctx);
65     return 0;
66   }
67   if(!rc->setup) {
68     const char *c = "<html><body><div id=\"foo\">Here</div>\n";
69     noitL(noit_error, "http: %s %s %s\n",
70           req->method_str, req->uri_str, req->protocol_str);
71     while(noit_hash_next(&req->headers, &iter, &key, &klen, (void **)&value)) {
72       noitL(noit_error, "http: [%s: %s]\n", key, value);
73     }
74     noit_http_response_status_set(ctx, 200, "OK");
75     noit_http_response_option_set(ctx, NOIT_HTTP_CHUNKED);
76     noit_http_response_option_set(ctx, NOIT_HTTP_DEFLATE);
77     noit_http_response_header_set(ctx, "Content-Type", "text/html");
78     noit_http_response_append(ctx, c, strlen(c));
79     noit_http_response_flush(ctx, false);
80     stratcon_realtime_ticker(NULL, 0, ctx, NULL);
81     rc->setup = 1;
82   }
83   return EVENTER_EXCEPTION;
84 }
85
86 int
87 stratcon_realtime_http_handler(eventer_t e, int mask, void *closure,
88                                struct timeval *now) {
89   acceptor_closure_t *ac = closure;
90   noit_http_session_ctx *http_ctx = ac->service_ctx;
91   if(!http_ctx) {
92     http_ctx = ac->service_ctx =
93       noit_http_session_ctx_new(stratcon_request_dispatcher,
94                                 alloc_realtime_context(),
95                                 e);
96   }
97   return http_ctx->drive(e, mask, http_ctx, now);
98 }
99
100 void
101 stratcon_realtime_http_init(const char *toplevel) {
102   eventer_name_callback("stratcon_realtime_http",
103                         stratcon_realtime_http_handler);
104 }
Note: See TracBrowser for help on using the browser.