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