Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions esp3d/src/modules/http/handlers/handle-command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "../../../core/esp3d_settings.h"
#include "../../../core/esp3d_string.h"
#include "../../authentication/authentication_service.h"
#if defined(SD_DEVICE) && defined(GCODE_HOST_FEATURE)
#include "../../filesystem/esp_sd.h"
#include "../../gcode_host/gcode_host.h"
#endif


// Handle web command query and send answer//////////////////////////////
Expand All @@ -44,8 +48,62 @@ void HTTP_Server::handle_web_command() {
// esp3d_log("Authentication = %d", auth_level);
String cmd = "";
bool isRealTimeCommand = false;
if (_webserver->hasArg("cmd")) {
cmd = _webserver->arg("cmd");
#if defined(SD_DEVICE) && defined(GCODE_HOST_FEATURE)
if (_webserver->hasArg("plain")) {
// Orca's ESP3D host uses "plain" for its test and post-upload commands.
// Its M23/M24 refer to the file just uploaded to ESP3D's own SD card,
// not to a file on the printer firmware's SD card.
cmd = _webserver->arg("plain");
cmd.trim();
if (cmd.startsWith("M23 ")) {
String name = cmd.substring(4);
name.trim();
if (name.length() == 0 || name != _orca_uploaded_name) {
_webserver->send(409, "text/plain", "No matching SD upload");
return;
}
_orca_selected_name = name;
_webserver->send(200, "text/plain", "File selected");
return;
}
if (cmd == "M24") {
if (_orca_selected_name.length() == 0) {
_webserver->send(409, "text/plain", "No selected SD upload");
return;
}
if (esp3d_gcode_host.getStatus() != HOST_NO_STREAM) {
_webserver->send(409, "text/plain", "A stream is already active");
return;
}
String path = "/" + _orca_selected_name;
if (!ESP_SD::accessFS()) {
_webserver->send(503, "text/plain", "SD card unavailable");
return;
}
bool exists = ESP_SD::exists(path.c_str());
ESP_SD::releaseFS();
if (!exists) {
_webserver->send(404, "text/plain", "Uploaded file not found");
return;
}
String stream_path = "/SD" + path;
if (!esp3d_gcode_host.processFile(stream_path.c_str(), auth_level)) {
_webserver->send(500, "text/plain", "Cannot start SD stream");
return;
}
_orca_uploaded_name = "";
_orca_selected_name = "";
_webserver->send(200, "text/plain", "ESP3D SD stream started");
return;
}
if (cmd != "M105") {
_webserver->send(400, "text/plain", "Unsupported plain command");
return;
}
}
#endif // SD_DEVICE && GCODE_HOST_FEATURE
if (_webserver->hasArg("cmd") || cmd.length() > 0) {
if (cmd.length() == 0) cmd = _webserver->arg("cmd");
esp3d_log("Command is %s", cmd.c_str());
if (!cmd.endsWith("\n")) {
esp3d_log("Command is not ending with \\n");
Expand Down
45 changes: 45 additions & 0 deletions esp3d/src/modules/http/handlers/upload-SD-files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void HTTP_Server::SDFileupload() {
ESP_SD::remove(filename.c_str());
}
String path = _webserver->arg("path");
if (path.length() == 0) path = "/";
if (path[0] != '/') {
path = "/" + path;
}
Expand Down Expand Up @@ -245,4 +246,48 @@ void HTTP_Server::SDFileupload() {
}
ESP3DHal::wait(5);
}
#ifdef GCODE_HOST_FEATURE
// Orca sends a single multipart field named "file" with a short filename.
// Reject paths: this endpoint always writes to the root of ESP3D's SD card.
static bool isOrcaFilenameValid(const String& name) {
if (name.length() == 0 || name.length() > 64 || name[0] == '.') return false;
for (size_t i = 0; i < name.length(); ++i) {
char c = name[i];
if (c == '/' || c == '\\' || static_cast<uint8_t>(c) < 0x20) return false;
}
return true;
}

void HTTP_Server::OrcaSDFileupload() {
HTTPUpload& upload = _webserver->upload();
if (upload.status == UPLOAD_FILE_START) {
_orca_uploaded_name = "";
_orca_selected_name = "";
_upload_status = UPLOAD_STATUS_NONE;
}
if (!isOrcaFilenameValid(upload.filename)) {
_upload_status = UPLOAD_STATUS_FAILED;
return;
}
SDFileupload();
if (upload.status == UPLOAD_FILE_END &&
_upload_status == UPLOAD_STATUS_SUCCESSFUL) {
_orca_uploaded_name = upload.filename;
}
}

void HTTP_Server::handleOrcaUpload() {
if (AuthenticationService::getAuthenticatedLevel() ==
ESP3DAuthenticationLevel::guest) {
_webserver->send(401, "text/plain", "Wrong authentication!");
} else if (_upload_status == UPLOAD_STATUS_SUCCESSFUL &&
_orca_uploaded_name.length() > 0) {
_webserver->send(200, "text/plain", "ok");
} else {
_orca_uploaded_name = "";
_webserver->send(500, "text/plain", "SD upload failed");
}
_upload_status = UPLOAD_STATUS_NONE;
}
#endif // GCODE_HOST_FEATURE
#endif // HTTP_FEATURE && SD_DEVICE
7 changes: 7 additions & 0 deletions esp3d/src/modules/http/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ bool HTTP_Server::_started = false;
uint16_t HTTP_Server::_port = 0;
WEBSERVER* HTTP_Server::_webserver = nullptr;
uint8_t HTTP_Server::_upload_status = UPLOAD_STATUS_NONE;
#if defined(SD_DEVICE) && defined(GCODE_HOST_FEATURE)
String HTTP_Server::_orca_uploaded_name;
String HTTP_Server::_orca_selected_name;
#endif

void HTTP_Server::init_handlers() {
_webserver->on("/", HTTP_ANY, handle_root);
Expand All @@ -74,6 +78,9 @@ void HTTP_Server::init_handlers() {
#ifdef SD_DEVICE
// SD
_webserver->on("/sdfiles", HTTP_ANY, handleSDFileList, SDFileupload);
#ifdef GCODE_HOST_FEATURE
_webserver->on("/upload_serial", HTTP_POST, handleOrcaUpload, OrcaSDFileupload);
#endif
#endif // SD_DEVICE
#ifdef WEB_UPDATE_FEATURE
// web update
Expand Down
8 changes: 8 additions & 0 deletions esp3d/src/modules/http/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class HTTP_Server {
static WEBSERVER* _webserver;
static uint16_t _port;
static uint8_t _upload_status;
#if defined(SD_DEVICE) && defined(GCODE_HOST_FEATURE)
static String _orca_uploaded_name;
static String _orca_selected_name;
#endif
static const char* get_Splited_Value(String data, char separator, int index);
#ifdef SSDP_FEATURE
static void handle_SSDP();
Expand Down Expand Up @@ -89,6 +93,10 @@ class HTTP_Server {
#ifdef SD_DEVICE
static void SDFileupload();
static void handleSDFileList();
#ifdef GCODE_HOST_FEATURE
static void OrcaSDFileupload();
static void handleOrcaUpload();
#endif
static bool StreamSDFile(const char* filename, const char* contentType);
#endif // SD_DEVICE
#if COMMUNICATION_PROTOCOL == MKS_SERIAL
Expand Down