Fix wifi but not ble builds. Add DELETE and PUT support
This commit is contained in:
parent
92593aa7a1
commit
847ac60dce
|
@ -30,10 +30,13 @@
|
|||
#include "extmod/vfs_fat.h"
|
||||
#include "genhdr/mpversion.h"
|
||||
#include "py/mpstate.h"
|
||||
#include "py/stackctrl.h"
|
||||
|
||||
#include "shared-bindings/wifi/Radio.h"
|
||||
#include "shared-module/storage/__init__.h"
|
||||
#include "supervisor/shared/translate/translate.h"
|
||||
#include "supervisor/shared/web_workflow/web_workflow.h"
|
||||
#include "supervisor/usb.h"
|
||||
|
||||
#include "shared-bindings/mdns/RemoteService.h"
|
||||
#include "shared-bindings/mdns/Server.h"
|
||||
|
@ -64,15 +67,16 @@ enum request_state {
|
|||
|
||||
typedef struct {
|
||||
enum request_state state;
|
||||
char method[6];
|
||||
char method[8];
|
||||
char path[256];
|
||||
char header_key[64];
|
||||
char header_value[64];
|
||||
uint32_t content_length;
|
||||
size_t content_length;
|
||||
size_t offset;
|
||||
bool redirect;
|
||||
bool done;
|
||||
bool authenticated;
|
||||
bool expect;
|
||||
} _request;
|
||||
|
||||
static wifi_radio_error_t wifi_status = WIFI_RADIO_ERROR_NONE;
|
||||
|
@ -306,10 +310,26 @@ static bool _endswith(const char *str, const char *suffix) {
|
|||
return strcmp(str + (strlen(str) - strlen(suffix)), suffix) == 0;
|
||||
}
|
||||
|
||||
static void _reply_continue(socketpool_socket_obj_t *socket) {
|
||||
const char *response = "HTTP/1.1 100 Continue\r\n\r\n";
|
||||
_send_str(socket, response);
|
||||
}
|
||||
|
||||
static void _reply_created(socketpool_socket_obj_t *socket) {
|
||||
const char *response = "HTTP/1.1 201 Created\r\n"
|
||||
"Content-Length: 0\r\n\r\n";
|
||||
_send_str(socket, response);
|
||||
}
|
||||
|
||||
static void _reply_no_content(socketpool_socket_obj_t *socket) {
|
||||
const char *response = "HTTP/1.1 204 No Content\r\n"
|
||||
"Content-Length: 0\r\n\r\n";
|
||||
_send_str(socket, response);
|
||||
}
|
||||
|
||||
static void _reply_missing(socketpool_socket_obj_t *socket) {
|
||||
const char *response = "HTTP/1.1 401 Unauthorized\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"WWW-Authenticate: Basic realm=\"CircuitPython\"\r\n\r\n";
|
||||
const char *response = "HTTP/1.1 404 Not Found\r\n"
|
||||
"Content-Length: 0\r\n\r\n";
|
||||
_send_str(socket, response);
|
||||
}
|
||||
|
||||
|
@ -319,6 +339,24 @@ static void _reply_forbidden(socketpool_socket_obj_t *socket) {
|
|||
_send_str(socket, response);
|
||||
}
|
||||
|
||||
static void _reply_conflict(socketpool_socket_obj_t *socket) {
|
||||
const char *response = "HTTP/1.1 409 Conflict\r\n"
|
||||
"Content-Length: 19\r\n\r\nUSB storage active.";
|
||||
_send_str(socket, response);
|
||||
}
|
||||
|
||||
static void _reply_payload_too_large(socketpool_socket_obj_t *socket) {
|
||||
const char *response = "HTTP/1.1 413 Payload Too Large\r\n"
|
||||
"Content-Length: 0\r\n\r\n";
|
||||
_send_str(socket, response);
|
||||
}
|
||||
|
||||
static void _reply_expectation_failed(socketpool_socket_obj_t *socket) {
|
||||
const char *response = "HTTP/1.1 417 Expectation Failed\r\n"
|
||||
"Content-Length: 0\r\n\r\n";
|
||||
_send_str(socket, response);
|
||||
}
|
||||
|
||||
static void _reply_unauthorized(socketpool_socket_obj_t *socket) {
|
||||
const char *response = "HTTP/1.1 401 Unauthorized\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
|
@ -326,17 +364,438 @@ static void _reply_unauthorized(socketpool_socket_obj_t *socket) {
|
|||
_send_str(socket, response);
|
||||
}
|
||||
|
||||
static void _reply_server_error(socketpool_socket_obj_t *socket) {
|
||||
const char *response = "HTTP/1.1 500 Internal Server Error\r\n"
|
||||
"Content-Length: 0\r\n\r\n";
|
||||
_send_str(socket, response);
|
||||
}
|
||||
|
||||
static void _reply_redirect(socketpool_socket_obj_t *socket, const char *path) {
|
||||
const char *redirect_response = "HTTP/1.1 301 Moved Permanently\r\nConnection: close\r\nContent-Length: 0\r\nLocation: http://";
|
||||
int nodelay = 1;
|
||||
lwip_setsockopt(socket->num, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay));
|
||||
_send_str(socket, redirect_response);
|
||||
_send_str(socket, common_hal_mdns_server_get_instance_name(&mdns));
|
||||
_send_str(socket, common_hal_mdns_server_get_hostname(&mdns));
|
||||
_send_str(socket, ".local");
|
||||
_send_str(socket, path);
|
||||
_send_str(socket, "\r\n\r\n");
|
||||
}
|
||||
|
||||
static void _reply_directory_html(socketpool_socket_obj_t *socket, FF_DIR *dir, const char *request_path, const char *path) {
|
||||
const char *ok_response = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"
|
||||
"Content-Type: text/html\r\n\r\n";
|
||||
socketpool_socket_send(socket, (const uint8_t *)ok_response, strlen(ok_response));
|
||||
_send_chunk(socket, "<!DOCTYPE html><html><head><title>");
|
||||
_send_chunk(socket, path);
|
||||
_send_chunk(socket, "</title><meta charset=\"UTF-8\"></head><body><h1>");
|
||||
_send_chunk(socket, path);
|
||||
_send_chunk(socket, "</h1><pre>");
|
||||
if (strlen(path) > 1) {
|
||||
_send_chunk(socket, "🗀\t<a href=\"..\">..</a><br/>");
|
||||
}
|
||||
|
||||
FILINFO file_info;
|
||||
char *fn = file_info.fname;
|
||||
FRESULT res = f_readdir(dir, &file_info);
|
||||
while (res == FR_OK && fn[0] != 0) {
|
||||
// uint64_t truncated_time = timeutils_mktime(1980 + (file_info.fdate >> 9),
|
||||
// (file_info.fdate >> 5) & 0xf,
|
||||
// file_info.fdate & 0x1f,
|
||||
// file_info.ftime >> 11,
|
||||
// (file_info.ftime >> 5) & 0x1f,
|
||||
// (file_info.ftime & 0x1f) * 2) * 1000000000ULL;
|
||||
// entry->truncated_time = truncated_time;
|
||||
// if ((file_info.fattrib & AM_DIR) != 0) {
|
||||
// entry->flags = 1; // Directory
|
||||
// entry->file_size = 0;
|
||||
// } else {
|
||||
// entry->flags = 0;
|
||||
// entry->file_size = file_info.fsize;
|
||||
// }
|
||||
// _send_chunk(socket, "<li>");
|
||||
bool editable = false;
|
||||
if ((file_info.fattrib & AM_DIR) != 0) {
|
||||
_send_chunk(socket, "🗀\t");
|
||||
} else if (_endswith(file_info.fname, ".txt") ||
|
||||
_endswith(file_info.fname, ".py") ||
|
||||
_endswith(file_info.fname, ".js") ||
|
||||
_endswith(file_info.fname, ".json")) {
|
||||
_send_chunk(socket, "🖹\t");
|
||||
editable = true;
|
||||
} else if (_endswith(file_info.fname, ".html")) {
|
||||
_send_chunk(socket, "🌐\t");
|
||||
editable = true;
|
||||
} else {
|
||||
_send_chunk(socket, "⬇\t");
|
||||
}
|
||||
|
||||
_send_chunk(socket, "<a href=\"");
|
||||
_send_chunk(socket, request_path);
|
||||
_send_chunk(socket, file_info.fname);
|
||||
if ((file_info.fattrib & AM_DIR) != 0) {
|
||||
_send_chunk(socket, "/");
|
||||
}
|
||||
_send_chunk(socket, "\">");
|
||||
|
||||
_send_chunk(socket, file_info.fname);
|
||||
_send_chunk(socket, "</a>");
|
||||
if (editable) {
|
||||
_send_chunk(socket, "<a>✏️</a>");
|
||||
}
|
||||
_send_chunk(socket, "<a>🗑️</a><br/>");
|
||||
res = f_readdir(dir, &file_info);
|
||||
}
|
||||
_send_chunk(socket, "</pre>Upload:<input type=\"file\" multiple></body></html>");
|
||||
_send_chunk(socket, "");
|
||||
}
|
||||
|
||||
static void _reply_with_file(socketpool_socket_obj_t *socket, const char *filename, FIL *active_file) {
|
||||
uint32_t total_length = f_size(active_file);
|
||||
char encoded_len[10];
|
||||
snprintf(encoded_len, sizeof(encoded_len), "%d", total_length);
|
||||
|
||||
_send_str(socket, "HTTP/1.1 200 OK\r\nContent-Length: ");
|
||||
_send_str(socket, encoded_len);
|
||||
_send_str(socket, "\r\n");
|
||||
if (_endswith(filename, ".txt") || _endswith(filename, ".py")) {
|
||||
_send_str(socket, "Content-Type: text/plain\r\n");
|
||||
} else if (_endswith(filename, ".js")) {
|
||||
_send_str(socket, "Content-Type: text/javascript\r\n");
|
||||
} else if (_endswith(filename, ".html")) {
|
||||
_send_str(socket, "Content-Type: text/html\r\n");
|
||||
} else if (_endswith(filename, ".json")) {
|
||||
_send_str(socket, "Content-Type: application/json\r\n");
|
||||
} else {
|
||||
_send_str(socket, "Content-Type: application/octet-stream\r\n");
|
||||
}
|
||||
_send_str(socket, "\r\n");
|
||||
|
||||
uint32_t total_read = 0;
|
||||
while (total_read < total_length) {
|
||||
uint8_t data_buffer[64];
|
||||
size_t quantity_read;
|
||||
f_read(active_file, data_buffer, 64, &quantity_read);
|
||||
total_read += quantity_read;
|
||||
uint32_t send_offset = 0;
|
||||
while (send_offset < quantity_read) {
|
||||
int sent = socketpool_socket_send(socket, data_buffer + send_offset, quantity_read - send_offset);
|
||||
if (sent < 0) {
|
||||
if (sent == -EAGAIN) {
|
||||
sent = 0;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "file send error %d", sent);
|
||||
break;
|
||||
}
|
||||
}
|
||||
send_offset += sent;
|
||||
}
|
||||
}
|
||||
ESP_LOGW(TAG, "file return done");
|
||||
if (total_read < total_length) {
|
||||
socketpool_socket_close(socket);
|
||||
}
|
||||
}
|
||||
|
||||
static void _reply_with_devices_json(socketpool_socket_obj_t *socket) {
|
||||
mdns_remoteservice_obj_t found_devices[32];
|
||||
size_t total_results = mdns_server_find(&mdns, "_circuitpython", "_tcp", 1, found_devices, MP_ARRAY_SIZE(found_devices));
|
||||
size_t count = MIN(total_results, MP_ARRAY_SIZE(found_devices));
|
||||
const char *ok_response = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Type: application/json\r\n\r\n";
|
||||
socketpool_socket_send(socket, (const uint8_t *)ok_response, strlen(ok_response));
|
||||
_send_chunk(socket, "{\"total\": ");
|
||||
char total_encoded[4];
|
||||
snprintf(total_encoded, sizeof(total_encoded), "%d", total_results);
|
||||
_send_chunk(socket, total_encoded);
|
||||
_send_chunk(socket, ", \"devices\": [");
|
||||
ESP_LOGW(TAG, "%d total devices", total_results);
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
if (i > 0) {
|
||||
_send_chunk(socket, ",");
|
||||
}
|
||||
_send_chunk(socket, "{\"hostname\": \"");
|
||||
_send_chunk(socket, common_hal_mdns_remoteservice_get_hostname(&found_devices[i]));
|
||||
_send_chunk(socket, "\", \"instance_name\": \"");
|
||||
_send_chunk(socket, common_hal_mdns_remoteservice_get_instance_name(&found_devices[i]));
|
||||
_send_chunk(socket, "\", \"port\": ");
|
||||
char port_encoded[4];
|
||||
int port = common_hal_mdns_remoteservice_get_port(&found_devices[i]);
|
||||
snprintf(port_encoded, sizeof(port_encoded), "%d", port);
|
||||
_send_chunk(socket, port_encoded);
|
||||
_send_chunk(socket, "}");
|
||||
common_hal_mdns_remoteservice_deinit(&found_devices[i]);
|
||||
}
|
||||
_send_chunk(socket, "]}");
|
||||
// Empty chunk signals the end of the response.
|
||||
_send_chunk(socket, "");
|
||||
}
|
||||
|
||||
static void _reply_with_version_json(socketpool_socket_obj_t *socket) {
|
||||
const char *ok_response = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Type: application/json\r\n\r\n";
|
||||
socketpool_socket_send(socket, (const uint8_t *)ok_response, strlen(ok_response));
|
||||
_send_chunk(socket, "{\"web_api_version\": 1, \"version\": \"");
|
||||
_send_chunk(socket, MICROPY_GIT_TAG);
|
||||
_send_chunk(socket, "\", \"build_date\": \"");
|
||||
_send_chunk(socket, MICROPY_BUILD_DATE);
|
||||
_send_chunk(socket, "\", \"board_name\": \"");
|
||||
_send_chunk(socket, MICROPY_HW_BOARD_NAME);
|
||||
_send_chunk(socket, "\", \"mcu_name\": \"");
|
||||
_send_chunk(socket, MICROPY_HW_MCU_NAME);
|
||||
_send_chunk(socket, "\", \"board_id\": \"");
|
||||
_send_chunk(socket, CIRCUITPY_BOARD_ID);
|
||||
_send_chunk(socket, "\", \"creator_id\": ");
|
||||
char encoded_id[8];
|
||||
snprintf(encoded_id, sizeof(encoded_id), "%d", CIRCUITPY_CREATOR_ID);
|
||||
_send_chunk(socket, encoded_id);
|
||||
_send_chunk(socket, ", \"creation_id\": ");
|
||||
snprintf(encoded_id, sizeof(encoded_id), "%d", CIRCUITPY_CREATION_ID);
|
||||
_send_chunk(socket, encoded_id);
|
||||
_send_chunk(socket, "}");
|
||||
// Empty chunk signals the end of the response.
|
||||
_send_chunk(socket, "");
|
||||
}
|
||||
|
||||
STATIC bool _usb_active(void) {
|
||||
// Check to see if USB has already been mounted. If not, then we "eject" from USB until we're done.
|
||||
#if CIRCUITPY_USB && CIRCUITPY_USB_MSC
|
||||
if (storage_usb_enabled() && !usb_msc_lock()) {
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copied from ble file_transfer.c. We should share it.
|
||||
STATIC FRESULT _delete_directory_contents(FATFS *fs, const TCHAR *path) {
|
||||
FF_DIR dir;
|
||||
FRESULT res = f_opendir(fs, &dir, path);
|
||||
FILINFO file_info;
|
||||
// Check the stack since we're putting paths on it.
|
||||
if (mp_stack_usage() >= MP_STATE_THREAD(stack_limit)) {
|
||||
return FR_INT_ERR;
|
||||
}
|
||||
while (res == FR_OK) {
|
||||
res = f_readdir(&dir, &file_info);
|
||||
if (res != FR_OK || file_info.fname[0] == '\0') {
|
||||
break;
|
||||
}
|
||||
size_t pathlen = strlen(path);
|
||||
size_t fnlen = strlen(file_info.fname);
|
||||
TCHAR full_path[pathlen + 1 + fnlen];
|
||||
memcpy(full_path, path, pathlen);
|
||||
full_path[pathlen] = '/';
|
||||
size_t full_pathlen = pathlen + 1 + fnlen;
|
||||
memcpy(full_path + pathlen + 1, file_info.fname, fnlen);
|
||||
full_path[full_pathlen] = '\0';
|
||||
if ((file_info.fattrib & AM_DIR) != 0) {
|
||||
res = _delete_directory_contents(fs, full_path);
|
||||
}
|
||||
if (res != FR_OK) {
|
||||
break;
|
||||
}
|
||||
res = f_unlink(fs, full_path);
|
||||
}
|
||||
f_closedir(&dir);
|
||||
return res;
|
||||
}
|
||||
|
||||
static void _write_file_and_reply(socketpool_socket_obj_t *socket, _request *request, FATFS *fs, const TCHAR *path) {
|
||||
FIL active_file;
|
||||
|
||||
if (_usb_active()) {
|
||||
_reply_conflict(socket);
|
||||
return;
|
||||
}
|
||||
FRESULT result = f_open(fs, &active_file, path, FA_WRITE);
|
||||
bool new_file = false;
|
||||
if (result == FR_NO_FILE) {
|
||||
new_file = true;
|
||||
result = f_open(fs, &active_file, path, FA_WRITE | FA_OPEN_ALWAYS);
|
||||
}
|
||||
|
||||
if (result != FR_OK) {
|
||||
ESP_LOGW(TAG, "file write error %d %s", result, path);
|
||||
_reply_server_error(socket);
|
||||
} else if (request->expect) {
|
||||
_reply_continue(socket);
|
||||
}
|
||||
|
||||
// Change the file size to start.
|
||||
f_lseek(&active_file, request->content_length);
|
||||
if (f_tell(&active_file) < request->content_length) {
|
||||
f_close(&active_file);
|
||||
#if CIRCUITPY_USB_MSC
|
||||
usb_msc_unlock();
|
||||
#endif
|
||||
// Too large.
|
||||
if (request->expect) {
|
||||
_reply_expectation_failed(socket);
|
||||
} else {
|
||||
_reply_payload_too_large(socket);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
f_truncate(&active_file);
|
||||
f_rewind(&active_file);
|
||||
|
||||
size_t total_read = 0;
|
||||
bool error = false;
|
||||
while (total_read < request->content_length && !error) {
|
||||
uint8_t bytes[64];
|
||||
size_t read_len = MIN(sizeof(bytes), request->content_length - total_read);
|
||||
int len = socketpool_socket_recv_into(socket, bytes, read_len);
|
||||
if (len < 0) {
|
||||
if (len == -EAGAIN) {
|
||||
continue;
|
||||
} else {
|
||||
ESP_LOGW(TAG, "other error %d", len);
|
||||
}
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
UINT actual;
|
||||
f_write(&active_file, bytes, len, &actual);
|
||||
if (actual != (UINT)len) {
|
||||
ESP_LOGE(TAG, "didn't write whole file");
|
||||
}
|
||||
total_read += len;
|
||||
}
|
||||
|
||||
f_close(&active_file);
|
||||
#if CIRCUITPY_USB_MSC
|
||||
usb_msc_unlock();
|
||||
#endif
|
||||
if (new_file) {
|
||||
_reply_created(socket);
|
||||
} else {
|
||||
_reply_no_content(socket);
|
||||
}
|
||||
}
|
||||
|
||||
static void _reply(socketpool_socket_obj_t *socket, _request *request) {
|
||||
if (request->redirect) {
|
||||
_reply_redirect(socket, request->path);
|
||||
} else if (memcmp(request->path, "/fs/", 4) == 0) {
|
||||
if (!request->authenticated) {
|
||||
ESP_LOGW(TAG, "not authed");
|
||||
if (_api_password[0] != '\0') {
|
||||
_reply_unauthorized(socket);
|
||||
} else {
|
||||
_reply_forbidden(socket);
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "filesystem %s %s", request->method, request->path + 3);
|
||||
char *path = request->path + 3;
|
||||
size_t pathlen = strlen(path);
|
||||
FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
|
||||
// Trailing / is a directory.
|
||||
bool directory = false;
|
||||
if (path[pathlen - 1] == '/') {
|
||||
// FATFS lib doesn't like a trailing / for paths besides root.
|
||||
if (pathlen > 1) {
|
||||
path[pathlen - 1] = '\0';
|
||||
}
|
||||
directory = true;
|
||||
}
|
||||
// Delete is almost identical for files and directories so share the
|
||||
// implementation.
|
||||
if (strcmp(request->method, "DELETE") == 0) {
|
||||
if (_usb_active()) {
|
||||
_reply_conflict(socket);
|
||||
return;
|
||||
}
|
||||
|
||||
FILINFO file;
|
||||
FRESULT result = f_stat(fs, path, &file);
|
||||
if (result == FR_OK) {
|
||||
if ((file.fattrib & AM_DIR) != 0) {
|
||||
result = _delete_directory_contents(fs, path);
|
||||
}
|
||||
if (result == FR_OK) {
|
||||
result = f_unlink(fs, path);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == FR_NO_PATH || result == FR_NO_FILE) {
|
||||
_reply_missing(socket);
|
||||
} else if (result != FR_OK) {
|
||||
ESP_LOGW(TAG, "rm error %d %s", result, path);
|
||||
_reply_server_error(socket);
|
||||
} else {
|
||||
_reply_no_content(socket);
|
||||
}
|
||||
} else if (directory) {
|
||||
if (strcmp(request->method, "GET") == 0) {
|
||||
FF_DIR dir;
|
||||
FRESULT res = f_opendir(fs, &dir, path);
|
||||
// Put the / back for replies.
|
||||
if (pathlen > 1) {
|
||||
path[pathlen - 1] = '/';
|
||||
}
|
||||
if (res != FR_OK) {
|
||||
ESP_LOGW(TAG, "unable to open %d %s", res, path);
|
||||
_reply_missing(socket);
|
||||
return;
|
||||
}
|
||||
_reply_directory_html(socket, &dir, request->path, path);
|
||||
f_closedir(&dir);
|
||||
} else if (strcmp(request->method, "PUT") == 0) {
|
||||
if (_usb_active()) {
|
||||
_reply_conflict(socket);
|
||||
return;
|
||||
}
|
||||
|
||||
FRESULT result = f_mkdir(fs, path);
|
||||
#if CIRCUITPY_USB_MSC
|
||||
usb_msc_unlock();
|
||||
#endif
|
||||
if (result == FR_EXIST) {
|
||||
_reply_no_content(socket);
|
||||
} else if (result == FR_NO_PATH) {
|
||||
_reply_missing(socket);
|
||||
} else if (result != FR_OK) {
|
||||
ESP_LOGE(TAG, "mkdir error %d %s", result, path);
|
||||
_reply_server_error(socket);
|
||||
} else {
|
||||
_reply_created(socket);
|
||||
}
|
||||
}
|
||||
} else { // Dealing with a file.
|
||||
if (strcmp(request->method, "GET") == 0) {
|
||||
FIL active_file;
|
||||
FRESULT result = f_open(fs, &active_file, path, FA_READ);
|
||||
|
||||
if (result != FR_OK) {
|
||||
// TODO: 404
|
||||
_reply_missing(socket);
|
||||
} else {
|
||||
_reply_with_file(socket, path, &active_file);
|
||||
}
|
||||
|
||||
f_close(&active_file);
|
||||
} else if (strcmp(request->method, "PUT") == 0) {
|
||||
_write_file_and_reply(socket, request, fs, path);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (memcmp(request->path, "/cp/", 4) == 0) {
|
||||
ESP_LOGW(TAG, "circuitpython %s %s", request->method, request->path + 3);
|
||||
const char *path = request->path + 3;
|
||||
if (strcmp(request->method, "GET") != 0) {
|
||||
// Return error if not a GET
|
||||
}
|
||||
if (strcmp(path, "/devices.json") == 0) {
|
||||
_reply_with_devices_json(socket);
|
||||
} else if (strcmp(path, "/version.json") == 0) {
|
||||
_reply_with_version_json(socket);
|
||||
}
|
||||
} else {
|
||||
const char *ok_response = "HTTP/1.1 200 OK\r\nContent-Length: 11\r\nContent-Type: text/plain\r\n\r\nHello World";
|
||||
int sent = socketpool_socket_send(socket, (const uint8_t *)ok_response, strlen(ok_response));
|
||||
ESP_LOGW(TAG, "sent ok %d", sent);
|
||||
}
|
||||
}
|
||||
|
||||
static void _process_request(socketpool_socket_obj_t *socket, _request *request) {
|
||||
bool more = true;
|
||||
bool error = false;
|
||||
|
@ -385,7 +844,6 @@ static void _process_request(socketpool_socket_obj_t *socket, _request *request)
|
|||
error = supported_version[request->offset] != c;
|
||||
request->offset++;
|
||||
if (request->offset == strlen(supported_version)) {
|
||||
ESP_LOGW(TAG, "Version ok");
|
||||
request->state = STATE_HEADER_KEY;
|
||||
request->offset = 0;
|
||||
}
|
||||
|
@ -394,7 +852,6 @@ static void _process_request(socketpool_socket_obj_t *socket, _request *request)
|
|||
case STATE_HEADER_KEY: {
|
||||
if (c == '\r') {
|
||||
request->state = STATE_BODY;
|
||||
ESP_LOGW(TAG, "Body");
|
||||
} else if (c == '\n') {
|
||||
// Consume the \n
|
||||
} else if (c == ':') {
|
||||
|
@ -421,10 +878,12 @@ static void _process_request(socketpool_socket_obj_t *socket, _request *request)
|
|||
const char *prefix = "Basic ";
|
||||
request->authenticated = memcmp(request->header_value, prefix, strlen(prefix)) == 0 &&
|
||||
strcmp(_api_password, request->header_value + strlen(prefix)) == 0;
|
||||
ESP_LOGW(TAG, "Authorization %d", request->authenticated);
|
||||
} else if (strcmp(request->header_key, "Host") == 0) {
|
||||
ESP_LOGW(TAG, "Host header check '%s'", request->header_value);
|
||||
request->redirect = strcmp(request->header_value, "circuitpython.local") == 0;
|
||||
} else if (strcmp(request->header_key, "Content-Length") == 0) {
|
||||
request->content_length = strtoul(request->header_value, NULL, 10);
|
||||
} else if (strcmp(request->header_key, "Expect") == 0) {
|
||||
request->expect = strcmp(request->header_value, "100-continue") == 0;
|
||||
}
|
||||
ESP_LOGW(TAG, "Header %s %s", request->header_key, request->header_value);
|
||||
} else if (request->offset > sizeof(request->header_value) - 1) {
|
||||
|
@ -436,7 +895,6 @@ static void _process_request(socketpool_socket_obj_t *socket, _request *request)
|
|||
break;
|
||||
}
|
||||
case STATE_BODY:
|
||||
ESP_LOGW(TAG, "BODY %c", c);
|
||||
request->done = true;
|
||||
request->state = STATE_METHOD;
|
||||
more = false;
|
||||
|
@ -453,201 +911,7 @@ static void _process_request(socketpool_socket_obj_t *socket, _request *request)
|
|||
if (!request->done) {
|
||||
return;
|
||||
}
|
||||
if (request->redirect) {
|
||||
_reply_redirect(socket, request->path);
|
||||
} else if (memcmp(request->path, "/fs/", 4) == 0) {
|
||||
if (!request->authenticated) {
|
||||
if (_api_password[0] != '\0') {
|
||||
_reply_unauthorized(socket);
|
||||
} else {
|
||||
_reply_forbidden(socket);
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "filesystem %s %s", request->method, request->path + 3);
|
||||
const char *path = request->path + 3;
|
||||
size_t pathlen = strlen(path);
|
||||
FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
|
||||
// Trailing / is a directory.
|
||||
if (path[pathlen - 1] == '/') {
|
||||
if (strcmp(request->method, "GET") == 0) {
|
||||
FF_DIR dir;
|
||||
FRESULT res = f_opendir(fs, &dir, path);
|
||||
if (res != FR_OK) {
|
||||
_reply_missing(socket);
|
||||
}
|
||||
const char *ok_response = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"
|
||||
"Content-Type: text/html\r\n\r\n";
|
||||
socketpool_socket_send(socket, (const uint8_t *)ok_response, strlen(ok_response));
|
||||
_send_chunk(socket, "<!DOCTYPE html><html><head><title>");
|
||||
_send_chunk(socket, path);
|
||||
_send_chunk(socket, "</title><meta charset=\"UTF-8\"></head><body><h1>");
|
||||
_send_chunk(socket, path);
|
||||
_send_chunk(socket, "</h1><pre>");
|
||||
if (strlen(path) > 1) {
|
||||
_send_chunk(socket, "🗀\t<a href=\"..\">..</a><br/>");
|
||||
}
|
||||
|
||||
FILINFO file_info;
|
||||
char *fn = file_info.fname;
|
||||
res = f_readdir(&dir, &file_info);
|
||||
while (res == FR_OK && fn[0] != 0) {
|
||||
// uint64_t truncated_time = timeutils_mktime(1980 + (file_info.fdate >> 9),
|
||||
// (file_info.fdate >> 5) & 0xf,
|
||||
// file_info.fdate & 0x1f,
|
||||
// file_info.ftime >> 11,
|
||||
// (file_info.ftime >> 5) & 0x1f,
|
||||
// (file_info.ftime & 0x1f) * 2) * 1000000000ULL;
|
||||
// entry->truncated_time = truncated_time;
|
||||
// if ((file_info.fattrib & AM_DIR) != 0) {
|
||||
// entry->flags = 1; // Directory
|
||||
// entry->file_size = 0;
|
||||
// } else {
|
||||
// entry->flags = 0;
|
||||
// entry->file_size = file_info.fsize;
|
||||
// }
|
||||
// _send_chunk(socket, "<li>");
|
||||
if ((file_info.fattrib & AM_DIR) != 0) {
|
||||
_send_chunk(socket, "🗀\t");
|
||||
} else if (_endswith(file_info.fname, ".txt") ||
|
||||
_endswith(file_info.fname, ".py")) {
|
||||
_send_chunk(socket, "🖹\t");
|
||||
} else {
|
||||
_send_chunk(socket, "⬇\t");
|
||||
}
|
||||
_send_chunk(socket, "<a href=\"");
|
||||
_send_chunk(socket, request->path);
|
||||
_send_chunk(socket, file_info.fname);
|
||||
if ((file_info.fattrib & AM_DIR) != 0) {
|
||||
_send_chunk(socket, "/");
|
||||
}
|
||||
_send_chunk(socket, "\">");
|
||||
|
||||
_send_chunk(socket, file_info.fname);
|
||||
_send_chunk(socket, "</a><a>✏️</a><a>🗑️</a><br/>");
|
||||
res = f_readdir(&dir, &file_info);
|
||||
}
|
||||
_send_chunk(socket, "</pre>Upload:<input type=\"file\" multiple></body></html>");
|
||||
_send_chunk(socket, "");
|
||||
f_closedir(&dir);
|
||||
}
|
||||
|
||||
} else { // Dealing with a file.
|
||||
if (strcmp(request->method, "GET") == 0) {
|
||||
FIL active_file;
|
||||
FRESULT result = f_open(fs, &active_file, path, FA_READ);
|
||||
|
||||
if (result != FR_OK) {
|
||||
// TODO: 404
|
||||
}
|
||||
uint32_t total_length = f_size(&active_file);
|
||||
char encoded_len[10];
|
||||
snprintf(encoded_len, sizeof(encoded_len), "%d", total_length);
|
||||
|
||||
_send_str(socket, "HTTP/1.1 200 OK\r\nContent-Length: ");
|
||||
_send_str(socket, encoded_len);
|
||||
_send_str(socket, "\r\n");
|
||||
if (_endswith(path, ".txt") || _endswith(path, ".py")) {
|
||||
_send_str(socket, "Content-Type: text/plain\r\n");
|
||||
} else {
|
||||
_send_str(socket, "Content-Type: application/octet-stream\r\n");
|
||||
}
|
||||
_send_str(socket, "\r\n");
|
||||
|
||||
uint32_t total_read = 0;
|
||||
while (total_read < total_length) {
|
||||
uint8_t data_buffer[64];
|
||||
size_t quantity_read;
|
||||
f_read(&active_file, data_buffer, 64, &quantity_read);
|
||||
total_read += quantity_read;
|
||||
uint32_t send_offset = 0;
|
||||
while (send_offset < quantity_read) {
|
||||
int sent = socketpool_socket_send(socket, data_buffer + send_offset, quantity_read - send_offset);
|
||||
if (sent < 0) {
|
||||
if (sent == -EAGAIN) {
|
||||
sent = 0;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "file send error %d", sent);
|
||||
break;
|
||||
}
|
||||
}
|
||||
send_offset += sent;
|
||||
}
|
||||
}
|
||||
ESP_LOGW(TAG, "file return done");
|
||||
if (total_read < total_length) {
|
||||
socketpool_socket_close(socket);
|
||||
}
|
||||
|
||||
f_close(&active_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (memcmp(request->path, "/cp/", 4) == 0) {
|
||||
ESP_LOGW(TAG, "circuitpython %s %s", request->method, request->path + 3);
|
||||
const char *path = request->path + 3;
|
||||
if (strcmp(request->method, "GET") != 0) {
|
||||
// Return error if not a GET
|
||||
}
|
||||
if (strcmp(path, "/devices.json") == 0) {
|
||||
mdns_remoteservice_obj_t found_devices[32];
|
||||
size_t total_results = mdns_server_find(&mdns, "_circuitpython", "_tcp", 1, found_devices, MP_ARRAY_SIZE(found_devices));
|
||||
size_t count = MIN(total_results, MP_ARRAY_SIZE(found_devices));
|
||||
const char *ok_response = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Type: application/json\r\n\r\n";
|
||||
socketpool_socket_send(socket, (const uint8_t *)ok_response, strlen(ok_response));
|
||||
_send_chunk(socket, "{\"total\": ");
|
||||
char total_encoded[4];
|
||||
snprintf(total_encoded, sizeof(total_encoded), "%d", total_results);
|
||||
_send_chunk(socket, total_encoded);
|
||||
_send_chunk(socket, ", \"devices\": [");
|
||||
ESP_LOGW(TAG, "%d total devices", total_results);
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
if (i > 0) {
|
||||
_send_chunk(socket, ",");
|
||||
}
|
||||
_send_chunk(socket, "{\"hostname\": \"");
|
||||
_send_chunk(socket, common_hal_mdns_remoteservice_get_hostname(&found_devices[i]));
|
||||
_send_chunk(socket, "\", \"instance_name\": \"");
|
||||
_send_chunk(socket, common_hal_mdns_remoteservice_get_instance_name(&found_devices[i]));
|
||||
_send_chunk(socket, "\", \"port\": ");
|
||||
char port_encoded[4];
|
||||
int port = common_hal_mdns_remoteservice_get_port(&found_devices[i]);
|
||||
snprintf(port_encoded, sizeof(port_encoded), "%d", port);
|
||||
_send_chunk(socket, port_encoded);
|
||||
_send_chunk(socket, "}");
|
||||
common_hal_mdns_remoteservice_deinit(&found_devices[i]);
|
||||
}
|
||||
_send_chunk(socket, "]}");
|
||||
// Empty chunk signals the end of the response.
|
||||
_send_chunk(socket, "");
|
||||
} else if (strcmp(path, "/version.json") == 0) {
|
||||
const char *ok_response = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Type: application/json\r\n\r\n";
|
||||
socketpool_socket_send(socket, (const uint8_t *)ok_response, strlen(ok_response));
|
||||
_send_chunk(socket, "{\"web_api_version\": 1, \"version\": \"");
|
||||
_send_chunk(socket, MICROPY_GIT_TAG);
|
||||
_send_chunk(socket, "\", \"build_date\": \"");
|
||||
_send_chunk(socket, MICROPY_BUILD_DATE);
|
||||
_send_chunk(socket, "\", \"board_name\": \"");
|
||||
_send_chunk(socket, MICROPY_HW_BOARD_NAME);
|
||||
_send_chunk(socket, "\", \"mcu_name\": \"");
|
||||
_send_chunk(socket, MICROPY_HW_MCU_NAME);
|
||||
_send_chunk(socket, "\", \"board_id\": \"");
|
||||
_send_chunk(socket, CIRCUITPY_BOARD_ID);
|
||||
_send_chunk(socket, "\", \"creator_id\": ");
|
||||
char encoded_id[8];
|
||||
snprintf(encoded_id, sizeof(encoded_id), "%d", CIRCUITPY_CREATOR_ID);
|
||||
_send_chunk(socket, encoded_id);
|
||||
_send_chunk(socket, ", \"creation_id\": ");
|
||||
snprintf(encoded_id, sizeof(encoded_id), "%d", CIRCUITPY_CREATION_ID);
|
||||
_send_chunk(socket, encoded_id);
|
||||
_send_chunk(socket, "}");
|
||||
// Empty chunk signals the end of the response.
|
||||
_send_chunk(socket, "");
|
||||
}
|
||||
} else {
|
||||
const char *ok_response = "HTTP/1.1 200 OK\r\nContent-Length: 11\r\nContent-Type: text/plain\r\n\r\nHello World";
|
||||
int sent = socketpool_socket_send(socket, (const uint8_t *)ok_response, strlen(ok_response));
|
||||
ESP_LOGW(TAG, "sent ok %d", sent);
|
||||
}
|
||||
_reply(socket, request);
|
||||
request->done = false;
|
||||
}
|
||||
|
||||
|
@ -663,7 +927,11 @@ void supervisor_web_workflow_background(void) {
|
|||
uint32_t ip;
|
||||
uint32_t port;
|
||||
int newsoc = socketpool_socket_accept(&listening, (uint8_t *)&ip, &port);
|
||||
if (newsoc != -11) {
|
||||
if (newsoc == -EBADF) {
|
||||
common_hal_socketpool_socket_close(&listening);
|
||||
return;
|
||||
}
|
||||
if (newsoc != -EAGAIN) {
|
||||
ESP_LOGI(TAG, "newsoc %d", newsoc);
|
||||
}
|
||||
if (newsoc > 0) {
|
||||
|
|
|
@ -20,6 +20,30 @@ SRC_SUPERVISOR = \
|
|||
|
||||
NO_USB ?= $(wildcard supervisor/usb.c)
|
||||
|
||||
|
||||
ifeq ($(CIRCUITPY_USB),1)
|
||||
CIRCUITPY_CREATOR_ID ?= $(USB_VID)
|
||||
CIRCUITPY_CREATION_ID ?= $(USB_PID)
|
||||
endif
|
||||
|
||||
ifneq ($(CIRCUITPY_CREATOR_ID),)
|
||||
CFLAGS += -DCIRCUITPY_CREATOR_ID=$(CIRCUITPY_CREATOR_ID)
|
||||
endif
|
||||
|
||||
ifneq ($(CIRCUITPY_CREATION_ID),)
|
||||
CFLAGS += -DCIRCUITPY_CREATION_ID=$(CIRCUITPY_CREATION_ID)
|
||||
endif
|
||||
|
||||
ifeq ($(CIRCUITPY_BLEIO),1)
|
||||
SRC_SUPERVISOR += supervisor/shared/bluetooth/bluetooth.c
|
||||
ifeq ($(CIRCUITPY_BLE_FILE_SERVICE),1)
|
||||
SRC_SUPERVISOR += supervisor/shared/bluetooth/file_transfer.c
|
||||
endif
|
||||
ifeq ($(CIRCUITPY_SERIAL_BLE),1)
|
||||
SRC_SUPERVISOR += supervisor/shared/bluetooth/serial.c
|
||||
endif
|
||||
endif
|
||||
|
||||
INTERNAL_FLASH_FILESYSTEM ?= 0
|
||||
CFLAGS += -DINTERNAL_FLASH_FILESYSTEM=$(INTERNAL_FLASH_FILESYSTEM)
|
||||
|
||||
|
@ -38,20 +62,6 @@ else
|
|||
SRC_SUPERVISOR += supervisor/shared/filesystem.c
|
||||
endif
|
||||
|
||||
ifeq ($(CIRCUITPY_BLEIO),1)
|
||||
SRC_SUPERVISOR += supervisor/shared/bluetooth/bluetooth.c
|
||||
CIRCUITPY_CREATOR_ID ?= $(USB_VID)
|
||||
CIRCUITPY_CREATION_ID ?= $(USB_PID)
|
||||
CFLAGS += -DCIRCUITPY_CREATOR_ID=$(CIRCUITPY_CREATOR_ID)
|
||||
CFLAGS += -DCIRCUITPY_CREATION_ID=$(CIRCUITPY_CREATION_ID)
|
||||
ifeq ($(CIRCUITPY_BLE_FILE_SERVICE),1)
|
||||
SRC_SUPERVISOR += supervisor/shared/bluetooth/file_transfer.c
|
||||
endif
|
||||
ifeq ($(CIRCUITPY_SERIAL_BLE),1)
|
||||
SRC_SUPERVISOR += supervisor/shared/bluetooth/serial.c
|
||||
endif
|
||||
endif
|
||||
|
||||
# Choose which flash filesystem impl to use.
|
||||
# (Right now INTERNAL_FLASH_FILESYSTEM and (Q)SPI_FLASH_FILESYSTEM are mutually exclusive.
|
||||
# But that might not be true in the future.)
|
||||
|
|
Loading…
Reference in New Issue