Browse Source

httpd: make zip archive work; change /firmware-upgrade -> /fwupdate

Fix serving files from the zip archive. It works now!

Change the needlessly long URL /firmware-upgrade to just
/fwupdate. This should be encapsulated in a simple Javascript script
on the front page anyway.
H. Peter Anvin 2 years ago
parent
commit
20f07396c7
4 changed files with 28 additions and 21 deletions
  1. 28 21
      esp32/max80/httpd.c
  2. BIN
      esp32/output/max80.ino.bin
  3. BIN
      fpga/output/v1.fw
  4. BIN
      fpga/output/v2.fw

+ 28 - 21
esp32/max80/httpd.c

@@ -83,15 +83,19 @@ static esp_err_t httpd_static_handler(httpd_req_t *req)
     uri = req->uri;
     while (*uri == '/')
 	uri++;			/* Skip leading slashes */
-    
+
     enduri = strchr(uri, '\0');
     if (enduri == uri) {
-	uri = index_filename;	/* Empty string */
-    } else if (enduri[0] == '/') {
+	add_index = true;
+    } else if (enduri[-1] == '/') {
 	add_index = true;
 	enduri--;		/* Drop terminal slash */
+    } else {
+	add_index = false;	/* Try the plain filename first */
     }
 
+    MSG("requesting: /%.*s\n", enduri - uri, uri);
+
     buffer = malloc(buffer_size);
     zip = malloc(sizeof *zip);
     if (!buffer || !zip) {
@@ -99,15 +103,22 @@ static esp_err_t httpd_static_handler(httpd_req_t *req)
 	goto out;
     }
 
-    if (enduri - req->uri + 1 + sizeof index_filename >= buffer_size) {
+    if (enduri - uri + 1 + sizeof index_filename >= buffer_size) {
 	err = httpd_resp_send_err(req, 414, "URI too long");
 	goto out;
     }
 
-    char *p = mempcpy(buffer, req->uri, enduri - req->uri);
+    char *p = mempcpy(buffer, uri, enduri - uri);
     *p = '\0';
 
-    int found;
+    unz = unzOpen(NULL, (void *)gwwwzipData, gwwwzipSize,
+		  zip, NULL, NULL, NULL, NULL);
+    if (!unz) {
+	MSG("[HTTP] unzOpen failed!\n");
+	err = httpd_resp_send_err(req, 500, "Cannot open content archive");
+	goto out;
+    }
+
     while (1) {
 	if (add_index) {
 	    if (p > buffer)
@@ -115,6 +126,7 @@ static esp_err_t httpd_static_handler(httpd_req_t *req)
 	    p = mempcpy(p, index_filename, sizeof index_filename);
 	}
 
+	MSG("trying to open: %s\n", buffer);
 	if (unzLocateFile(unz, buffer, 1) == UNZ_OK)
 	    break;
 
@@ -123,14 +135,14 @@ static esp_err_t httpd_static_handler(httpd_req_t *req)
 	    goto out;
 	}
 
-	add_index = true;	/* Try again with the index file */
+	add_index = true;	/* Try again with the index filename */
     }
 
     /* Note: p points to the end of the filename string */
     size_t filelen = p - buffer;
-    
+
     const struct mime_type *mime_type = mime_types;
-    /* The default entry with extension "" will always match */
+    /* The default entry with length 0 will always match */
     while (mime_type->ext_len) {
 	len = mime_type->ext_len;
 
@@ -139,13 +151,6 @@ static esp_err_t httpd_static_handler(httpd_req_t *req)
 
 	mime_type++;
     }
-    
-    unz = unzOpen(NULL, (void *)gwwwzipData, gwwwzipSize,
-		  zip, NULL, NULL, NULL, NULL);
-    if (!unz) {
-	err = httpd_resp_send_err(req, 500, "Cannot open content archive");
-	goto out;
-    }
 
     unz_file_info fileinfo;
     memset(&fileinfo, 0, sizeof fileinfo);
@@ -156,7 +161,7 @@ static esp_err_t httpd_static_handler(httpd_req_t *req)
      * way to not have to build the whole response in memory even
      * though the length is known a priori.
      */
-    
+
     len = snprintf(buffer, buffer_size,
 		   "HTTP/1.1 200 OK\r\n"
 		   "Content-Type: %s\r\n"
@@ -188,7 +193,7 @@ static esp_err_t httpd_static_handler(httpd_req_t *req)
 	goto out;
     }
     file_open = true;
-    
+
     len = fileinfo.uncompressed_size;
     while (len) {
 	size_t chunk = len;
@@ -223,19 +228,19 @@ out:
 
 static const httpd_uri_t uri_handlers[] = {
     {
-	.uri      = "/",
+	.uri      = "/*",
 	.method   = HTTP_GET,
 	.handler  = httpd_static_handler,
 	.user_ctx = NULL
     },
     {
-	.uri      = "/",
+	.uri      = "/*",
 	.method   = HTTP_HEAD,
 	.handler  = httpd_static_handler,
 	.user_ctx = NULL
     },
     {
-	.uri      = "/firmware-upgrade",
+	.uri      = "/fwupdate/?",
 	.method   = HTTP_POST,
 	.handler  = httpd_firmware_upgrade_handler,
 	.user_ctx = NULL
@@ -265,6 +270,8 @@ void my_httpd_start(void)
     config.stack_size <<= 2;
     printf("[HTTP] Requesting stack size: %zu\n", config.stack_size);
 
+    config.uri_match_fn = httpd_uri_match_wildcard;
+
     if (httpd_start(&server, &config) != ESP_OK)
       return;
 

BIN
esp32/output/max80.ino.bin


BIN
fpga/output/v1.fw


BIN
fpga/output/v2.fw