|
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 |
|
|
65 #include <cyg/athttpd/http.h> |
|
|
66 #include <cyg/athttpd/socket.h> |
|
|
67 #include <cyg/athttpd/handler.h> |
|
|
68 #include <cyg/athttpd/forms.h> |
|
|
69 |
|
|
70 #ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER |
|
|
71 #include <cyg/objloader/elf.h> |
|
|
72 #include <cyg/objloader/objelf.h> |
|
|
73 #endif |
|
|
74 |
|
|
75 // ============================================================================= |
|
|
76 // Objloader CGI Support |
|
|
77 // ============================================================================= |
|
|
78 #ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER |
|
|
79 CYG_LDR_TABLE_STDIO() |
|
|
80 CYG_LDR_TABLE_STRING() |
|
|
81 |
|
|
82 // Chunked transfers. |
|
|
83 CYG_LDR_TABLE_ENTRY(cyg_httpd_start_chunked_entry, \ |
|
|
84 "cyg_httpd_start_chunked", \ |
|
|
85 cyg_httpd_start_chunked); |
|
|
86 CYG_LDR_TABLE_ENTRY(cyg_httpd_write_chunked_entry, \ |
|
|
87 "cyg_httpd_write_chunked", \ |
|
|
88 cyg_httpd_write_chunked); |
|
|
89 CYG_LDR_TABLE_ENTRY(cyg_httpd_end_chunked_entry, \ |
|
|
90 "cyg_httpd_end_chunked", \ |
|
|
91 cyg_httpd_end_chunked); |
|
|
92 |
|
|
93 // Mime strings. |
|
|
94 CYG_LDR_TABLE_ENTRY(cyg_httpd_write_entry, \ |
|
|
95 "cyg_httpd_write", \ |
|
|
96 cyg_httpd_write); |
|
|
97 |
|
|
98 // Entry to find a GET/POST variable in the table. |
|
|
99 CYG_LDR_TABLE_ENTRY(cyg_httpd_find_form_variable_entry, \ |
|
|
100 "cyg_httpd_find_form_variable", \ |
|
|
101 cyg_httpd_find_form_variable); |
|
|
102 |
|
|
103 // Finds an internal resource given the URL. Returns a pointer to the entry |
|
|
104 // in the resource table. |
|
|
105 CYG_LDR_TABLE_ENTRY(cyg_httpd_find_ires_entry, \ |
|
|
106 "cyg_httpd_find_ires", \ |
|
|
107 cyg_httpd_find_ires); |
|
|
108 // Sends an internal resource out, given the entry in the reource table. |
|
|
109 CYG_LDR_TABLE_ENTRY(cyg_httpd_send_ires_entry, \ |
|
|
110 "cyg_httpd_send_ires", \ |
|
|
111 cyg_httpd_send_ires); |
|
|
112 |
|
|
113 CYG_LDR_TABLE_ENTRY(cyg_httpd_format_header_entry, \ |
|
|
114 "cyg_httpd_format_header", \ |
|
|
115 cyg_httpd_format_header); |
|
|
116 CYG_LDR_TABLE_ENTRY(diag_printf_entry, "diag_printf", diag_printf); |
|
|
117 CYG_LDR_TABLE_ENTRY(cyg_httpd_find_mime_string_entry, \ |
|
|
118 "cyg_httpd_find_mime_string", \ |
|
|
119 cyg_httpd_find_mime_string); |
|
|
120 #endif // CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER |
|
|
121 |
|
|
122 #ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER |
|
|
123 cyg_int32 cyg_httpd_exec_cgi_objloader(char *file_name) |
|
|
124 { |
|
|
125 // Check if the file is in the file system. |
|
|
126 struct stat sp; |
|
|
127 cyg_int32 (*fn)(CYG_HTTPD_STATE*); |
|
|
128 |
|
|
129 cyg_int32 rc = stat(file_name, &sp); |
|
|
130 if(rc < 0) |
|
|
131 { |
|
|
132 cyg_httpd_send_error(CYG_HTTPD_STATUS_NOT_FOUND); |
|
|
133 return 0; |
|
|
134 } |
|
|
135 |
|
|
136 void *lib_handle = cyg_ldr_open_library((CYG_ADDRWORD)file_name, 0); |
|
|
137 CYG_ASSERT(lib_handle != 0, "Error opening the library"); |
|
|
138 if (lib_handle == 0) |
|
|
139 { |
|
|
140 cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR); |
|
|
141 return 0; |
|
|
142 } |
|
|
143 fn = cyg_ldr_find_symbol(lib_handle, "exec_cgi"); |
|
|
144 if (fn) |
|
|
145 { |
|
|
146 cyg_int32 rc = fn(&httpstate); |
|
|
147 cyg_ldr_close_library(lib_handle); |
|
|
148 return rc; |
|
|
149 } |
|
|
150 else |
|
|
151 { |
|
|
152 cyg_ldr_close_library(lib_handle); |
|
|
153 cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR); |
|
|
154 return 0; |
|
|
155 } |
|
|
156 } |
|
|
157 #endif |
|
|
158 |
|
|
159 // ============================================================================= |
|
|
160 // tcl CGI Support |
|
|
161 // ============================================================================= |
|
|
162 #ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_TCL |
|
|
163 cyg_int32 cyg_httpd_exec_cgi_tcl(char *file_name) |
|
|
164 { |
|
|
165 struct stat sp; |
|
|
166 // Check if the file is in the file system. |
|
|
167 cyg_int32 rc = stat(file_name, &sp); |
|
|
168 if (rc < 0) |
|
|
169 { |
|
|
170 cyg_httpd_send_error(CYG_HTTPD_STATUS_NOT_FOUND); |
|
|
171 return 0; |
|
|
172 } |
|
|
173 |
|
|
174 FILE *fp = fopen(file_name, "rb"); |
|
|
175 CYG_ASSERT(fp != NULL, "Cannot open file()"); |
|
|
176 if (fp == NULL) |
|
|
177 { |
|
|
178 cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR); |
|
|
179 return 0; |
|
|
180 } |
|
|
181 |
|
2265
|
182 char* tcl_buffer = (char*)malloc(sp.st_size); |
|
2250
|
183 CYG_ASSERT(tcl_buffer != NULL, "Cannot malloc() for tcl CGI"); |
|
|
184 if (tcl_buffer == NULL) |
|
|
185 { |
|
|
186 cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR); |
|
|
187 return 0; |
|
|
188 } |
|
|
189 |
|
|
190 rc = fread(tcl_buffer, sizeof(char), sp.st_size, fp); |
|
|
191 if (rc != sp.st_size) |
|
|
192 { |
|
|
193 cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR); |
|
|
194 return 0; |
|
|
195 } |
|
|
196 |
|
|
197 // Make sure that tcl sees the internal variables. |
|
|
198 cyg_httpd_fvars_table_entry *entry = cyg_httpd_fvars_table; |
|
|
199 while (entry != cyg_httpd_fvars_table_end) |
|
|
200 { |
|
|
201 if (strlen(entry->buf) != 0) |
|
|
202 Jim_SetVariableStrWithStr(httpstate.jim_interp, |
|
|
203 entry->name, |
|
|
204 entry->buf); |
|
|
205 entry++; |
|
|
206 } |
|
|
207 |
|
|
208 Jim_Eval(httpstate.jim_interp, tcl_buffer); |
|
|
209 free(tcl_buffer); |
|
|
210 return 0; |
|
|
211 } |
|
|
212 |
|
|
213 Jim_Interp *ExportedJimCreateInterp(void); |
|
|
214 static void Jim_InitEmbedded(void) { |
|
|
215 Jim_Interp *i = ExportedJimCreateInterp(); |
|
|
216 Jim_InitExtension(i); |
|
|
217 Jim_FreeInterp(i); |
|
|
218 } |
|
|
219 |
|
|
220 int cyg_httpd_Jim_Command_startchunked(Jim_Interp *interp, |
|
|
221 int argc, |
|
|
222 Jim_Obj *const *argv) |
|
|
223 { |
|
|
224 Jim_Obj *objPtr = (Jim_Obj *)argv[1]; |
|
|
225 cyg_httpd_start_chunked(objPtr->bytes); |
|
|
226 return JIM_OK; |
|
|
227 } |
|
|
228 |
|
|
229 int cyg_httpd_Jim_Command_writechunked(Jim_Interp *interp, |
|
|
230 int argc, |
|
|
231 Jim_Obj *const *argv) |
|
|
232 { |
|
|
233 Jim_Obj *objPtr = (Jim_Obj *)argv[1]; |
|
|
234 cyg_httpd_write_chunked(objPtr->bytes, objPtr->length); |
|
|
235 return JIM_OK; |
|
|
236 } |
|
|
237 |
|
|
238 int cyg_httpd_Jim_Command_endchunked(Jim_Interp *interp, |
|
|
239 int argc, |
|
|
240 Jim_Obj *const *argv) |
|
|
241 { |
|
|
242 cyg_httpd_end_chunked(); |
|
|
243 return JIM_OK; |
|
|
244 } |
|
|
245 |
|
|
246 void cyg_httpd_init_tcl_interpreter(void) |
|
|
247 { |
|
|
248 // Start the TCL interpreter. |
|
|
249 Jim_InitEmbedded(); |
|
|
250 httpstate.jim_interp = Jim_CreateInterp(); |
|
|
251 Jim_RegisterCoreCommands(httpstate.jim_interp); |
|
|
252 |
|
|
253 // Add a new cyg_httpd_write_chunked command for tcl. |
|
|
254 Jim_CreateCommand(httpstate.jim_interp, |
|
|
255 "start_chunked", |
|
|
256 cyg_httpd_Jim_Command_startchunked, |
|
|
257 NULL, |
|
|
258 NULL); |
|
|
259 Jim_CreateCommand(httpstate.jim_interp, |
|
|
260 "write_chunked", |
|
|
261 cyg_httpd_Jim_Command_writechunked, |
|
|
262 NULL, |
|
|
263 NULL); |
|
|
264 Jim_CreateCommand(httpstate.jim_interp, |
|
|
265 "end_chunked", |
|
|
266 cyg_httpd_Jim_Command_endchunked, |
|
|
267 NULL, |
|
|
268 NULL); |
|
|
269 } |
|
|
270 #endif |
|
|
271 |
|
|
272 // ============================================================================= |
|
|
273 // CGI Support |
|
|
274 // ============================================================================= |
|
|
275 #if (CYGOPT_NET_ATHTTPD_USE_CGIBIN_TCL == 1 ) || \ |
|
|
276 (CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER == 1 ) |
|
|
277 cyg_int32 |
|
|
278 cyg_httpd_exec_cgi(void) |
|
|
279 { |
|
|
280 char file_name[CYG_HTTPD_MAXPATH]; |
|
|
281 |
|
|
282 strcpy(file_name, CYGDAT_NET_ATHTTPD_SERVEROPT_ROOTDIR); |
|
|
283 if (file_name[strlen(file_name)-1] != '/') |
|
|
284 strcat(file_name, "/"); |
|
|
285 strcat(file_name, httpstate.url); |
|
|
286 cyg_httpd_cleanup_filename(file_name); |
|
|
287 |
|
|
288 char *extension = rindex(httpstate.url, '.'); |
|
|
289 if (extension == NULL) |
|
|
290 { |
|
|
291 // File in CGI request _must_ have an extension. |
|
|
292 cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR); |
|
|
293 return 0; |
|
|
294 } |
|
|
295 |
|
|
296 #ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER |
|
|
297 if ( strcmp(extension, CYG_HTTPD_DEFAULT_CGIBIN_OBJLOADER_EXTENSION) == 0) |
|
|
298 { |
|
|
299 // Load a cgibin via OBJLOADER. |
|
|
300 cyg_int32 rc = cyg_httpd_exec_cgi_objloader(file_name); |
|
|
301 return rc; |
|
|
302 } |
|
|
303 #endif |
|
|
304 #ifdef CYGOPT_NET_ATHTTPD_USE_CGIBIN_TCL |
|
|
305 if ( strcmp(extension, CYG_HTTPD_DEFAULT_CGIBIN_TCL_EXTENSION) == 0) |
|
|
306 { |
|
2265
|
307 // Load a cgibin via the TCL interpreter. |
|
2250
|
308 cyg_int32 rc = cyg_httpd_exec_cgi_tcl(file_name); |
|
|
309 return rc; |
|
|
310 } |
|
|
311 #endif |
|
|
312 cyg_httpd_send_error(CYG_HTTPD_STATUS_SYSTEM_ERROR); |
|
|
313 return 0; |
|
|
314 } |
|
|
315 #endif |
|
|
316 |