httpd wip
[openocd.git] / src / server / httpd.c
index 96e338244ba005398f04020adfc90b1eb5b00dd2..348a081b631fb5c8d434983c228749fc727af03c 100644 (file)
@@ -261,13 +261,89 @@ static int record_arg(void *cls, enum MHD_ValueKind kind, const char *key,
        return MHD_YES;
 }
 
-static int ahc_echo(void * cls, struct MHD_Connection * connection,
-               const char * url, const char * method, const char * version,
-               const char * upload_data, unsigned int * upload_data_size, void ** ptr)
+
+int handle_request(struct MHD_Connection * connection, const char * url)
 {
        struct MHD_Response * response;
+
        int ret;
+       const char *suffix;
+       suffix = strrchr(url, '.');
+       if ((suffix != NULL) && (strcmp(suffix, ".tcl") == 0))
+       {
+               printf("Run tcl %s\n", url);
+
+               int retcode;
+
+               const char *script = alloc_printf(
+                               "global httpdata; source {%s}; set httpdata", url);
+               retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
+               free((void *) script);
+
+               if (retcode == JIM_ERR)
+               {
+                       printf("Tcl failed\n");
+                       const char *t = httpd_exec_cgi_tcl_error(interp);
+                       if (t == NULL)
+                               return MHD_NO;
+
+                       response = MHD_create_response_from_data(strlen(t), (void *) t,
+                                       MHD_YES, MHD_NO);
+                       ret = MHD_queue_response(connection,
+                                       MHD_HTTP_INTERNAL_SERVER_ERROR, response);
+                       MHD_destroy_response(response);
+                       return ret;
+               }
+               else
+               {
+                       printf("Tcl OK\n");
+                       /* FIX!!! how to handle mime types??? */
+                       const char *result;
+                       int reslen;
+                       result = Jim_GetString(Jim_GetResult(interp), &reslen);
 
+                       response = MHD_create_response_from_data(reslen, (void *) result,
+                                       MHD_NO, MHD_YES);
+                       ret = MHD_queue_response(connection,
+                                       MHD_HTTP_INTERNAL_SERVER_ERROR, response);
+                       MHD_destroy_response(response);
+                       return ret;
+               }
+       }
+       else
+       {
+               void *data;
+               int len;
+
+               int retval = loadFile(url, &data, &len);
+               if (retval != ERROR_OK)
+               {
+                       printf("Did not find %s\n", url);
+
+                       response = MHD_create_response_from_data(strlen(PAGE_NOT_FOUND),
+                                       (void *) PAGE_NOT_FOUND, MHD_NO, MHD_NO);
+                       ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
+                       MHD_destroy_response(response);
+                       return ret;
+               }
+
+               LOG_DEBUG("Serving %s length=%d", url, len);
+               /* serve file directly */
+               response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
+               MHD_add_response_header(response, "Content-Type", "image/png");
+
+               ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
+               MHD_destroy_response(response);
+
+               //free(data);
+               return ret;
+       }
+}
+
+static int ahc_echo(void * cls, struct MHD_Connection * connection,
+               const char * url, const char * method, const char * version,
+               const char * upload_data, unsigned int * upload_data_size, void ** ptr)
+{
        int post = 0;
 
        if (0 == strcmp(method, "POST"))
@@ -337,79 +413,19 @@ static int ahc_echo(void * cls, struct MHD_Connection * connection,
         * being subverted to evil purposes
         */
 
-       url++; /* skip '/' */
+       const char *httpd_dir=PKGLIBDIR "/httpd";
 
-       const char *suffix;
-       suffix = strrchr(url, '.');
-       if ((suffix != NULL) && (strcmp(suffix, ".tcl") == 0))
+       if (*url=='/')
        {
-               printf("Run tcl %s\n", url);
-
-               int retcode;
-
-               const char *script = alloc_printf(
-                               "global httpdata; source {%s}; set httpdata", url);
-               retcode = Jim_Eval_Named(interp, script, "httpd.c", __LINE__ );
-               free((void *) script);
-
-               if (retcode == JIM_ERR)
-               {
-                       printf("Tcl failed\n");
-                       const char *t = httpd_exec_cgi_tcl_error(interp);
-                       if (t == NULL)
-                               return MHD_NO;
-
-                       response = MHD_create_response_from_data(strlen(t), (void *) t,
-                                       MHD_YES, MHD_NO);
-                       ret = MHD_queue_response(connection,
-                                       MHD_HTTP_INTERNAL_SERVER_ERROR, response);
-                       MHD_destroy_response(response);
-                       return ret;
-               }
-               else
-               {
-                       printf("Tcl OK\n");
-                       /* FIX!!! how to handle mime types??? */
-                       const char *result;
-                       int reslen;
-                       result = Jim_GetString(Jim_GetResult(interp), &reslen);
-
-                       response = MHD_create_response_from_data(reslen, (void *) result,
-                                       MHD_NO, MHD_YES);
-                       ret = MHD_queue_response(connection,
-                                       MHD_HTTP_INTERNAL_SERVER_ERROR, response);
-                       MHD_destroy_response(response);
-                       return ret;
-               }
+               url++; /* skip '/' */
        }
-       else
-       {
-               void *data;
-               int len;
-
-               int retval = loadFile(url, &data, &len);
-               if (retval != ERROR_OK)
-               {
-                       printf("Did not find %s\n", url);
-
-                       response = MHD_create_response_from_data(strlen(PAGE_NOT_FOUND),
-                                       (void *) PAGE_NOT_FOUND, MHD_NO, MHD_NO);
-                       ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
-                       MHD_destroy_response(response);
-                       return ret;
-               }
-
-               printf("Serving %s length=%d\n", url, len);
-               /* serve file directly */
-               response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
-               MHD_add_response_header(response, "Content-Type", "image/png");
+       if (!*url)
+               url="index.tcl";
 
-               ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
-               MHD_destroy_response(response);
-
-               //free(data);
-               return ret;
-       }
+       const char *file_name=alloc_printf("%s/%s", httpd_dir, url);
+       int result = handle_request(connection, file_name);
+       free((void *)file_name);
+       return result;
 }
 
 static struct MHD_Daemon * d;

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)