xref: /lighttpd1.4/src/request.h (revision 3392e8fb)
1 #ifndef _REQUEST_H_
2 #define _REQUEST_H_
3 #include "first.h"
4 
5 #include <time.h>       /* (struct timespec) */
6 
7 #include "base_decls.h"
8 #include "buffer.h"
9 #include "array.h"
10 #include "chunk.h"
11 #include "http_kv.h"
12 
13 struct log_error_st;    /* declaration */
14 struct chunkqueue;      /* declaration */
15 struct cond_cache_t;    /* declaration */
16 struct cond_match_t;    /* declaration */
17 
18 typedef struct {
19     unsigned int http_parseopts;
20     uint32_t max_request_field_size;
21     const array *mimetypes;
22 
23     /* virtual-servers */
24     const buffer *document_root;
25     const buffer *server_name;
26     const buffer *server_tag;
27     struct log_error_st *errh;
28 
29     unsigned int max_request_size;
30     unsigned short max_keep_alive_requests;
31     unsigned short max_keep_alive_idle;
32     unsigned short max_read_idle;
33     unsigned short max_write_idle;
34     unsigned short stream_request_body;
35     unsigned short stream_response_body;
36     unsigned char high_precision_timestamps;
37     unsigned char allow_http11;
38     unsigned char follow_symlink;
39     unsigned char etag_flags;
40     unsigned char force_lowercase_filenames; /*(case-insensitive file systems)*/
41     unsigned char use_xattr;
42     unsigned char range_requests;
43     unsigned char error_intercept;
44 
45     unsigned char h2proto; /*(global setting copied for convenient access)*/
46 
47     /* debug */
48     unsigned char log_file_not_found;
49     unsigned char log_request_header;
50     unsigned char log_request_handling;
51     unsigned char log_response_header;
52     unsigned char log_condition_handling;
53     unsigned char log_timeouts;
54     unsigned char log_state_handling;
55     unsigned char log_request_header_on_error;
56 
57     unsigned int bytes_per_second; /* connection bytes/sec limit */
58     unsigned int global_bytes_per_second;/*total bytes/sec limit for scope*/
59 
60     /* server-wide traffic-shaper
61      *
62      * each context has the counter which is inited once
63      * a second by the global_bytes_per_second config-var
64      *
65      * as soon as global_bytes_per_second gets below 0
66      * the connected conns are "offline" a little bit
67      *
68      * the problem:
69      * we somehow have to lose our "we are writable" signal on the way.
70      *
71      */
72     off_t *global_bytes_per_second_cnt_ptr; /*  */
73 
74     const buffer *error_handler;
75     const buffer *error_handler_404;
76     const buffer *errorfile_prefix;
77     struct log_error_st *serrh; /* script errh */
78 } request_config;
79 
80 typedef struct {
81     buffer scheme; /* scheme without colon or slashes ( "http" or "https" ) */
82 
83     /* authority with optional portnumber ("site.name" or "site.name:8080" ) NOTE: without "username:password@" */
84     buffer authority;
85 
86     /* path including leading slash ("/" or "/index.html") - urldecoded, and sanitized  ( buffer_path_simplify() && buffer_urldecode_path() ) */
87     buffer path;
88     buffer query; /* querystring ( everything after "?", ie: in "/index.php?foo=1", query is "foo=1" ) */
89 } request_uri;
90 
91 typedef struct {
92     buffer path;
93     buffer basedir; /* path = "(basedir)(.*)" */
94 
95     buffer doc_root; /* path = doc_root + rel_path */
96     buffer rel_path;
97 
98     buffer etag;
99 } physical;
100 
101 typedef struct {
102     off_t gw_chunked;
103     buffer b;
104     int done;
105 } response_dechunk;
106 
107 /* the order of the items should be the same as they are processed
108  * read before write as we use this later e.g. <= CON_STATE_REQUEST_END */
109 typedef enum {
110     CON_STATE_CONNECT,
111     CON_STATE_REQUEST_START,
112     CON_STATE_READ,
113     CON_STATE_REQUEST_END,
114     CON_STATE_READ_POST,
115     CON_STATE_HANDLE_REQUEST,
116     CON_STATE_RESPONSE_START,
117     CON_STATE_WRITE,
118     CON_STATE_RESPONSE_END,
119     CON_STATE_ERROR,
120     CON_STATE_CLOSE
121 } request_state_t;
122 
123 struct request_st {
124     request_state_t state; /*(modules should not modify request state)*/
125     int http_status;
126     uint32_t h2state;      /*(modules should not modify request h2state)*/
127     uint32_t h2id;
128      int32_t h2_rwin;
129      int32_t h2_swin;
130 
131     http_method_t http_method;
132     http_version_t http_version;
133 
134     const plugin *handler_module;
135     void **plugin_ctx;           /* plugin connection specific config */
136     connection *con;
137 
138     /* config conditions (internal) */
139     uint32_t conditional_is_valid;
140     struct cond_cache_t *cond_cache;
141     struct cond_match_t *cond_match;
142 
143     request_config conf;
144 
145     /* request */
146     uint32_t rqst_header_len;
147     uint64_t rqst_htags;/* bitfield of flagged headers present in request */
148     array rqst_headers;
149 
150     request_uri uri;
151     physical physical;
152     array env; /* used to pass lighttpd internal stuff */
153 
154     off_t reqbody_length; /* request Content-Length */
155     off_t te_chunked;
156     off_t resp_body_scratchpad;
157 
158     buffer *http_host; /* copy of array value buffer ptr; not alloc'ed */
159     const buffer *server_name;
160 
161     buffer target;
162     buffer target_orig;
163 
164     buffer pathinfo;
165     buffer server_name_buf;
166 
167     /* response */
168     uint32_t resp_header_len;
169     uint64_t resp_htags; /*bitfield of flagged headers present in response*/
170     array resp_headers;
171     char resp_body_finished;
172     char resp_body_started;
173     char resp_send_chunked;
174     char resp_decode_chunked;
175     char resp_header_repeated;
176 
177     char loops_per_request;  /* catch endless loops in a single request */
178     int8_t keep_alive; /* only request.c can enable it, all other just disable */
179     char async_callback;
180 
181     buffer *tmp_buf;                    /* shared; same as srv->tmp_buf */
182     response_dechunk *gw_dechunk;
183 
184     off_t bytes_written_ckpt; /* used by mod_accesslog */
185     off_t bytes_read_ckpt;    /* used by mod_accesslog */
186     struct timespec start_hp;
187 
188     int error_handler_saved_status; /* error-handler */
189     http_method_t error_handler_saved_method; /* error-handler */
190 
191     struct chunkqueue write_queue;     /* HTTP response queue [ file, mem ] */
192     struct chunkqueue read_queue;      /* HTTP request queue  [ mem ] */
193     struct chunkqueue reqbody_queue; /*(might use tempfiles)*/
194 };
195 
196 
197 typedef struct http_header_parse_ctx {
198     char *k;
199     char *v;
200     uint32_t klen;
201     uint32_t vlen;
202     uint32_t hlen;
203     uint8_t pseudo;
204     uint8_t scheme;
205     uint8_t trailers;
206     uint8_t id;
207     uint32_t max_request_field_size;
208     unsigned int http_parseopts;
209 } http_header_parse_ctx;
210 
211 
212 int http_request_validate_pseudohdrs (request_st * restrict r, int scheme, unsigned int http_parseopts);
213 int http_request_parse_header (request_st * restrict r, http_header_parse_ctx * restrict hpctx);
214 void http_request_headers_process_h2 (request_st * restrict r, int scheme_port);
215 void http_request_headers_process (request_st * restrict r, char * restrict hdrs, const unsigned short * restrict hoff, int scheme_port);
216 int http_request_parse_target(request_st *r, int scheme_port);
217 int http_request_host_normalize(buffer *b, int scheme_port);
218 int http_request_host_policy(buffer *b, unsigned int http_parseopts, int scheme_port);
219 
220 int64_t li_restricted_strtoint64 (const char *v, const uint32_t vlen, const char ** const err);
221 
222 #endif
223