22 #include "../../include/esp3d_config.h"
24 #if defined (HTTP_FEATURE)
25 #if defined (ARDUINO_ARCH_ESP32)
26 #include <WebServer.h>
27 #endif //ARDUINO_ARCH_ESP32
28 #if defined (ARDUINO_ARCH_ESP8266)
29 #include <ESP8266WebServer.h>
30 #endif //ARDUINO_ARCH_ESP8266
32 #include "../authentication/authentication_service.h"
33 #include "../../core/settings_esp3d.h"
34 #include "../filesystem/esp_filesystem.h"
35 #include "../websocket/websocket_server.h"
37 bool HTTP_Server::_started =
false;
38 uint16_t HTTP_Server::_port = 0;
39 WEBSERVER * HTTP_Server::_webserver =
nullptr;
50 void HTTP_Server::init_handlers()
52 _webserver->on(
"/",HTTP_ANY, handle_root);
54 _webserver->onNotFound (handle_not_found);
56 _webserver->on (
"/command", HTTP_ANY, handle_web_command);
58 _webserver->on (
"/config", HTTP_ANY, handle_config);
60 _webserver->on(
"/login", HTTP_ANY, handle_login);
61 #ifdef FILESYSTEM_FEATURE
63 _webserver->on (
"/files", HTTP_ANY, handleFSFileList, FSFileupload);
64 #endif //FILESYSTEM_FEATURE
67 _webserver->on (
"/sdfiles", HTTP_ANY, handleSDFileList, SDFileupload);
69 #ifdef WEB_UPDATE_FEATURE
71 _webserver->on (
"/updatefw", HTTP_ANY, handleUpdate, WebUpdateUpload);
72 #endif //WEB_UPDATE_FEATURE
74 if(WiFi.getMode() != WIFI_AP) {
75 _webserver->on(
"/description.xml", HTTP_GET, handle_SSDP);
78 #ifdef CAPTIVE_PORTAL_FEATURE
79 if(WiFi.getMode() == WIFI_AP) {
80 _webserver->on (
"/generate_204", HTTP_ANY, handle_root);
81 _webserver->on (
"/gconnectivitycheck.gstatic.com", HTTP_ANY, handle_root);
83 _webserver->on (
"/fwlink/", HTTP_ANY, handle_root);
88 bool HTTP_Server::StreamFSFile(
const char* filename,
const char * contentType)
94 size_t totalFileSize = datafile.
size();
97 _webserver->setContentLength(totalFileSize);
98 _webserver->send(200, contentType,
"");
102 int v = datafile.
read(buf,1024);
103 if ((v == -1) || (v == 0)) {
106 _webserver->client().write(buf,v);
109 if (i >= totalFileSize) {
114 if ( i != totalFileSize) {
120 void HTTP_Server::pushError(
int code,
const char * st,
bool web_error, uint16_t timeout)
123 String s =
"ERROR:" + String(code) +
":";
126 if (web_error != 0) {
128 if (_webserver->client().available() > 0) {
129 _webserver->send (web_error,
"text/xml", st);
133 uint32_t t = millis();
134 while (millis() - t < timeout) {
141 void HTTP_Server::cancelUpload()
143 HTTPUpload& upload = _webserver->upload();
144 upload.status = UPLOAD_FILE_ABORTED;
145 #if defined ( ARDUINO_ARCH_ESP8266)
146 _webserver->client().stopAll();
148 errno = ECONNABORTED;
149 _webserver->client().stop();
157 bool no_error =
true;
163 _webserver=
new WEBSERVER(_port);
169 #ifdef AUTHENTICATION_FEATURE
172 const char * headerkeys[] = {
"Cookie"} ;
173 size_t headerkeyssize =
sizeof (headerkeys) /
sizeof (
char*);
175 _webserver->collectHeaders (headerkeys, headerkeyssize );
178 #ifdef AUTHENTICATION_FEATURE
179 AuthenticationService::begin(_webserver);
180 #endif //AUTHENTICATION_FEATURE
190 #ifdef AUTHENTICATION_FEATURE
191 AuthenticationService::end();
192 #endif //AUTHENTICATION_FEATURE
204 _webserver->handleClient();
209 const char * HTTP_Server::get_Splited_Value(String data,
char separator,
int index)
212 int strIndex[] = {0, -1};
213 int maxIndex = data.length()-1;
215 for(
int i=0; i<=maxIndex && found<=index; i++) {
216 if(data.charAt(i)==separator || i==maxIndex) {
218 strIndex[0] = strIndex[1]+1;
219 strIndex[1] = (i == maxIndex) ? i+1 : i;
223 return found>index ? data.substring(strIndex[0], strIndex[1]).c_str() :
"";
228 const char* HTTP_Server::getContentType (
const char* filename)
230 String file_name = filename;
231 file_name.toLowerCase();
232 if (file_name.endsWith (
".htm") ) {
234 }
else if (file_name.endsWith (
".html") ) {
236 }
else if (file_name.endsWith (
".css") ) {
238 }
else if (file_name.endsWith (
".js") ) {
239 return "application/javascript";
240 }
else if (file_name.endsWith (
".png") ) {
242 }
else if (file_name.endsWith (
".gif") ) {
244 }
else if (file_name.endsWith (
".jpeg") ) {
246 }
else if (file_name.endsWith (
".jpg") ) {
248 }
else if (file_name.endsWith (
".ico") ) {
249 return "image/x-icon";
250 }
else if (file_name.endsWith (
".xml") ) {
252 }
else if (file_name.endsWith (
".pdf") ) {
253 return "application/x-pdf";
254 }
else if (file_name.endsWith (
".zip") ) {
255 return "application/x-zip";
256 }
else if (file_name.endsWith (
".gz") ) {
257 return "application/x-gzip";
258 }
else if (file_name.endsWith (
".txt") ) {
261 return "application/octet-stream";
264 #endif // Enable HTTP