ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
handle-filenotfound.cpp
Go to the documentation of this file.
1 /*
2  handle-filenotfound.cpp - ESP3D http handle
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "../../../include/esp3d_config.h"
21 #if defined (HTTP_FEATURE)
22 #include "../http_server.h"
23 #if defined (ARDUINO_ARCH_ESP32)
24 #include <WebServer.h>
25 #endif //ARDUINO_ARCH_ESP32
26 #if defined (ARDUINO_ARCH_ESP8266)
27 #include <ESP8266WebServer.h>
28 #endif //ARDUINO_ARCH_ESP8266
29 #include "../../filesystem/esp_filesystem.h"
30 #include "../../authentication/authentication_service.h"
31 
32 //Handle not registred path on FS neither SD ///////////////////////
33 void HTTP_Server:: handle_not_found()
34 {
36  _webserver->sendContent("HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n");
37  return;
38  }
39  String path = _webserver->urlDecode(_webserver->uri());
40  String contentType = getContentType(path.c_str());
41  String pathWithGz = path + ".gz";
42 #if defined (FILESYSTEM_FEATURE)
43  if(ESP_FileSystem::exists(pathWithGz.c_str()) || ESP_FileSystem::exists(path.c_str())) {
44  if(ESP_FileSystem::exists(pathWithGz.c_str())) {
45  path = pathWithGz;
46  }
47  if(!StreamFSFile(path.c_str(),contentType.c_str())) {
48  log_esp3d("Stream `%s` failed", path.c_str());
49  }
50  return;
51  }
52 #endif //#if defined (FILESYSTEM_FEATURE)
53 
54 #ifdef FILESYSTEM_FEATURE
55  //check local page
56  path = "/404.htm";
57  contentType = getContentType(path.c_str());
58  pathWithGz = path + ".gz";
59  if(ESP_FileSystem::exists(pathWithGz.c_str()) || ESP_FileSystem::exists(path.c_str())) {
60  if(ESP_FileSystem::exists(pathWithGz.c_str())) {
61  path = pathWithGz;
62  }
63  if(!StreamFSFile(path.c_str(),contentType.c_str())) {
64  log_esp3d("Stream `%s` failed", path.c_str());
65  }
66  return;
67  }
68 #endif //FILESYSTEM_FEATURE
69  //let's keep simple just send minimum
70  _webserver->send(404);
71 
72  /*
73 
74  #ifdef ENABLE_SD_CARD
75  if ((path.substring(0,4) == "/SD/")) {
76  //remove /SD
77  path = path.substring(3);
78  if(SD.exists((char *)pathWithGz.c_str()) || SD.exists((char *)path.c_str())) {
79  if(SD.exists((char *)pathWithGz.c_str())) {
80  path = pathWithGz;
81  }
82  File datafile = SD.open((char *)path.c_str());
83  if (datafile) {
84  if( _webserver->streamFile(datafile, contentType) == datafile.size()) {
85  datafile.close();
86  COMMANDS::wait(0);
87  return;
88  } else{
89  datafile.close();
90  }
91  }
92  }
93  String content = "cannot find ";
94  content+=path;
95  _webserver->send(404,"text/plain",content.c_str());
96  return;
97  } else
98  #endif*/
99 }
100 #endif //HTTP_FEATURE
AuthenticationService::authenticated_level
static level_authenticate_type authenticated_level(const char *pwd=nullptr)
Definition: authentication_service.cpp:61
LEVEL_GUEST
@ LEVEL_GUEST
Definition: authentication_service.h:26
log_esp3d
#define log_esp3d(format,...)
Definition: debug_esp3d.h:29
ESP_FileSystem::exists
static bool exists(const char *path)