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