You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

460 lines
11 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007,2008 Øyvind Harboe *
  3. * oyvind.harboe@zylin.com *
  4. * *
  5. * Copyright (C) 2008 Free Software Foundation
  6. * *
  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. *
  11. * *
  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. *
  16. * *
  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. ***************************************************************************/
  22. /* some bits were copied from ahttpd which is under eCos license and
  23. * copyright to FSF
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include "replacements.h"
  29. #include "server.h"
  30. #include "log.h"
  31. #include "telnet_server.h"
  32. #include "target.h"
  33. #include <command.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <unistd.h>
  38. #include <sys/types.h>
  39. #include <fcntl.h>
  40. #include <signal.h>
  41. #include <sys/types.h>
  42. #include <sys/select.h>
  43. #include <sys/socket.h>
  44. #include <microhttpd.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <stdio.h>
  48. #define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>"
  49. static const char *appendf(const char *prev, const char *format, ...)
  50. {
  51. va_list ap;
  52. va_start(ap, format);
  53. char *string = alloc_vprintf(format, ap);
  54. va_end(ap);
  55. char *string2 = NULL;
  56. if (string != NULL)
  57. {
  58. string2 = alloc_printf("%s%s", (prev == NULL) ? "" : prev, string);
  59. }
  60. if (prev != NULL)
  61. {
  62. free((void *)prev);
  63. }
  64. if (string == NULL)
  65. free(string);
  66. return string2;
  67. }
  68. static const char *httpd_exec_cgi_tcl_error(Jim_Interp *interp)
  69. {
  70. int len, i;
  71. const char *t = NULL;
  72. t = appendf(t, "<html><body>\n");
  73. t = appendf(t, "Runtime error, file \"%s\", line %d:<br>",
  74. interp->errorFileName, interp->errorLine);
  75. t = appendf(t, " %s<br>", Jim_GetString(interp->result, NULL));
  76. Jim_ListLength(interp, interp->stackTrace, &len);
  77. for (i = 0; i < len; i += 3)
  78. {
  79. Jim_Obj *objPtr;
  80. const char *proc, *file, *line;
  81. Jim_ListIndex(interp, interp->stackTrace, i, &objPtr, JIM_NONE);
  82. proc = Jim_GetString(objPtr, NULL);
  83. Jim_ListIndex(interp, interp->stackTrace, i + 1, &objPtr, JIM_NONE);
  84. file = Jim_GetString(objPtr, NULL);
  85. Jim_ListIndex(interp, interp->stackTrace, i + 2, &objPtr, JIM_NONE);
  86. line = Jim_GetString(objPtr, NULL);
  87. t = appendf(t, "In procedure '%s' called at file \"%s\", line %s<br>",
  88. proc, file, line);
  89. }
  90. t = appendf(t, "</html></body>\n");
  91. return t;
  92. }
  93. static int httpd_Jim_Command_writeform(Jim_Interp *interp, int argc,
  94. Jim_Obj * const *argv)
  95. {
  96. if (argc != 3)
  97. {
  98. Jim_WrongNumArgs(interp, 1, argv, "method ?args ...?");
  99. return JIM_ERR;
  100. }
  101. char *name = (char*) Jim_GetString(argv[1], NULL);
  102. char *file = (char*) Jim_GetString(argv[2], NULL);
  103. // Find length
  104. const char *data;
  105. int actual;
  106. int retcode;
  107. const char *script = alloc_printf("set dummy_val $httppostdata(%s); set dummy_val",
  108. name);
  109. retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
  110. free((void *) script);
  111. if (retcode != JIM_OK)
  112. return retcode;
  113. data = Jim_GetString(Jim_GetResult(interp), &actual);
  114. FILE *f;
  115. f = fopen(file, "wb");
  116. if (f != NULL)
  117. {
  118. int ok;
  119. ok = fwrite(data, 1, actual, f) == actual;
  120. fclose(f);
  121. if (!ok)
  122. {
  123. Jim_SetResultString(interp, "Could not write to file", -1);
  124. return JIM_ERR;
  125. }
  126. }
  127. else
  128. {
  129. Jim_SetResultString(interp, "Could not create file", -1);
  130. return JIM_ERR;
  131. }
  132. return JIM_OK;
  133. }
  134. int
  135. httpd_Jim_Command_formfetch(Jim_Interp *interp,
  136. int argc,
  137. Jim_Obj *const *argv)
  138. {
  139. if (argc!=2)
  140. {
  141. Jim_WrongNumArgs(interp, 1, argv, "method ?args ...?");
  142. return JIM_ERR;
  143. }
  144. char *name = (char*)Jim_GetString(argv[1], NULL);
  145. const char *script = alloc_printf("set dummy_val $httppostdata(%s); set dummy_val",
  146. name);
  147. int retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
  148. free((void *) script);
  149. if (retcode != JIM_OK)
  150. {
  151. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  152. } else
  153. {
  154. Jim_SetResult(interp, Jim_GetResult(interp));
  155. }
  156. return JIM_OK;
  157. }
  158. struct httpd_request
  159. {
  160. int post;
  161. struct MHD_PostProcessor *postprocessor;
  162. //Jim_Obj *dict;
  163. int complete; /* did we receive the entire post ? */
  164. };
  165. static void request_completed(void *cls, struct MHD_Connection *connection,
  166. void **con_cls, enum MHD_RequestTerminationCode toe)
  167. {
  168. struct httpd_request *r = (struct httpd_request*) *con_cls;
  169. if (NULL == r)
  170. return;
  171. if (r->postprocessor)
  172. {
  173. MHD_destroy_post_processor(r->postprocessor);
  174. }
  175. free(r);
  176. *con_cls = NULL;
  177. }
  178. /* append to said key in dictonary */
  179. static void append_key(struct httpd_request *r, const char *key,
  180. const char *data, size_t off, size_t size)
  181. {
  182. Jim_Obj *keyObj = Jim_NewStringObj(interp, key, -1);
  183. Jim_Obj *value = NULL;
  184. Jim_Obj *dict = Jim_GetVariableStr(interp, "httppostdata", 0);
  185. if (dict!=NULL)
  186. {
  187. if (Jim_DictKey(interp, dict, keyObj, &value, 0) != JIM_OK)
  188. {
  189. value = NULL;
  190. }
  191. }
  192. if (value == NULL)
  193. value = Jim_NewStringObj(interp, "", -1);
  194. /* create a new object we append to and insert into this location */
  195. Jim_Obj *newObj = Jim_NewStringObj(interp, "", -1);
  196. Jim_AppendObj(interp, newObj, value);
  197. Jim_AppendString(interp, newObj, data, size);
  198. /* uhh... use name here of dictionary */
  199. Jim_SetDictKeysVector(interp, Jim_NewStringObj(interp, "httppostdata", -1), &keyObj, 1, newObj);
  200. }
  201. /* append data to each key */
  202. static int iterate_post(void *con_cls, enum MHD_ValueKind kind,
  203. const char *key, const char *filename, const char *content_type,
  204. const char *transfer_encoding, const char *data, size_t off,
  205. size_t size)
  206. {
  207. struct httpd_request *r = (struct httpd_request*) con_cls;
  208. append_key(r, key, data, off, size);
  209. return MHD_YES;
  210. }
  211. static int record_arg(void *cls, enum MHD_ValueKind kind, const char *key,
  212. const char *value)
  213. {
  214. struct httpd_request *r = (struct httpd_request*) cls;
  215. append_key(r, key, value, 0, strlen(value));
  216. return MHD_YES;
  217. }
  218. static int ahc_echo(void * cls, struct MHD_Connection * connection,
  219. const char * url, const char * method, const char * version,
  220. const char * upload_data, unsigned int * upload_data_size, void ** ptr)
  221. {
  222. struct MHD_Response * response;
  223. int ret;
  224. int post = 0;
  225. if (0 == strcmp(method, "POST"))
  226. {
  227. post = 1;
  228. }
  229. else if (0 == strcmp(method, "GET"))
  230. {
  231. }
  232. else
  233. {
  234. return MHD_NO; /* unexpected method */
  235. }
  236. struct httpd_request *r;
  237. if (*ptr == NULL)
  238. {
  239. /* The first time only the headers are valid,
  240. do not respond in the first round... */
  241. *ptr = malloc(sizeof(struct httpd_request));
  242. if (*ptr == NULL)
  243. return MHD_NO;
  244. memset(*ptr, 0, sizeof(struct httpd_request));
  245. r = (struct httpd_request *) *ptr;
  246. r->post = post;
  247. Jim_SetVariableStr(interp, "httppostdata", Jim_NewDictObj(interp, NULL, 0));
  248. /* fill in url query strings in dictonary */
  249. MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND,
  250. record_arg, r);
  251. if (r->post)
  252. {
  253. r->postprocessor = MHD_create_post_processor(connection, 2048
  254. * 1024, iterate_post, r);
  255. }
  256. return MHD_YES;
  257. }
  258. r = (struct httpd_request *) *ptr;
  259. if (r->post)
  260. {
  261. /* consume post data */
  262. if (*upload_data_size)
  263. {
  264. MHD_post_process(r->postprocessor, upload_data, *upload_data_size);
  265. *upload_data_size = 0;
  266. return MHD_YES;
  267. }
  268. else
  269. {
  270. }
  271. } else
  272. {
  273. }
  274. /* hand over to request who will be using it. */
  275. // r->dict = NULL;
  276. /* FIX!!!! we need more advanced handling of url's to avoid them
  277. * being subverted to evil purposes
  278. */
  279. url++; /* skip '/' */
  280. const char *suffix;
  281. suffix = strrchr(url, '.');
  282. if ((suffix != NULL) && (strcmp(suffix, ".tcl") == 0))
  283. {
  284. printf("Run tcl %s\n", url);
  285. int retcode;
  286. const char *script = alloc_printf(
  287. "global httpdata; source {%s}; set httpdata", url);
  288. retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
  289. free((void *) script);
  290. if (retcode == JIM_ERR)
  291. {
  292. printf("Tcl failed\n");
  293. const char *t = httpd_exec_cgi_tcl_error(interp);
  294. if (t == NULL)
  295. return MHD_NO;
  296. response = MHD_create_response_from_data(strlen(t), (void *) t,
  297. MHD_YES, MHD_NO);
  298. ret = MHD_queue_response(connection,
  299. MHD_HTTP_INTERNAL_SERVER_ERROR, response);
  300. MHD_destroy_response(response);
  301. return ret;
  302. }
  303. else
  304. {
  305. printf("Tcl OK\n");
  306. /* FIX!!! how to handle mime types??? */
  307. const char *result;
  308. int reslen;
  309. result = Jim_GetString(Jim_GetResult(interp), &reslen);
  310. response = MHD_create_response_from_data(reslen, (void *) result,
  311. MHD_NO, MHD_YES);
  312. ret = MHD_queue_response(connection,
  313. MHD_HTTP_INTERNAL_SERVER_ERROR, response);
  314. MHD_destroy_response(response);
  315. return ret;
  316. }
  317. }
  318. else
  319. {
  320. void *data;
  321. int len;
  322. int retval = loadFile(url, &data, &len);
  323. if (retval != ERROR_OK)
  324. {
  325. printf("Did not find %s\n", url);
  326. response = MHD_create_response_from_data(strlen(PAGE_NOT_FOUND),
  327. (void *) PAGE_NOT_FOUND, MHD_NO, MHD_NO);
  328. ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
  329. MHD_destroy_response(response);
  330. return ret;
  331. }
  332. printf("Serving %s length=%d\n", url, len);
  333. /* serve file directly */
  334. response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
  335. MHD_add_response_header(response, "Content-Type", "image/png");
  336. ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
  337. MHD_destroy_response(response);
  338. //free(data);
  339. return ret;
  340. }
  341. }
  342. static struct MHD_Daemon * d;
  343. int httpd_start(void)
  344. {
  345. int port = 8888;
  346. LOG_USER("Launching httpd server on port %d", port);
  347. d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, port, NULL, NULL,
  348. &ahc_echo, NULL, /* could be data for handler, but we only have a single handler, use global variables instead */
  349. MHD_OPTION_NOTIFY_COMPLETED, request_completed, NULL, /* Closure... what's that??? */
  350. MHD_OPTION_END);
  351. if (d == NULL)
  352. return ERROR_FAIL;
  353. Jim_CreateCommand(interp,
  354. "formfetch",
  355. httpd_Jim_Command_formfetch,
  356. NULL,
  357. NULL);
  358. Jim_CreateCommand(interp,
  359. "writeform",
  360. httpd_Jim_Command_writeform,
  361. NULL,
  362. NULL);
  363. return ERROR_OK;
  364. }
  365. void httpd_stop(void)
  366. {
  367. MHD_stop_daemon(d);
  368. }
  369. void openocd_sleep_prelude(void)
  370. {
  371. /* FIX!!!! add locking here!!!! */
  372. }
  373. void openocd_sleep_postlude(void)
  374. {
  375. /* FIX!!!! add locking here!!!! */
  376. }