root/src/noit_http.c

Revision 7719f70626feb48af78367f00e63c5de8155605d, 37.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

fixes compile issues on linux

  • Property mode set to 100644
Line 
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 "noit_http.h"
35 #include "utils/noit_str.h"
36
37 #include <errno.h>
38 #include <ctype.h>
39 #include <assert.h>
40 #include <zlib.h>
41 #include <libxml/tree.h>
42
43 #define REQ_PAT "\r\n\r\n"
44 #define REQ_PATSIZE 4
45 #define HEADER_CONTENT_LENGTH "content-length"
46 #define HEADER_EXPECT "expect"
47
48 static noit_log_stream_t http_debug = NULL;
49 static noit_log_stream_t http_io = NULL;
50 static noit_log_stream_t http_access = NULL;
51
52 #define CTX_ADD_HEADER(a,b) \
53     noit_hash_replace(&ctx->res.headers, \
54                       strdup(a), strlen(a), strdup(b), free, free)
55 static const char _hexchars[16] =
56   {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
57 static void inplace_urldecode(char *c) {
58   char *o = c;
59   while(*c) {
60     if(*c == '%') {
61       int i, ord = 0;
62       for(i = 0; i < 2; i++) {
63         if(c[i] >= '0' && c[i] <= '9') ord = (ord << 4) | (c[i] - '0');
64         else if (c[i] >= 'a' && c[i] <= 'f') ord = (ord << 4) | (c[i] - 'a');
65         else if (c[i] >= 'A' && c[i] <= 'F') ord = (ord << 4) | (c[i] - 'A');
66         else break;
67       }
68       if(i==2) {
69         *((unsigned char *)o++) = ord;
70         c+=3;
71         continue;
72       }
73     }
74     *o++ = *c++;
75   }
76   *o = '\0';
77 }
78
79 struct bchain *bchain_alloc(size_t size, int line) {
80   struct bchain *n;
81   n = malloc(size + (int)((char *)((struct bchain *)0)->buff));
82   /*noitL(noit_error, "bchain_alloc(%p) : %d\n", n, line);*/
83   if(!n) return NULL;
84   n->prev = n->next = NULL;
85   n->start = n->size = 0;
86   n->allocd = size;
87   return n;
88 }
89 void bchain_free(struct bchain *b, int line) {
90   /*noitL(noit_error, "bchain_free(%p) : %d\n", b, line);*/
91   free(b);
92 }
93 #define ALLOC_BCHAIN(s) bchain_alloc(s, __LINE__)
94 #define FREE_BCHAIN(a) bchain_free(a, __LINE__)
95 #define RELEASE_BCHAIN(a) do { \
96   while(a) { \
97     struct bchain *__b; \
98     __b = a; \
99     a = __b->next; \
100     bchain_free(__b, __LINE__); \
101   } \
102 } while(0)
103 struct bchain *bchain_from_data(const void *d, size_t size) {
104   struct bchain *n;
105   n = ALLOC_BCHAIN(size);
106   if(!n) return NULL;
107   memcpy(n->buff, d, size);
108   n->size = size;
109   return n;
110 }
111
112 static noit_http_method
113 _method_enum(const char *s) {
114   switch(*s) {
115    case 'G':
116     if(!strcasecmp(s, "GET")) return NOIT_HTTP_GET;
117     break;
118    case 'H':
119     if(!strcasecmp(s, "HEAD")) return NOIT_HTTP_HEAD;
120     break;
121    case 'P':
122     if(!strcasecmp(s, "POST")) return NOIT_HTTP_POST;
123     break;
124    default:
125     break;
126   }
127   return NOIT_HTTP_OTHER;
128 }
129 static noit_http_protocol
130 _protocol_enum(const char *s) {
131   if(!strcasecmp(s, "HTTP/1.1")) return NOIT_HTTP11;
132   if(!strcasecmp(s, "HTTP/1.0")) return NOIT_HTTP10;
133   return NOIT_HTTP09;
134 }
135 static noit_boolean
136 _fixup_bchain(struct bchain *b) {
137   /* make sure lines (CRLF terminated) don't cross chain boundaries */
138   while(b) {
139     struct bchain *f;
140     int start_in_b, end_in_f;
141     size_t new_size;
142     const char *str_in_f;
143
144     start_in_b = b->start;
145     if(b->size > 2) {
146       if(memcmp(b->buff + b->start + b->size - 2, "\r\n", 2) == 0) {
147         b = b->next;
148         continue;
149       }
150       start_in_b = b->start + b->size - 3; /* we already checked -2 */
151       while(start_in_b >= b->start) {
152         if(b->buff[start_in_b] == '\r' && b->buff[start_in_b+1] == '\n') {
153           start_in_b += 2;
154           break;
155         }
156         start_in_b--;
157       }
158     }
159
160     /* start_in_b points to the beginning of the string we need to build
161      * into a new buffer.
162      */
163     f = b->next;
164     if(!f) return noit_false; /* Nothing left, can't complete the line */
165     str_in_f = strnstrn("\r\n", 2, f->buff + f->start, f->size);
166     if(!str_in_f) return noit_false; /* nothing in next chain -- too long */
167     str_in_f += 2;
168     end_in_f = (str_in_f - f->buff - f->start);
169     new_size = end_in_f + (b->start + b->size - start_in_b);
170     if(new_size > DEFAULT_BCHAINSIZE) return noit_false; /* string too long */
171     f = ALLOC_BCHAIN(new_size);
172     f->prev = b;
173     f->next = b->next;
174     f->start = 0;
175     f->size = new_size;
176     memcpy(f->buff, b->buff + start_in_b, b->start + b->size - start_in_b);
177     memcpy(f->buff + b->start + b->size - start_in_b,
178            f->buff + f->start, end_in_f);
179     f->next->prev = f;
180     f->prev->next = f;
181     f->prev->size -= start_in_b - b->start;
182     f->next->size -= end_in_f;
183     f->next->start += end_in_f;
184     b = f->next; /* skip f, we know it is right */
185   }
186   return noit_true;
187 }
188 static noit_boolean
189 _extract_header(char *l, const char **n, const char **v) {
190   *n = NULL;
191   if(*l == ' ' || *l == '\t') {
192     while(*l == ' ' || *l == '\t') l++;
193     *v = l;
194     return noit_true;
195   }
196   *n = l;
197   while(*l != ':' && *l) { *l = tolower(*l); l++; }
198   if(!*l) return noit_false;
199   *v = l+1;
200   /* Right trim the name */
201   *l-- = '\0';
202   while(*l == ' ' || *l == '\t') *l-- = '\0';
203   while(**v == ' ' || **v == '\t') (*v)++;
204   return noit_true;
205 }
206 static void
207 noit_http_log_request(noit_http_session_ctx *ctx) {
208   char ip[64], timestr[64];
209   double time_ms;
210   struct tm *tm, tbuf;
211   time_t now;
212   struct timeval end_time, diff;
213
214   if(ctx->req.start_time.tv_sec == 0) return;
215   gettimeofday(&end_time, NULL);
216   now = end_time.tv_sec;
217   tm = gmtime_r(&now, &tbuf);
218   strftime(timestr, sizeof(timestr), "%d/%b/%Y:%H:%M:%S -0000", tm);
219   sub_timeval(end_time, ctx->req.start_time, &diff);
220   time_ms = diff.tv_sec * 1000 + diff.tv_usec / 1000;
221   noit_convert_sockaddr_to_buff(ip, sizeof(ip), &ctx->ac->remote.remote_addr);
222   noitL(http_access, "%s - - [%s] \"%s %s%s%s %s\" %d %llu %.3f\n",
223         ip, timestr,
224         ctx->req.method_str, ctx->req.uri_str,
225         ctx->req.orig_qs ? "?" : "", ctx->req.orig_qs ? ctx->req.orig_qs : "",
226         ctx->req.protocol_str,
227         ctx->res.status_code,
228         (long long unsigned)ctx->res.bytes_written,
229         time_ms);
230 }
231
232 static int
233 _http_perform_write(noit_http_session_ctx *ctx, int *mask) {
234   int len, tlen = 0;
235   struct bchain **head, *b;
236  choose_bucket:
237   head = ctx->res.leader ? &ctx->res.leader : &ctx->res.output_raw;
238   b = *head;
239
240   if(!ctx->conn.e) return 0;
241 #if 0
242   if(ctx->res.output_started == noit_false) return EVENTER_EXCEPTION;
243 #endif
244   if(!b) {
245     if(ctx->res.closed) ctx->res.complete = noit_true;
246     *mask = EVENTER_EXCEPTION;
247     return tlen;
248   }
249
250   if(ctx->res.output_raw_offset >= b->size) {
251     *head = b->next;
252     FREE_BCHAIN(b);
253     b = *head;
254     if(b) b->prev = NULL;
255     ctx->res.output_raw_offset = 0;
256     goto choose_bucket;
257   }
258
259   len = ctx->conn.e->opset->
260           write(ctx->conn.e->fd,
261                 b->buff + b->start + ctx->res.output_raw_offset,
262                 b->size - ctx->res.output_raw_offset,
263                 mask, ctx->conn.e);
264   if(len == -1 && errno == EAGAIN) {
265     *mask |= EVENTER_EXCEPTION;
266     return tlen;
267   }
268   if(len == -1) {
269     /* socket error */
270     ctx->res.complete = noit_true;
271     ctx->conn.needs_close = noit_true;
272     noit_http_log_request(ctx);
273     *mask |= EVENTER_EXCEPTION;
274     return -1;
275   }
276   noitL(http_io, " http_write(%d) => %d [\n%.*s\n]\n", ctx->conn.e->fd,
277         len, len, b->buff + b->start + ctx->res.output_raw_offset);
278   ctx->res.output_raw_offset += len;
279   ctx->res.bytes_written += len;
280   tlen += len;
281   goto choose_bucket;
282 }
283 static noit_boolean
284 noit_http_request_finalize_headers(noit_http_request *req, noit_boolean *err) {
285   int start;
286   void *vval;
287   const char *mstr, *last_name = NULL;
288   struct bchain *b;
289
290   if(req->state != NOIT_HTTP_REQ_HEADERS) return noit_false;
291   if(!req->current_input) req->current_input = req->first_input;
292   if(!req->current_input) return noit_false;
293   if(req->start_time.tv_sec == 0) gettimeofday(&req->start_time, NULL);
294  restart:
295   while(req->current_input->prev &&
296         (req->current_offset < (req->current_input->start + REQ_PATSIZE - 1))) {
297     int inset;
298     /* cross bucket */
299     if(req->current_input == req->last_input &&
300        req->current_offset >= (req->last_input->start + req->last_input->size))
301       return noit_false;
302     req->current_offset++;
303     inset = req->current_offset - req->current_input->start;
304     if(memcmp(req->current_input->buff + req->current_input->start,
305               REQ_PAT + (REQ_PATSIZE - inset), inset) == 0 &&
306        memcmp(req->current_input->prev->buff +
307                 req->current_input->prev->start +
308                 req->current_input->prev->size - REQ_PATSIZE + inset,
309               REQ_PAT + inset,
310               REQ_PATSIZE - inset) == 0) goto match;
311   }
312   start = MAX(req->current_offset - REQ_PATSIZE, req->current_input->start);
313   mstr = strnstrn(REQ_PAT, REQ_PATSIZE,
314                   req->current_input->buff + start,
315                   req->current_input->size -
316                     (start - req->current_input->start));
317   if(!mstr && req->current_input->next) {
318     req->current_input = req->current_input->next;
319     req->current_offset = req->current_input->start;
320     goto restart;
321   }
322   if(!mstr) return noit_false;
323   req->current_offset = mstr - req->current_input->buff + REQ_PATSIZE;
324  match:
325   req->current_request_chain = req->first_input;
326   noitL(http_debug, " noit_http_request_finalize : match(%d in %d)\n",
327         (int)(req->current_offset - req->current_input->start),
328         (int)req->current_input->size);
329   if(req->current_offset <
330      req->current_input->start + req->current_input->size) {
331     /* There are left-overs */
332     int lsize = req->current_input->size - req->current_offset;
333     noitL(http_debug, " noit_http_request_finalize -- leftovers: %d\n", lsize);
334     req->first_input = ALLOC_BCHAIN(lsize);
335     req->first_input->prev = NULL;
336     req->first_input->next = req->current_input->next;
337     req->first_input->start = 0;
338     req->first_input->size = lsize;
339     memcpy(req->first_input->buff,
340            req->current_input->buff + req->current_offset,
341            req->first_input->size);
342     req->current_input->size -= lsize;
343     if(req->last_input == req->current_input)
344       req->last_input = req->first_input;
345     else
346       FREE_BCHAIN(req->current_input);
347   }
348   else {
349     req->first_input = req->last_input = NULL;
350   }
351   req->current_input = NULL;
352   req->current_offset = 0;
353
354   /* Now we need to dissect the current_request_chain into an HTTP request */
355   /* First step: make sure that no line crosses a chain boundary by
356    * inserting new chains as necessary.
357    */
358   if(!_fixup_bchain(req->current_request_chain)) {
359     *err = noit_true;
360     return noit_false;
361   }
362   /* Second step is to parse out the request itself */
363   for(b = req->current_request_chain; b; b = b->next) {
364     char *curr_str, *next_str;
365     b->buff[b->start + b->size - 2] = '\0';
366     curr_str = b->buff + b->start;
367     do {
368       next_str = strstr(curr_str, "\r\n");
369       if(next_str) {
370         *((char *)next_str) = '\0';
371         next_str += 2;
372       }
373       if(req->method_str && *curr_str == '\0')
374         break; /* our CRLFCRLF... end of req */
375 #define FAIL do { *err = noit_true; return noit_false; } while(0)
376       if(!req->method_str) { /* request line */
377         req->method_str = (char *)curr_str;
378         req->uri_str = strchr(curr_str, ' ');
379         if(!req->uri_str) FAIL;
380         *(req->uri_str) = '\0';
381         req->uri_str++;
382         req->protocol_str = strchr(req->uri_str, ' ');
383         if(!req->protocol_str) FAIL;
384         *(req->protocol_str) = '\0';
385         req->protocol_str++;
386         req->method = _method_enum(req->method_str);
387         req->protocol = _protocol_enum(req->protocol_str);
388         req->opts |= NOIT_HTTP_CLOSE;
389         if(req->protocol == NOIT_HTTP11) req->opts |= NOIT_HTTP_CHUNKED;
390       }
391       else { /* request headers */
392         const char *name, *value;
393         if(_extract_header(curr_str, &name, &value) == noit_false) FAIL;
394         if(!name && !last_name) FAIL;
395         if(!strcmp(name ? name : last_name, "accept-encoding")) {
396           if(strstr(value, "gzip")) req->opts |= NOIT_HTTP_GZIP;
397           if(strstr(value, "deflate")) req->opts |= NOIT_HTTP_DEFLATE;
398         }
399         if(name)
400           noit_hash_replace(&req->headers, name, strlen(name), (void *)value,
401                             NULL, NULL);
402         else {
403           struct bchain *b;
404           const char *prefix = NULL;
405           int l1, l2;
406           noit_hash_retr_str(&req->headers, last_name, strlen(last_name),
407                              &prefix);
408           if(!prefix) FAIL;
409           l1 = strlen(prefix);
410           l2 = strlen(value);
411           b = ALLOC_BCHAIN(l1 + l2 + 2);
412           b->next = req->current_request_chain;
413           b->next->prev = b;
414           req->current_request_chain = b;
415           b->size = l1 + l2 + 2;
416           memcpy(b->buff, prefix, l1);
417           b->buff[l1] = ' ';
418           memcpy(b->buff + l1 + 1, value, l2);
419           b->buff[l1 + 1 + l2] = '\0';
420           noit_hash_replace(&req->headers, last_name, strlen(last_name),
421                             b->buff, NULL, NULL);
422         }
423         if(name) last_name = name;
424       }
425       curr_str = next_str;
426     } while(next_str);
427   }
428
429   /* headers are done... we could need to read a payload */
430   if(noit_hash_retrieve(&req->headers,
431                         HEADER_CONTENT_LENGTH,
432                         sizeof(HEADER_CONTENT_LENGTH)-1, &vval)) {
433     const char *val = vval;
434     req->has_payload = noit_true;
435     req->content_length = strtoll(val, NULL, 10);
436   }
437   if(noit_hash_retrieve(&req->headers, HEADER_EXPECT,
438                         sizeof(HEADER_EXPECT)-1, &vval)) {
439     const char *val = vval;
440     if(strncmp(val, "100-", 4) || /* Bad expect header */
441        req->has_payload == noit_false) /* expect, but no content length */
442       FAIL;
443     /* We need to tell the client to "go-ahead" -- HTTP sucks */
444     req->state = NOIT_HTTP_REQ_EXPECT;
445     return noit_false;
446   }
447   if(req->content_length > 0) {
448     /* switch modes... let's go read the payload */
449     req->state = NOIT_HTTP_REQ_PAYLOAD;
450     return noit_false;
451   }
452
453   req->complete = noit_true;
454   return noit_true;
455 }
456 void
457 noit_http_process_querystring(noit_http_request *req) {
458   char *cp, *interest, *brk;
459   cp = strchr(req->uri_str, '?');
460   if(!cp) return;
461   *cp++ = '\0';
462   req->orig_qs = strdup(cp);
463   for (interest = strtok_r(cp, "&", &brk);
464        interest;
465        interest = strtok_r(NULL, "&", &brk)) {
466     char *eq;
467     eq = strchr(interest, '=');
468     if(!eq) {
469       inplace_urldecode(interest);
470       noit_hash_store(&req->querystring, interest, strlen(interest), NULL);
471     }
472     else {
473       *eq++ = '\0';
474       inplace_urldecode(interest);
475       inplace_urldecode(eq);
476       noit_hash_store(&req->querystring, interest, strlen(interest), eq);
477     }
478   }
479 }
480 static noit_boolean
481 noit_http_request_finalize_payload(noit_http_request *req, noit_boolean *err) {
482   req->complete = noit_true;
483   return noit_true;
484 }
485 static noit_boolean
486 noit_http_request_finalize(noit_http_request *req, noit_boolean *err) {
487   if(req->state == NOIT_HTTP_REQ_HEADERS)
488     if(noit_http_request_finalize_headers(req, err)) return noit_true;
489   if(req->state == NOIT_HTTP_REQ_EXPECT) return noit_false;
490   if(req->state == NOIT_HTTP_REQ_PAYLOAD)
491     if(noit_http_request_finalize_payload(req, err)) return noit_true;
492   return noit_false;
493 }
494 static int
495 noit_http_complete_request(noit_http_session_ctx *ctx, int mask) {
496   struct bchain *in;
497   noit_boolean rv, err = noit_false;
498
499   if(mask & EVENTER_EXCEPTION) {
500    full_error:
501     ctx->conn.e->opset->close(ctx->conn.e->fd, &mask, ctx->conn.e);
502     ctx->conn.e = NULL;
503     return 0;
504   }
505   if(ctx->req.complete == noit_true) return EVENTER_EXCEPTION;
506
507   /* We could have a complete request in the tail of a previous request */
508   rv = noit_http_request_finalize(&ctx->req, &err);
509   if(rv == noit_true) return EVENTER_WRITE | EVENTER_EXCEPTION;
510   if(err == noit_true) goto full_error;
511
512   while(1) {
513     int len;
514
515     in = ctx->req.last_input;
516     if(!in) {
517       in = ctx->req.first_input = ctx->req.last_input =
518         ALLOC_BCHAIN(DEFAULT_BCHAINSIZE);
519       if(!in) goto full_error;
520     }
521     if(in->size > 0 && /* we've read something */
522        DEFAULT_BCHAINMINREAD > BCHAIN_SPACE(in) && /* we'd like read more */
523        DEFAULT_BCHAINMINREAD < DEFAULT_BCHAINSIZE) { /* and we can */
524       in->next = ctx->req.last_input =
525         ALLOC_BCHAIN(DEFAULT_BCHAINSIZE);
526       in->next->prev = in;
527       in = in->next;
528       if(!in) goto full_error;
529     }
530
531     len = ctx->conn.e->opset->read(ctx->conn.e->fd,
532                                    in->buff + in->start + in->size,
533                                    in->allocd - in->size - in->start,
534                                    &mask, ctx->conn.e);
535     noitL(http_debug, " noit_http -> read(%d) = %d\n", ctx->conn.e->fd, len);
536     noitL(http_io, " noit_http:read(%d) => %d [\n%.*s\n]\n", ctx->conn.e->fd, len, len, in->buff + in->start + in->size);
537     if(len == -1 && errno == EAGAIN) return mask;
538     if(len <= 0) goto full_error;
539     if(len > 0) in->size += len;
540     rv = noit_http_request_finalize(&ctx->req, &err);
541     if(len == -1 || err == noit_true) goto full_error;
542     if(ctx->req.state == NOIT_HTTP_REQ_EXPECT) {
543       const char *expect;
544       ctx->req.state = NOIT_HTTP_REQ_PAYLOAD;
545       assert(ctx->res.leader == NULL);
546       expect = "HTTP/1.1 100 Continue\r\n\r\n";
547       ctx->res.leader = bchain_from_data(expect, strlen(expect));
548       _http_perform_write(ctx, &mask);
549       if(ctx->res.leader != NULL) return mask;
550     }
551     if(rv == noit_true) return mask | EVENTER_WRITE | EVENTER_EXCEPTION;
552   }
553   /* Not reached:
554    * return EVENTER_READ | EVENTER_EXCEPTION;
555    */
556 }
557 noit_boolean
558 noit_http_session_prime_input(noit_http_session_ctx *ctx,
559                               const void *data, size_t len) {
560   if(ctx->req.first_input != NULL) return noit_false;
561   if(len > DEFAULT_BCHAINSIZE) return noit_false;
562   ctx->req.first_input = ctx->req.last_input =
563       ALLOC_BCHAIN(DEFAULT_BCHAINSIZE);
564   memcpy(ctx->req.first_input->buff, data, len);
565   ctx->req.first_input->size = len;
566   return noit_true;
567 }
568
569 void
570 noit_http_request_release(noit_http_session_ctx *ctx) {
571   noit_hash_destroy(&ctx->req.querystring, NULL, NULL);
572   noit_hash_destroy(&ctx->req.headers, NULL, NULL);
573   /* If we expected a payload, we expect a trailing \r\n */
574   if(ctx->req.has_payload) {
575     int drained, mask;
576     ctx->drainage = ctx->req.content_length - ctx->req.content_length_read;
577     /* best effort, we'll drain it before the next request anyway */
578     drained = noit_http_session_req_consume(ctx, NULL, ctx->drainage, &mask);
579     ctx->drainage -= drained;
580   }
581   RELEASE_BCHAIN(ctx->req.current_request_chain);
582   if(ctx->req.orig_qs) free(ctx->req.orig_qs);
583   memset(&ctx->req.state, 0,
584          sizeof(ctx->req) - (unsigned long)&(((noit_http_request *)0)->state));
585 }
586 void
587 noit_http_response_release(noit_http_session_ctx *ctx) {
588   noit_hash_destroy(&ctx->res.headers, free, free);
589   if(ctx->res.status_reason) free(ctx->res.status_reason);
590   RELEASE_BCHAIN(ctx->res.leader);
591   RELEASE_BCHAIN(ctx->res.output);
592   RELEASE_BCHAIN(ctx->res.output_raw);
593   memset(&ctx->res, 0, sizeof(ctx->res));
594 }
595 void
596 noit_http_ctx_session_release(noit_http_session_ctx *ctx) {
597   if(noit_atomic_dec32(&ctx->ref_cnt) == 0) {
598     noit_http_request_release(ctx);
599     if(ctx->req.first_input) RELEASE_BCHAIN(ctx->req.first_input);
600     noit_http_response_release(ctx);
601     free(ctx);
602   }
603 }
604 void
605 noit_http_ctx_acceptor_free(void *v) {
606   noit_http_ctx_session_release((noit_http_session_ctx *)v);
607 }
608 int
609 noit_http_session_req_consume(noit_http_session_ctx *ctx,
610                               void *buf, size_t len, int *mask) {
611   size_t bytes_read = 0;
612   /* We attempt to consume from the first_input */
613   struct bchain *in, *tofree;
614   noitL(http_debug, " ... noit_http_session_req_consume(%d) %d of %d\n",
615         ctx->conn.e->fd, (int)len,
616         (int)(ctx->req.content_length - ctx->req.content_length_read));
617   len = MIN(len, ctx->req.content_length - ctx->req.content_length_read);
618   while(bytes_read < len) {
619     int crlen = 0;
620     in = ctx->req.first_input;
621     while(in && bytes_read < len) {
622       int partial_len = MIN(in->size, len - bytes_read);
623       if(buf) memcpy((char *)buf+bytes_read, in->buff+in->start, partial_len);
624       bytes_read += partial_len;
625       ctx->req.content_length_read += partial_len;
626       noitL(http_debug, " ... filling %d bytes (read through %d/%d)\n",
627             (int)bytes_read, (int)ctx->req.content_length_read,
628             (int)ctx->req.content_length);
629       in->start += partial_len;
630       in->size -= partial_len;
631       if(in->size == 0) {
632         tofree = in;
633         ctx->req.first_input = in = in->next;
634         tofree->next = NULL;
635         RELEASE_BCHAIN(tofree);
636         if(in == NULL) {
637           ctx->req.last_input = NULL;
638           noitL(http_debug, " ... noit_http_session_req_consume = %d\n",
639                 (int)bytes_read);
640           return bytes_read;
641         }
642       }
643     }
644     while(bytes_read + crlen < len) {
645       int rlen;
646       in = ctx->req.last_input;
647       if(!in)
648         in = ctx->req.first_input = ctx->req.last_input =
649             ALLOC_BCHAIN(DEFAULT_BCHAINSIZE);
650       else if(in->start + in->size >= in->allocd) {
651         in->next = ALLOC_BCHAIN(DEFAULT_BCHAINSIZE);
652         in = ctx->req.last_input = in->next;
653       }
654       /* pull next chunk */
655       rlen = ctx->conn.e->opset->read(ctx->conn.e->fd,
656                                       in->buff + in->start + in->size,
657                                       in->allocd - in->size - in->start,
658                                       mask, ctx->conn.e);
659       noitL(http_debug, " noit_http -> read(%d) = %d\n", ctx->conn.e->fd, rlen);
660     noitL(http_io, " noit_http:read(%d) => %d [\n%.*s\n]\n", ctx->conn.e->fd, rlen, rlen, in->buff + in->start + in->size);
661       if(rlen == -1 && errno == EAGAIN) {
662         /* We'd block to read more, but we have data,
663          * so do a short read */
664         if(ctx->req.first_input && ctx->req.first_input->size) break;
665         /* We've got nothing... */
666         noitL(http_debug, " ... noit_http_session_req_consume = -1 (EAGAIN)\n");
667         return -1;
668       }
669       if(rlen <= 0) {
670         noitL(http_debug, " ... noit_http_session_req_consume = -1 (error)\n");
671         return -1;
672       }
673       in->size += rlen;
674       crlen += rlen;
675     }
676   }
677   /* NOT REACHED */
678   return bytes_read;
679 }
680
681 int
682 noit_http_session_drive(eventer_t e, int origmask, void *closure,
683                         struct timeval *now, int *done) {
684   noit_http_session_ctx *ctx = closure;
685   int rv = 0;
686   int mask = origmask;
687
688   if(origmask & EVENTER_EXCEPTION)
689     goto abort_drive;
690
691   /* Drainage -- this is as nasty as it sounds
692    * The last request could have unread upload content, we would have
693    * noted that in noit_http_request_release.
694    */
695   noitL(http_debug, " -> noit_http_session_drive(%d) [%x]\n", e->fd, origmask);
696   while(ctx->drainage > 0) {
697     int len;
698     noitL(http_debug, "   ... draining last request(%d)\n", e->fd);
699     len = noit_http_session_req_consume(ctx, NULL, ctx->drainage, &mask);
700     if(len == -1 && errno == EAGAIN) {
701       noitL(http_debug, " <- noit_http_session_drive(%d) [%x]\n", e->fd, mask);
702       return mask;
703     }
704     if(len <= 0) goto abort_drive;
705     ctx->drainage -= len;
706   }
707
708  next_req:
709   if(ctx->req.complete != noit_true) {
710     int maybe_write_mask;
711     noitL(http_debug, "   -> noit_http_complete_request(%d)\n", e->fd);
712     mask = noit_http_complete_request(ctx, origmask);
713     noitL(http_debug, "   <- noit_http_complete_request(%d) = %d\n",
714           e->fd, mask);
715     _http_perform_write(ctx, &maybe_write_mask);
716     if(ctx->conn.e == NULL) goto release;
717     if(ctx->req.complete != noit_true) {
718       noitL(http_debug, " <- noit_http_session_drive(%d) [%x]\n", e->fd,
719             mask|maybe_write_mask);
720       return mask | maybe_write_mask;
721     }
722     noitL(http_debug, "HTTP start request (%s)\n", ctx->req.uri_str);
723     noit_http_process_querystring(&ctx->req);
724   }
725
726   /* only dispatch if the response is not closed */
727   if(ctx->res.closed == noit_false) {
728     noitL(http_debug, "   -> dispatch(%d)\n", e->fd);
729     rv = ctx->dispatcher(ctx);
730     noitL(http_debug, "   <- dispatch(%d) = %d\n", e->fd, rv);
731   }
732
733   _http_perform_write(ctx, &mask);
734   if(ctx->res.complete == noit_true &&
735      ctx->conn.e &&
736      ctx->conn.needs_close == noit_true) {
737    abort_drive:
738     noit_http_log_request(ctx);
739     if(ctx->conn.e) {
740       ctx->conn.e->opset->close(ctx->conn.e->fd, &mask, ctx->conn.e);
741       ctx->conn.e = NULL;
742     }
743     goto release;
744   }
745   if(ctx->res.complete == noit_true) {
746     noit_http_log_request(ctx);
747     noit_http_request_release(ctx);
748     noit_http_response_release(ctx);
749   }
750   if(ctx->req.complete == noit_false) goto next_req;
751   if(ctx->conn.e) {
752     noitL(http_debug, " <- noit_http_session_drive(%d) [%x]\n", e->fd, mask|rv);
753     return mask | rv;
754   }
755   noitL(http_debug, " <- noit_http_session_drive(%d) [%x]\n", e->fd, 0);
756   goto abort_drive;
757
758  release:
759   *done = 1;
760   /* We're about to release, unhook us from the acceptor_closure so we
761    * don't get double freed */
762   if(ctx->ac->service_ctx == ctx) ctx->ac->service_ctx = NULL;
763   noit_http_ctx_session_release(ctx);
764   noitL(http_debug, " <- noit_http_session_drive(%d) [%x]\n", e->fd, 0);
765   return 0;
766 }
767
768 noit_http_session_ctx *
769 noit_http_session_ctx_new(noit_http_dispatch_func f, void *c, eventer_t e,
770                           acceptor_closure_t *ac) {
771   noit_http_session_ctx *ctx;
772   ctx = calloc(1, sizeof(*ctx));
773   ctx->ref_cnt = 1;
774   ctx->req.complete = noit_false;
775   ctx->conn.e = e;
776   ctx->dispatcher = f;
777   ctx->dispatcher_closure = c;
778   ctx->ac = ac;
779   return ctx;
780 }
781
782 noit_boolean
783 noit_http_response_status_set(noit_http_session_ctx *ctx,
784                               int code, const char *reason) {
785   if(ctx->res.output_started == noit_true) return noit_false;
786   ctx->res.protocol = ctx->req.protocol;
787   if(code < 100 || code > 999) return noit_false;
788   ctx->res.status_code = code;
789   if(ctx->res.status_reason) free(ctx->res.status_reason);
790   ctx->res.status_reason = strdup(reason);
791   return noit_true;
792 }
793 noit_boolean
794 noit_http_response_header_set(noit_http_session_ctx *ctx,
795                               const char *name, const char *value) {
796   if(ctx->res.output_started == noit_true) return noit_false;
797   noit_hash_replace(&ctx->res.headers, strdup(name), strlen(name),
798                     strdup(value), free, free);
799   return noit_true;
800 }
801 noit_boolean
802 noit_http_response_option_set(noit_http_session_ctx *ctx, u_int32_t opt) {
803   if(ctx->res.output_started == noit_true) return noit_false;
804   /* transfer and content encodings only allowed in HTTP/1.1 */
805   if(ctx->res.protocol != NOIT_HTTP11 &&
806      (opt & NOIT_HTTP_CHUNKED))
807     return noit_false;
808   if(ctx->res.protocol != NOIT_HTTP11 &&
809      (opt & (NOIT_HTTP_GZIP | NOIT_HTTP_DEFLATE)))
810     return noit_false;
811   if(((ctx->res.output_options | opt) &
812       (NOIT_HTTP_GZIP | NOIT_HTTP_DEFLATE)) ==
813         (NOIT_HTTP_GZIP | NOIT_HTTP_DEFLATE))
814     return noit_false;
815
816   /* Check out "accept" set */
817   if(!(opt & ctx->req.opts)) return noit_false;
818
819   ctx->res.output_options |= opt;
820   if(ctx->res.output_options & NOIT_HTTP_CHUNKED)
821     CTX_ADD_HEADER("Transfer-Encoding", "chunked");
822   if(ctx->res.output_options & (NOIT_HTTP_GZIP | NOIT_HTTP_DEFLATE)) {
823     CTX_ADD_HEADER("Vary", "Accept-Encoding");
824     if(ctx->res.output_options & NOIT_HTTP_GZIP)
825       CTX_ADD_HEADER("Content-Encoding", "gzip");
826     else if(ctx->res.output_options & NOIT_HTTP_DEFLATE)
827       CTX_ADD_HEADER("Content-Encoding", "deflate");
828   }
829   if(ctx->res.output_options & NOIT_HTTP_CLOSE) {
830     CTX_ADD_HEADER("Connection", "close");
831     ctx->conn.needs_close = noit_true;
832   }
833   return noit_true;
834 }
835 noit_boolean
836 noit_http_response_append(noit_http_session_ctx *ctx,
837                           const void *b, size_t l) {
838   struct bchain *o;
839   int boff = 0;
840   if(ctx->res.closed == noit_true) return noit_false;
841   if(ctx->res.output_started == noit_true &&
842      !(ctx->res.output_options & (NOIT_HTTP_CLOSE | NOIT_HTTP_CHUNKED)))
843     return noit_false;
844   if(!ctx->res.output)
845     assert(ctx->res.output = ALLOC_BCHAIN(DEFAULT_BCHAINSIZE));
846   o = ctx->res.output;
847   while(o->next) o = o->next;
848   while(l > 0) {
849     if(o->allocd == o->start + o->size) {
850       /* Filled up, need another */
851       o->next = ALLOC_BCHAIN(DEFAULT_BCHAINSIZE);
852       o->next->prev = o->next;
853       o = o->next;
854     }
855     if(o->allocd > o->start + o->size) {
856       int tocopy = MIN(l, o->allocd - o->start - o->size);
857       memcpy(o->buff + o->start + o->size, (const char *)b + boff, tocopy);
858       o->size += tocopy;
859       boff += tocopy;
860       l -= tocopy;
861     }
862   }
863   return noit_true;
864 }
865 noit_boolean
866 noit_http_response_append_bchain(noit_http_session_ctx *ctx,
867                                  struct bchain *b) {
868   struct bchain *o;
869   if(ctx->res.closed == noit_true) return noit_false;
870   if(ctx->res.output_started == noit_true &&
871      !(ctx->res.output_options & (NOIT_HTTP_CHUNKED | NOIT_HTTP_CLOSE)))
872     return noit_false;
873   if(!ctx->res.output)
874     ctx->res.output = b;
875   else {
876     o = ctx->res.output;
877     while(o->next) o = o->next;
878     o->next = b;
879     b->prev = o;
880   }
881   return noit_true;
882 }
883 static int
884 _http_construct_leader(noit_http_session_ctx *ctx) {
885   int len = 0, tlen;
886   struct bchain *b;
887   const char *protocol_str;
888   const char *key, *value;
889   int klen;
890   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
891
892   assert(!ctx->res.leader);
893   ctx->res.leader = b = ALLOC_BCHAIN(DEFAULT_BCHAINSIZE);
894
895   protocol_str = ctx->res.protocol == NOIT_HTTP11 ?
896                    "HTTP/1.1" :
897                    (ctx->res.protocol == NOIT_HTTP10 ?
898                      "HTTP/1.0" :
899                      "HTTP/0.9");
900   tlen = snprintf(b->buff, b->allocd, "%s %03d %s\r\n",
901                   protocol_str, ctx->res.status_code, ctx->res.status_reason);
902   if(tlen < 0) return -1;
903   len = b->size = tlen;
904
905 #define CTX_LEADER_APPEND(s, slen) do { \
906   if(b->size + slen > DEFAULT_BCHAINSIZE) { \
907     b->next = ALLOC_BCHAIN(DEFAULT_BCHAINSIZE); \
908     assert(b->next); \
909     b->next->prev = b; \
910     b = b->next; \
911   } \
912   assert(DEFAULT_BCHAINSIZE >= b->size + slen); \
913   memcpy(b->buff + b->start + b->size, s, slen); \
914   b->size += slen; \
915 } while(0)
916   while(noit_hash_next_str(&ctx->res.headers, &iter,
917                            &key, &klen, &value)) {
918     int vlen = strlen(value);
919     CTX_LEADER_APPEND(key, klen);
920     CTX_LEADER_APPEND(": ", 2);
921     CTX_LEADER_APPEND(value, vlen);
922     CTX_LEADER_APPEND("\r\n", 2);
923   }
924   CTX_LEADER_APPEND("\r\n", 2);
925   return len;
926 }
927 static int memgzip2(Bytef *dest, uLongf *destLen,
928                     const Bytef *source, uLong sourceLen, int level) {
929   z_stream stream;
930   int err;
931
932   memset(&stream, 0, sizeof(stream));
933   stream.next_in = (Bytef*)source;
934   stream.avail_in = (uInt)sourceLen;
935   stream.next_out = dest;
936   stream.avail_out = (uInt)*destLen;
937   if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
938
939   err = deflateInit2(&stream, level, Z_DEFLATED, 15+16, 8,
940                      Z_DEFAULT_STRATEGY);
941   if (err != Z_OK) return err;
942
943   err = deflate(&stream, Z_FINISH);
944   if (err != Z_STREAM_END) {
945     deflateEnd(&stream);
946     return err == Z_OK ? Z_BUF_ERROR : err;
947   }
948   *destLen = stream.total_out;
949
950   err = deflateEnd(&stream);
951   return err;
952 }
953 static noit_boolean
954 _http_encode_chain(struct bchain *out, struct bchain *in, int opts) {
955   /* implement gzip and deflate! */
956   if(opts & NOIT_HTTP_GZIP) {
957     uLongf olen;
958     olen = out->allocd - out->start;
959     if(Z_OK != memgzip2((Bytef *)(out->buff + out->start), &olen,
960                         (Bytef *)(in->buff + in->start), (uLong)in->size,
961                         9)) {
962       noitL(noit_error, "zlib compress2 error\n");
963       return noit_false;
964     }
965     out->size += olen;
966   }
967   else if(opts & NOIT_HTTP_DEFLATE) {
968     uLongf olen;
969     olen = out->allocd - out->start;
970     if(Z_OK != compress2((Bytef *)(out->buff + out->start), &olen,
971                          (Bytef *)(in->buff + in->start), (uLong)in->size,
972                          9)) {
973       noitL(noit_error, "zlib compress2 error\n");
974       return noit_false;
975     }
976     out->size += olen;
977   }
978   else {
979     if(in->size > out->allocd - out->start) return noit_false;
980     memcpy(out->buff + out->start, in->buff + in->start, in->size);
981     out->size += in->size;
982   }
983   return noit_true;
984 }
985 struct bchain *
986 noit_http_process_output_bchain(noit_http_session_ctx *ctx,
987                                 struct bchain *in) {
988   struct bchain *out;
989   int ilen, maxlen = in->size, hexlen;
990   int opts = ctx->res.output_options;
991
992   /* a chunked header looks like: hex*\r\ndata\r\n */
993   /* let's assume that content never gets "larger" */
994   if(opts & NOIT_HTTP_GZIP) maxlen = deflateBound(NULL, in->size);
995   else if(opts & NOIT_HTTP_DEFLATE) maxlen = compressBound(in->size);
996
997   /* So, the link size is the len(data) + 4 + ceil(log(len(data))/log(16)) */
998   ilen = maxlen;
999   hexlen = 0;
1000   while(ilen) { ilen >>= 4; hexlen++; }
1001   if(hexlen == 0) hexlen = 1;
1002
1003   out = ALLOC_BCHAIN(hexlen + 4 + maxlen);
1004   /* if we're chunked, let's give outselved hexlen + 2 prefix space */
1005   if(opts & NOIT_HTTP_CHUNKED) out->start = hexlen + 2;
1006   if(_http_encode_chain(out, in, opts) == noit_false) {
1007     free(out);
1008     return NULL;
1009   }
1010   /* Too long! Out "larger" assumption is bad */
1011   if(opts & NOIT_HTTP_CHUNKED) {
1012     ilen = out->size;
1013     assert(out->start+out->size+2 <= out->allocd);
1014     out->buff[out->start + out->size++] = '\r';
1015     out->buff[out->start + out->size++] = '\n';
1016     out->start = 0;
1017     /* terminate */
1018     out->size += 2;
1019     out->buff[hexlen] = '\r';
1020     out->buff[hexlen+1] = '\n';
1021     /* backfill */
1022     out->size += hexlen;
1023     while(hexlen > 0) {
1024       out->buff[hexlen - 1] = _hexchars[ilen & 0xf];
1025       ilen >>= 4;
1026       hexlen--;
1027     }
1028     while(out->buff[out->start] == '0') {
1029       out->start++;
1030       out->size--;
1031     }
1032   }
1033   return out;
1034 }
1035 noit_boolean
1036 noit_http_response_flush(noit_http_session_ctx *ctx, noit_boolean final) {
1037   struct bchain *o, *r;
1038   int mask, rv;
1039
1040   if(ctx->res.closed == noit_true) return noit_false;
1041   if(ctx->res.output_started == noit_false) {
1042     _http_construct_leader(ctx);
1043     ctx->res.output_started = noit_true;
1044   }
1045   /* encode output to output_raw */
1046   r = ctx->res.output_raw;
1047   while(r && r->next) r = r->next;
1048   /* r is the last raw output link */
1049   o = ctx->res.output;
1050   /* o is the first output link to process */
1051   while(o) {
1052     struct bchain *tofree, *n;
1053     n = noit_http_process_output_bchain(ctx, o);
1054     if(!n) {
1055       /* Bad, response stops here! */
1056       noitL(noit_error, "noit_http_process_output_bchain: NULL\n");
1057       while(o) { tofree = o; o = o->next; free(tofree); }
1058       final = noit_true;
1059       break;
1060     }
1061     if(r) {
1062       r->next = n;
1063       n->prev = r;
1064       r = n;
1065     }
1066     else {
1067       r = ctx->res.output_raw = n;
1068     }
1069     tofree = o; o = o->next; FREE_BCHAIN(tofree); /* advance and free */
1070   }
1071   ctx->res.output = NULL;
1072   if(final) {
1073     struct bchain *n;
1074     ctx->res.closed = noit_true;
1075     if(ctx->res.output_options & NOIT_HTTP_CHUNKED)
1076       n = bchain_from_data("0\r\n\r\n", 5);
1077     else
1078       n = bchain_from_data("\r\n", 2);
1079     if(r) {
1080       r->next = n;
1081       n->prev = r;
1082     }
1083     else {
1084       ctx->res.output_raw = n;
1085     }
1086   }
1087
1088   rv = _http_perform_write(ctx, &mask);
1089   if(ctx->conn.e) {
1090     eventer_update(ctx->conn.e, mask);
1091   }
1092   if(rv < 0) return noit_false;
1093   /* If the write fails completely, the event will not be closed,
1094    * the following should not trigger the false case.
1095    */
1096   return ctx->conn.e ? noit_true : noit_false;
1097 }
1098
1099 noit_boolean
1100 noit_http_response_end(noit_http_session_ctx *ctx) {
1101   if(!noit_http_response_flush(ctx, noit_true)) return noit_false;
1102   return noit_true;
1103 }
1104
1105
1106 /* Helper functions */
1107
1108 static int
1109 noit_http_write_xml(void *vctx, const char *buffer, int len) {
1110   if(noit_http_response_append((noit_http_session_ctx *)vctx, buffer, len))
1111     return len;
1112   return -1;
1113 }
1114 static int
1115 noit_http_close_xml(void *vctx) {
1116   noit_http_response_end((noit_http_session_ctx *)vctx);
1117   return 0;
1118 }
1119 void
1120 noit_http_response_xml(noit_http_session_ctx *ctx, xmlDocPtr doc) {
1121   xmlOutputBufferPtr out;
1122   xmlCharEncodingHandlerPtr enc;
1123   enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8);
1124   out = xmlOutputBufferCreateIO(noit_http_write_xml,
1125                                 noit_http_close_xml,
1126                                 ctx, enc);
1127   xmlSaveFormatFileTo(out, doc, "utf8", 1);
1128 }
1129
1130 void
1131 noit_http_init() {
1132   http_debug = noit_log_stream_find("debug/http");
1133   http_access = noit_log_stream_find("http/access");
1134   http_io = noit_log_stream_find("http/io");
1135 }
Note: See TracBrowser for help on using the browser.