Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Crocker <dcrocker@eschertech.com>2020-12-07 19:44:32 +0300
committerDavid Crocker <dcrocker@eschertech.com>2020-12-07 19:44:32 +0300
commitcb94535f3e41697f76746b504bec3fbb9579d6ed (patch)
tree855cf425547bc4f1beae57570c551b199f1f9751 /src/Networking
parentaee471a972962cb61cdc5c0c490b2f8d65eb00d3 (diff)
Minor improvements to new HTTP CORS code
Also moved the FirmwareUpdate class out of the Network folder
Diffstat (limited to 'src/Networking')
-rw-r--r--src/Networking/FirmwareUpdater.cpp130
-rw-r--r--src/Networking/FirmwareUpdater.h25
-rw-r--r--src/Networking/HttpResponder.cpp37
-rw-r--r--src/Networking/HttpResponder.h1
-rw-r--r--src/Networking/Network.h2
5 files changed, 17 insertions, 178 deletions
diff --git a/src/Networking/FirmwareUpdater.cpp b/src/Networking/FirmwareUpdater.cpp
deleted file mode 100644
index aa3c5f9f..00000000
--- a/src/Networking/FirmwareUpdater.cpp
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
- * FirmwareUpdater.cpp
- *
- * Created on: 21 May 2016
- * Author: David
- */
-
-#include "FirmwareUpdater.h"
-
-#include "RepRapFirmware.h"
-#include "Network.h"
-#include "Platform.h"
-#include "RepRap.h"
-#include "GCodes/GCodes.h"
-
-#if HAS_WIFI_NETWORKING
-# include "ESP8266WiFi/WifiFirmwareUploader.h"
-#endif
-#if HAS_AUX_DEVICES
-# include "Comms/PanelDueUpdater.h"
-#endif
-
-namespace FirmwareUpdater
-{
- const unsigned int WifiFirmwareModule = 1;
- // Module 2 used to be the DWC binary file but is no longer used
- const unsigned int WifiExternalFirmwareModule = 3;
-
- // Check that the prerequisites are satisfied.
- // Return true if yes, else print a message and return false.
- GCodeResult CheckFirmwareUpdatePrerequisites(uint8_t moduleMap, GCodeBuffer& gb, const StringRef& reply, const size_t serialChannel) noexcept
- {
-#if HAS_WIFI_NETWORKING
- GCodeResult result;
- if (!reprap.GetGCodes().CheckNetworkCommandAllowed(gb, reply, result))
- {
- return result;
- }
- if ((moduleMap & (1 << WifiExternalFirmwareModule)) != 0 && (moduleMap & (1 << WifiFirmwareModule)) != 0)
- {
- reply.copy("Invalid combination of firmware update modules");
- return GCodeResult::error;
- }
- if ((moduleMap & (1 << WifiFirmwareModule)) != 0
- && !reprap.GetPlatform().FileExists(DEFAULT_SYS_DIR, WIFI_FIRMWARE_FILE))
- {
- reply.printf("File %s not found", WIFI_FIRMWARE_FILE);
- return GCodeResult::error;
- }
-#endif
-#if HAS_AUX_DEVICES
- if ((moduleMap & (1 << PanelDueFirmwareModule)) != 0)
- {
- if (!reprap.GetPlatform().IsAuxEnabled(serialChannel-1) || reprap.GetPlatform().IsAuxRaw(serialChannel-1))
- {
- reply.printf("Aux port %d is not enabled or not in PanelDue mode", serialChannel-1);
- return GCodeResult::error;
- }
- if (!reprap.GetPlatform().FileExists(DEFAULT_SYS_DIR, PANEL_DUE_FIRMWARE_FILE))
- {
- reply.printf("File %s not found", PanelDueUpdater::firmwareFilename);
- return GCodeResult::error;
- }
- }
-#endif
- return GCodeResult::ok;
- }
-
- bool IsReady() noexcept
- {
-#if HAS_WIFI_NETWORKING
- WifiFirmwareUploader * const uploader = reprap.GetNetwork().GetWifiUploader();
- if(!(uploader == nullptr || uploader->IsReady())) {
- return false;
- }
-#endif
-#if HAS_AUX_DEVICES
- PanelDueUpdater *panelDueUpdater = reprap.GetPlatform().GetPanelDueUpdater();
- if (panelDueUpdater != nullptr && !panelDueUpdater->Idle()) {
- return false;
- }
-#endif
- return true;
- }
-
- void UpdateModule(unsigned int module, const size_t serialChannel) noexcept
- {
-#if HAS_WIFI_NETWORKING || HAS_AUX_DEVICES
- switch(module)
- {
-# if HAS_WIFI_NETWORKING
- case WifiExternalFirmwareModule:
-# ifdef DUET_NG
- if (reprap.GetPlatform().IsDuetWiFi())
-# endif
- {
- reprap.GetNetwork().ResetWiFiForUpload(true);
- }
- break;
-
- case WifiFirmwareModule:
-# ifdef DUET_NG
- if (reprap.GetPlatform().IsDuetWiFi())
-# endif
- {
- WifiFirmwareUploader * const uploader = reprap.GetNetwork().GetWifiUploader();
- if (uploader != nullptr)
- {
- uploader->SendUpdateFile(WIFI_FIRMWARE_FILE, DEFAULT_SYS_DIR, WifiFirmwareUploader::FirmwareAddress);
- }
- }
- break;
-# endif
-# if HAS_AUX_DEVICES
- case PanelDueFirmwareModule:
- {
- Platform& platform = reprap.GetPlatform();
- if (platform.GetPanelDueUpdater() == nullptr)
- {
- platform.InitPanelDueUpdater();
- }
- platform.GetPanelDueUpdater()->Start(serialChannel);
- }
-# endif
- }
-#endif
- }
-}
-
-// End
diff --git a/src/Networking/FirmwareUpdater.h b/src/Networking/FirmwareUpdater.h
deleted file mode 100644
index aa9f8f0a..00000000
--- a/src/Networking/FirmwareUpdater.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * FirmwareUpdater.h
- *
- * Created on: 21 May 2016
- * Author: David
- */
-
-#ifndef SRC_NETWORKING_FIRMWAREUPDATER_H_
-#define SRC_NETWORKING_FIRMWAREUPDATER_H_
-
-#include "RepRapFirmware.h"
-#include "GCodes/GCodeResult.h"
-
-namespace FirmwareUpdater
-{
-#if HAS_AUX_DEVICES
- const unsigned int PanelDueFirmwareModule = 4;
-#endif
-
- GCodeResult CheckFirmwareUpdatePrerequisites(uint8_t moduleMap, GCodeBuffer& gb, const StringRef& reply, size_t serialChannel) noexcept;
- bool IsReady() noexcept;
- void UpdateModule(unsigned int module, const size_t serialChannel) noexcept;
-}
-
-#endif /* SRC_NETWORKING_FIRMWAREUPDATER_H_ */
diff --git a/src/Networking/HttpResponder.cpp b/src/Networking/HttpResponder.cpp
index c39bcf1c..905bf6ad 100644
--- a/src/Networking/HttpResponder.cpp
+++ b/src/Networking/HttpResponder.cpp
@@ -670,10 +670,7 @@ bool HttpResponder::SendFileInfo(bool quitEarly) noexcept
"Content-Type: application/json\r\n"
);
outBuf->catf("Content-Length: %u\r\n", (jsonResponse != nullptr) ? jsonResponse->Length() : 0);
- if (reprap.GetNetwork().GetCorsSite() != nullptr)
- {
- outBuf->catf("Access-Control-Allow-Origin: %s\r\n", reprap.GetNetwork().GetCorsSite());
- }
+ AddCorsHeader();
outBuf->cat("Connection: close\r\n\r\n");
outBuf->Append(jsonResponse);
if (outBuf->HadOverflow())
@@ -862,10 +859,7 @@ void HttpResponder::SendFile(const char* nameOfFileToSend, bool isWebFile) noexc
"Pragma: no-cache\r\n"
"Expires: 0\r\n"
);
- if (reprap.GetNetwork().GetCorsSite() != nullptr)
- {
- outBuf->catf("Access-Control-Allow-Origin: %s\r\n", reprap.GetNetwork().GetCorsSite());
- }
+ AddCorsHeader();
}
const char* contentType;
@@ -953,10 +947,7 @@ void HttpResponder::SendGCodeReply() noexcept
"Content-Type: text/plain\r\n"
);
outBuf->catf("Content-Length: %u\r\n", gcodeReply.DataLength());
- if (reprap.GetNetwork().GetCorsSite() != nullptr)
- {
- outBuf->catf("Access-Control-Allow-Origin: %s\r\n", reprap.GetNetwork().GetCorsSite());
- }
+ AddCorsHeader();
outBuf->cat("Connection: close\r\n\r\n");
outStack.Append(gcodeReply);
@@ -1061,10 +1052,7 @@ void HttpResponder::SendJsonResponse(const char* command) noexcept
);
const unsigned int replyLength = (jsonResponse != nullptr) ? jsonResponse->Length() : 0;
outBuf->catf("Content-Length: %u\r\n", replyLength);
- if (reprap.GetNetwork().GetCorsSite() != nullptr)
- {
- outBuf->catf("Access-Control-Allow-Origin: %s\r\n", reprap.GetNetwork().GetCorsSite());
- }
+ AddCorsHeader();
outBuf->catf("Connection: %s\r\n\r\n", keepOpen ? "keep-alive" : "close");
outBuf->Append(jsonResponse);
@@ -1153,8 +1141,8 @@ void HttpResponder::ProcessRequest() noexcept
);
if (reprap.GetNetwork().GetCorsSite() != nullptr)
{
- outBuf->catf("Access-Control-Allow-Headers: Content-Type\r\n"
- "Access-Control-Allow-Origin: %s\r\n", reprap.GetNetwork().GetCorsSite());
+ outBuf->catf("Access-Control-Allow-Headers: Content-Type\r\n");
+ AddCorsHeader();
}
if (outBuf->HadOverflow())
{
@@ -1282,10 +1270,7 @@ void HttpResponder::RejectMessage(const char* response, unsigned int code) noexc
{
outBuf->printf("HTTP/1.1 %u %s\r\n"
"Connection: close\r\n", code, response);
- if (reprap.GetNetwork().GetCorsSite() != nullptr)
- {
- outBuf->catf("Access-Control-Allow-Origin: %s\r\n", reprap.GetNetwork().GetCorsSite());
- }
+ AddCorsHeader();
outBuf->catf("\r\n%s%s%s", ErrorPagePart1, response, ErrorPagePart2);
Commit();
}
@@ -1519,6 +1504,14 @@ void HttpResponder::Diagnostics(MessageType mt) const noexcept
GetPlatform().MessageF(mtype, "HTTP sessions: %u of %u\n", numSessions, MaxHttpSessions);
}
+void HttpResponder::AddCorsHeader() noexcept
+{
+ if (reprap.GetNetwork().GetCorsSite() != nullptr)
+ {
+ outBuf->catf("Access-Control-Allow-Origin: %s\r\n", reprap.GetNetwork().GetCorsSite());
+ }
+}
+
// Static data
HttpResponder::HttpSession HttpResponder::sessions[MaxHttpSessions];
diff --git a/src/Networking/HttpResponder.h b/src/Networking/HttpResponder.h
index eb975f04..e820d833 100644
--- a/src/Networking/HttpResponder.h
+++ b/src/Networking/HttpResponder.h
@@ -89,6 +89,7 @@ private:
void ProcessRequest() noexcept;
void RejectMessage(const char* s, unsigned int code = 500) noexcept;
bool SendFileInfo(bool quitEarly) noexcept;
+ void AddCorsHeader() noexcept;
#if HAS_MASS_STORAGE
void DoUpload() noexcept;
diff --git a/src/Networking/Network.h b/src/Networking/Network.h
index 88790bb4..47b455d7 100644
--- a/src/Networking/Network.h
+++ b/src/Networking/Network.h
@@ -143,7 +143,7 @@ private:
uint32_t fastLoop, slowLoop;
#if SUPPORT_HTTP
- String<16> corsSite;
+ String<StringLength20> corsSite;
#endif
char hostname[16]; // Limit DHCP hostname to 15 characters + terminating 0
};