Mercurial > ecos-v3_0-branch
annotate packages/net/athttpd/current/src/cgi.c @ 2729:74dbf4c3f2e1 after-copyright-change-20090129
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
| author | jlarmour |
|---|---|
| date | Thu, 29 Jan 2009 17:47:46 +0000 |
| parents | 0e9d1d1df4a0 |
| children |
| rev | line source |
|---|---|
| 2250 | 1 /* ================================================================= |
| 2 * | |
| 3 * cgi.c | |
| 4 * | |
| 5 * Handles the CGI requests via OBJLOADER or tcl | |
| 6 * | |
| 7 * ================================================================= | |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
8 * ####ECOSGPLCOPYRIGHTBEGIN#### |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
9 * ------------------------------------------- |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
10 * This file is part of eCos, the Embedded Configurable Operating System. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
11 * Copyright (C) 2005 Free Software Foundation, Inc. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
12 * |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
13 * eCos is free software; you can redistribute it and/or modify it under |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
14 * the terms of the GNU General Public License as published by the Free |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
15 * Software Foundation; either version 2 or (at your option) any later |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
16 * version. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
17 * |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
18 * eCos is distributed in the hope that it will be useful, but WITHOUT |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
21 * for more details. |
| 2250 | 22 * |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
23 * You should have received a copy of the GNU General Public License |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
24 * along with eCos; if not, write to the Free Software Foundation, Inc., |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
26 * |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
27 * As a special exception, if other files instantiate templates or use |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
28 * macros or inline functions from this file, or you compile this file |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
29 * and link it with other works to produce a work based on this file, |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
30 * this file does not by itself cause the resulting work to be covered by |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
31 * the GNU General Public License. However the source code for this file |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
32 * must still be made available in accordance with section (3) of the GNU |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
33 * General Public License v2. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
34 * |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
35 * This exception does not invalidate any other reasons why a work based |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
36 * on this file might be covered by the GNU General Public License. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
37 * ------------------------------------------- |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2485
diff
changeset
|
38 * ####ECOSGPLCOPYRIGHTEND#### |
| 2250 | 39 * ================================================================= |
| 40 * #####DESCRIPTIONBEGIN#### | |
| 41 * | |
| 42 * Author(s): Anthony Tonizzo (atonizzo@gmail.com) | |
| 2265 | 43 * Contributors: Sergei Gavrikov (w3sg@SoftHome.net) |
| 2250 | 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 | |
| 2326 | 62 #define JIM_EMBEDDED |
| 2250 | 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 | |
| 2326 | 161 int Jim_AioInit(Jim_Interp *); |
| 162 cyg_int32 | |
| 163 cyg_httpd_exec_cgi_tcl(char *file_name) | |
| 2250 | 164 { |
| 2326 | 165 // Make sure that tcl sees the internal variables including the post_data. |
| 2250 | 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 } | |
| 2326 | 175 |
| 176 if (httpstate.post_data != NULL) | |
| 177 Jim_SetVariableStrWithStr(httpstate.jim_interp, | |
| 178 "post_data", | |
| 179 httpstate.post_data); | |
| 2250 | 180 |
|
2485
0e9d1d1df4a0
* src/jim-aio.c (JimAioHandlerCommand): GCC 4.3.1 pointed out a
asl
parents:
2326
diff
changeset
|
181 char tcl_cmd[CYG_HTTPD_MAXPATH]; |
|
0e9d1d1df4a0
* src/jim-aio.c (JimAioHandlerCommand): GCC 4.3.1 pointed out a
asl
parents:
2326
diff
changeset
|
182 sprintf(tcl_cmd, "source %s", file_name); |
| 2326 | 183 Jim_Eval(httpstate.jim_interp, tcl_cmd); |
| 2250 | 184 return 0; |
| 185 } | |
| 186 | |
| 2326 | 187 int |
| 188 cyg_httpd_Jim_Command_startchunked(Jim_Interp *interp, | |
| 189 int argc, | |
| 190 Jim_Obj *const *argv) | |
| 2250 | 191 { |
| 2326 | 192 char *buf = (char*)Jim_GetString(argv[1], NULL); |
| 193 cyg_httpd_start_chunked(buf); | |
| 2250 | 194 return JIM_OK; |
| 195 } | |
| 196 | |
| 2326 | 197 int |
| 198 cyg_httpd_Jim_Command_writechunked(Jim_Interp *interp, | |
| 199 int argc, | |
| 200 Jim_Obj *const *argv) | |
| 2250 | 201 { |
| 2326 | 202 int len; |
| 203 char *buf = (char*)Jim_GetString(argv[1], &len); | |
| 204 cyg_httpd_write_chunked(buf, len); | |
| 2250 | 205 return JIM_OK; |
| 206 } | |
| 207 | |
| 2326 | 208 int |
| 209 cyg_httpd_Jim_Command_endchunked(Jim_Interp *interp, | |
| 210 int argc, | |
| 211 Jim_Obj *const *argv) | |
| 2250 | 212 { |
| 213 cyg_httpd_end_chunked(); | |
| 214 return JIM_OK; | |
| 215 } | |
| 216 | |
| 2326 | 217 void |
| 218 cyg_httpd_init_tcl_interpreter(void) | |
| 2250 | 219 { |
| 220 // Start the TCL interpreter. | |
| 221 Jim_InitEmbedded(); | |
| 222 httpstate.jim_interp = Jim_CreateInterp(); | |
| 223 Jim_RegisterCoreCommands(httpstate.jim_interp); | |
| 2326 | 224 Jim_AioInit(httpstate.jim_interp); |
| 2250 | 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 // ============================================================================= | |
| 2326 | 248 #if defined(CYGOPT_NET_ATHTTPD_USE_CGIBIN_TCL ) || \ |
| 249 defined(CYGOPT_NET_ATHTTPD_USE_CGIBIN_OBJLOADER) | |
| 2250 | 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 | |
|
2485
0e9d1d1df4a0
* src/jim-aio.c (JimAioHandlerCommand): GCC 4.3.1 pointed out a
asl
parents:
2326
diff
changeset
|
270 if (strcmp(extension, CYG_HTTPD_DEFAULT_CGIBIN_OBJLOADER_EXTENSION) == 0) |
| 2250 | 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 | |
|
2485
0e9d1d1df4a0
* src/jim-aio.c (JimAioHandlerCommand): GCC 4.3.1 pointed out a
asl
parents:
2326
diff
changeset
|
278 if (strcmp(extension, CYG_HTTPD_DEFAULT_CGIBIN_TCL_EXTENSION) == 0) |
| 2250 | 279 { |
| 2265 | 280 // Load a cgibin via the TCL interpreter. |
| 2250 | 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 |
