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