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