Security Plugin: set http status for URL rule match
[MonkeyD.git] / src / header.c
blob5be1c4e13b97366cd0e43a05c82bf5b1470f07e2
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 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "monkey.h"
27 #include "header.h"
28 #include "memory.h"
29 #include "request.h"
30 #include "iov.h"
31 #include "http_status.h"
32 #include "config.h"
33 #include "socket.h"
34 #include "utils.h"
35 #include "clock.h"
36 #include "cache.h"
37 #include "http.h"
38 #include "str.h"
40 int mk_header_iov_add_entry(struct mk_iov *mk_io, mk_pointer data,
41 mk_pointer sep, int free)
43 return mk_iov_add_entry(mk_io, data.data, data.len, sep, free);
46 struct mk_iov *mk_header_iov_get()
48 return mk_cache_get(mk_cache_iov_header);
51 void mk_header_iov_free(struct mk_iov *iov)
53 mk_iov_free_marked(iov);
56 /* Send_Header , envia las cabeceras principales */
57 int mk_header_send(int fd, struct client_request *cr,
58 struct request *sr)
60 int fd_status = 0;
61 unsigned long len = 0;
62 char *buffer = 0;
63 struct header_values *sh;
64 struct mk_iov *iov;
66 sh = sr->headers;
68 iov = mk_header_iov_get();
70 /* Status Code */
71 switch (sh->status) {
72 case M_HTTP_OK:
73 mk_header_iov_add_entry(iov, mk_hr_http_ok,
74 mk_iov_none, MK_IOV_NOT_FREE_BUF);
75 break;
77 case M_HTTP_PARTIAL:
78 mk_header_iov_add_entry(iov, mk_hr_http_partial,
79 mk_iov_none, MK_IOV_NOT_FREE_BUF);
80 break;
82 case M_REDIR_MOVED:
83 mk_header_iov_add_entry(iov, mk_hr_redir_moved,
84 mk_iov_none, MK_IOV_NOT_FREE_BUF);
85 break;
87 case M_REDIR_MOVED_T:
88 mk_header_iov_add_entry(iov, mk_hr_redir_moved_t,
89 mk_iov_none, MK_IOV_NOT_FREE_BUF);
90 break;
92 case M_NOT_MODIFIED:
93 mk_header_iov_add_entry(iov, mk_hr_not_modified,
94 mk_iov_none, MK_IOV_NOT_FREE_BUF);
95 break;
97 case M_CLIENT_BAD_REQUEST:
98 mk_header_iov_add_entry(iov, mk_hr_client_bad_request,
99 mk_iov_none, MK_IOV_NOT_FREE_BUF);
100 break;
102 case M_CLIENT_FORBIDDEN:
103 mk_header_iov_add_entry(iov, mk_hr_client_forbidden,
104 mk_iov_none, MK_IOV_NOT_FREE_BUF);
105 break;
107 case M_CLIENT_NOT_FOUND:
108 mk_header_iov_add_entry(iov, mk_hr_client_not_found,
109 mk_iov_none, MK_IOV_NOT_FREE_BUF);
110 break;
112 case M_CLIENT_METHOD_NOT_ALLOWED:
113 mk_header_iov_add_entry(iov, mk_hr_client_method_not_allowed,
114 mk_iov_none, MK_IOV_NOT_FREE_BUF);
115 break;
117 case M_CLIENT_REQUEST_TIMEOUT:
118 mk_header_iov_add_entry(iov, mk_hr_client_request_timeout,
119 mk_iov_none, MK_IOV_NOT_FREE_BUF);
120 break;
122 case M_CLIENT_LENGTH_REQUIRED:
123 mk_header_iov_add_entry(iov, mk_hr_client_length_required,
124 mk_iov_none, MK_IOV_NOT_FREE_BUF);
125 break;
127 case M_CLIENT_REQUEST_ENTITY_TOO_LARGE:
128 mk_header_iov_add_entry(iov, mk_hr_client_request_entity_too_large,
129 mk_iov_none, MK_IOV_NOT_FREE_BUF);
130 break;
132 case M_SERVER_NOT_IMPLEMENTED:
133 mk_header_iov_add_entry(iov, mk_hr_server_not_implemented,
134 mk_iov_none, MK_IOV_NOT_FREE_BUF);
135 break;
137 case M_SERVER_INTERNAL_ERROR:
138 mk_header_iov_add_entry(iov, mk_hr_server_internal_error,
139 mk_iov_none, MK_IOV_NOT_FREE_BUF);
140 break;
142 case M_SERVER_HTTP_VERSION_UNSUP:
143 mk_header_iov_add_entry(iov,
144 mk_hr_server_http_version_unsup,
145 mk_iov_none, MK_IOV_NOT_FREE_BUF);
146 break;
149 if (fd_status < 0) {
150 mk_header_iov_free(iov);
151 return -1;
154 /* Server details */
155 mk_iov_add_entry(iov, sr->host_conf->header_host_signature.data,
156 sr->host_conf->header_host_signature.len,
157 mk_iov_crlf, MK_IOV_NOT_FREE_BUF);
159 /* Date */
160 mk_iov_add_entry(iov,
161 mk_header_short_date.data,
162 mk_header_short_date.len,
163 mk_iov_none, MK_IOV_NOT_FREE_BUF);
164 mk_iov_add_entry(iov,
165 header_current_time.data,
166 header_current_time.len,
167 mk_iov_none, MK_IOV_NOT_FREE_BUF);
169 /* Last-Modified */
170 if (sh->last_modified > 0) {
171 mk_pointer *lm;
172 lm = mk_cache_get(mk_cache_header_lm);
173 mk_utils_utime2gmt(&lm, sh->last_modified);
175 mk_iov_add_entry(iov, mk_header_last_modified.data,
176 mk_header_last_modified.len,
177 mk_iov_none, MK_IOV_NOT_FREE_BUF);
178 mk_iov_add_entry(iov, lm->data, lm->len,
179 mk_iov_none, MK_IOV_NOT_FREE_BUF);
182 /* Connection */
183 if (config->keep_alive == VAR_ON &&
184 sr->keep_alive == VAR_ON &&
185 cr->counter_connections < config->max_keep_alive_request) {
187 /* A valid connection header */
188 if (sr->connection.len > 0) {
189 mk_string_build(&buffer,
190 &len,
191 "Keep-Alive: timeout=%i, max=%i"
192 MK_CRLF,
193 config->keep_alive_timeout,
194 (config->max_keep_alive_request -
195 cr->counter_connections)
197 mk_iov_add_entry(iov, buffer, len, mk_iov_none, MK_IOV_FREE_BUF);
198 mk_iov_add_entry(iov,
199 mk_header_conn_ka.data,
200 mk_header_conn_ka.len,
201 mk_iov_none, MK_IOV_NOT_FREE_BUF);
204 else if(sr->close_now == VAR_ON) {
205 mk_iov_add_entry(iov,
206 mk_header_conn_close.data,
207 mk_header_conn_close.len,
208 mk_iov_none, MK_IOV_NOT_FREE_BUF);
211 /* Location */
212 if (sh->location != NULL) {
213 mk_iov_add_entry(iov,
214 mk_header_short_location.data,
215 mk_header_short_location.len,
216 mk_iov_none, MK_IOV_NOT_FREE_BUF);
218 mk_iov_add_entry(iov,
219 sh->location,
220 strlen(sh->location), mk_iov_crlf, MK_IOV_FREE_BUF);
223 /* Content type */
224 if (sh->content_type.len > 0) {
225 mk_iov_add_entry(iov,
226 mk_header_short_ct.data,
227 mk_header_short_ct.len,
228 mk_iov_none, MK_IOV_NOT_FREE_BUF);
230 mk_iov_add_entry(iov,
231 sh->content_type.data,
232 sh->content_type.len,
233 mk_iov_none, MK_IOV_NOT_FREE_BUF);
236 /* Transfer Encoding */
237 switch (sh->transfer_encoding) {
238 case MK_HEADER_TE_TYPE_CHUNKED:
239 mk_iov_add_entry(iov,
240 mk_header_te_chunked.data,
241 mk_header_te_chunked.len,
242 mk_iov_none, MK_IOV_NOT_FREE_BUF);
243 break;
246 /* Content-Length */
247 if (sh->content_length >= 0 && sr->method != HTTP_METHOD_HEAD) {
248 /* Map content length to MK_POINTER */
249 mk_pointer *cl;
250 cl = mk_cache_get(mk_cache_header_cl);
251 mk_string_itop(sh->content_length, cl);
253 /* Set headers */
254 mk_iov_add_entry(iov, mk_header_content_length.data,
255 mk_header_content_length.len,
256 mk_iov_none, MK_IOV_NOT_FREE_BUF);
258 mk_iov_add_entry(iov, cl->data, cl->len,
259 mk_iov_none, MK_IOV_NOT_FREE_BUF);
263 if ((sh->content_length != 0 &&
264 (sh->ranges[0] >= 0 || sh->ranges[1] >= 0)) &&
265 config->resume == VAR_ON) {
266 buffer = 0;
268 /* yyy- */
269 if (sh->ranges[0] >= 0 && sh->ranges[1] == -1) {
270 mk_string_build(&buffer,
271 &len,
272 "%s bytes %d-%d/%d",
273 RH_CONTENT_RANGE,
274 sh->ranges[0],
275 (sh->real_length - 1), sh->real_length);
276 mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF);
279 /* yyy-xxx */
280 if (sh->ranges[0] >= 0 && sh->ranges[1] >= 0) {
281 mk_string_build(&buffer,
282 &len,
283 "%s bytes %d-%d/%d",
284 RH_CONTENT_RANGE,
285 sh->ranges[0], sh->ranges[1], sh->real_length);
287 mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF);
290 /* -xxx */
291 if (sh->ranges[0] == -1 && sh->ranges[1] > 0) {
292 mk_string_build(&buffer,
293 &len,
294 "%s bytes %d-%d/%d",
295 RH_CONTENT_RANGE,
296 (sh->real_length - sh->ranges[1]),
297 (sh->real_length - 1), sh->real_length);
298 mk_iov_add_entry(iov, buffer, len, mk_iov_crlf, MK_IOV_FREE_BUF);
302 if (sh->cgi == SH_NOCGI || sh->breakline == MK_HEADER_BREAKLINE) {
303 mk_iov_add_entry(iov, mk_iov_crlf.data, mk_iov_crlf.len,
304 mk_iov_none, MK_IOV_NOT_FREE_BUF);
307 mk_socket_set_cork_flag(fd, TCP_CORK_ON);
308 mk_iov_send(fd, iov, MK_IOV_SEND_TO_SOCKET);
310 #ifdef TRACE
311 MK_TRACE("Headers sent to FD %i", fd);
312 printf("%s", ANSI_YELLOW);
313 fflush(stdout);
314 mk_iov_send(0, iov, MK_IOV_SEND_TO_SOCKET);
315 printf("%s", ANSI_RESET);
316 fflush(stdout);
317 #endif
319 mk_header_iov_free(iov);
320 return 0;
323 char *mk_header_chunked_line(int len)
325 char *buf;
327 buf = mk_mem_malloc_z(10);
328 snprintf(buf, 9, "%x%s", len, MK_CRLF);
330 return buf;
333 void mk_header_set_http_status(struct request *sr, int status)
335 sr->headers->status = status;
338 struct header_values *mk_header_create()
340 struct header_values *headers;
342 headers =
343 (struct header_values *) mk_mem_malloc(sizeof(struct header_values));
344 headers->ranges[0] = -1;
345 headers->ranges[1] = -1;
346 headers->content_length = -1;
347 headers->transfer_encoding = -1;
348 headers->last_modified = -1;
349 mk_pointer_reset(&headers->content_type);
350 headers->location = NULL;
352 return headers;