xref: /unit/src/nxt_http.h (revision 2147:7bf58b1b18c4)
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_str_t                       args_decoded;
152     nxt_array_t                     *arguments;  /* of nxt_http_name_value_t */
153     nxt_array_t                     *cookies;    /* of nxt_http_name_value_t */
154     nxt_list_t                      *fields;
155     nxt_http_field_t                *content_type;
156     nxt_http_field_t                *content_length;
157     nxt_http_field_t                *cookie;
158     nxt_http_field_t                *referer;
159     nxt_http_field_t                *user_agent;
160     nxt_http_field_t                *authorization;
161     nxt_off_t                       content_length_n;
162 
163     nxt_sockaddr_t                  *remote;
164     nxt_sockaddr_t                  *local;
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                         tls;          /* 1 bit  */
193     uint8_t                         logged;       /* 1 bit  */
194     uint8_t                         header_sent;  /* 1 bit  */
195     uint8_t                         inconsistent; /* 1 bit  */
196     uint8_t                         error;        /* 1 bit  */
197     uint8_t                         websocket_handshake;  /* 1 bit */
198 };
199 
200 
201 typedef struct {
202     uint16_t                        hash;
203     uint16_t                        name_length;
204     uint32_t                        value_length;
205     u_char                          *name;
206     u_char                          *value;
207 } nxt_http_name_value_t;
208 
209 
210 typedef enum {
211     NXT_HTTP_URI_ENCODING_NONE = 0,
212     NXT_HTTP_URI_ENCODING,
213     NXT_HTTP_URI_ENCODING_PLUS
214 } nxt_http_uri_encoding_t;
215 
216 
217 typedef struct nxt_http_route_s            nxt_http_route_t;
218 typedef struct nxt_http_route_rule_s       nxt_http_route_rule_t;
219 typedef struct nxt_http_route_addr_rule_s  nxt_http_route_addr_rule_t;
220 
221 
222 typedef struct {
223     nxt_conf_value_t                *pass;
224     nxt_conf_value_t                *ret;
225     nxt_conf_value_t                *location;
226     nxt_conf_value_t                *proxy;
227     nxt_conf_value_t                *share;
228     nxt_conf_value_t                *index;
229     nxt_str_t                       chroot;
230     nxt_conf_value_t                *follow_symlinks;
231     nxt_conf_value_t                *traverse_mounts;
232     nxt_conf_value_t                *types;
233     nxt_conf_value_t                *fallback;
234 } nxt_http_action_conf_t;
235 
236 
237 struct nxt_http_action_s {
238     nxt_http_action_t               *(*handler)(nxt_task_t *task,
239                                         nxt_http_request_t *r,
240                                         nxt_http_action_t *action);
241     union {
242         void                        *conf;
243         nxt_http_route_t            *route;
244         nxt_upstream_t              *upstream;
245         uint32_t                    upstream_number;
246         nxt_var_t                   *var;
247         nxt_str_t                   *pass;
248     } u;
249 
250     nxt_http_action_t               *fallback;
251 };
252 
253 
254 typedef struct {
255     void (*body_read)(nxt_task_t *task, nxt_http_request_t *r);
256     void (*local_addr)(nxt_task_t *task, nxt_http_request_t *r);
257     void (*header_send)(nxt_task_t *task, nxt_http_request_t *r,
258         nxt_work_handler_t body_handler, void *data);
259     void (*send)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out);
260     nxt_off_t (*body_bytes_sent)(nxt_task_t *task, nxt_http_proto_t proto);
261     void (*discard)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *last);
262     void (*close)(nxt_task_t *task, nxt_http_proto_t proto,
263         nxt_socket_conf_joint_t *joint);
264 
265     void (*peer_connect)(nxt_task_t *task, nxt_http_peer_t *peer);
266     void (*peer_header_send)(nxt_task_t *task, nxt_http_peer_t *peer);
267     void (*peer_header_read)(nxt_task_t *task, nxt_http_peer_t *peer);
268     void (*peer_read)(nxt_task_t *task, nxt_http_peer_t *peer);
269     void (*peer_close)(nxt_task_t *task, nxt_http_peer_t *peer);
270 
271     void (*ws_frame_start)(nxt_task_t *task, nxt_http_request_t *r,
272         nxt_buf_t *ws_frame);
273 } nxt_http_proto_table_t;
274 
275 
276 typedef struct {
277     nxt_str_t                   *header;
278     uint32_t                    header_hash;
279 } nxt_http_forward_header_t;
280 
281 
282 struct nxt_http_forward_s {
283     nxt_http_forward_header_t   client_ip;
284     nxt_http_forward_header_t   protocol;
285     nxt_http_route_addr_rule_t  *source;
286     uint8_t                     recursive;    /* 1 bit */
287 };
288 
289 
290 #define NXT_HTTP_DATE_LEN  nxt_length("Wed, 31 Dec 1986 16:40:00 GMT")
291 
292 nxt_inline u_char *
nxt_http_date(u_char * buf,struct tm * tm)293 nxt_http_date(u_char *buf, struct tm *tm)
294 {
295     static const char  *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
296                                    "Sat" };
297 
298     static const char  *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
299                                     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
300 
301     return nxt_sprintf(buf, buf + NXT_HTTP_DATE_LEN,
302                        "%s, %02d %s %4d %02d:%02d:%02d GMT",
303                        week[tm->tm_wday], tm->tm_mday,
304                        month[tm->tm_mon], tm->tm_year + 1900,
305                        tm->tm_hour, tm->tm_min, tm->tm_sec);
306 }
307 
308 
309 nxt_int_t nxt_http_init(nxt_task_t *task);
310 nxt_int_t nxt_h1p_init(nxt_task_t *task);
311 nxt_int_t nxt_http_response_hash_init(nxt_task_t *task);
312 
313 void nxt_http_conn_init(nxt_task_t *task, void *obj, void *data);
314 nxt_http_request_t *nxt_http_request_create(nxt_task_t *task);
315 void nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r,
316     nxt_http_status_t status);
317 void nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r);
318 void nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
319     nxt_work_handler_t body_handler, void *data);
320 void nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r,
321     nxt_buf_t *ws_frame);
322 void nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r,
323     nxt_buf_t *out);
324 nxt_buf_t *nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r,
325     size_t size);
326 nxt_buf_t *nxt_http_buf_last(nxt_http_request_t *r);
327 void nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data);
328 void nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data);
329 
330 nxt_int_t nxt_http_request_host(void *ctx, nxt_http_field_t *field,
331     uintptr_t data);
332 nxt_int_t nxt_http_request_field(void *ctx, nxt_http_field_t *field,
333     uintptr_t offset);
334 nxt_int_t nxt_http_request_content_length(void *ctx, nxt_http_field_t *field,
335     uintptr_t data);
336 
337 nxt_array_t *nxt_http_arguments_parse(nxt_http_request_t *r);
338 nxt_array_t *nxt_http_cookies_parse(nxt_http_request_t *r);
339 
340 int64_t nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name,
341     nxt_bool_t case_sensitive, uint8_t encoding);
342 int64_t nxt_http_argument_hash(nxt_mp_t *mp, nxt_str_t *name);
343 int64_t nxt_http_header_hash(nxt_mp_t *mp, nxt_str_t *name);
344 int64_t nxt_http_cookie_hash(nxt_mp_t *mp, nxt_str_t *name);
345 
346 nxt_http_routes_t *nxt_http_routes_create(nxt_task_t *task,
347     nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *routes_conf);
348 nxt_http_action_t *nxt_http_action_create(nxt_task_t *task,
349     nxt_router_temp_conf_t *tmcf, nxt_str_t *pass);
350 nxt_int_t nxt_http_routes_resolve(nxt_task_t *task,
351     nxt_router_temp_conf_t *tmcf);
352 nxt_int_t nxt_http_pass_segments(nxt_mp_t *mp, nxt_str_t *pass,
353     nxt_str_t *segments, nxt_uint_t n);
354 nxt_http_action_t *nxt_http_pass_application(nxt_task_t *task,
355     nxt_router_conf_t *rtcf, nxt_str_t *name);
356 nxt_http_route_addr_rule_t *nxt_http_route_addr_rule_create(
357     nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *cv);
358 nxt_int_t nxt_http_route_addr_rule(nxt_http_request_t *r,
359     nxt_http_route_addr_rule_t *addr_rule, nxt_sockaddr_t *sockaddr);
360 nxt_http_route_rule_t *nxt_http_route_types_rule_create(nxt_task_t *task,
361     nxt_mp_t *mp, nxt_conf_value_t *types);
362 nxt_int_t nxt_http_route_test_rule(nxt_http_request_t *r,
363     nxt_http_route_rule_t *rule, u_char *start, size_t length);
364 
365 nxt_int_t nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
366     nxt_conf_value_t *cv, nxt_http_action_t *action);
367 void nxt_http_request_action(nxt_task_t *task, nxt_http_request_t *r,
368     nxt_http_action_t *action);
369 
370 nxt_int_t nxt_upstreams_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
371     nxt_conf_value_t *conf);
372 nxt_int_t nxt_upstreams_joint_create(nxt_router_temp_conf_t *tmcf,
373     nxt_upstream_t ***upstream_joint);
374 
375 nxt_int_t nxt_http_return_init(nxt_router_conf_t *rtcf,
376     nxt_http_action_t *action, nxt_http_action_conf_t *acf);
377 
378 nxt_int_t nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
379     nxt_http_action_t *action, nxt_http_action_conf_t *acf);
380 nxt_int_t nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash);
381 nxt_int_t nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash,
382     const nxt_str_t *exten, nxt_str_t *type);
383 nxt_str_t *nxt_http_static_mtype_get(nxt_lvlhsh_t *hash,
384     const nxt_str_t *exten);
385 
386 nxt_http_action_t *nxt_http_application_handler(nxt_task_t *task,
387     nxt_http_request_t *r, nxt_http_action_t *action);
388 nxt_int_t nxt_upstream_find(nxt_upstreams_t *upstreams, nxt_str_t *name,
389     nxt_http_action_t *action);
390 nxt_http_action_t *nxt_upstream_proxy_handler(nxt_task_t *task,
391     nxt_http_request_t *r, nxt_upstream_t *upstream);
392 
393 nxt_int_t nxt_http_proxy_init(nxt_mp_t *mp, nxt_http_action_t *action,
394     nxt_http_action_conf_t *acf);
395 nxt_int_t nxt_http_proxy_date(void *ctx, nxt_http_field_t *field,
396     uintptr_t data);
397 nxt_int_t nxt_http_proxy_content_length(void *ctx, nxt_http_field_t *field,
398     uintptr_t data);
399 nxt_int_t nxt_http_proxy_skip(void *ctx, nxt_http_field_t *field,
400     uintptr_t data);
401 nxt_buf_t *nxt_http_proxy_buf_mem_alloc(nxt_task_t *task, nxt_http_request_t *r,
402     size_t size);
403 void nxt_http_proxy_buf_mem_free(nxt_task_t *task, nxt_http_request_t *r,
404     nxt_buf_t *b);
405 
406 extern nxt_time_string_t  nxt_http_date_cache;
407 
408 extern nxt_lvlhsh_t                        nxt_response_fields_hash;
409 
410 extern const nxt_http_proto_table_t  nxt_http_proto[];
411 
412 void nxt_h1p_websocket_first_frame_start(nxt_task_t *task,
413     nxt_http_request_t *r, nxt_buf_t *ws_frame);
414 void nxt_h1p_websocket_frame_start(nxt_task_t *task, nxt_http_request_t *r,
415     nxt_buf_t *ws_frame);
416 void nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p,
417     nxt_bool_t all);
418 nxt_msec_t nxt_h1p_conn_request_timer_value(nxt_conn_t *c, uintptr_t data);
419 
420 extern const nxt_conn_state_t  nxt_h1p_idle_close_state;
421 
422 #endif  /* _NXT_HTTP_H_INCLUDED_ */
423