1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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.
31 #include "http_status.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
,
61 unsigned long len
= 0;
63 struct header_values
*sh
;
68 iov
= mk_header_iov_get();
73 mk_header_iov_add_entry(iov
, mk_hr_http_ok
,
74 mk_iov_none
, MK_IOV_NOT_FREE_BUF
);
78 mk_header_iov_add_entry(iov
, mk_hr_http_partial
,
79 mk_iov_none
, MK_IOV_NOT_FREE_BUF
);
83 mk_header_iov_add_entry(iov
, mk_hr_redir_moved
,
84 mk_iov_none
, MK_IOV_NOT_FREE_BUF
);
88 mk_header_iov_add_entry(iov
, mk_hr_redir_moved_t
,
89 mk_iov_none
, MK_IOV_NOT_FREE_BUF
);
93 mk_header_iov_add_entry(iov
, mk_hr_not_modified
,
94 mk_iov_none
, MK_IOV_NOT_FREE_BUF
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
150 mk_header_iov_free(iov
);
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
);
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
);
170 if (sh
->last_modified
> 0) {
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
);
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
,
191 "Keep-Alive: timeout=%i, max=%i"
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
);
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
,
220 strlen(sh
->location
), mk_iov_crlf
, MK_IOV_FREE_BUF
);
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
);
247 if (sh
->content_length
>= 0) {
248 /* Map content length to MK_POINTER */
250 cl
= mk_cache_get(mk_cache_header_cl
);
251 mk_string_itop(sh
->content_length
, cl
);
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
) {
269 if (sh
->ranges
[0] >= 0 && sh
->ranges
[1] == -1) {
270 mk_string_build(&buffer
,
275 (sh
->real_length
- 1), sh
->real_length
);
276 mk_iov_add_entry(iov
, buffer
, len
, mk_iov_crlf
, MK_IOV_FREE_BUF
);
280 if (sh
->ranges
[0] >= 0 && sh
->ranges
[1] >= 0) {
281 mk_string_build(&buffer
,
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
);
291 if (sh
->ranges
[0] == -1 && sh
->ranges
[1] > 0) {
292 mk_string_build(&buffer
,
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
);
311 MK_TRACE("Headers sent to FD %i", fd
);
312 printf("%s", ANSI_YELLOW
);
314 mk_iov_send(0, iov
, MK_IOV_SEND_TO_SOCKET
);
315 printf("%s", ANSI_RESET
);
319 mk_header_iov_free(iov
);
323 char *mk_header_chunked_line(int len
)
327 buf
= mk_mem_malloc_z(10);
328 snprintf(buf
, 9, "%x%s", len
, MK_CRLF
);
333 void mk_header_set_http_status(struct request
*sr
, int status
)
335 sr
->headers
->status
= status
;
336 sr
->headers
->status_p
= mk_http_status_get(status
);
339 struct header_values
*mk_header_create()
341 struct header_values
*headers
;
344 (struct header_values
*) mk_mem_malloc(sizeof(struct header_values
));
345 headers
->ranges
[0] = -1;
346 headers
->ranges
[1] = -1;
347 headers
->content_length
= -1;
348 headers
->transfer_encoding
= -1;
349 headers
->last_modified
= -1;
350 // mk_pointer_reset(&headers->content_length_p);
351 mk_pointer_reset(&headers
->content_type
);
352 headers
->location
= NULL
;