|
|
@@ -2,6 +2,7 @@
|
|
|
FlipperHTTP fhttp;
|
|
|
char rx_line_buffer[RX_LINE_BUFFER_SIZE];
|
|
|
uint8_t file_buffer[FILE_BUFFER_SIZE];
|
|
|
+size_t file_buffer_len = 0;
|
|
|
// Function to append received data to file
|
|
|
// make sure to initialize the file path before calling this function
|
|
|
bool flipper_http_append_to_file(
|
|
|
@@ -147,14 +148,15 @@ int32_t flipper_http_worker(void *context)
|
|
|
{
|
|
|
UNUSED(context);
|
|
|
size_t rx_line_pos = 0;
|
|
|
- static size_t file_buffer_len = 0;
|
|
|
|
|
|
while (1)
|
|
|
{
|
|
|
uint32_t events = furi_thread_flags_wait(
|
|
|
WorkerEvtStop | WorkerEvtRxDone, FuriFlagWaitAny, FuriWaitForever);
|
|
|
if (events & WorkerEvtStop)
|
|
|
+ {
|
|
|
break;
|
|
|
+ }
|
|
|
if (events & WorkerEvtRxDone)
|
|
|
{
|
|
|
// Continuously read from the stream buffer until it's empty
|
|
|
@@ -178,6 +180,7 @@ int32_t flipper_http_worker(void *context)
|
|
|
// Write to file if buffer is full
|
|
|
if (file_buffer_len >= FILE_BUFFER_SIZE)
|
|
|
{
|
|
|
+ // remove [POST/END] and/or [GET/END] from the file
|
|
|
if (!flipper_http_append_to_file(
|
|
|
file_buffer, file_buffer_len, false, fhttp.file_path))
|
|
|
{
|
|
|
@@ -210,45 +213,6 @@ int32_t flipper_http_worker(void *context)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (fhttp.save_bytes)
|
|
|
- {
|
|
|
- // Write the remaining data to the file
|
|
|
- if (file_buffer_len > 0)
|
|
|
- {
|
|
|
- if (!flipper_http_append_to_file(file_buffer, file_buffer_len, false, fhttp.file_path))
|
|
|
- {
|
|
|
- FURI_LOG_E(HTTP_TAG, "Failed to append remaining data to file");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // remove [POST/END] and/or [GET/END] from the file
|
|
|
- if (fhttp.save_bytes)
|
|
|
- {
|
|
|
- char *end = NULL;
|
|
|
- if ((end = strstr(fhttp.file_path, "[POST/END]")) != NULL)
|
|
|
- {
|
|
|
- *end = '\0';
|
|
|
- }
|
|
|
- else if ((end = strstr(fhttp.file_path, "[GET/END]")) != NULL)
|
|
|
- {
|
|
|
- *end = '\0';
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // remove newline from the from the end of the file
|
|
|
- if (fhttp.save_bytes)
|
|
|
- {
|
|
|
- char *end = NULL;
|
|
|
- if ((end = strstr(fhttp.file_path, "\n")) != NULL)
|
|
|
- {
|
|
|
- *end = '\0';
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Reset the file buffer length
|
|
|
- file_buffer_len = 0;
|
|
|
-
|
|
|
return 0;
|
|
|
}
|
|
|
// Timer callback function
|
|
|
@@ -1128,8 +1092,39 @@ void flipper_http_rx_callback(const char *line, void *context)
|
|
|
fhttp.just_started_get = false;
|
|
|
fhttp.state = IDLE;
|
|
|
fhttp.save_bytes = false;
|
|
|
- fhttp.is_bytes_request = false;
|
|
|
fhttp.save_received_data = false;
|
|
|
+
|
|
|
+ if (fhttp.is_bytes_request)
|
|
|
+ {
|
|
|
+ // Search for the binary marker `[GET/END]` in the file buffer
|
|
|
+ const char marker[] = "[GET/END]";
|
|
|
+ const size_t marker_len = sizeof(marker) - 1; // Exclude null terminator
|
|
|
+
|
|
|
+ for (size_t i = 0; i <= file_buffer_len - marker_len; i++)
|
|
|
+ {
|
|
|
+ // Check if the marker is found
|
|
|
+ if (memcmp(&file_buffer[i], marker, marker_len) == 0)
|
|
|
+ {
|
|
|
+ // Remove the marker by shifting the remaining data left
|
|
|
+ size_t remaining_len = file_buffer_len - (i + marker_len);
|
|
|
+ memmove(&file_buffer[i], &file_buffer[i + marker_len], remaining_len);
|
|
|
+ file_buffer_len -= marker_len;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // If there is data left in the buffer, append it to the file
|
|
|
+ if (file_buffer_len > 0)
|
|
|
+ {
|
|
|
+ if (!flipper_http_append_to_file(file_buffer, file_buffer_len, false, fhttp.file_path))
|
|
|
+ {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
|
|
|
+ }
|
|
|
+ file_buffer_len = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fhttp.is_bytes_request = false;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -1167,8 +1162,39 @@ void flipper_http_rx_callback(const char *line, void *context)
|
|
|
fhttp.just_started_post = false;
|
|
|
fhttp.state = IDLE;
|
|
|
fhttp.save_bytes = false;
|
|
|
- fhttp.is_bytes_request = false;
|
|
|
fhttp.save_received_data = false;
|
|
|
+
|
|
|
+ if (fhttp.is_bytes_request)
|
|
|
+ {
|
|
|
+ // Search for the binary marker `[POST/END]` in the file buffer
|
|
|
+ const char marker[] = "[POST/END]";
|
|
|
+ const size_t marker_len = sizeof(marker) - 1; // Exclude null terminator
|
|
|
+
|
|
|
+ for (size_t i = 0; i <= file_buffer_len - marker_len; i++)
|
|
|
+ {
|
|
|
+ // Check if the marker is found
|
|
|
+ if (memcmp(&file_buffer[i], marker, marker_len) == 0)
|
|
|
+ {
|
|
|
+ // Remove the marker by shifting the remaining data left
|
|
|
+ size_t remaining_len = file_buffer_len - (i + marker_len);
|
|
|
+ memmove(&file_buffer[i], &file_buffer[i + marker_len], remaining_len);
|
|
|
+ file_buffer_len -= marker_len;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // If there is data left in the buffer, append it to the file
|
|
|
+ if (file_buffer_len > 0)
|
|
|
+ {
|
|
|
+ if (!flipper_http_append_to_file(file_buffer, file_buffer_len, false, fhttp.file_path))
|
|
|
+ {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
|
|
|
+ }
|
|
|
+ file_buffer_len = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fhttp.is_bytes_request = false;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -1291,6 +1317,7 @@ void flipper_http_rx_callback(const char *line, void *context)
|
|
|
fhttp.state = RECEIVING;
|
|
|
// for GET request, save data only if it's a bytes request
|
|
|
fhttp.save_bytes = fhttp.is_bytes_request;
|
|
|
+ file_buffer_len = 0;
|
|
|
return;
|
|
|
}
|
|
|
else if (strstr(line, "[POST/SUCCESS]") != NULL)
|
|
|
@@ -1301,6 +1328,7 @@ void flipper_http_rx_callback(const char *line, void *context)
|
|
|
fhttp.state = RECEIVING;
|
|
|
// for POST request, save data only if it's a bytes request
|
|
|
fhttp.save_bytes = fhttp.is_bytes_request;
|
|
|
+ file_buffer_len = 0;
|
|
|
return;
|
|
|
}
|
|
|
else if (strstr(line, "[PUT/SUCCESS]") != NULL)
|