1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#ifndef _NGX_HTTP_REQUEST_H_INCLUDED_
9#define _NGX_HTTP_REQUEST_H_INCLUDED_
10
11
12#define NGX_HTTP_MAX_URI_CHANGES           10
13#define NGX_HTTP_MAX_SUBREQUESTS           50
14
15/* must be 2^n */
16#define NGX_HTTP_LC_HEADER_LEN             32
17
18
19#define NGX_HTTP_DISCARD_BUFFER_SIZE       4096
20#define NGX_HTTP_LINGERING_BUFFER_SIZE     4096
21
22
23#define NGX_HTTP_VERSION_9                 9
24#define NGX_HTTP_VERSION_10                1000
25#define NGX_HTTP_VERSION_11                1001
26#define NGX_HTTP_VERSION_20                2000
27
28#define NGX_HTTP_UNKNOWN                   0x0001
29#define NGX_HTTP_GET                       0x0002
30#define NGX_HTTP_HEAD                      0x0004
31#define NGX_HTTP_POST                      0x0008
32#define NGX_HTTP_PUT                       0x0010
33#define NGX_HTTP_DELETE                    0x0020
34#define NGX_HTTP_MKCOL                     0x0040
35#define NGX_HTTP_COPY                      0x0080
36#define NGX_HTTP_MOVE                      0x0100
37#define NGX_HTTP_OPTIONS                   0x0200
38#define NGX_HTTP_PROPFIND                  0x0400
39#define NGX_HTTP_PROPPATCH                 0x0800
40#define NGX_HTTP_LOCK                      0x1000
41#define NGX_HTTP_UNLOCK                    0x2000
42#define NGX_HTTP_PATCH                     0x4000
43#define NGX_HTTP_TRACE                     0x8000
44
45#define NGX_HTTP_CONNECTION_CLOSE          1
46#define NGX_HTTP_CONNECTION_KEEP_ALIVE     2
47
48
49#define NGX_NONE                           1
50
51
52#define NGX_HTTP_PARSE_HEADER_DONE         1
53
54#define NGX_HTTP_CLIENT_ERROR              10
55#define NGX_HTTP_PARSE_INVALID_METHOD      10
56#define NGX_HTTP_PARSE_INVALID_REQUEST     11
57#define NGX_HTTP_PARSE_INVALID_09_METHOD   12
58
59#define NGX_HTTP_PARSE_INVALID_HEADER      13
60
61
62/* unused                                  1 */
63#define NGX_HTTP_SUBREQUEST_IN_MEMORY      2
64#define NGX_HTTP_SUBREQUEST_WAITED         4
65#define NGX_HTTP_SUBREQUEST_CLONE          8
66
67#define NGX_HTTP_LOG_UNSAFE                1
68
69
70#define NGX_HTTP_CONTINUE                  100
71#define NGX_HTTP_SWITCHING_PROTOCOLS       101
72#define NGX_HTTP_PROCESSING                102
73
74#define NGX_HTTP_OK                        200
75#define NGX_HTTP_CREATED                   201
76#define NGX_HTTP_ACCEPTED                  202
77#define NGX_HTTP_NO_CONTENT                204
78#define NGX_HTTP_PARTIAL_CONTENT           206
79
80#define NGX_HTTP_SPECIAL_RESPONSE          300
81#define NGX_HTTP_MOVED_PERMANENTLY         301
82#define NGX_HTTP_MOVED_TEMPORARILY         302
83#define NGX_HTTP_SEE_OTHER                 303
84#define NGX_HTTP_NOT_MODIFIED              304
85#define NGX_HTTP_TEMPORARY_REDIRECT        307
86
87#define NGX_HTTP_BAD_REQUEST               400
88#define NGX_HTTP_UNAUTHORIZED              401
89#define NGX_HTTP_FORBIDDEN                 403
90#define NGX_HTTP_NOT_FOUND                 404
91#define NGX_HTTP_NOT_ALLOWED               405
92#define NGX_HTTP_REQUEST_TIME_OUT          408
93#define NGX_HTTP_CONFLICT                  409
94#define NGX_HTTP_LENGTH_REQUIRED           411
95#define NGX_HTTP_PRECONDITION_FAILED       412
96#define NGX_HTTP_REQUEST_ENTITY_TOO_LARGE  413
97#define NGX_HTTP_REQUEST_URI_TOO_LARGE     414
98#define NGX_HTTP_UNSUPPORTED_MEDIA_TYPE    415
99#define NGX_HTTP_RANGE_NOT_SATISFIABLE     416
100#define NGX_HTTP_MISDIRECTED_REQUEST       421
101#define NGX_HTTP_TOO_MANY_REQUESTS         429
102
103
104/* Our own HTTP codes */
105
106/* The special code to close connection without any response */
107#define NGX_HTTP_CLOSE                     444
108
109#define NGX_HTTP_NGINX_CODES               494
110
111#define NGX_HTTP_REQUEST_HEADER_TOO_LARGE  494
112
113#define NGX_HTTPS_CERT_ERROR               495
114#define NGX_HTTPS_NO_CERT                  496
115
116/*
117 * We use the special code for the plain HTTP requests that are sent to
118 * HTTPS port to distinguish it from 4XX in an error page redirection
119 */
120#define NGX_HTTP_TO_HTTPS                  497
121
122/* 498 is the canceled code for the requests with invalid host name */
123
124/*
125 * HTTP does not define the code for the case when a client closed
126 * the connection while we are processing its request so we introduce
127 * own code to log such situation when a client has closed the connection
128 * before we even try to send the HTTP header to it
129 */
130#define NGX_HTTP_CLIENT_CLOSED_REQUEST     499
131
132
133#define NGX_HTTP_INTERNAL_SERVER_ERROR     500
134#define NGX_HTTP_NOT_IMPLEMENTED           501
135#define NGX_HTTP_BAD_GATEWAY               502
136#define NGX_HTTP_SERVICE_UNAVAILABLE       503
137#define NGX_HTTP_GATEWAY_TIME_OUT          504
138#define NGX_HTTP_INSUFFICIENT_STORAGE      507
139
140
141#define NGX_HTTP_LOWLEVEL_BUFFERED         0xf0
142#define NGX_HTTP_WRITE_BUFFERED            0x10
143#define NGX_HTTP_GZIP_BUFFERED             0x20
144#define NGX_HTTP_SSI_BUFFERED              0x01
145#define NGX_HTTP_SUB_BUFFERED              0x02
146#define NGX_HTTP_COPY_BUFFERED             0x04
147
148
149typedef enum {
150    NGX_HTTP_INITING_REQUEST_STATE = 0,
151    NGX_HTTP_READING_REQUEST_STATE,
152    NGX_HTTP_PROCESS_REQUEST_STATE,
153
154    NGX_HTTP_CONNECT_UPSTREAM_STATE,
155    NGX_HTTP_WRITING_UPSTREAM_STATE,
156    NGX_HTTP_READING_UPSTREAM_STATE,
157
158    NGX_HTTP_WRITING_REQUEST_STATE,
159    NGX_HTTP_LINGERING_CLOSE_STATE,
160    NGX_HTTP_KEEPALIVE_STATE
161} ngx_http_state_e;
162
163
164typedef struct {
165    ngx_str_t                         name;
166    ngx_uint_t                        offset;
167    ngx_http_header_handler_pt        handler;
168} ngx_http_header_t;
169
170
171typedef struct {
172    ngx_str_t                         name;
173    ngx_uint_t                        offset;
174} ngx_http_header_out_t;
175
176
177typedef struct {
178    ngx_list_t                        headers;
179
180    ngx_table_elt_t                  *host;
181    ngx_table_elt_t                  *connection;
182    ngx_table_elt_t                  *if_modified_since;
183    ngx_table_elt_t                  *if_unmodified_since;
184    ngx_table_elt_t                  *if_match;
185    ngx_table_elt_t                  *if_none_match;
186    ngx_table_elt_t                  *user_agent;
187    ngx_table_elt_t                  *referer;
188    ngx_table_elt_t                  *content_length;
189    ngx_table_elt_t                  *content_range;
190    ngx_table_elt_t                  *content_type;
191
192    ngx_table_elt_t                  *range;
193    ngx_table_elt_t                  *if_range;
194
195    ngx_table_elt_t                  *transfer_encoding;
196    ngx_table_elt_t                  *expect;
197    ngx_table_elt_t                  *upgrade;
198
199#if (NGX_HTTP_GZIP)
200    ngx_table_elt_t                  *accept_encoding;
201    ngx_table_elt_t                  *via;
202#endif
203
204    ngx_table_elt_t                  *authorization;
205
206    ngx_table_elt_t                  *keep_alive;
207
208#if (NGX_HTTP_X_FORWARDED_FOR)
209    ngx_array_t                       x_forwarded_for;
210#endif
211
212#if (NGX_HTTP_REALIP)
213    ngx_table_elt_t                  *x_real_ip;
214#endif
215
216#if (NGX_HTTP_HEADERS)
217    ngx_table_elt_t                  *accept;
218    ngx_table_elt_t                  *accept_language;
219#endif
220
221#if (NGX_HTTP_DAV)
222    ngx_table_elt_t                  *depth;
223    ngx_table_elt_t                  *destination;
224    ngx_table_elt_t                  *overwrite;
225    ngx_table_elt_t                  *date;
226#endif
227
228    ngx_str_t                         user;
229    ngx_str_t                         passwd;
230
231    ngx_array_t                       cookies;
232
233    ngx_str_t                         server;
234    off_t                             content_length_n;
235    time_t                            keep_alive_n;
236
237    unsigned                          connection_type:2;
238    unsigned                          chunked:1;
239    unsigned                          msie:1;
240    unsigned                          msie6:1;
241    unsigned                          opera:1;
242    unsigned                          gecko:1;
243    unsigned                          chrome:1;
244    unsigned                          safari:1;
245    unsigned                          konqueror:1;
246} ngx_http_headers_in_t;
247
248
249typedef struct {
250    ngx_list_t                        headers;
251
252    ngx_uint_t                        status;
253    ngx_str_t                         status_line;
254
255    ngx_table_elt_t                  *server;
256    ngx_table_elt_t                  *date;
257    ngx_table_elt_t                  *content_length;
258    ngx_table_elt_t                  *content_encoding;
259    ngx_table_elt_t                  *location;
260    ngx_table_elt_t                  *refresh;
261    ngx_table_elt_t                  *last_modified;
262    ngx_table_elt_t                  *content_range;
263    ngx_table_elt_t                  *accept_ranges;
264    ngx_table_elt_t                  *www_authenticate;
265    ngx_table_elt_t                  *expires;
266    ngx_table_elt_t                  *etag;
267
268    ngx_str_t                        *override_charset;
269
270    size_t                            content_type_len;
271    ngx_str_t                         content_type;
272    ngx_str_t                         charset;
273    u_char                           *content_type_lowcase;
274    ngx_uint_t                        content_type_hash;
275
276    ngx_array_t                       cache_control;
277
278    off_t                             content_length_n;
279    off_t                             content_offset;
280    time_t                            date_time;
281    time_t                            last_modified_time;
282} ngx_http_headers_out_t;
283
284
285typedef void (*ngx_http_client_body_handler_pt)(ngx_http_request_t *r);
286
287typedef struct {
288    ngx_temp_file_t                  *temp_file;
289    ngx_chain_t                      *bufs;
290    ngx_buf_t                        *buf;
291    off_t                             rest;
292    off_t                             received;
293    ngx_chain_t                      *free;
294    ngx_chain_t                      *busy;
295    ngx_http_chunked_t               *chunked;
296    ngx_http_client_body_handler_pt   post_handler;
297} ngx_http_request_body_t;
298
299
300typedef struct ngx_http_addr_conf_s  ngx_http_addr_conf_t;
301
302typedef struct {
303    ngx_http_addr_conf_t             *addr_conf;
304    ngx_http_conf_ctx_t              *conf_ctx;
305
306#if (NGX_HTTP_SSL || NGX_COMPAT)
307    ngx_str_t                        *ssl_servername;
308#if (NGX_PCRE)
309    ngx_http_regex_t                 *ssl_servername_regex;
310#endif
311#endif
312
313    ngx_chain_t                      *busy;
314    ngx_int_t                         nbusy;
315
316    ngx_chain_t                      *free;
317
318    unsigned                          ssl:1;
319    unsigned                          proxy_protocol:1;
320} ngx_http_connection_t;
321
322
323typedef void (*ngx_http_cleanup_pt)(void *data);
324
325typedef struct ngx_http_cleanup_s  ngx_http_cleanup_t;
326
327struct ngx_http_cleanup_s {
328    ngx_http_cleanup_pt               handler;
329    void                             *data;
330    ngx_http_cleanup_t               *next;
331};
332
333
334typedef ngx_int_t (*ngx_http_post_subrequest_pt)(ngx_http_request_t *r,
335    void *data, ngx_int_t rc);
336
337typedef struct {
338    ngx_http_post_subrequest_pt       handler;
339    void                             *data;
340} ngx_http_post_subrequest_t;
341
342
343typedef struct ngx_http_postponed_request_s  ngx_http_postponed_request_t;
344
345struct ngx_http_postponed_request_s {
346    ngx_http_request_t               *request;
347    ngx_chain_t                      *out;
348    ngx_http_postponed_request_t     *next;
349};
350
351
352typedef struct ngx_http_posted_request_s  ngx_http_posted_request_t;
353
354struct ngx_http_posted_request_s {
355    ngx_http_request_t               *request;
356    ngx_http_posted_request_t        *next;
357};
358
359
360typedef ngx_int_t (*ngx_http_handler_pt)(ngx_http_request_t *r);
361typedef void (*ngx_http_event_handler_pt)(ngx_http_request_t *r);
362
363
364struct ngx_http_request_s {
365    uint32_t                          signature;         /* "HTTP" */
366
367    ngx_connection_t                 *connection;
368
369    void                            **ctx;
370    void                            **main_conf;
371    void                            **srv_conf;
372    void                            **loc_conf;
373
374    ngx_http_event_handler_pt         read_event_handler;
375    ngx_http_event_handler_pt         write_event_handler;
376
377#if (NGX_HTTP_CACHE)
378    ngx_http_cache_t                 *cache;
379#endif
380
381    ngx_http_upstream_t              *upstream;
382    ngx_array_t                      *upstream_states;
383                                         /* of ngx_http_upstream_state_t */
384
385    ngx_pool_t                       *pool;
386    ngx_buf_t                        *header_in;
387
388    ngx_http_headers_in_t             headers_in;
389    ngx_http_headers_out_t            headers_out;
390
391    ngx_http_request_body_t          *request_body;
392
393    time_t                            lingering_time;
394    time_t                            start_sec;
395    ngx_msec_t                        start_msec;
396
397    ngx_uint_t                        method;
398    ngx_uint_t                        http_version;
399
400    ngx_str_t                         request_line;
401    ngx_str_t                         uri;
402    ngx_str_t                         args;
403    ngx_str_t                         exten;
404    ngx_str_t                         unparsed_uri;
405
406    ngx_str_t                         method_name;
407    ngx_str_t                         http_protocol;
408
409    ngx_chain_t                      *out;
410    ngx_http_request_t               *main;
411    ngx_http_request_t               *parent;
412    ngx_http_postponed_request_t     *postponed;
413    ngx_http_post_subrequest_t       *post_subrequest;
414    ngx_http_posted_request_t        *posted_requests;
415
416    ngx_int_t                         phase_handler;
417    ngx_http_handler_pt               content_handler;
418    ngx_uint_t                        access_code;
419
420    ngx_http_variable_value_t        *variables;
421
422#if (NGX_PCRE)
423    ngx_uint_t                        ncaptures;
424    int                              *captures;
425    u_char                           *captures_data;
426#endif
427
428    size_t                            limit_rate;
429    size_t                            limit_rate_after;
430
431    /* used to learn the Apache compatible response length without a header */
432    size_t                            header_size;
433
434    off_t                             request_length;
435
436    ngx_uint_t                        err_status;
437
438    ngx_http_connection_t            *http_connection;
439    ngx_http_v2_stream_t             *stream;
440
441    ngx_http_log_handler_pt           log_handler;
442
443    ngx_http_cleanup_t               *cleanup;
444
445    unsigned                          count:16;
446    unsigned                          subrequests:8;
447    unsigned                          blocked:8;
448
449    unsigned                          aio:1;
450
451    unsigned                          http_state:4;
452
453    /* URI with "/." and on Win32 with "//" */
454    unsigned                          complex_uri:1;
455
456    /* URI with "%" */
457    unsigned                          quoted_uri:1;
458
459    /* URI with "+" */
460    unsigned                          plus_in_uri:1;
461
462    /* URI with " " */
463    unsigned                          space_in_uri:1;
464
465    unsigned                          invalid_header:1;
466
467    unsigned                          add_uri_to_alias:1;
468    unsigned                          valid_location:1;
469    unsigned                          valid_unparsed_uri:1;
470    unsigned                          uri_changed:1;
471    unsigned                          uri_changes:4;
472
473    unsigned                          request_body_in_single_buf:1;
474    unsigned                          request_body_in_file_only:1;
475    unsigned                          request_body_in_persistent_file:1;
476    unsigned                          request_body_in_clean_file:1;
477    unsigned                          request_body_file_group_access:1;
478    unsigned                          request_body_file_log_level:3;
479    unsigned                          request_body_no_buffering:1;
480
481    unsigned                          subrequest_in_memory:1;
482    unsigned                          waited:1;
483
484#if (NGX_HTTP_CACHE)
485    unsigned                          cached:1;
486    unsigned                          cache_updater:1;
487#endif
488
489#if (NGX_HTTP_GZIP)
490    unsigned                          gzip_tested:1;
491    unsigned                          gzip_ok:1;
492    unsigned                          gzip_vary:1;
493#endif
494
495    unsigned                          proxy:1;
496    unsigned                          bypass_cache:1;
497    unsigned                          no_cache:1;
498
499    /*
500     * instead of using the request context data in
501     * ngx_http_limit_conn_module and ngx_http_limit_req_module
502     * we use the single bits in the request structure
503     */
504    unsigned                          limit_conn_set:1;
505    unsigned                          limit_req_set:1;
506
507#if 0
508    unsigned                          cacheable:1;
509#endif
510
511    unsigned                          pipeline:1;
512    unsigned                          chunked:1;
513    unsigned                          header_only:1;
514    unsigned                          keepalive:1;
515    unsigned                          lingering_close:1;
516    unsigned                          discard_body:1;
517    unsigned                          reading_body:1;
518    unsigned                          internal:1;
519    unsigned                          error_page:1;
520    unsigned                          filter_finalize:1;
521    unsigned                          post_action:1;
522    unsigned                          request_complete:1;
523    unsigned                          request_output:1;
524    unsigned                          header_sent:1;
525    unsigned                          expect_tested:1;
526    unsigned                          root_tested:1;
527    unsigned                          done:1;
528    unsigned                          logged:1;
529
530    unsigned                          buffered:4;
531
532    unsigned                          main_filter_need_in_memory:1;
533    unsigned                          filter_need_in_memory:1;
534    unsigned                          filter_need_temporary:1;
535    unsigned                          allow_ranges:1;
536    unsigned                          subrequest_ranges:1;
537    unsigned                          single_range:1;
538    unsigned                          disable_not_modified:1;
539    unsigned                          stat_reading:1;
540    unsigned                          stat_writing:1;
541    unsigned                          stat_processing:1;
542
543    unsigned                          health_check:1;
544
545    /* used to parse HTTP headers */
546
547    ngx_uint_t                        state;
548
549    ngx_uint_t                        header_hash;
550    ngx_uint_t                        lowcase_index;
551    u_char                            lowcase_header[NGX_HTTP_LC_HEADER_LEN];
552
553    u_char                           *header_name_start;
554    u_char                           *header_name_end;
555    u_char                           *header_start;
556    u_char                           *header_end;
557
558    /*
559     * a memory that can be reused after parsing a request line
560     * via ngx_http_ephemeral_t
561     */
562
563    u_char                           *uri_start;
564    u_char                           *uri_end;
565    u_char                           *uri_ext;
566    u_char                           *args_start;
567    u_char                           *request_start;
568    u_char                           *request_end;
569    u_char                           *method_end;
570    u_char                           *schema_start;
571    u_char                           *schema_end;
572    u_char                           *host_start;
573    u_char                           *host_end;
574    u_char                           *port_start;
575    u_char                           *port_end;
576
577    unsigned                          http_minor:16;
578    unsigned                          http_major:16;
579};
580
581
582typedef struct {
583    ngx_http_posted_request_t         terminal_posted_request;
584} ngx_http_ephemeral_t;
585
586
587#define ngx_http_ephemeral(r)  (void *) (&r->uri_start)
588
589
590extern ngx_http_header_t       ngx_http_headers_in[];
591extern ngx_http_header_out_t   ngx_http_headers_out[];
592
593
594#define ngx_http_set_log_request(log, r)                                      \
595    ((ngx_http_log_ctx_t *) log->data)->current_request = r
596
597
598#endif /* _NGX_HTTP_REQUEST_H_INCLUDED_ */
599