1 /***************************************************************************
2 * Copyright (C) 2007,2008 Øyvind Harboe *
3 * oyvind.harboe@zylin.com *
5 * Copyright (C) 2008 Free Software Foundation
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
23 /* some bits were copied from ahttpd which is under eCos license and
30 #include "replacements.h"
35 #include "telnet_server.h"
47 #include <sys/select.h>
48 #include <sys/socket.h>
49 #include <microhttpd.h>
54 #define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
56 int loadFile(const char *name, void **data, size_t *len);
58 static const char *appendf(const char *prev, const char *format, ...)
62 char *string = alloc_vprintf(format, ap);
68 string2 = alloc_printf("%s%s", (prev == NULL) ? "" : prev, string);
82 static const char *httpd_exec_cgi_tcl_error(Jim_Interp *interp)
87 t = appendf(t, "<html><body>\n");
89 t = appendf(t, "Runtime error, file \"%s\", line %d:<br>",
90 interp->errorFileName, interp->errorLine);
91 t = appendf(t, " %s<br>", Jim_GetString(interp->result, NULL));
92 Jim_ListLength(interp, interp->stackTrace, &len);
93 for (i = 0; i < len; i += 3)
96 const char *proc, *file, *line;
98 Jim_ListIndex(interp, interp->stackTrace, i, &objPtr, JIM_NONE);
99 proc = Jim_GetString(objPtr, NULL);
100 Jim_ListIndex(interp, interp->stackTrace, i + 1, &objPtr, JIM_NONE);
101 file = Jim_GetString(objPtr, NULL);
102 Jim_ListIndex(interp, interp->stackTrace, i + 2, &objPtr, JIM_NONE);
103 line = Jim_GetString(objPtr, NULL);
104 t = appendf(t, "In procedure '%s' called at file \"%s\", line %s<br>",
107 t = appendf(t, "</html></body>\n");
112 static int httpd_Jim_Command_writeform(Jim_Interp *interp, int argc,
113 Jim_Obj * const *argv)
117 Jim_WrongNumArgs(interp, 1, argv, "method ?args ...?");
120 char *name = (char*) Jim_GetString(argv[1], NULL);
121 char *file = (char*) Jim_GetString(argv[2], NULL);
129 const char *script = alloc_printf("set dummy_val $httppostdata(%s); set dummy_val",
131 retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
132 free((void *) script);
133 if (retcode != JIM_OK)
136 data = Jim_GetString(Jim_GetResult(interp), &actual);
138 FILE *f = fopen(file, "wb");
141 Jim_SetResultString(interp, "Could not create file", -1);
145 int result = fwrite(data, 1, actual, f);
148 if (result != actual)
150 Jim_SetResultString(interp, "Could not write to file", -1);
158 httpd_Jim_Command_formfetch(Jim_Interp *interp,
160 Jim_Obj *const *argv)
164 Jim_WrongNumArgs(interp, 1, argv, "method ?args ...?");
167 char *name = (char*)Jim_GetString(argv[1], NULL);
170 const char *script = alloc_printf("set dummy_val $httppostdata(%s); set dummy_val",
172 int retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
173 free((void *) script);
174 if (retcode != JIM_OK)
176 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
179 Jim_SetResult(interp, Jim_GetResult(interp));
188 struct MHD_PostProcessor *postprocessor;
192 int complete; /* did we receive the entire post ? */
196 static void request_completed(void *cls, struct MHD_Connection *connection,
197 void **con_cls, enum MHD_RequestTerminationCode toe)
199 struct httpd_request *r = (struct httpd_request*) *con_cls;
204 if (r->postprocessor)
206 MHD_destroy_post_processor(r->postprocessor);
213 /* append to said key in dictonary */
214 static void append_key(struct httpd_request *r, const char *key,
215 const char *data, size_t off, size_t size)
217 Jim_Obj *keyObj = Jim_NewStringObj(interp, key, -1);
218 Jim_Obj *value = NULL;
220 Jim_Obj *dict = Jim_GetVariableStr(interp, "httppostdata", 0);
224 if (Jim_DictKey(interp, dict, keyObj, &value, 0) != JIM_OK)
230 value = Jim_NewStringObj(interp, "", -1);
232 /* create a new object we append to and insert into this location */
233 Jim_Obj *newObj = Jim_NewStringObj(interp, "", -1);
234 Jim_AppendObj(interp, newObj, value);
235 Jim_AppendString(interp, newObj, data, size);
236 /* uhh... use name here of dictionary */
237 Jim_SetDictKeysVector(interp, Jim_NewStringObj(interp, "httppostdata", -1), &keyObj, 1, newObj);
240 /* append data to each key */
241 static int iterate_post(void *con_cls, enum MHD_ValueKind kind,
242 const char *key, const char *filename, const char *content_type,
243 const char *transfer_encoding, const char *data, uint64_t off,
246 struct httpd_request *r = (struct httpd_request*) con_cls;
248 append_key(r, key, data, off, size);
253 static int record_arg(void *cls, enum MHD_ValueKind kind, const char *key,
256 struct httpd_request *r = (struct httpd_request*) cls;
257 append_key(r, key, value, 0, strlen(value));
262 int handle_request(struct MHD_Connection * connection, const char * url)
264 struct MHD_Response * response;
268 suffix = strrchr(url, '.');
269 if ((suffix != NULL) && (strcmp(suffix, ".tcl") == 0))
271 printf("Run tcl %s\n", url);
275 const char *script = alloc_printf(
276 "global httpdata; source {%s}; set httpdata", url);
277 retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
278 free((void *) script);
280 if (retcode == JIM_ERR)
282 printf("Tcl failed\n");
283 const char *t = httpd_exec_cgi_tcl_error(interp);
287 response = MHD_create_response_from_data(strlen(t), (void *) t,
289 ret = MHD_queue_response(connection,
290 MHD_HTTP_INTERNAL_SERVER_ERROR, response);
291 MHD_destroy_response(response);
297 /* FIX!!! how to handle mime types??? */
300 result = Jim_GetString(Jim_GetResult(interp), &reslen);
302 response = MHD_create_response_from_data(reslen, (void *) result,
304 ret = MHD_queue_response(connection,
305 MHD_HTTP_INTERNAL_SERVER_ERROR, response);
306 MHD_destroy_response(response);
315 int retval = loadFile(url, &data, &len);
316 if (retval != ERROR_OK)
318 printf("Did not find %s\n", url);
320 response = MHD_create_response_from_data(strlen(PAGE_NOT_FOUND),
321 (void *) PAGE_NOT_FOUND, MHD_NO, MHD_NO);
322 ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
323 MHD_destroy_response(response);
327 LOG_DEBUG("Serving %s length=%u", url, len);
328 /* serve file directly */
329 response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
330 MHD_add_response_header(response, "Content-Type", "image/png");
332 ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
333 MHD_destroy_response(response);
340 static int ahc_echo(void * cls, struct MHD_Connection * connection,
341 const char * url, const char * method, const char * version,
342 const char * upload_data, unsigned int * upload_data_size, void ** ptr)
346 if (0 == strcmp(method, "POST"))
350 else if (0 == strcmp(method, "GET"))
355 return MHD_NO; /* unexpected method */
358 struct httpd_request *r;
361 /* The first time only the headers are valid,
362 do not respond in the first round... */
364 *ptr = malloc(sizeof(struct httpd_request));
367 memset(*ptr, 0, sizeof(struct httpd_request));
369 r = (struct httpd_request *) *ptr;
372 Jim_SetVariableStr(interp, "httppostdata", Jim_NewDictObj(interp, NULL, 0));
374 /* fill in url query strings in dictonary */
375 MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND,
380 r->postprocessor = MHD_create_post_processor(connection, 2048
381 * 1024, &iterate_post, r);
387 r = (struct httpd_request *) *ptr;
391 /* consume post data */
392 if (*upload_data_size)
394 MHD_post_process(r->postprocessor, upload_data, *upload_data_size);
395 *upload_data_size = 0;
405 /* hand over to request who will be using it. */
409 /* FIX!!!! we need more advanced handling of url's to avoid them
410 * being subverted to evil purposes
413 const char *httpd_dir=PKGLIBDIR "/httpd";
417 url++; /* skip '/' */
422 const char *file_name=alloc_printf("%s/%s", httpd_dir, url);
423 int result = handle_request(connection, file_name);
424 free((void *)file_name);
428 static struct MHD_Daemon * d;
429 static pthread_mutex_t mutex;
432 int httpd_start(void)
434 pthread_mutexattr_t attr;
435 pthread_mutexattr_init( &attr );
436 pthread_mutex_init( &mutex, &attr );
439 LOG_USER("Launching httpd server on port %d", port);
440 d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL,
441 &ahc_echo, NULL, /* could be data for handler, but we only have a single handler, use global variables instead */
442 MHD_OPTION_NOTIFY_COMPLETED, request_completed, NULL, /* Closure... what's that??? */
447 Jim_CreateCommand(interp,
449 httpd_Jim_Command_formfetch,
453 Jim_CreateCommand(interp,
455 httpd_Jim_Command_writeform,
463 void httpd_stop(void)
466 pthread_mutex_destroy( &mutex );
469 void openocd_sleep_prelude(void)
471 pthread_mutex_unlock( &mutex );
474 void openocd_sleep_postlude(void)
476 pthread_mutex_lock( &mutex );