root/src/noit_http.h

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

simple static handler, closes #235

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