xref: /unit/src/nxt_http.h (revision 1991:61155eb41a96)
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     void                            *tls;
166     nxt_task_t                      task;
167 
168     nxt_timer_t                     timer;
169     void                            *timer_data;
170 
171     nxt_var_query_t                 *var_query;
172 
173     void                            *req_rpc_data;
174 
175 #if (NXT_HAVE_REGEX)
176     nxt_regex_match_t               *regex_match;
177 #endif
178 
179     nxt_http_peer_t                 *peer;
180     nxt_buf_t                       *last;
181 
182     nxt_queue_link_t                app_link;   /* nxt_app_t.ack_waiting_req */
183     nxt_event_engine_t              *engine;
184     nxt_work_t                      err_work;
185 
186     nxt_http_response_t             resp;
187 
188     nxt_http_status_t               status:16;
189 
190     uint8_t                         pass_count;   /* 8 bits */
191     uint8_t                         app_target;
192     nxt_http_protocol_t             protocol:8;   /* 2 bits */
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 nxt_http_route_s            nxt_http_route_t;
202 typedef struct nxt_http_route_rule_s       nxt_http_route_rule_t;
203 typedef struct nxt_http_route_addr_rule_s  nxt_http_route_addr_rule_t;
204 
205 
206 typedef struct {
207     nxt_conf_value_t                *pass;
208     nxt_conf_value_t                *ret;
209     nxt_str_t                       location;
210     nxt_conf_value_t                *proxy;
211     nxt_conf_value_t                *share;
212     nxt_str_t                       chroot;
213     nxt_conf_value_t                *follow_symlinks;
214     nxt_conf_value_t                *traverse_mounts;
215     nxt_conf_value_t                *types;
216     nxt_conf_value_t                *fallback;
217 } nxt_http_action_conf_t;
218 
219 
220 struct nxt_http_action_s {
221     nxt_http_action_t               *(*handler)(nxt_task_t *task,
222                                         nxt_http_request_t *r,
223                                         nxt_http_action_t *action);
224     union {
225         void                        *conf;
226         nxt_http_route_t            *route;
227         nxt_upstream_t              *upstream;
228         uint32_t                    upstream_number;
229         nxt_var_t                   *var;
230         nxt_str_t                   *pass;
231     } u;
232 
233     nxt_http_action_t               *fallback;
234 };
235 
236 
237 typedef struct {
238     void (*body_read)(nxt_task_t *task, nxt_http_request_t *r);
239     void (*local_addr)(nxt_task_t *task, nxt_http_request_t *r);
240     void (*header_send)(nxt_task_t *task, nxt_http_request_t *r,
241          nxt_work_handler_t body_handler, void *data);
242     void (*send)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out);
243     nxt_off_t (*body_bytes_sent)(nxt_task_t *task, nxt_http_proto_t proto);
244     void (*discard)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *last);
245     void (*close)(nxt_task_t *task, nxt_http_proto_t proto,
246         nxt_socket_conf_joint_t *joint);
247 
248     void (*peer_connect)(nxt_task_t *task, nxt_http_peer_t *peer);
249     void (*peer_header_send)(nxt_task_t *task, nxt_http_peer_t *peer);
250     void (*peer_header_read)(nxt_task_t *task, nxt_http_peer_t *peer);
251     void (*peer_read)(nxt_task_t *task, nxt_http_peer_t *peer);
252     void (*peer_close)(nxt_task_t *task, nxt_http_peer_t *peer);
253 
254     void (*ws_frame_start)(nxt_task_t *task, nxt_http_request_t *r,
255         nxt_buf_t *ws_frame);
256 } nxt_http_proto_table_t;
257 
258 
259 struct nxt_http_client_ip_s {
260     nxt_http_route_addr_rule_t  *source;
261     nxt_str_t                   *header;
262     uint32_t                    header_hash;
263     uint8_t                     recursive;    /* 1 bit */
264 };
265 
266 
267 #define NXT_HTTP_DATE_LEN  nxt_length("Wed, 31 Dec 1986 16:40:00 GMT")
268 
269 nxt_inline u_char *
nxt_http_date(u_char * buf,struct tm * tm)270 nxt_http_date(u_char *buf, struct tm *tm)
271 {
272     static const char  *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
273                                    "Sat" };
274 
275     static const char  *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
276                                     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
277 
278     return nxt_sprintf(buf, buf + NXT_HTTP_DATE_LEN,
279                        "%s, %02d %s %4d %02d:%02d:%02d GMT",
280                        week[tm->tm_wday], tm->tm_mday,
281                        month[tm->tm_mon], tm->tm_year + 1900,
282                        tm->tm_hour, tm->tm_min, tm->tm_sec);
283 }
284 
285 
286 nxt_int_t nxt_http_init(nxt_task_t *task);
287 nxt_int_t nxt_h1p_init(nxt_task_t *task);
288 nxt_int_t nxt_http_response_hash_init(nxt_task_t *task);
289 
290 void nxt_http_conn_init(nxt_task_t *task, void *obj, void *data);
291 nxt_http_request_t *nxt_http_request_create(nxt_task_t *task);
292 void nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r,
293     nxt_http_status_t status);
294 void nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r);
295 void nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
296     nxt_work_handler_t body_handler, void *data);
297 void nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r,
298     nxt_buf_t *ws_frame);
299 void nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r,
300     nxt_buf_t *out);
301 nxt_buf_t *nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r,
302     size_t size);
303 nxt_buf_t *nxt_http_buf_last(nxt_http_request_t *r);
304 void nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data);
305 void nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data);
306 
307 nxt_int_t nxt_http_request_host(void *ctx, nxt_http_field_t *field,
308     uintptr_t data);
309 nxt_int_t nxt_http_request_field(void *ctx, nxt_http_field_t *field,
310     uintptr_t offset);
311 nxt_int_t nxt_http_request_content_length(void *ctx, nxt_http_field_t *field,
312     uintptr_t data);
313 
314 nxt_http_routes_t *nxt_http_routes_create(nxt_task_t *task,
315     nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *routes_conf);
316 nxt_http_action_t *nxt_http_action_create(nxt_task_t *task,
317     nxt_router_temp_conf_t *tmcf, nxt_str_t *pass);
318 nxt_int_t nxt_http_routes_resolve(nxt_task_t *task,
319     nxt_router_temp_conf_t *tmcf);
320 nxt_int_t nxt_http_pass_segments(nxt_mp_t *mp, nxt_str_t *pass,
321     nxt_str_t *segments, nxt_uint_t n);
322 nxt_http_action_t *nxt_http_pass_application(nxt_task_t *task,
323     nxt_router_conf_t *rtcf, nxt_str_t *name);
324 nxt_http_route_addr_rule_t *nxt_http_route_addr_rule_create(
325     nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *cv);
326 nxt_int_t nxt_http_route_addr_rule(nxt_http_request_t *r,
327     nxt_http_route_addr_rule_t *addr_rule, nxt_sockaddr_t *sockaddr);
328 nxt_http_route_rule_t *nxt_http_route_types_rule_create(nxt_task_t *task,
329     nxt_mp_t *mp, nxt_conf_value_t *types);
330 nxt_int_t nxt_http_route_test_rule(nxt_http_request_t *r,
331     nxt_http_route_rule_t *rule, u_char *start, size_t length);
332 
333 nxt_int_t nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
334     nxt_conf_value_t *cv, nxt_http_action_t *action);
335 void nxt_http_request_action(nxt_task_t *task, nxt_http_request_t *r,
336     nxt_http_action_t *action);
337 
338 nxt_int_t nxt_upstreams_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
339     nxt_conf_value_t *conf);
340 nxt_int_t nxt_upstreams_joint_create(nxt_router_temp_conf_t *tmcf,
341     nxt_upstream_t ***upstream_joint);
342 
343 nxt_int_t nxt_http_return_init(nxt_mp_t *mp, nxt_http_action_t *action,
344     nxt_http_action_conf_t *acf);
345 
346 nxt_int_t nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
347     nxt_http_action_t *action, nxt_http_action_conf_t *acf);
348 nxt_int_t nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash);
349 nxt_int_t nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash,
350     nxt_str_t *exten, nxt_str_t *type);
351 nxt_str_t *nxt_http_static_mtype_get(nxt_lvlhsh_t *hash, nxt_str_t *exten);
352 
353 nxt_http_action_t *nxt_http_application_handler(nxt_task_t *task,
354     nxt_http_request_t *r, nxt_http_action_t *action);
355 nxt_int_t nxt_upstream_find(nxt_upstreams_t *upstreams, nxt_str_t *name,
356     nxt_http_action_t *action);
357 nxt_http_action_t *nxt_upstream_proxy_handler(nxt_task_t *task,
358     nxt_http_request_t *r, nxt_upstream_t *upstream);
359 
360 nxt_int_t nxt_http_proxy_init(nxt_mp_t *mp, nxt_http_action_t *action,
361     nxt_http_action_conf_t *acf);
362 nxt_int_t nxt_http_proxy_date(void *ctx, nxt_http_field_t *field,
363     uintptr_t data);
364 nxt_int_t nxt_http_proxy_content_length(void *ctx, nxt_http_field_t *field,
365     uintptr_t data);
366 nxt_int_t nxt_http_proxy_skip(void *ctx, nxt_http_field_t *field,
367     uintptr_t data);
368 nxt_buf_t *nxt_http_proxy_buf_mem_alloc(nxt_task_t *task, nxt_http_request_t *r,
369     size_t size);
370 void nxt_http_proxy_buf_mem_free(nxt_task_t *task, nxt_http_request_t *r,
371     nxt_buf_t *b);
372 
373 extern nxt_time_string_t  nxt_http_date_cache;
374 
375 extern nxt_lvlhsh_t                        nxt_response_fields_hash;
376 
377 extern const nxt_http_proto_table_t  nxt_http_proto[];
378 
379 void nxt_h1p_websocket_first_frame_start(nxt_task_t *task,
380     nxt_http_request_t *r, nxt_buf_t *ws_frame);
381 void nxt_h1p_websocket_frame_start(nxt_task_t *task, nxt_http_request_t *r,
382     nxt_buf_t *ws_frame);
383 void nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p,
384     nxt_bool_t all);
385 nxt_msec_t nxt_h1p_conn_request_timer_value(nxt_conn_t *c, uintptr_t data);
386 
387 extern const nxt_conn_state_t  nxt_h1p_idle_close_state;
388 
389 #endif  /* _NXT_HTTP_H_INCLUDED_ */
390