Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 778093a

Browse files
igrrsandeepmistry
authored andcommitted
Web server refactoring
1 parent 829669f commit 778093a

File tree

4 files changed

+41
-42
lines changed

4 files changed

+41
-42
lines changed

esp8266com/esp8266/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
1+
/*
22
SDWebServer - Example WebServer with SD Card backend for esp8266
33
44
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
55
This file is part of the ESP8266WebServer library for Arduino environment.
6-
6+
77
This library is free software; you can redistribute it and/or
88
modify it under the terms of the GNU Lesser General Public
99
License as published by the Free Software Foundation; either
@@ -23,7 +23,7 @@
2323
File extensions with more than 3 charecters are not supported by the SD Library
2424
File Names longer than 8 charecters will be truncated by the SD library, so keep filenames shorter
2525
index.htm is the default index (works on subfolders as well)
26-
26+
2727
upload the contents of SdRoot to the root of the SDcard and access the editor by going to http://esp8266sd.local/edit
2828
2929
*/
@@ -62,7 +62,7 @@ void returnFail(String msg) {
6262
bool loadFromSdCard(String path){
6363
String dataType = "text/plain";
6464
if(path.endsWith("/")) path += "index.htm";
65-
65+
6666
if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
6767
else if(path.endsWith(".htm")) dataType = "text/html";
6868
else if(path.endsWith(".css")) dataType = "text/css";
@@ -74,7 +74,7 @@ bool loadFromSdCard(String path){
7474
else if(path.endsWith(".xml")) dataType = "text/xml";
7575
else if(path.endsWith(".pdf")) dataType = "application/pdf";
7676
else if(path.endsWith(".zip")) dataType = "application/zip";
77-
77+
7878
File dataFile = SD.open(path.c_str());
7979
if(dataFile.isDirectory()){
8080
path += "/index.htm";
@@ -84,9 +84,9 @@ bool loadFromSdCard(String path){
8484

8585
if (!dataFile)
8686
return false;
87-
87+
8888
if(server.hasArg("download")) dataType = "application/octet-stream";
89-
89+
9090
server.sendHeader("Content-Length", String(dataFile.size()));
9191
server.sendHeader("Connection", "close");
9292
server.sendHeader("Access-Control-Allow-Origin", "*");
@@ -159,7 +159,7 @@ void handleDelete(){
159159
void handleCreate(){
160160
if(server.args() == 0) return returnFail("BAD ARGS");
161161
String path = server.arg(0);
162-
if(path == "/" || SD.exists((char *)path.c_str())) {
162+
if(path == "/" || SD.exists((char *)path.c_str())) {
163163
returnFail("BAD PATH");
164164
return;
165165
}
@@ -187,7 +187,7 @@ void printDirectory() {
187187
return returnFail("NOT DIR");
188188
}
189189
dir.rewindDirectory();
190-
190+
191191
server.send(200, "text/json", "");
192192
WiFiClient client = server.client();
193193

@@ -197,9 +197,9 @@ void printDirectory() {
197197
break;
198198

199199
String output;
200-
if (cnt == 0)
200+
if (cnt == 0)
201201
output = '[';
202-
else
202+
else
203203
output = ',';
204204

205205
output += "{\"type\":\"";
@@ -252,25 +252,25 @@ void setup(void){
252252
}
253253
DBG_OUTPUT_PORT.print("Connected! IP address: ");
254254
DBG_OUTPUT_PORT.println(WiFi.localIP());
255-
255+
256256
if (mdns.begin(hostname, WiFi.localIP())) {
257257
DBG_OUTPUT_PORT.println("MDNS responder started");
258258
DBG_OUTPUT_PORT.print("You can now connect to http://");
259259
DBG_OUTPUT_PORT.print(hostname);
260260
DBG_OUTPUT_PORT.println(".local");
261261
}
262-
263-
262+
263+
264264
server.on("/list", HTTP_GET, printDirectory);
265265
server.on("/edit", HTTP_DELETE, handleDelete);
266266
server.on("/edit", HTTP_PUT, handleCreate);
267267
server.on("/edit", HTTP_POST, [](){ returnOK(); });
268268
server.onNotFound(handleNotFound);
269269
server.onFileUpload(handleFileUpload);
270-
270+
271271
server.begin();
272272
DBG_OUTPUT_PORT.println("HTTP server started");
273-
273+
274274
if (SD.begin(SS)){
275275
DBG_OUTPUT_PORT.println("SD Card initialized.");
276276
hasSD = true;
@@ -279,4 +279,4 @@ void setup(void){
279279

280280
void loop(void){
281281
server.handleClient();
282-
}
282+
}

esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
1+
/*
22
ESP8266WebServer.cpp - Dead simple web-server.
33
Supports only one simultaneous client, knows how to handle GET and POST.
44
55
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
6-
6+
77
This library is free software; you can redistribute it and/or
88
modify it under the terms of the GNU Lesser General Public
99
License as published by the Free Software Foundation; either
@@ -137,7 +137,7 @@ void ESP8266WebServer::send(int code, const char* content_type, String content)
137137
if (!content_type)
138138
content_type = "text/html";
139139
sendHeader("Content-Type", content_type, true);
140-
140+
141141
response += _responseHeaders;
142142
response += "\r\n";
143143
response += content;
@@ -238,4 +238,4 @@ const char* ESP8266WebServer::_responseCodeToString(int code) {
238238
case 500: return "Fail";
239239
default: return "";
240240
}
241-
}
241+
}

esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
1+
/*
22
ESP8266WebServer.h - Dead simple web-server.
33
Supports only one simultaneous client, knows how to handle GET and POST.
44
55
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
6-
6+
77
This library is free software; you can redistribute it and/or
88
modify it under the terms of the GNU Lesser General Public
99
License as published by the Free Software Foundation; either
@@ -61,13 +61,13 @@ class ESP8266WebServer
6161
HTTPMethod method() { return _currentMethod; }
6262
WiFiClient client() { return _currentClient; }
6363
HTTPUpload& upload() { return _currentUpload; }
64-
64+
6565
String arg(const char* name); // get request argument value by name
6666
String arg(int i); // get request argument value by number
6767
String argName(int i); // get request argument name by number
6868
int args(); // get arguments count
6969
bool hasArg(const char* name); // check if argument exists
70-
70+
7171
// send response to the client
7272
// code - HTTP response code, can be 200 or 404
7373
// content_type - HTTP content type, like "text/plain" or "image/png"
@@ -83,7 +83,7 @@ class ESP8266WebServer
8383
static const char* _responseCodeToString(int code);
8484
void _parseForm(WiFiClient& client, String boundary, uint32_t len);
8585
void _uploadWriteByte(uint8_t b);
86-
86+
8787
struct RequestHandler;
8888
struct RequestArgument {
8989
String key;
@@ -110,4 +110,4 @@ class ESP8266WebServer
110110
};
111111

112112

113-
#endif //ESP8266WEBSERVER_H
113+
#endif //ESP8266WEBSERVER_H

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/Parsing.cpp renamed to esp8266com/esp8266/libraries/ESP8266WebServer/src/Parsing.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*
1+
/*
22
Parsing.cpp - HTTP request parsing.
33
44
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
5-
5+
66
This library is free software; you can redistribute it and/or
77
modify it under the terms of the GNU Lesser General Public
88
License as published by the Free Software Foundation; either
@@ -43,7 +43,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
4343
#endif
4444
return false;
4545
}
46-
46+
4747
String methodStr = req.substring(0, addr_start);
4848
String url = req.substring(addr_start + 1, addr_end);
4949
String searchStr = "";
@@ -53,7 +53,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
5353
url = url.substring(0, hasSearch);
5454
}
5555
_currentUri = url;
56-
56+
5757
HTTPMethod method = HTTP_GET;
5858
if (methodStr == "POST") {
5959
method = HTTP_POST;
@@ -65,7 +65,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
6565
method = HTTP_PATCH;
6666
}
6767
_currentMethod = method;
68-
68+
6969
#ifdef DEBUG
7070
DEBUG_OUTPUT.print("method: ");
7171
DEBUG_OUTPUT.print(methodStr);
@@ -105,7 +105,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
105105
contentLength = headerValue.toInt();
106106
}
107107
}
108-
108+
109109
if (!isForm){
110110
if (searchStr != "") searchStr += '&';
111111
searchStr += client.readStringUntil('\r');
@@ -215,7 +215,7 @@ void ESP8266WebServer::_uploadWriteByte(uint8_t b){
215215
}
216216

217217
void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
218-
218+
219219
#ifdef DEBUG
220220
DEBUG_OUTPUT.print("Parse Form: Boundary: ");
221221
DEBUG_OUTPUT.print(boundary);
@@ -235,7 +235,7 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
235235
String argType;
236236
String argFilename;
237237
bool argIsFile = false;
238-
238+
239239
line = client.readStringUntil('\r');
240240
client.readStringUntil('\n');
241241
if (line.startsWith("Content-Disposition")){
@@ -286,11 +286,11 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
286286
DEBUG_OUTPUT.println(argValue);
287287
DEBUG_OUTPUT.println();
288288
#endif
289-
289+
290290
RequestArgument& arg = postArgs[postArgsLen++];
291291
arg.key = argName;
292292
arg.value = argValue;
293-
293+
294294
if (line == ("--"+boundary+"--")){
295295
#ifdef DEBUG
296296
DEBUG_OUTPUT.println("Done Parsing POST");
@@ -318,7 +318,7 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
318318
_uploadWriteByte(argByte);
319319
argByte = client.read();
320320
}
321-
321+
322322
argByte = client.read();
323323
if (argByte == 0x0A){
324324
argByte = client.read();
@@ -337,10 +337,10 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
337337
goto readfile;
338338
}
339339
}
340-
340+
341341
uint8_t endBuf[boundary.length()];
342342
client.readBytes(endBuf, boundary.length());
343-
343+
344344
if (strstr((const char*)endBuf, boundary.c_str()) != NULL){
345345
if (_fileUploadHandler) _fileUploadHandler();
346346
_currentUpload.totalSize += _currentUpload.currentSize;
@@ -382,7 +382,7 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
382382
}
383383
}
384384
}
385-
385+
386386
int iarg;
387387
int totalArgs = ((32 - postArgsLen) < _currentArgCount)?(32 - postArgsLen):_currentArgCount;
388388
for (iarg = 0; iarg < totalArgs; iarg++){
@@ -402,4 +402,3 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
402402
}
403403
}
404404

405-

0 commit comments

Comments
 (0)