1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
5 * Copyright (C) 2001-2009, Eduardo Silva P.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <sys/types.h>
31 #include <sys/socket.h>
38 #include "http_status.h"
51 struct palm
*mk_palm_get_handler(mk_pointer
*file
)
58 /* looking for extension */
59 while(file
->data
[j
]!='.' && j
>=0)
69 if(strcasecmp(file
->data
+j
+1, p
->ext
)==0)
79 char *mk_palm_check_request(struct client_request
*cr
, struct request
*sr
)
81 int sock
, ret
, n
=0, total
=0;
87 p
= mk_palm_get_handler(&sr
->real_path
);
93 sock
= mk_socket_create();
94 ret
= mk_socket_connect(sock
, p
->host
, p
->port
);
96 iov
= mk_palm_create_env(cr
, sr
);
98 mk_socket_set_tcp_nodelay(sock
);
99 mk_socket_set_cork_flag(sock
, TCP_CORK_ON
);
100 mk_iov_send(sock
, iov
, MK_IOV_SEND_TO_SOCKET
);
102 n
= write(sock
, "\r\n\r\n", 2);
105 mk_socket_set_cork_flag(sock
, TCP_CORK_OFF
);
107 buf
= mk_mem_malloc_z(len
);
109 n
=read(sock
, buf
+total
, len
);
116 struct mk_iov
*mk_palm_create_env(struct client_request
*cr
,
121 iov
= mk_iov_create(100, 0);
123 mk_iov_add_entry(iov
, sr
->real_path
.data
,
126 MK_IOV_NOT_FREE_BUF
);
128 mk_iov_add_entry(iov
, mk_cgi_document_root
.data
,
129 mk_cgi_document_root
.len
,
131 MK_IOV_NOT_FREE_BUF
);
134 mk_iov_add_entry(iov
, sr
->host_conf
->documentroot
.data
,
135 sr
->host_conf
->documentroot
.len
, mk_iov_crlf
,
136 MK_IOV_NOT_FREE_BUF
);
139 if(sr
->method
== HTTP_METHOD_POST
&& sr
->content_length
>0){
140 /* FIX Content length:
141 mk_palm_iov_add_header(iov, mk_cgi_content_length,
144 mk_palm_iov_add_header(iov
, mk_cgi_content_type
,
149 //mk_palm_iov_add_header(iov, mk_cgi_server_addr, config->server_addr);
150 mk_palm_iov_add_header(iov
, mk_cgi_server_name
, sr
->host
);
151 mk_palm_iov_add_header(iov
, mk_cgi_server_protocol
, mk_monkey_protocol
);
152 mk_palm_iov_add_header(iov
, mk_cgi_server_software
,
153 config
->server_software
);
154 // mk_palm_iov_add_header(iov, mk_cgi_server_signature,
155 // sr->host_conf->host_signature);
157 if(sr
->user_agent
.data
)
158 mk_palm_iov_add_header(iov
, mk_cgi_http_user_agent
,
162 mk_palm_iov_add_header(iov
, mk_cgi_http_accept
, sr
->accept
);
164 if(sr
->accept_charset
.data
)
165 mk_palm_iov_add_header(iov
, mk_cgi_http_accept_charset
,
168 if(sr
->accept_encoding
.data
)
169 mk_palm_iov_add_header(iov
, mk_cgi_http_accept_encoding
,
170 sr
->accept_encoding
);
172 if(sr
->accept_language
.data
)
173 mk_palm_iov_add_header(iov
, mk_cgi_http_accept_language
,
174 sr
->accept_language
);
177 mk_palm_iov_add_header(iov
, mk_cgi_http_host
, sr
->host
);
180 mk_palm_iov_add_header(iov
, mk_cgi_http_cookie
, sr
->cookies
);
183 mk_palm_iov_add_header(iov
, mk_cgi_http_referer
, sr
->referer
);
185 // mk_palm_iov_add_header(iov, mk_cgi_server_port, mk_monkey_port);
186 mk_palm_iov_add_header(iov
, mk_cgi_gateway_interface
, mk_cgi_version
);
187 mk_palm_iov_add_header(iov
, mk_cgi_remote_addr
, cr
->ip
);
188 mk_palm_iov_add_header(iov
, mk_cgi_request_uri
, sr
->uri
);
189 //mk_palm_iov_add_header(iov, mk_cgi_request_method, sr->method);
190 mk_palm_iov_add_header(iov
, mk_cgi_script_name
, sr
->uri
);
193 /* real path is not an mk_pointer */
194 mk_palm_iov_add_header(iov
, mk_cgi_script_filename
, sr
->real_path
);
195 //mk_palm_iov_add_header(iov, mk_cgi_remote_port, cr->port);
196 mk_palm_iov_add_header(iov
, mk_cgi_query_string
, sr
->query_string
);
197 //mk_palm_iov_add_header(iov, mk_cgi_post_vars, sr->post_variables);
200 mk_iov_add_entry(iov
, mk_crlf
.data
, mk_crlf
.len
,
201 mk_iov_none
, MK_IOV_NOT_FREE_BUF
);
205 void mk_palm_iov_add_header(struct mk_iov
*iov
,
206 mk_pointer header
, mk_pointer value
)
208 mk_iov_add_entry(iov
, header
.data
, header
.len
,
209 mk_iov_equal
, MK_IOV_NOT_FREE_BUF
);
210 mk_iov_add_entry(iov
, value
.data
, value
.len
,
211 mk_iov_crlf
, MK_IOV_NOT_FREE_BUF
);
214 int mk_palm_send_response(struct client_request
*cr
, struct request
*sr
,
221 char *status_msg
= "Status: ";
224 if(strncasecmp(buf
, status_msg
, len
)==0)
226 i
= mk_string_search(buf
+len
, " ");
227 s
= mk_string_copy_substr(buf
, len
, len
+i
);
228 sr
->headers
->status
= atoi(s
);
229 i
= mk_string_search(buf
, mk_crlf
.data
) + mk_crlf
.len
;
233 sr
->headers
->status
= M_HTTP_OK
;
236 sr
->headers
->cgi
= SH_CGI
;
237 sr
->headers
->content_length
= 0;
239 mk_socket_set_cork_flag(cr
->socket
, TCP_CORK_ON
);
240 mk_header_send(cr
->socket
, cr
, sr
, sr
->log
);
241 n
= write(cr
->socket
, buf
+i
, strlen(buf
+i
));
245 void mk_palm_set_env()
247 mk_pointer_set(&mk_cgi_document_root
, MK_CGI_DOCUMENT_ROOT
);
248 mk_pointer_set(&mk_cgi_content_length
, MK_CGI_CONTENT_LENGTH
);
249 mk_pointer_set(&mk_cgi_content_type
, MK_CGI_CONTENT_TYPE
);
250 mk_pointer_set(&mk_cgi_server_addr
, MK_CGI_SERVER_ADDR
);
251 mk_pointer_set(&mk_cgi_server_name
, MK_CGI_SERVER_NAME
);
252 mk_pointer_set(&mk_cgi_server_protocol
, MK_CGI_SERVER_PROTOCOL
);
253 mk_pointer_set(&mk_cgi_server_software
, MK_CGI_SERVER_SOFTWARE
);
254 mk_pointer_set(&mk_cgi_server_signature
, MK_CGI_SERVER_SIGNATURE
);
255 mk_pointer_set(&mk_cgi_http_user_agent
, MK_CGI_HTTP_USER_AGENT
);
256 mk_pointer_set(&mk_cgi_http_accept
, MK_CGI_HTTP_ACCEPT
);
257 mk_pointer_set(&mk_cgi_http_accept_charset
, MK_CGI_HTTP_ACCEPT_CHARSET
);
258 mk_pointer_set(&mk_cgi_http_accept_encoding
, MK_CGI_HTTP_ACCEPT_ENCODING
);
259 mk_pointer_set(&mk_cgi_http_accept_language
, MK_CGI_HTTP_ACCEPT_LANGUAGE
);
260 mk_pointer_set(&mk_cgi_http_host
, MK_CGI_HTTP_HOST
);
261 mk_pointer_set(&mk_cgi_http_cookie
, MK_CGI_HTTP_COOKIE
);
262 mk_pointer_set(&mk_cgi_http_referer
, MK_CGI_HTTP_REFERER
);
263 mk_pointer_set(&mk_cgi_server_port
, MK_CGI_SERVER_PORT
);
264 mk_pointer_set(&mk_cgi_cgi_version
, MK_CGI_CGI_VERSION
);
265 mk_pointer_set(&mk_cgi_gateway_interface
, MK_CGI_GATEWAY_INTERFACE
);
266 mk_pointer_set(&mk_cgi_remote_addr
, MK_CGI_REMOTE_ADDR
);
267 mk_pointer_set(&mk_cgi_request_uri
, MK_CGI_REQUEST_URI
);
268 mk_pointer_set(&mk_cgi_request_method
, MK_CGI_REQUEST_METHOD
);
269 mk_pointer_set(&mk_cgi_script_name
, MK_CGI_SCRIPT_NAME
);
270 mk_pointer_set(&mk_cgi_script_filename
, MK_CGI_SCRIPT_FILENAME
);
271 mk_pointer_set(&mk_cgi_remote_port
, MK_CGI_REMOTE_PORT
);
272 mk_pointer_set(&mk_cgi_query_string
, MK_CGI_QUERY_STRING
);
273 mk_pointer_set(&mk_cgi_post_vars
, MK_CGI_POST_VARS
);