Refactoring mk_request_error()
[MonkeyD.git] / src / include / request.h
blob780022d030325e4b4b31e3b54bc272c59e9aa126
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /* request.c */
24 #include "memory.h"
25 #include "scheduler.h"
27 #ifndef MK_REQUEST_H
28 #define MK_REQUEST_H
30 #define MK_REQUEST_DEFAULT_PAGE "<HTML><BODY><H1>%s</H1>%s<BR><HR><ADDRESS>%s</ADDRESS></BODY></HTML>"
32 /* Handle index file names: index.* */
33 #define MAX_INDEX_NAME 50
34 struct indexfile
36 char indexname[MAX_INDEX_NAME];
37 struct indexfile *next;
38 } *first_index;
40 #define MK_CRLF "\r\n"
41 #define MK_ENDBLOCK "\r\n\r\n"
43 mk_pointer mk_crlf;
44 mk_pointer mk_endblock;
46 /* Headers */
47 #define RH_ACCEPT "Accept:"
48 #define RH_ACCEPT_CHARSET "Accept-Charset:"
49 #define RH_ACCEPT_ENCODING "Accept-Encoding:"
50 #define RH_ACCEPT_LANGUAGE "Accept-Language:"
51 #define RH_CONNECTION "Connection:"
52 #define RH_COOKIE "Cookie:"
53 #define RH_CONTENT_LENGTH "Content-Length:"
54 #define RH_CONTENT_RANGE "Content-Range:"
55 #define RH_CONTENT_TYPE "Content-type:"
56 #define RH_IF_MODIFIED_SINCE "If-Modified-Since:"
57 #define RH_HOST "Host:"
58 #define RH_LAST_MODIFIED "Last-Modified:"
59 #define RH_LAST_MODIFIED_SINCE "Last-Modified-Since:"
60 #define RH_REFERER "Referer:"
61 #define RH_RANGE "Range:"
62 #define RH_USER_AGENT "User-Agent:"
64 mk_pointer mk_rh_accept;
65 mk_pointer mk_rh_accept_charset;
66 mk_pointer mk_rh_accept_encoding;
67 mk_pointer mk_rh_accept_language;
68 mk_pointer mk_rh_connection;
69 mk_pointer mk_rh_cookie;
70 mk_pointer mk_rh_content_length;
71 mk_pointer mk_rh_content_range;
72 mk_pointer mk_rh_content_type;
73 mk_pointer mk_rh_if_modified_since;
74 mk_pointer mk_rh_host;
75 mk_pointer mk_rh_last_modified;
76 mk_pointer mk_rh_last_modified_since;
77 mk_pointer mk_rh_referer;
78 mk_pointer mk_rh_range;
79 mk_pointer mk_rh_user_agent;
81 /* Aqui se registran temporalmente los
82 parametros de una peticion */
83 #define MAX_REQUEST_METHOD 10
84 #define MAX_REQUEST_URI 1025
85 #define MAX_REQUEST_PROTOCOL 10
86 #define MAX_SCRIPTALIAS 3
88 #define MK_REQUEST_STATUS_INCOMPLETE -1
89 #define MK_REQUEST_STATUS_COMPLETED 0
91 #define EXIT_NORMAL 0
92 #define EXIT_ERROR -1
93 #define EXIT_ABORT -2
94 #define EXIT_PCONNECTION 24
96 struct request_idx
98 struct client_request *first;
99 struct client_request *last;
102 struct client_request
104 int pipelined; /* Pipelined request */
105 int socket;
106 int counter_connections; /* Count persistent connections */
107 int status; /* Request status */
108 char *body; /* Original request sent */
110 mk_pointer *ipv4;
112 int body_length;
114 int body_pos_end;
115 int first_method;
117 time_t init_time;
118 struct request *request; /* Parsed request */
119 struct client_request *next;
122 pthread_key_t request_index;
124 struct header_toc
126 char *init;
127 char *end;
128 int status; /* 0: not found, 1: found = skip! */
129 struct header_toc *next;
132 /* Request plugin Handler, each request can be handled by
133 * several plugins, we handle list in a simple list */
134 struct handler
136 struct plugin *p;
137 struct handler *next;
140 struct request
142 int status;
144 int pipelined; /* Pipelined request */
145 mk_pointer body;
147 /*----First header of client request--*/
148 int method;
149 mk_pointer method_p;
150 mk_pointer uri; /* original request */
151 char *uri_processed; /* processed request */
152 int uri_twin;
154 int protocol;
155 mk_pointer protocol_p;
157 /* If request specify Connection: close, Monkey will
158 * close the connection after send the response, by
159 * default this var is set to VAR_OFF;
161 int close_now;
163 /*---Request headers--*/
164 int content_length;
165 mk_pointer accept;
166 mk_pointer accept_language;
167 mk_pointer accept_encoding;
168 mk_pointer accept_charset;
169 mk_pointer content_type;
170 mk_pointer connection;
171 mk_pointer cookies;
172 mk_pointer host;
173 mk_pointer if_modified_since;
174 mk_pointer last_modified_since;
175 mk_pointer range;
176 mk_pointer referer;
177 mk_pointer resume;
178 mk_pointer user_agent;
179 /*---------------------*/
181 /* POST */
182 mk_pointer post_variables;
183 /*-----------------*/
185 /*-Internal-*/
186 mk_pointer real_path; /* Absolute real path */
187 char *user_uri; /* ~user/...path */
188 mk_pointer query_string; /* ?... */
190 char *virtual_user; /* Virtualhost user */
191 char *script_filename;
192 int keep_alive;
193 int user_home; /* user_home request(VAR_ON/VAR_OFF) */
195 /*-Connection-*/
196 int port;
197 /*------------*/
199 /* file descriptors */
200 int fd_file;
202 struct file_info *file_info;
203 struct host *host_conf;
204 struct header_values *headers; /* headers response */
205 struct request *next;
207 long loop;
208 long bytes_to_send;
209 off_t bytes_offset;
211 /* Plugin handlers */
212 struct plugin *handled_by;
215 struct header_values
217 int status;
218 mk_pointer *status_p;
220 /* Length of the content to send */
221 long content_length;
223 /* Private value, real length of the file requested */
224 long real_length;
226 int cgi;
227 int pconnections_left;
228 int ranges[2];
229 int transfer_encoding;
230 int breakline;
232 time_t last_modified;
233 mk_pointer content_type;
234 char *location;
237 struct request *mk_request_parse(struct client_request *cr);
238 int mk_request_process(struct client_request *cr, struct request *s_request);
239 mk_pointer mk_request_index(char *pathfile);
242 /* Custom HTML Page for errors */
243 mk_pointer *mk_request_set_default_page(char *title, mk_pointer message,
244 char *signature);
246 int mk_request_header_process(struct request *sr);
247 mk_pointer mk_request_header_find(struct header_toc *toc, int toc_len,
248 char *request_body, mk_pointer header);
250 void mk_request_error(int http_status, struct client_request *cr,
251 struct request *sr);
253 struct request *mk_request_alloc();
254 void mk_request_free_list(struct client_request *cr);
255 void mk_request_free(struct request *sr);
257 struct client_request *mk_request_client_create(int socket);
258 struct client_request *mk_request_client_get(int socket);
259 void mk_request_client_remove(int socket);
261 void mk_request_init_error_msgs();
263 int mk_handler_read(int socket, struct client_request *cr);
264 int mk_handler_write(int socket, struct client_request *cr);
267 struct header_toc *mk_request_header_toc_create(int len);
268 void mk_request_header_toc_parse(struct header_toc *toc, int toc_len,
269 char *data, int len);
271 void mk_request_ka_next(struct client_request *cr);
272 #endif