root/src/noit_http.h

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

always process the querystring after finalizing the request, fixes #274

  • 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 #ifndef _NOIT_HTTP_H
34 #define _NOIT_HTTP_H
35
36 #include "noit_defines.h"
37 #include <libxml/tree.h>
38 #include "eventer/eventer.h"
39 #include "utils/noit_hash.h"
40 #include "utils/noit_atomic.h"
41 #include "noit_listener.h"
42
43 typedef enum {
44   NOIT_HTTP_OTHER, NOIT_HTTP_GET, NOIT_HTTP_HEAD, NOIT_HTTP_POST
45 } noit_http_method;
46 typedef enum {
47   NOIT_HTTP09, NOIT_HTTP10, NOIT_HTTP11
48 } noit_http_protocol;
49
50 #define NOIT_HTTP_CHUNKED 0x0001
51 #define NOIT_HTTP_CLOSE   0x0002
52 #define NOIT_HTTP_GZIP    0x0010
53 #define NOIT_HTTP_DEFLATE 0x0020
54
55 #define DEFAULT_BCHAINSIZE ((1 << 15)-(3*sizeof(size_t))-(2*sizeof(void *)))
56 /* 64k - delta */
57 #define DEFAULT_BCHAINMINREAD (DEFAULT_BCHAINSIZE/4)
58
59 struct bchain {
60   struct bchain *next, *prev;
61   size_t start; /* where data starts (buff + start) */
62   size_t size;  /* data length (past start) */
63   size_t allocd;/* total allocation */
64   char buff[1]; /* over allocate as needed */
65 };
66
67 #define BCHAIN_SPACE(a) ((a)->allocd - (a)->size - (a)->start)
68
69 API_EXPORT(struct bchain *) bchain_alloc(size_t size);
70 API_EXPORT(void) bchain_free(struct bchain *);
71
72 typedef struct {
73   eventer_t e;
74   int needs_close;
75 } noit_http_connection;
76
77 typedef struct {
78   struct bchain *first_input; /* The start of the input chain */
79   struct bchain *last_input;  /* The end of the input chain */
80   struct bchain *current_input;  /* The point of the input where we */
81   size_t         current_offset; /* analyzing. */
82
83   enum { NOIT_HTTP_REQ_HEADERS = 0,
84          NOIT_HTTP_REQ_EXPECT,
85          NOIT_HTTP_REQ_PAYLOAD } state;
86   struct bchain *current_request_chain;
87   noit_boolean has_payload;
88   int64_t content_length;
89   int64_t content_length_read;
90   char *method_str;
91   char *uri_str;
92   char *protocol_str;
93   noit_hash_table querystring;
94   u_int32_t opts;
95   noit_http_method method;
96   noit_http_protocol protocol;
97   noit_hash_table headers;
98   noit_boolean complete;
99   struct timeval start_time;
100   char *orig_qs;
101 } noit_http_request;
102
103 typedef struct {
104   noit_http_protocol protocol;
105   int status_code;
106   char *status_reason;
107
108   noit_hash_table headers;
109   struct bchain *leader; /* serialization of status line and headers */
110
111   u_int32_t output_options;
112   struct bchain *output;       /* data is pushed in here */
113   struct bchain *output_raw;   /* internally transcoded here for output */
114   size_t output_raw_offset;    /* tracks our offset */
115   noit_boolean output_started; /* locks the options and leader */
116                                /*   and possibly output. */
117   noit_boolean closed;         /* set by _end() */
118   noit_boolean complete;       /* complete, drained and disposable */
119   size_t bytes_written;        /* tracks total bytes written */
120 } noit_http_response;
121
122 struct noit_http_session_ctx;
123 typedef int (*noit_http_dispatch_func) (struct noit_http_session_ctx *);
124
125 typedef struct noit_http_session_ctx {
126   noit_atomic32_t ref_cnt;
127   int64_t drainage;
128   noit_http_connection conn;
129   noit_http_request req;
130   noit_http_response res;
131   noit_http_dispatch_func dispatcher;
132   void *dispatcher_closure;
133   eventer_func_t drive;
134   acceptor_closure_t *ac;
135 } noit_http_session_ctx;
136
137 API_EXPORT(noit_http_session_ctx *)
138   noit_http_session_ctx_new(noit_http_dispatch_func, void *, eventer_t,
139                             acceptor_closure_t *);
140 API_EXPORT(void)
141   noit_http_session_ctx_release(noit_http_session_ctx *);
142 API_EXPORT(void)
143   noit_http_process_querystring(noit_http_request *);
144
145 API_EXPORT(int)
146   noit_http_session_drive(eventer_t, int, void *, struct timeval *);
147
148 API_EXPORT(noit_boolean)
149   noit_http_session_prime_input(noit_http_session_ctx *, const void *, size_t);
150 API_EXPORT(int)
151   noit_http_session_req_consume(noit_http_session_ctx *ctx,
152                                 void *buf, size_t len, int *mask);
153 API_EXPORT(noit_boolean)
154   noit_http_response_status_set(noit_http_session_ctx *, int, const char *);
155 API_EXPORT(noit_boolean)
156   noit_http_response_header_set(noit_http_session_ctx *,
157                                 const char *, const char *);
158 API_EXPORT(noit_boolean)
159   noit_http_response_option_set(noit_http_session_ctx *, u_int32_t);
160 API_EXPORT(noit_boolean)
161   noit_http_response_append(noit_http_session_ctx *, const void *, size_t);
162 API_EXPORT(noit_boolean)
163   noit_http_response_append_bchain(noit_http_session_ctx *, struct bchain *);
164 API_EXPORT(noit_boolean)
165   noit_http_response_flush(noit_http_session_ctx *, noit_boolean);
166 API_EXPORT(noit_boolean) noit_http_response_end(noit_http_session_ctx *);
167
168 #define noit_http_response_server_error(ctx, type) \
169   noit_http_response_standard(ctx, 500, "ERROR", type)
170 #define noit_http_response_ok(ctx, type) \
171   noit_http_response_standard(ctx, 200, "OK", type)
172 #define noit_http_response_not_found(ctx, type) \
173   noit_http_response_standard(ctx, 404, "NOT FOUND", type)
174 #define noit_http_response_denied(ctx, type) \
175   noit_http_response_standard(ctx, 403, "DENIED", type)
176
177 #define noit_http_response_standard(ctx, code, name, type) do { \
178   noit_http_response_status_set(ctx, code, name); \
179   noit_http_response_header_set(ctx, "Content-Type", type); \
180   if(noit_http_response_option_set(ctx, NOIT_HTTP_CHUNKED) == noit_false) \
181     noit_http_response_option_set(ctx, NOIT_HTTP_CLOSE); \
182   noit_http_response_option_set(ctx, NOIT_HTTP_DEFLATE); \
183 } while(0)
184
185 API_EXPORT(void)
186   noit_http_response_xml(noit_http_session_ctx *, xmlDocPtr);
187
188 API_EXPORT(void)
189   noit_http_init();
190
191 #endif
Note: See TracBrowser for help on using the browser.