root/src/noit_http.h

Revision 84b15c2c96cf473ee3add485c0e1ac3ee2be9906, 7.1 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

implement and prefer gzip

  • 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 typedef enum {
56   BCHAIN_INLINE = 0,
57   BCHAIN_MMAP
58 } bchain_type_t;
59
60 struct bchain;
61
62 #define DEFAULT_MAXWRITE 1<<14 /* 32k */
63 #define DEFAULT_BCHAINSIZE ((1 << 15)-(offsetof(struct bchain, _buff)))
64 /* 64k - delta */
65 #define DEFAULT_BCHAINMINREAD (DEFAULT_BCHAINSIZE/4)
66 #define BCHAIN_SPACE(a) ((a)->allocd - (a)->size - (a)->start)
67
68 struct noit_http_connection;
69 typedef struct noit_http_connection noit_http_connection;
70 struct noit_http_request;
71 typedef struct noit_http_request noit_http_request;
72 struct noit_http_response;
73 typedef struct noit_http_response noit_http_response;
74
75 struct bchain {
76   bchain_type_t type;
77   struct bchain *next, *prev;
78   size_t start; /* where data starts (buff + start) */
79   size_t size;  /* data length (past start) */
80   size_t allocd;/* total allocation */
81   char *buff;
82   char _buff[1]; /* over allocate as needed */
83 };
84
85 struct noit_http_session_ctx;
86 typedef struct noit_http_session_ctx noit_http_session_ctx;
87 typedef int (*noit_http_dispatch_func) (noit_http_session_ctx *);
88
89 API_EXPORT(noit_http_session_ctx *)
90   noit_http_session_ctx_new(noit_http_dispatch_func, void *, eventer_t,
91                             acceptor_closure_t *);
92 API_EXPORT(void)
93   noit_http_ctx_session_release(noit_http_session_ctx *ctx);
94 API_EXPORT(uint32_t)
95   noit_http_session_ref_cnt(noit_http_session_ctx *);
96 API_EXPORT(uint32_t)
97   noit_http_session_ref_dec(noit_http_session_ctx *);
98 API_EXPORT(uint32_t)
99   noit_http_session_ref_inc(noit_http_session_ctx *);
100 API_EXPORT(void)
101   noit_http_session_trigger(noit_http_session_ctx *, int state);
102
103 API_EXPORT(noit_http_request *)
104   noit_http_session_request(noit_http_session_ctx *);
105 API_EXPORT(noit_http_response *)
106   noit_http_session_response(noit_http_session_ctx *);
107 API_EXPORT(noit_http_connection *)
108   noit_http_session_connection(noit_http_session_ctx *);
109
110 API_EXPORT(void *)
111   noit_http_session_dispatcher_closure(noit_http_session_ctx *);
112 API_EXPORT(void)
113   noit_http_session_set_dispatcher(noit_http_session_ctx *,
114                                    int (*)(noit_http_session_ctx *), void *);
115
116 API_EXPORT(eventer_t)
117   noit_http_connection_event(noit_http_connection *);
118
119 API_EXPORT(const char *)
120   noit_http_request_uri_str(noit_http_request *);
121 API_EXPORT(const char *)
122   noit_http_request_method_str(noit_http_request *);
123 API_EXPORT(const char *)
124   noit_http_request_protocol_str(noit_http_request *);
125 API_EXPORT(size_t)
126   noit_http_request_content_length(noit_http_request *);
127 API_EXPORT(const char *)
128   noit_http_request_querystring(noit_http_request *, const char *);
129 API_EXPORT(noit_hash_table *)
130   noit_http_request_querystring_table(noit_http_request *);
131 API_EXPORT(noit_hash_table *)
132   noit_http_request_headers_table(noit_http_request *);
133
134 API_EXPORT(noit_boolean)
135   noit_http_response_closed(noit_http_response *);
136
137 API_EXPORT(void)
138   noit_http_ctx_acceptor_free(void *); /* just calls noit_http_session_ctx_release */
139
140 API_EXPORT(void)
141   noit_http_process_querystring(noit_http_request *);
142
143 API_EXPORT(int)
144   noit_http_session_drive(eventer_t, int, void *, struct timeval *, int *done);
145
146 API_EXPORT(noit_boolean)
147   noit_http_session_prime_input(noit_http_session_ctx *, const void *, size_t);
148 API_EXPORT(int)
149   noit_http_session_req_consume(noit_http_session_ctx *ctx,
150                                 void *buf, size_t len, int *mask);
151 API_EXPORT(noit_boolean)
152   noit_http_response_status_set(noit_http_session_ctx *, int, const char *);
153 API_EXPORT(noit_boolean)
154   noit_http_response_header_set(noit_http_session_ctx *,
155                                 const char *, const char *);
156 API_EXPORT(noit_boolean)
157   noit_http_response_option_set(noit_http_session_ctx *, u_int32_t);
158 API_EXPORT(noit_boolean)
159   noit_http_response_append(noit_http_session_ctx *, const void *, size_t);
160 API_EXPORT(noit_boolean)
161   noit_http_response_append_bchain(noit_http_session_ctx *, struct bchain *);
162 API_EXPORT(noit_boolean)
163   noit_http_response_append_mmap(noit_http_session_ctx *,
164                                  int fd, size_t len, int flags, off_t offset);
165 API_EXPORT(noit_boolean)
166   noit_http_response_flush(noit_http_session_ctx *, noit_boolean);
167 API_EXPORT(noit_boolean) noit_http_response_end(noit_http_session_ctx *);
168
169 #define noit_http_response_server_error(ctx, type) \
170   noit_http_response_standard(ctx, 500, "ERROR", type)
171 #define noit_http_response_ok(ctx, type) \
172   noit_http_response_standard(ctx, 200, "OK", type)
173 #define noit_http_response_not_found(ctx, type) \
174   noit_http_response_standard(ctx, 404, "NOT FOUND", type)
175 #define noit_http_response_denied(ctx, type) \
176   noit_http_response_standard(ctx, 403, "DENIED", type)
177
178 #define noit_http_response_standard(ctx, code, name, type) do { \
179   noit_http_response_status_set(ctx, code, name); \
180   noit_http_response_header_set(ctx, "Content-Type", type); \
181   if(noit_http_response_option_set(ctx, NOIT_HTTP_CHUNKED) == noit_false) \
182     noit_http_response_option_set(ctx, NOIT_HTTP_CLOSE); \
183   noit_http_response_option_set(ctx, NOIT_HTTP_GZIP); \
184 } while(0)
185
186 API_EXPORT(void)
187   noit_http_response_xml(noit_http_session_ctx *, xmlDocPtr);
188
189 API_EXPORT(void)
190   noit_http_init();
191
192 #endif
Note: See TracBrowser for help on using the browser.