root/src/noit_http.c

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

refs #284

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