1 /***************************************************************************
\r
2 * Copyright (C) 2007,2008 Øyvind Harboe *
\r
3 * oyvind.harboe@zylin.com *
\r
5 * Copyright (C) 2008 Free Software Foundation
\r
7 * This program is free software; you can redistribute it and/or modify *
\r
8 * it under the terms of the GNU General Public License as published by *
\r
9 * the Free Software Foundation; either version 2 of the License, or *
\r
10 * (at your option) any later version. *
\r
12 * This program is distributed in the hope that it will be useful, *
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
\r
15 * GNU General Public License for more details. *
\r
17 * You should have received a copy of the GNU General Public License *
\r
18 * along with this program; if not, write to the *
\r
19 * Free Software Foundation, Inc., *
\r
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
\r
21 ***************************************************************************/
\r
23 /* some bits were copied from ahttpd which is under eCos license and
\r
26 #ifdef HAVE_CONFIG_H
\r
30 #include "replacements.h"
\r
35 #include "telnet_server.h"
\r
38 #include <command.h>
\r
43 #include <sys/types.h>
\r
47 #include <sys/types.h>
\r
48 #include <sys/select.h>
\r
49 #include <sys/socket.h>
\r
50 #include <microhttpd.h>
\r
55 #define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
\r
57 static const char *appendf(const *prev, const char *format, ...)
\r
60 va_start(ap, format);
\r
61 char *string = alloc_vprintf(format, ap);
\r
63 char *string2 = NULL;
\r
67 string2 = alloc_printf("%s%s", (prev == NULL) ? "" : prev, string);
\r
81 static const char *httpd_exec_cgi_tcl_error(Jim_Interp *interp)
\r
85 const char *t = NULL;
\r
86 t = appendf(t, "<html><body>\n");
\r
88 t = appendf(t, "Runtime error, file \"%s\", line %d:<br>",
\r
89 interp->errorFileName, interp->errorLine);
\r
90 t = appendf(t, " %s<br>", Jim_GetString(interp->result, NULL));
\r
91 Jim_ListLength(interp, interp->stackTrace, &len);
\r
92 for (i = 0; i < len; i += 3)
\r
95 const char *proc, *file, *line;
\r
97 Jim_ListIndex(interp, interp->stackTrace, i, &objPtr, JIM_NONE);
\r
98 proc = Jim_GetString(objPtr, NULL);
\r
99 Jim_ListIndex(interp, interp->stackTrace, i + 1, &objPtr, JIM_NONE);
\r
100 file = Jim_GetString(objPtr, NULL);
\r
101 Jim_ListIndex(interp, interp->stackTrace, i + 2, &objPtr, JIM_NONE);
\r
102 line = Jim_GetString(objPtr, NULL);
\r
103 t = appendf(t, "In procedure '%s' called at file \"%s\", line %s<br>",
\r
106 t = appendf(t, "</html></body>\n");
\r
111 static int httpd_Jim_Command_writeform(Jim_Interp *interp, int argc,
\r
112 Jim_Obj * const *argv)
\r
116 Jim_WrongNumArgs(interp, 1, argv, "method ?args ...?");
\r
119 char *name = (char*) Jim_GetString(argv[1], NULL);
\r
120 char *file = (char*) Jim_GetString(argv[2], NULL);
\r
128 const char *script = alloc_printf("set dummy_val $httppostdata(%s); set dummy_val",
\r
130 retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
\r
131 free((void *) script);
\r
132 if (retcode != JIM_OK)
\r
135 data = Jim_GetString(Jim_GetResult(interp), &actual);
\r
138 f = fopen(file, "wb");
\r
142 ok = fwrite(data, 1, actual, f) == actual;
\r
147 Jim_SetResultString(interp, "Could not write to file", -1);
\r
153 Jim_SetResultString(interp, "Could not create file", -1);
\r
161 httpd_Jim_Command_formfetch(Jim_Interp *interp,
\r
163 Jim_Obj *const *argv)
\r
167 Jim_WrongNumArgs(interp, 1, argv, "method ?args ...?");
\r
170 char *name = (char*)Jim_GetString(argv[1], NULL);
\r
173 const char *script = alloc_printf("set dummy_val $httppostdata(%s); set dummy_val",
\r
175 int retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
\r
176 free((void *) script);
\r
177 if (retcode != JIM_OK)
\r
180 Jim_SetResult(interp, Jim_GetResult(interp));
\r
184 struct httpd_request
\r
187 struct MHD_PostProcessor *postprocessor;
\r
191 int complete; /* did we receive the entire post ? */
\r
195 static void request_completed(void *cls, struct MHD_Connection *connection,
\r
196 void **con_cls, enum MHD_RequestTerminationCode toe)
\r
198 struct httpd_request *r = (struct httpd_request*) *con_cls;
\r
203 if (r->postprocessor)
\r
205 MHD_destroy_post_processor(r->postprocessor);
\r
212 /* append to said key in dictonary */
\r
213 static void append_key(struct httpd_request *r, const char *key,
\r
214 const char *data, size_t off, size_t size)
\r
216 Jim_Obj *keyObj = Jim_NewStringObj(interp, key, -1);
\r
217 Jim_Obj *value = NULL;
\r
219 Jim_Obj *dict = Jim_GetVariableStr(interp, "httppostdata", 0);
\r
223 if (Jim_DictKey(interp, dict, keyObj, &value, 0) != JIM_OK)
\r
229 value = Jim_NewStringObj(interp, "", -1);
\r
231 /* create a new object we append to and insert into this location */
\r
232 Jim_Obj *newObj = Jim_NewStringObj(interp, "", -1);
\r
233 Jim_AppendObj(interp, newObj, value);
\r
234 Jim_AppendString(interp, newObj, data, size);
\r
235 /* uhh... use name here of dictionary */
\r
236 Jim_SetDictKeysVector(interp, Jim_NewStringObj(interp, "httppostdata", -1), &keyObj, 1, newObj);
\r
239 /* append data to each key */
\r
240 static int iterate_post(void *con_cls, enum MHD_ValueKind kind,
\r
241 const char *key, const char *filename, const char *content_type,
\r
242 const char *transfer_encoding, const char *data, size_t off,
\r
245 struct httpd_request *r = (struct httpd_request*) con_cls;
\r
247 append_key(r, key, data, off, size);
\r
252 static int record_arg(void *cls, enum MHD_ValueKind kind, const char *key,
\r
255 struct httpd_request *r = (struct httpd_request*) cls;
\r
256 append_key(r, key, value, 0, strlen(value));
\r
260 static int ahc_echo(void * cls, struct MHD_Connection * connection,
\r
261 const char * url, const char * method, const char * version,
\r
262 const char * upload_data, unsigned int * upload_data_size, void ** ptr)
\r
264 struct MHD_Response * response;
\r
269 if (0 == strcmp(method, "POST"))
\r
273 else if (0 == strcmp(method, "GET"))
\r
278 return MHD_NO; /* unexpected method */
\r
281 struct httpd_request *r;
\r
284 /* The first time only the headers are valid,
\r
285 do not respond in the first round... */
\r
287 *ptr = malloc(sizeof(struct httpd_request));
\r
290 memset(*ptr, 0, sizeof(struct httpd_request));
\r
292 r = (struct httpd_request *) *ptr;
\r
296 // r->dict = Jim_NewDictObj(interp, NULL, 0);
\r
298 /* fill in url query strings in dictonary */
\r
299 MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND,
\r
304 r->postprocessor = MHD_create_post_processor(connection, 2048
\r
305 * 1024, iterate_post, r);
\r
311 r = (struct httpd_request *) *ptr;
\r
315 /* consume post data */
\r
316 if (*upload_data_size)
\r
318 MHD_post_process(r->postprocessor, upload_data, *upload_data_size);
\r
319 *upload_data_size = 0;
\r
329 /* hand over to request who will be using it. */
\r
330 //Jim_SetGlobalVariableStr(interp, "httppostdata", Jim_GetVariableStr(interp, "httppostdata", 0));
\r
334 /* FIX!!!! we need more advanced handling of url's to avoid them
\r
335 * being subverted to evil purposes
\r
338 url++; /* skip '/' */
\r
340 const char *suffix;
\r
341 suffix = strrchr(url, '.');
\r
342 if ((suffix != NULL) && (strcmp(suffix, ".tcl") == 0))
\r
344 printf("Run tcl %s\n", url);
\r
348 const char *script = alloc_printf(
\r
349 "global httpdata; source {%s}; set httpdata", url);
\r
350 retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
\r
351 free((void *) script);
\r
353 if (retcode == JIM_ERR)
\r
355 printf("Tcl failed\n");
\r
356 const char *t = httpd_exec_cgi_tcl_error(interp);
\r
360 response = MHD_create_response_from_data(strlen(t), (void *) t,
\r
362 ret = MHD_queue_response(connection,
\r
363 MHD_HTTP_INTERNAL_SERVER_ERROR, response);
\r
364 MHD_destroy_response(response);
\r
369 printf("Tcl OK\n");
\r
370 /* FIX!!! how to handle mime types??? */
\r
371 const char *result;
\r
373 result = Jim_GetString(Jim_GetResult(interp), &reslen);
\r
375 response = MHD_create_response_from_data(reslen, (void *) result,
\r
377 ret = MHD_queue_response(connection,
\r
378 MHD_HTTP_INTERNAL_SERVER_ERROR, response);
\r
379 MHD_destroy_response(response);
\r
388 int retval = loadFile(url, &data, &len);
\r
389 if (retval != ERROR_OK)
\r
391 printf("Did not find %s\n", url);
\r
393 response = MHD_create_response_from_data(strlen(PAGE_NOT_FOUND),
\r
394 (void *) PAGE_NOT_FOUND, MHD_NO, MHD_NO);
\r
395 ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
\r
396 MHD_destroy_response(response);
\r
400 printf("Serving %s length=%d\n", url, len);
\r
401 /* serve file directly */
\r
402 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
\r
403 MHD_add_response_header(response, "Content-Type", "image/png");
\r
405 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
\r
406 MHD_destroy_response(response);
\r
413 static struct MHD_Daemon * d;
\r
415 int httpd_start(void)
\r
419 LOG_USER("Launching httpd server on port %d", port);
\r
420 d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL,
\r
421 &ahc_echo, NULL, /* could be data for handler, but we only have a single handler, use global variables instead */
\r
422 MHD_OPTION_NOTIFY_COMPLETED, request_completed, NULL, /* Closure... what's that??? */
\r
427 Jim_CreateCommand(interp,
\r
429 httpd_Jim_Command_formfetch,
\r
433 Jim_CreateCommand(interp,
\r
435 httpd_Jim_Command_writeform,
\r
443 void httpd_stop(void)
\r
445 MHD_stop_daemon(d);
\r
448 void openocd_sleep_prelude(void)
\r
452 void openocd_sleep_postlude(void)
\r