Remove old CGI call: M_CGI_change_dir
[MonkeyD.git] / src / cgi.c
blobc3a42e15025ebb5e6ff95513f42d319e680030e6
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
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.
22 #include <pthread.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
30 #include <sys/stat.h>
31 #include <sys/socket.h>
32 #include <sys/wait.h>
34 #include <string.h>
36 #include "monkey.h"
37 #include "http.h"
38 #include "http_status.h"
39 #include "cgi.h"
40 #include "method.h"
41 #include "str.h"
42 #include "memory.h"
43 #include "utils.h"
44 #include "config.h"
45 #include "support.h"
46 #include "logfile.h"
47 #include "socket.h"
48 #include "header.h"
49 #include "iov.h"
51 struct palm *mk_palm_get_handler(mk_pointer *file)
53 struct palm *p;
54 int j, len;
56 j = len = file->len;
58 /* looking for extension */
59 while(file->data[j]!='.' && j>=0)
60 j--;
62 if(j==0){
63 return NULL;
66 p = palms;
67 while(p)
69 if(strcasecmp(file->data+j+1, p->ext)==0)
71 return p;
73 p = p->next;
76 return NULL;
79 char *mk_palm_check_request(struct client_request *cr, struct request *sr)
81 int sock, ret, n=0, total=0;
82 char *buf;
83 int len = 50024;
84 struct palm *p;
85 struct mk_iov *iov;
87 p = mk_palm_get_handler(&sr->real_path);
88 if(!p)
90 return NULL;
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);
103 fflush(stdout);
105 mk_socket_set_cork_flag(sock, TCP_CORK_OFF);
107 buf = mk_mem_malloc_z(len);
108 do {
109 n=read(sock, buf+total, len);
110 total+=n;
111 }while(n>0);
113 return (char *)buf;
116 struct mk_iov *mk_palm_create_env(struct client_request *cr,
117 struct request *sr)
119 struct mk_iov *iov;
121 iov = mk_iov_create(100, 0);
123 mk_iov_add_entry(iov, sr->real_path.data,
124 sr->real_path.len,
125 mk_crlf,
126 MK_IOV_NOT_FREE_BUF);
128 mk_iov_add_entry(iov, mk_cgi_document_root.data,
129 mk_cgi_document_root.len,
130 mk_iov_equal,
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,
142 sr->content_length);
144 mk_palm_iov_add_header(iov, mk_cgi_content_type,
145 sr->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,
159 sr->user_agent);
161 if(sr->accept.data)
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,
166 sr->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);
176 if(sr->host.data)
177 mk_palm_iov_add_header(iov, mk_cgi_http_host, sr->host);
179 if(sr->cookies.data)
180 mk_palm_iov_add_header(iov, mk_cgi_http_cookie, sr->cookies);
182 if(sr->referer.data)
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);
199 /* CRLF */
200 mk_iov_add_entry(iov, mk_crlf.data, mk_crlf.len,
201 mk_iov_none, MK_IOV_NOT_FREE_BUF);
202 return iov;
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,
215 char *buf)
217 int len;
218 int i;
219 long n;
220 char *s;
221 char *status_msg = "Status: ";
223 len = 8;
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;
231 else{
232 i = 0;
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));
242 return 0;
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);