xref: /lighttpd1.4/src/request.h (revision 3a8fc4bc)
1 #ifndef _REQUEST_H_
2 #define _REQUEST_H_
3 #include "first.h"
4 
5 #include "sys-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 chunkqueue;      /* declaration */
14 struct cond_cache_t;    /* declaration */
15 struct cond_match_t;    /* declaration */
16 struct stat_cache_entry;/* declaration */
17 
18 typedef struct request_config {
19     fdlog_st *errh;
20     unsigned int http_parseopts;
21     uint32_t max_request_field_size;
22     const array *mimetypes;
23 
24     /* virtual-servers */
25     const buffer *document_root;
26     const buffer *server_name;
27     const buffer *server_tag;
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 int high_precision_timestamps:1;
37     unsigned int allow_http11:1;
38     unsigned int range_requests:1;
39     unsigned int follow_symlink:1;
40     unsigned int etag_flags:3;
41     unsigned int use_xattr:1;
42     unsigned int force_lowercase_filenames:2;/*(case-insensitive file systems)*/
43     unsigned int error_intercept:1;
44 
45     unsigned int h2proto:2; /*(global setting copied for convenient access)*/
46 
47     /* debug */
48     unsigned int log_request_handling:1;
49     unsigned int log_state_handling:1;
50     unsigned int log_condition_handling:1;
51     unsigned int log_response_header:1;
52     unsigned int log_request_header:1;
53     unsigned int log_request_header_on_error:1;
54     unsigned int log_file_not_found:1;
55     unsigned int log_timeouts:1;
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     fdlog_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 } physical;
98 
99 typedef struct {
100     off_t gw_chunked;
101     buffer b;
102     int done;
103 } response_dechunk;
104 
105 /* the order of the items should be the same as they are processed
106  * read before write as we use this later e.g. <= CON_STATE_REQUEST_END */
107 /* NB: must sync with http_request_state_short(), http_request_state_append() */
108 typedef enum {
109     CON_STATE_CONNECT,
110     CON_STATE_REQUEST_START,
111     CON_STATE_READ,
112     CON_STATE_REQUEST_END,
113     CON_STATE_READ_POST,
114     CON_STATE_HANDLE_REQUEST,
115     CON_STATE_RESPONSE_START,
116     CON_STATE_WRITE,
117     CON_STATE_RESPONSE_END,
118     CON_STATE_ERROR,
119     CON_STATE_CLOSE
120 } request_state_t;
121 
122 struct request_st {
123     request_state_t state; /*(modules should not modify request state)*/
124     int http_status;
125     uint32_t h2state;      /*(modules should not modify request h2state)*/
126     uint32_t h2id;
127      int32_t h2_rwin;
128      int32_t h2_swin;
129      int16_t h2_rwin_fudge;
130      uint8_t h2_prio;
131 
132     http_method_t http_method;
133     http_version_t http_version;
134 
135     const plugin *handler_module;
136     void **plugin_ctx;           /* plugin connection specific config */
137     connection *con;
138 
139     /* config conditions (internal) */
140     uint32_t conditional_is_valid;
141     struct cond_cache_t *cond_cache;
142     struct cond_match_t **cond_match;
143     struct cond_match_t *cond_match_data;
144 
145     request_config conf;
146 
147     /* request */
148     uint32_t rqst_header_len;
149     uint64_t rqst_htags;/* bitfield of flagged headers present in request */
150     array rqst_headers;
151 
152     request_uri uri;
153     physical physical;
154     array env; /* used to pass lighttpd internal stuff */
155 
156     off_t reqbody_length; /* request Content-Length */
157     off_t te_chunked;
158     off_t resp_body_scratchpad;
159 
160     buffer *http_host; /* copy of array value buffer ptr; not alloc'ed */
161     const buffer *server_name;
162 
163     buffer target;
164     buffer target_orig;
165 
166     buffer pathinfo;
167     buffer server_name_buf;
168 
169     void *dst_addr;
170     buffer *dst_addr_buf;
171 
172     /* response */
173     uint32_t resp_header_len;
174     uint64_t resp_htags; /*bitfield of flagged headers present in response*/
175     array resp_headers;
176     char resp_body_finished;
177     char resp_body_started;
178     char resp_send_chunked;
179     char resp_decode_chunked;
180     char resp_header_repeated;
181 
182     char loops_per_request;  /* catch endless loops in a single request */
183     int8_t keep_alive; /* only request.c can enable it, all other just disable */
184     char async_callback;
185 
186     buffer *tmp_buf;                    /* shared; same as srv->tmp_buf */
187     response_dechunk *gw_dechunk;
188 
189     off_t bytes_written_ckpt; /* used by mod_accesslog */
190     off_t bytes_read_ckpt;    /* used by mod_accesslog */
191     unix_timespec64_t start_hp;
192 
193     int error_handler_saved_status; /* error-handler */
194     http_method_t error_handler_saved_method; /* error-handler */
195 
196     struct chunkqueue write_queue;     /* HTTP response queue [ file, mem ] */
197     struct chunkqueue read_queue;      /* HTTP request queue  [ mem ] */
198     struct chunkqueue reqbody_queue; /*(might use tempfiles)*/
199 
200     struct stat_cache_entry *tmp_sce; /*(value valid only in sequential code)*/
201     int cond_captures;
202     int h2_connect_ext;
203 };
204 
205 
206 typedef struct http_header_parse_ctx {
207     char *k;
208     char *v;
209     uint32_t klen;
210     uint32_t vlen;
211     uint32_t hlen;
212     uint8_t pseudo;
213     uint8_t scheme;
214     uint8_t trailers;
215     int8_t id;
216     uint32_t max_request_field_size;
217     unsigned int http_parseopts;
218 } http_header_parse_ctx;
219 
220 
221 int http_request_validate_pseudohdrs (request_st * restrict r, int scheme, unsigned int http_parseopts);
222 int http_request_parse_header (request_st * restrict r, http_header_parse_ctx * restrict hpctx);
223 void http_request_headers_process_h2 (request_st * restrict r, int scheme_port);
224 void http_request_headers_process (request_st * restrict r, char * restrict hdrs, const unsigned short * restrict hoff, int scheme_port);
225 int http_request_parse_target(request_st *r, int scheme_port);
226 int http_request_host_normalize(buffer *b, int scheme_port);
227 int http_request_host_policy(buffer *b, unsigned int http_parseopts, int scheme_port);
228 
229 int64_t li_restricted_strtoint64 (const char *v, const uint32_t vlen, const char ** const err);
230 
231 
232 /* convenience macros/functions for display purposes */
233 
234 #define http_request_state_is_keep_alive(r) \
235   (CON_STATE_READ == (r)->state && !buffer_is_blank(&(r)->target_orig))
236 
237 #define http_con_state_is_keep_alive(con) \
238   ((con)->h2                              \
239    ? 0 == (con)->h2->rused                \
240    : http_request_state_is_keep_alive(&(con)->request))
241 
242 #define http_con_state_append(b, con)                            \
243    (http_con_state_is_keep_alive(con)                            \
244     ? buffer_append_string_len((b), CONST_STR_LEN("keep-alive")) \
245     : http_request_state_append((b), (con)->request.state))
246 
247 #define http_con_state_short(con)     \
248    (http_con_state_is_keep_alive(con) \
249     ? "k"                             \
250     : http_request_state_short((con)->request.state))
251 
252 #define http_request_stats_bytes_in(r) \
253    ((r)->read_queue.bytes_out - (r)->bytes_read_ckpt)
254 
255 #define http_request_stats_bytes_out(r) \
256    ((r)->write_queue.bytes_out - (r)->bytes_written_ckpt)
257 
258 __attribute_pure__
259 const char * http_request_state_short (request_state_t state);
260 
261 void http_request_state_append (buffer *b, request_state_t state);
262 
263 
264 #endif
265