Ver Fonte

Add BROWSE method

jblanked há 1 ano atrás
pai
commit
fac68db692
4 ficheiros alterados com 76 adições e 20 exclusões
  1. 2 0
      CHANGELOG.md
  2. 1 2
      README.md
  3. 72 17
      callback/web_crawler_callback.c
  4. 1 1
      web_crawler.c

+ 2 - 0
CHANGELOG.md

@@ -2,6 +2,8 @@
 - Updated the HTTP Method toggle to work as intended.
 - Updated FlipperHTTP to the latest version.
 - Improved memory allocation
+- Fixed loading diplay messages.
+- Added a BROWSE method, which fetches HTML data from the specified path, parses the HTML, then displays the data as a "webpage". 
 
 ## 0.8
 Updates from Derek Jamison:

+ 1 - 2
README.md

@@ -4,7 +4,6 @@ Configure and manage HTTP requests directly from your Flipper Zero.
 - WiFi Developer Board, Raspberry Pi, or ESP32 device with the FlipperHTTP Flash: https://github.com/jblanked/FlipperHTTP
 - 2.4 GHz WiFi Access Point
 
-
 ## Installation
 - Download from the Official Flipper App Store: https://lab.flipper.net/apps/web_crawler
 - Video tutorial: https://www.youtube.com/watch?v=lkui2Smckq4
@@ -50,7 +49,7 @@ Configure and manage HTTP requests directly from your Flipper Zero.
    - Enter the complete URL of the website you intend to crawl (e.g., https://www.example.com/).
 
 2. **HTTP Method**
-   - Choose between GET, POST, DELETE, PUT, and DOWNLOAD.
+   - Choose between GET, POST, DELETE, PUT, DOWNLOAD, and BROWSE.
 
 3. **Headers**
    - Add your required headers to be used in your HTTP requests

+ 72 - 17
callback/web_crawler_callback.c

@@ -178,7 +178,7 @@ static bool alloc_variable_item_list(WebCrawlerApp *app, uint32_t view)
         }
         if (!app->http_method_item)
         {
-            app->http_method_item = variable_item_list_add(app->variable_item_list, "HTTP Method", 5, web_crawler_http_method_change, app);
+            app->http_method_item = variable_item_list_add(app->variable_item_list, "HTTP Method", 6, web_crawler_http_method_change, app);
             variable_item_set_current_value_text(app->http_method_item, ""); // Initialize
             variable_item_set_current_value_index(app->http_method_item, 0); // Initialize
         }
@@ -207,6 +207,7 @@ static bool alloc_variable_item_list(WebCrawlerApp *app, uint32_t view)
                                                      : strstr(http_method, "PUT") != NULL      ? 2
                                                      : strstr(http_method, "DELETE") != NULL   ? 3
                                                      : strstr(http_method, "DOWNLOAD") != NULL ? 4
+                                                     : strstr(http_method, "BROWSE") != NULL   ? 5
                                                                                                : 0);
         }
         else
@@ -704,7 +705,7 @@ static bool web_crawler_fetch(DataLoaderModel *model)
         // Perform DELETE request and handle the response
         return flipper_http_delete_request_with_headers(model->fhttp, url, headers, payload);
     }
-    else
+    else if (strstr(http_method, "DOWNLOAD") != NULL)
     {
         model->fhttp->save_received_data = false;
         model->fhttp->is_bytes_request = true;
@@ -712,39 +713,93 @@ static bool web_crawler_fetch(DataLoaderModel *model)
         // Perform GET request and handle the response
         return flipper_http_get_request_bytes(model->fhttp, url, headers);
     }
+    else // BROWSE
+    {
+        model->fhttp->save_received_data = true;
+        model->fhttp->is_bytes_request = false;
+
+        // Perform GET request and handle the response
+        if (strlen(headers) == 0)
+        {
+            return flipper_http_get_request(model->fhttp, url);
+        }
+        else
+        {
+            return flipper_http_get_request_with_headers(model->fhttp, url, headers);
+        }
+    }
     return false;
 }
 
 static char *web_crawler_parse(DataLoaderModel *model)
 {
     UNUSED(model);
-    // there is no parsing since everything is saved to file
+    // parse HTML response if BROWSE request
+    char http_method[16];
+    if (!load_char("http_method", http_method, 16))
+    {
+        FURI_LOG_E(TAG, "Failed to load http method");
+    }
+    else
+    {
+        if (strstr(http_method, "BROWSE") != NULL)
+        {
+            // parse HTML then return response
+            return "HTML response parsed.\nPress BACK to return.";
+        }
+    }
     return "Data saved to file.\nPress BACK to return.";
 }
 
 static void web_crawler_data_switch_to_view(WebCrawlerApp *app)
 {
     furi_check(app, "web_crawler_data_switch_to_view: WebCrawlerApp is NULL");
-    char *title = "GET Request";
-    if (strstr(app->http_method, "GET") != NULL)
-    {
-        title = "GET Request";
-    }
-    else if (strstr(app->http_method, "POST") != NULL)
-    {
-        title = "POST Request";
-    }
-    else if (strstr(app->http_method, "PUT") != NULL)
+
+    // Allocate title on the heap.
+    char *title = malloc(32);
+    if (title == NULL)
     {
-        title = "PUT Request";
+        FURI_LOG_E(TAG, "Failed to allocate memory for title");
+        return; // or handle the error as needed
     }
-    else if (strstr(app->http_method, "DELETE") != NULL)
+
+    char http_method[16];
+    if (!load_char("http_method", http_method, sizeof(http_method)))
     {
-        title = "DELETE Request";
+        FURI_LOG_E(TAG, "Failed to load http method");
+        snprintf(title, 32, "Request");
     }
     else
     {
-        title = "File Download";
+        if (strstr(http_method, "GET") != NULL)
+        {
+            snprintf(title, 32, "GET Request");
+        }
+        else if (strstr(http_method, "POST") != NULL)
+        {
+            snprintf(title, 32, "POST Request");
+        }
+        else if (strstr(http_method, "PUT") != NULL)
+        {
+            snprintf(title, 32, "PUT Request");
+        }
+        else if (strstr(http_method, "DELETE") != NULL)
+        {
+            snprintf(title, 32, "DELETE Request");
+        }
+        else if (strstr(http_method, "DOWNLOAD") != NULL)
+        {
+            snprintf(title, 32, "File Download");
+        }
+        else if (strstr(http_method, "BROWSE") != NULL)
+        {
+            snprintf(title, 32, "Browse URL");
+        }
+        else
+        {
+            // Provide a default title if no known http method is found.
+            snprintf(title, 32, "Request");
+        }
     }
     web_crawler_generic_switch_to_view(app, title, web_crawler_fetch, web_crawler_parse, 1, web_crawler_back_to_main_callback, WebCrawlerViewLoader);
 }

+ 1 - 1
web_crawler.c

@@ -48,4 +48,4 @@ void web_crawler_app_free(WebCrawlerApp *app)
     view_dispatcher_free(app->view_dispatcher);
     free(app);
 }
-char *http_method_names[] = {"GET", "POST", "PUT", "DELETE", "DOWNLOAD"};
+char *http_method_names[] = {"GET", "POST", "PUT", "DELETE", "DOWNLOAD", "BROWSE"};