xref: /unit/src/nxt_http.h (revision 1855:a946d8cd7f8c)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_HTTP_H_INCLUDED_
8 #define _NXT_HTTP_H_INCLUDED_
9 
10 #include <nxt_regex.h>
11 
12 
13 typedef enum {
14     NXT_HTTP_UNSET = -1,
15     NXT_HTTP_INVALID = 0,
16 
17     NXT_HTTP_CONTINUE = 100,
18     NXT_HTTP_SWITCHING_PROTOCOLS = 101,
19 
20     NXT_HTTP_OK = 200,
21     NXT_HTTP_NO_CONTENT = 204,
22 
23     NXT_HTTP_MULTIPLE_CHOICES = 300,
24     NXT_HTTP_MOVED_PERMANENTLY = 301,
25     NXT_HTTP_FOUND = 302,
26     NXT_HTTP_SEE_OTHER = 303,
27     NXT_HTTP_NOT_MODIFIED = 304,
28     NXT_HTTP_TEMPORARY_REDIRECT = 307,
29     NXT_HTTP_PERMANENT_REDIRECT = 308,
30 
31     NXT_HTTP_BAD_REQUEST = 400,
32     NXT_HTTP_FORBIDDEN = 403,
33     NXT_HTTP_NOT_FOUND = 404,
34     NXT_HTTP_METHOD_NOT_ALLOWED = 405,
35     NXT_HTTP_REQUEST_TIMEOUT = 408,
36     NXT_HTTP_LENGTH_REQUIRED = 411,
37     NXT_HTTP_PAYLOAD_TOO_LARGE = 413,
38     NXT_HTTP_URI_TOO_LONG = 414,
39     NXT_HTTP_UPGRADE_REQUIRED = 426,
40     NXT_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
41 
42     NXT_HTTP_TO_HTTPS = 497,
43 
44     NXT_HTTP_INTERNAL_SERVER_ERROR = 500,
45     NXT_HTTP_NOT_IMPLEMENTED = 501,
46     NXT_HTTP_BAD_GATEWAY = 502,
47     NXT_HTTP_SERVICE_UNAVAILABLE = 503,
48     NXT_HTTP_GATEWAY_TIMEOUT = 504,
49     NXT_HTTP_VERSION_NOT_SUPPORTED = 505,
50     NXT_HTTP_SERVER_ERROR_MAX = 599,
51 
52     NXT_HTTP_STATUS_MAX = 999,
53 } nxt_http_status_t;
54 
55 
56 typedef enum {
57     NXT_HTTP_TE_NONE = 0,
58     NXT_HTTP_TE_CHUNKED = 1,
59     NXT_HTTP_TE_UNSUPPORTED = 2,
60 } nxt_http_te_t;
61 
62 
63 typedef enum {
64     NXT_HTTP_PROTO_H1 = 0,
65     NXT_HTTP_PROTO_H2,
66     NXT_HTTP_PROTO_DEVNULL,
67 } nxt_http_protocol_t;
68 
69 
70 typedef struct {
71     nxt_work_handler_t              ready_handler;
72     nxt_work_handler_t              error_handler;
73 } nxt_http_request_state_t;
74 
75 
76 typedef struct nxt_h1proto_s        nxt_h1proto_t;
77 
78 struct nxt_h1p_websocket_timer_s {
79     nxt_timer_t                     timer;
80     nxt_h1proto_t                   *h1p;
81     nxt_msec_t                      keepalive_interval;
82 };
83 
84 
85 typedef union {
86     void                            *any;
87     nxt_h1proto_t                   *h1;
88 } nxt_http_proto_t;
89 
90 
91 #define nxt_http_field_name_set(_field, _name)                                \
92     do {                                                                      \
93          (_field)->name_length = nxt_length(_name);                           \
94          (_field)->name = (u_char *) _name;                                   \
95     } while (0)
96 
97 
98 #define nxt_http_field_set(_field, _name, _value)                             \
99     do {                                                                      \
100          (_field)->name_length = nxt_length(_name);                           \
101          (_field)->value_length = nxt_length(_value);                         \
102          (_field)->name = (u_char *) _name;                                   \
103          (_field)->value = (u_char *) _value;                                 \
104     } while (0)
105 
106 
107 typedef struct {
108     nxt_list_t                      *fields;
109     nxt_http_field_t                *date;
110     nxt_http_field_t                *content_type;
111     nxt_http_field_t                *content_length;
112     nxt_off_t                       content_length_n;
113 } nxt_http_response_t;
114 
115 
116 typedef struct nxt_upstream_server_s  nxt_upstream_server_t;
117 
118 typedef struct {
119     nxt_http_proto_t                proto;
120     nxt_http_request_t              *request;
121     nxt_upstream_server_t           *server;
122     nxt_list_t                      *fields;
123     nxt_buf_t                       *body;
124 
125     nxt_http_status_t               status:16;
126     nxt_http_protocol_t             protocol:8;       /* 2 bits */
127     uint8_t                         header_received;  /* 1 bit  */
128     uint8_t                         closed;           /* 1 bit  */
129 } nxt_http_peer_t;
130 
131 
132 struct nxt_http_request_s {
133     nxt_http_proto_t                proto;
134     nxt_socket_conf_joint_t         *conf;
135 
136     nxt_mp_t                        *mem_pool;
137 
138     nxt_buf_t                       *body;
139     nxt_buf_t                       *ws_frame;
140     nxt_buf_t                       *out;
141     const nxt_http_request_state_t  *state;
142 
143     nxt_str_t                       host;
144     nxt_str_t                       server_name;
145     nxt_str_t                       target;
146     nxt_str_t                       version;
147     nxt_str_t                       *method;
148     nxt_str_t                       *path;
149     nxt_str_t                       *args;
150 
151     nxt_array_t                     *arguments;  /* of nxt_http_name_value_t */
152     nxt_array_t                     *cookies;    /* of nxt_http_name_value_t */
153     nxt_list_t                      *fields;
154     nxt_http_field_t                *content_type;
155     nxt_http_field_t                *content_length;
156     nxt_http_field_t                *cookie;
157     nxt_http_field_t                *referer;
158     nxt_http_field_t                *user_agent;
159     nxt_http_field_t                *authorization;
160     nxt_off_t                       content_length_n;
161 
162     nxt_sockaddr_t                  *remote;
163     nxt_sockaddr_t                  *local;
164     void                            *tls;
165     nxt_task_t                      task;
166 
167     nxt_timer_t                     timer;
168     void                            *timer_data;
169 
170     nxt_var_query_t                 *var_query;
171 
172     void                            *req_rpc_data;
173 
174 #if (NXT_HAVE_REGEX)
175     nxt_regex_match_t               *regex_match;
176 #endif
177 
178     nxt_http_peer_t                 *peer;
179     nxt_buf_t                       *last;
180 
181     nxt_queue_link_t                app_link;   /* nxt_app_t.ack_waiting_req */
182     nxt_event_engine_t              *engine;
183     nxt_work_t                      err_work;
184 
185     nxt_http_response_t             resp;
186 
187     nxt_http_status_t               status:16;
188 
189     uint8_t                         pass_count;   /* 8 bits */
190     uint8_t                         app_target;
191     nxt_http_protocol_t             protocol:8;   /* 2 bits */
192     uint8_t                         logged;       /* 1 bit  */
193     uint8_t                         header_sent;  /* 1 bit  */
194     uint8_t                         inconsistent; /* 1 bit  */
195     uint8_t                         error;        /* 1 bit  */
196     uint8_t                         websocket_handshake;  /* 1 bit */
197 };
198 
199 
200 typedef struct nxt_http_route_s     nxt_http_route_t;
201 
202 
203 struct nxt_http_action_s {
204     nxt_http_action_t               *(*handler)(nxt_task_t *task,
205                                         nxt_http_request_t *r,
206                                         nxt_http_action_t *action);
207     union {
208         nxt_http_route_t            *route;
209         nxt_upstream_t              *upstream;
210         uint32_t                    upstream_number;
211         nxt_http_status_t           return_code;
212         nxt_var_t                   *var;
213 
214         struct {
215             nxt_app_t               *application;
216             nxt_int_t               target;
217         } app;
218 
219         struct {
220             nxt_str_t               chroot;
221             nxt_uint_t              resolve;
222             nxt_http_action_t       *fallback;
223         } share;
224     } u;
225 
226     nxt_str_t                       name;
227 };
228 
229 
230 typedef struct {
231     void (*body_read)(nxt_task_t *task, nxt_http_request_t *r);
232     void (*local_addr)(nxt_task_t *task, nxt_http_request_t *r);
233     void (*header_send)(nxt_task_t *task, nxt_http_request_t *r,
234          nxt_work_handler_t body_handler, void *data);
235     void (*send)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out);
236     nxt_off_t (*body_bytes_sent)(nxt_task_t *task, nxt_http_proto_t proto);
237     void (*discard)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *last);
238     void (*close)(nxt_task_t *task, nxt_http_proto_t proto,
239         nxt_socket_conf_joint_t *joint);
240 
241     void (*peer_connect)(nxt_task_t *task, nxt_http_peer_t *peer);
242     void (*peer_header_send)(nxt_task_t *task, nxt_http_peer_t *peer);
243     void (*peer_header_read)(nxt_task_t *task, nxt_http_peer_t *peer);
244     void (*peer_read)(nxt_task_t *task, nxt_http_peer_t *peer);
245     void (*peer_close)(nxt_task_t *task, nxt_http_peer_t *peer);
246 
247     void (*ws_frame_start)(nxt_task_t *task, nxt_http_request_t *r,
248         nxt_buf_t *ws_frame);
249 } nxt_http_proto_table_t;
250 
251 
252 #define NXT_HTTP_DATE_LEN  nxt_length("Wed, 31 Dec 1986 16:40:00 GMT")
253 
254 nxt_inline u_char *
255 nxt_http_date(u_char *buf, struct tm *tm)
256 {
257     static const char  *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
258                                    "Sat" };
259 
260     static const char  *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
261                                     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
262 
263     return nxt_sprintf(buf, buf + NXT_HTTP_DATE_LEN,
264                        "%s, %02d %s %4d %02d:%02d:%02d GMT",
265                        week[tm->tm_wday], tm->tm_mday,
266                        month[tm->tm_mon], tm->tm_year + 1900,
267                        tm->tm_hour, tm->tm_min, tm->tm_sec);
268 }
269 
270 
271 nxt_int_t nxt_http_init(nxt_task_t *task);
272 nxt_int_t nxt_h1p_init(nxt_task_t *task);
273 nxt_int_t nxt_http_response_hash_init(nxt_task_t *task);
274 
275 void nxt_http_conn_init(nxt_task_t *task, void *obj, void *data);
276 nxt_http_request_t *nxt_http_request_create(nxt_task_t *task);
277 void nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r,
278     nxt_http_status_t status);
279 void nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r);
280 void nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
281     nxt_work_handler_t body_handler, void *data);
282 void nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r,
283     nxt_buf_t *ws_frame);
284 void nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r,
285     nxt_buf_t *out);
286 nxt_buf_t *nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r,
287     size_t size);
288 nxt_buf_t *nxt_http_buf_last(nxt_http_request_t *r);
289 void nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data);
290 void nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data);
291 
292 nxt_int_t nxt_http_request_host(void *ctx, nxt_http_field_t *field,
293     uintptr_t data);
294 nxt_int_t nxt_http_request_field(void *ctx, nxt_http_field_t *field,
295     uintptr_t offset);
296 nxt_int_t nxt_http_request_content_length(void *ctx, nxt_http_field_t *field,
297     uintptr_t data);
298 
299 nxt_http_routes_t *nxt_http_routes_create(nxt_task_t *task,
300     nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *routes_conf);
301 nxt_http_action_t *nxt_http_action_create(nxt_task_t *task,
302     nxt_router_temp_conf_t *tmcf, nxt_str_t *name);
303 nxt_int_t nxt_http_routes_resolve(nxt_task_t *task,
304     nxt_router_temp_conf_t *tmcf);
305 nxt_int_t nxt_http_pass_segments(nxt_mp_t *mp, nxt_str_t *pass,
306     nxt_str_t *segments, nxt_uint_t n);
307 nxt_http_action_t *nxt_http_pass_application(nxt_task_t *task,
308     nxt_router_conf_t *rtcf, nxt_str_t *name);
309 
310 nxt_int_t nxt_upstreams_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
311     nxt_conf_value_t *conf);
312 nxt_int_t nxt_upstreams_joint_create(nxt_router_temp_conf_t *tmcf,
313     nxt_upstream_t ***upstream_joint);
314 
315 void nxt_http_request_action(nxt_task_t *task, nxt_http_request_t *r,
316     nxt_http_action_t *action);
317 
318 nxt_http_action_t *nxt_http_return_handler(nxt_task_t *task,
319     nxt_http_request_t *r, nxt_http_action_t *action);
320 
321 nxt_http_action_t *nxt_http_static_handler(nxt_task_t *task,
322     nxt_http_request_t *r, nxt_http_action_t *action);
323 nxt_int_t nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash);
324 nxt_int_t nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash,
325     nxt_str_t *extension, nxt_str_t *type);
326 nxt_str_t *nxt_http_static_mtypes_hash_find(nxt_lvlhsh_t *hash,
327     nxt_str_t *extension);
328 
329 nxt_http_action_t *nxt_http_application_handler(nxt_task_t *task,
330     nxt_http_request_t *r, nxt_http_action_t *action);
331 nxt_int_t nxt_upstream_find(nxt_upstreams_t *upstreams, nxt_str_t *name,
332     nxt_http_action_t *action);
333 nxt_http_action_t *nxt_upstream_proxy_handler(nxt_task_t *task,
334     nxt_http_request_t *r, nxt_upstream_t *upstream);
335 
336 
337 nxt_int_t nxt_http_proxy_create(nxt_mp_t *mp, nxt_http_action_t *action);
338 nxt_int_t nxt_http_proxy_date(void *ctx, nxt_http_field_t *field,
339     uintptr_t data);
340 nxt_int_t nxt_http_proxy_content_length(void *ctx, nxt_http_field_t *field,
341     uintptr_t data);
342 nxt_int_t nxt_http_proxy_skip(void *ctx, nxt_http_field_t *field,
343     uintptr_t data);
344 nxt_buf_t *nxt_http_proxy_buf_mem_alloc(nxt_task_t *task, nxt_http_request_t *r,
345     size_t size);
346 void nxt_http_proxy_buf_mem_free(nxt_task_t *task, nxt_http_request_t *r,
347     nxt_buf_t *b);
348 
349 extern nxt_time_string_t  nxt_http_date_cache;
350 
351 extern nxt_lvlhsh_t                        nxt_response_fields_hash;
352 
353 extern const nxt_http_proto_table_t  nxt_http_proto[];
354 
355 void nxt_h1p_websocket_first_frame_start(nxt_task_t *task,
356     nxt_http_request_t *r, nxt_buf_t *ws_frame);
357 void nxt_h1p_websocket_frame_start(nxt_task_t *task, nxt_http_request_t *r,
358     nxt_buf_t *ws_frame);
359 void nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p,
360     nxt_bool_t all);
361 nxt_msec_t nxt_h1p_conn_request_timer_value(nxt_conn_t *c, uintptr_t data);
362 
363 extern const nxt_conn_state_t  nxt_h1p_idle_close_state;
364 
365 #endif  /* _NXT_HTTP_H_INCLUDED_ */
366