1 |
786 |
skrzyp |
/* =================================================================
|
2 |
|
|
*
|
3 |
|
|
* cgi.c
|
4 |
|
|
*
|
5 |
|
|
* Handles the CGI requests via OBJLOADER or tcl
|
6 |
|
|
*
|
7 |
|
|
* =================================================================
|
8 |
|
|
* ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
* -------------------------------------------
|
10 |
|
|
* This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
* Copyright (C) 2005 Free Software Foundation, Inc.
|
12 |
|
|
*
|
13 |
|
|
* eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
* the terms of the GNU General Public License as published by the Free
|
15 |
|
|
* Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
* version.
|
17 |
|
|
*
|
18 |
|
|
* eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
* for more details.
|
22 |
|
|
*
|
23 |
|
|
* You should have received a copy of the GNU General Public License
|
24 |
|
|
* along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
*
|
27 |
|
|
* As a special exception, if other files instantiate templates or use
|
28 |
|
|
* macros or inline functions from this file, or you compile this file
|
29 |
|
|
* and link it with other works to produce a work based on this file,
|
30 |
|
|
* this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
* the GNU General Public License. However the source code for this file
|
32 |
|
|
* must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
* General Public License v2.
|
34 |
|
|
*
|
35 |
|
|
* This exception does not invalidate any other reasons why a work based
|
36 |
|
|
* on this file might be covered by the GNU General Public License.
|
37 |
|
|
* -------------------------------------------
|
38 |
|
|
* ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
* =================================================================
|
40 |
|
|
* #####DESCRIPTIONBEGIN####
|
41 |
|
|
*
|
42 |
|
|
* Author(s): Anthony Tonizzo (atonizzo@gmail.com)
|
43 |
|
|
* Contributors: Sergei Gavrikov (w3sg@SoftHome.net)
|
44 |
|
|
* Date: 2006-06-12
|
45 |
|
|
* Purpose:
|
46 |
|
|
* Description:
|
47 |
|
|
*
|
48 |
|
|
* ####DESCRIPTIONEND####
|
49 |
|
|
*
|
50 |
|
|
* =================================================================
|
51 |
|
|
*/
|
52 |
|
|
#include <pkgconf/hal.h>
|
53 |
|
|
#include <pkgconf/kernel.h>
|
54 |
|
|
#include <cyg/hal/hal_tables.h>
|
55 |
|
|
#include <cyg/fileio/fileio.h>
|
56 |
|
|
#include <dirent.h>
|
57 |
|
|
#include <network.h>
|
58 |
|
|
|
59 |
|
|
#include <string.h>
|
60 |
|
|
#include <stdio.h> // sprintf().
|
61 |
|
|
|
62 |
|
|
#define JIM_EMBEDDED
|
63 |
|
|
#include <cyg/athttpd/http.h>
|
64 |
|
|
#include <cyg/athttpd/socket.h>
|
65 |
|
|
#include <cyg/athttpd/handler.h>
|
66 |
|
|
#include <cyg/athttpd/forms.h>
|
67 |
|
|
|
68 |
|
|
#ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER
|
69 |
|
|
#include <cyg/objloader/elf.h>
|
70 |
|
|
#include <cyg/objloader/objelf.h>
|
71 |
|
|
#endif
|
72 |
|
|
|
73 |
|
|
// =============================================================================
|
74 |
|
|
// Objloader CGI Support
|
75 |
|
|
// =============================================================================
|
76 |
|
|
#ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER
|
77 |
|
|
CYG_LDR_TABLE_STDIO()
|
78 |
|
|
CYG_LDR_TABLE_STRING()
|
79 |
|
|
|
80 |
|
|
// Chunked transfers.
|
81 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_httpd_start_chunked_entry, \
|
82 |
|
|
"cyg_httpd_start_chunked", \
|
83 |
|
|
cyg_httpd_start_chunked);
|
84 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_httpd_write_chunked_entry, \
|
85 |
|
|
"cyg_httpd_write_chunked", \
|
86 |
|
|
cyg_httpd_write_chunked);
|
87 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_httpd_end_chunked_entry, \
|
88 |
|
|
"cyg_httpd_end_chunked", \
|
89 |
|
|
cyg_httpd_end_chunked);
|
90 |
|
|
|
91 |
|
|
// Mime strings.
|
92 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_httpd_write_entry, \
|
93 |
|
|
"cyg_httpd_write", \
|
94 |
|
|
cyg_httpd_write);
|
95 |
|
|
|
96 |
|
|
// Entry to find a GET/POST variable in the table.
|
97 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_httpd_find_form_variable_entry, \
|
98 |
|
|
"cyg_httpd_find_form_variable", \
|
99 |
|
|
cyg_httpd_find_form_variable);
|
100 |
|
|
|
101 |
|
|
// Finds an internal resource given the URL. Returns a pointer to the entry
|
102 |
|
|
// in the resource table.
|
103 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_httpd_find_ires_entry, \
|
104 |
|
|
"cyg_httpd_find_ires", \
|
105 |
|
|
cyg_httpd_find_ires);
|
106 |
|
|
// Sends an internal resource out, given the entry in the reource table.
|
107 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_httpd_send_ires_entry, \
|
108 |
|
|
"cyg_httpd_send_ires", \
|
109 |
|
|
cyg_httpd_send_ires);
|
110 |
|
|
|
111 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_httpd_format_header_entry, \
|
112 |
|
|
"cyg_httpd_format_header", \
|
113 |
|
|
cyg_httpd_format_header);
|
114 |
|
|
CYG_LDR_TABLE_ENTRY(diag_printf_entry, "diag_printf", diag_printf);
|
115 |
|
|
CYG_LDR_TABLE_ENTRY(cyg_httpd_find_mime_string_entry, \
|
116 |
|
|
"cyg_httpd_find_mime_string", \
|
117 |
|
|
cyg_httpd_find_mime_string);
|
118 |
|
|
#endif // CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER
|
119 |
|
|
|
120 |
|
|
#ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER
|
121 |
|
|
cyg_int32 cyg_httpd_exec_cgi_objloader(char *file_name)
|
122 |
|
|
{
|
123 |
|
|
// Check if the file is in the file system.
|
124 |
|
|
struct stat sp;
|
125 |
|
|
cyg_int32 (*fn)(CYG_HTTPD_STATE*);
|
126 |
|
|
|
127 |
|
|
cyg_int32 rc = stat(file_name, &sp);
|
128 |
|
|
if(rc < 0)
|
129 |
|
|
{
|
130 |
|
|
cyg_httpd_send_error(CYG_HTTPD_STATUS_NOT_FOUND);
|
131 |
|
|
return 0;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
void *lib_handle = cyg_ldr_open_library((CYG_ADDRWORD)file_name, 0);
|
135 |
|
|
CYG_ASSERT(lib_handle != 0, "Error opening the library");
|
136 |
|
|
if (lib_handle == 0)
|
137 |
|
|
{
|
138 |
|
|
cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR);
|
139 |
|
|
return 0;
|
140 |
|
|
}
|
141 |
|
|
fn = cyg_ldr_find_symbol(lib_handle, "exec_cgi");
|
142 |
|
|
if (fn)
|
143 |
|
|
{
|
144 |
|
|
cyg_int32 rc = fn(&httpstate);
|
145 |
|
|
cyg_ldr_close_library(lib_handle);
|
146 |
|
|
return rc;
|
147 |
|
|
}
|
148 |
|
|
else
|
149 |
|
|
{
|
150 |
|
|
cyg_ldr_close_library(lib_handle);
|
151 |
|
|
cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR);
|
152 |
|
|
return 0;
|
153 |
|
|
}
|
154 |
|
|
}
|
155 |
|
|
#endif
|
156 |
|
|
|
157 |
|
|
// =============================================================================
|
158 |
|
|
// tcl CGI Support
|
159 |
|
|
// =============================================================================
|
160 |
|
|
#ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_TCL
|
161 |
|
|
int Jim_AioInit(Jim_Interp *);
|
162 |
|
|
cyg_int32
|
163 |
|
|
cyg_httpd_exec_cgi_tcl(char *file_name)
|
164 |
|
|
{
|
165 |
|
|
// Make sure that tcl sees the internal variables including the post_data.
|
166 |
|
|
cyg_httpd_fvars_table_entry *entry = cyg_httpd_fvars_table;
|
167 |
|
|
while (entry != cyg_httpd_fvars_table_end)
|
168 |
|
|
{
|
169 |
|
|
if (strlen(entry->buf) != 0)
|
170 |
|
|
Jim_SetVariableStrWithStr(httpstate.jim_interp,
|
171 |
|
|
entry->name,
|
172 |
|
|
entry->buf);
|
173 |
|
|
entry++;
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
if (httpstate.post_data != NULL)
|
177 |
|
|
Jim_SetVariableStrWithStr(httpstate.jim_interp,
|
178 |
|
|
"post_data",
|
179 |
|
|
httpstate.post_data);
|
180 |
|
|
|
181 |
|
|
char tcl_cmd[CYG_HTTPD_MAXPATH];
|
182 |
|
|
sprintf(tcl_cmd, "source %s", file_name);
|
183 |
|
|
Jim_Eval(httpstate.jim_interp, tcl_cmd);
|
184 |
|
|
return 0;
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
int
|
188 |
|
|
cyg_httpd_Jim_Command_startchunked(Jim_Interp *interp,
|
189 |
|
|
int argc,
|
190 |
|
|
Jim_Obj *const *argv)
|
191 |
|
|
{
|
192 |
|
|
char *buf = (char*)Jim_GetString(argv[1], NULL);
|
193 |
|
|
cyg_httpd_start_chunked(buf);
|
194 |
|
|
return JIM_OK;
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
int
|
198 |
|
|
cyg_httpd_Jim_Command_writechunked(Jim_Interp *interp,
|
199 |
|
|
int argc,
|
200 |
|
|
Jim_Obj *const *argv)
|
201 |
|
|
{
|
202 |
|
|
int len;
|
203 |
|
|
char *buf = (char*)Jim_GetString(argv[1], &len);
|
204 |
|
|
cyg_httpd_write_chunked(buf, len);
|
205 |
|
|
return JIM_OK;
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
int
|
209 |
|
|
cyg_httpd_Jim_Command_endchunked(Jim_Interp *interp,
|
210 |
|
|
int argc,
|
211 |
|
|
Jim_Obj *const *argv)
|
212 |
|
|
{
|
213 |
|
|
cyg_httpd_end_chunked();
|
214 |
|
|
return JIM_OK;
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
void
|
218 |
|
|
cyg_httpd_init_tcl_interpreter(void)
|
219 |
|
|
{
|
220 |
|
|
// Start the TCL interpreter.
|
221 |
|
|
Jim_InitEmbedded();
|
222 |
|
|
httpstate.jim_interp = Jim_CreateInterp();
|
223 |
|
|
Jim_RegisterCoreCommands(httpstate.jim_interp);
|
224 |
|
|
Jim_AioInit(httpstate.jim_interp);
|
225 |
|
|
|
226 |
|
|
// Add a new cyg_httpd_write_chunked command for tcl.
|
227 |
|
|
Jim_CreateCommand(httpstate.jim_interp,
|
228 |
|
|
"start_chunked",
|
229 |
|
|
cyg_httpd_Jim_Command_startchunked,
|
230 |
|
|
NULL,
|
231 |
|
|
NULL);
|
232 |
|
|
Jim_CreateCommand(httpstate.jim_interp,
|
233 |
|
|
"write_chunked",
|
234 |
|
|
cyg_httpd_Jim_Command_writechunked,
|
235 |
|
|
NULL,
|
236 |
|
|
NULL);
|
237 |
|
|
Jim_CreateCommand(httpstate.jim_interp,
|
238 |
|
|
"end_chunked",
|
239 |
|
|
cyg_httpd_Jim_Command_endchunked,
|
240 |
|
|
NULL,
|
241 |
|
|
NULL);
|
242 |
|
|
}
|
243 |
|
|
#endif
|
244 |
|
|
|
245 |
|
|
// =============================================================================
|
246 |
|
|
// CGI Support
|
247 |
|
|
// =============================================================================
|
248 |
|
|
#if defined(CYGOPT_NET_ATHTTPD_USE_CGIBIN_TCL ) || \
|
249 |
|
|
defined(CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER)
|
250 |
|
|
cyg_int32
|
251 |
|
|
cyg_httpd_exec_cgi(void)
|
252 |
|
|
{
|
253 |
|
|
char file_name[CYG_HTTPD_MAXPATH];
|
254 |
|
|
|
255 |
|
|
strcpy(file_name, CYGDAT_NET_ATHTTPD_SERVEROPT_ROOTDIR);
|
256 |
|
|
if (file_name[strlen(file_name)-1] != '/')
|
257 |
|
|
strcat(file_name, "/");
|
258 |
|
|
strcat(file_name, httpstate.url);
|
259 |
|
|
cyg_httpd_cleanup_filename(file_name);
|
260 |
|
|
|
261 |
|
|
char *extension = rindex(httpstate.url, '.');
|
262 |
|
|
if (extension == NULL)
|
263 |
|
|
{
|
264 |
|
|
// File in CGI request _must_ have an extension.
|
265 |
|
|
cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR);
|
266 |
|
|
return 0;
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
#ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER
|
270 |
|
|
if (strcmp(extension, CYG_HTTPD_DEFAULT_CGIBIN_OBJLOADER_EXTENSION) == 0)
|
271 |
|
|
{
|
272 |
|
|
// Load a cgibin via OBJLOADER.
|
273 |
|
|
cyg_int32 rc = cyg_httpd_exec_cgi_objloader(file_name);
|
274 |
|
|
return rc;
|
275 |
|
|
}
|
276 |
|
|
#endif
|
277 |
|
|
#ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_TCL
|
278 |
|
|
if (strcmp(extension, CYG_HTTPD_DEFAULT_CGIBIN_TCL_EXTENSION) == 0)
|
279 |
|
|
{
|
280 |
|
|
// Load a cgibin via the TCL interpreter.
|
281 |
|
|
cyg_int32 rc = cyg_httpd_exec_cgi_tcl(file_name);
|
282 |
|
|
return rc;
|
283 |
|
|
}
|
284 |
|
|
#endif
|
285 |
|
|
cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR);
|
286 |
|
|
return 0;
|
287 |
|
|
}
|
288 |
|
|
#endif
|
289 |
|
|
|