From 1f0f807abec85a3471df09dd4d36124d564397ff Mon Sep 17 00:00:00 2001 From: ardyesp Date: Sat, 10 Mar 2018 10:06:42 -0500 Subject: [PATCH] Changed method signatures --- ESPWebDAV.cpp | 24 +++++++++++++++------ ESPWebDAV.h | 17 +++++++++------ WebDAV.ino | 20 +++++++++++------ WebSrv.cpp | 59 ++++++++++++++++++--------------------------------- 4 files changed, 61 insertions(+), 59 deletions(-) diff --git a/ESPWebDAV.cpp b/ESPWebDAV.cpp index 3beb631..3205680 100644 --- a/ESPWebDAV.cpp +++ b/ESPWebDAV.cpp @@ -14,14 +14,14 @@ const char *wdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // ------------------------ -bool ESPWebDAV::init(int chipSelectPin, int serverPort) { +bool ESPWebDAV::init(int chipSelectPin, SPISettings spiSettings, int serverPort) { // ------------------------ // start the wifi server server = new WiFiServer(serverPort); server->begin(); // initialize the SD card - return sd.begin(chipSelectPin, SPI_FULL_SPEED); + return sd.begin(chipSelectPin, spiSettings); } @@ -48,14 +48,24 @@ void ESPWebDAV::handleReject(String rejectMessage) { // ------------------------ DBG_PRINT("Rejecting request: "); DBG_PRINTLN(rejectMessage); + // handle options + if(method.equals("OPTIONS")) + return handleOptions(RESOURCE_NONE); + // handle properties - if(method.equals("PROPFIND") && depthHeader.equals("1")) { + if(method.equals("PROPFIND")) { sendHeader("Allow", "PROPFIND,OPTIONS,DELETE,COPY,MOVE"); setContentLength(CONTENT_LENGTH_UNKNOWN); send("207 Multi-Status", "application/xml;charset=utf-8", ""); - sendContent(F("/HTTP/1.1 200 OKFri, 30 Nov 1979 00:00:00 GMT\"3333333333333333333333333333333333333333\"/")); - sendContent(rejectMessage); - sendContent(F("HTTP/1.1 200 OKFri, 01 Apr 2016 16:07:40 GMT\"2222222222222222222222222222222222222222\"0application/octet-stream")); + sendContent(F("/HTTP/1.1 200 OKFri, 30 Nov 1979 00:00:00 GMT\"3333333333333333333333333333333333333333\"")); + + if(depthHeader.equals("1")) { + sendContent(F("/")); + sendContent(rejectMessage); + sendContent(F("HTTP/1.1 200 OKFri, 01 Apr 2016 16:07:40 GMT\"2222222222222222222222222222222222222222\"0application/octet-stream")); + } + + sendContent(F("")); return; } else @@ -71,7 +81,7 @@ void ESPWebDAV::handleReject(String rejectMessage) { // Test PUT a file: curl -v -T c.txt -H "Expect:" http://Rigidbot/c.txt // C:\Users\gsbal>curl -v -X LOCK http://Rigidbot/EMA_CPP_TRCC_Tutorial/Consumer.cpp -d "CARBON2\gsbal" // ------------------------ -void ESPWebDAV::handleRequest() { +void ESPWebDAV::handleRequest(String blank) { // ------------------------ ResourceType resource = RESOURCE_NONE; diff --git a/ESPWebDAV.h b/ESPWebDAV.h index 947f20a..d64835a 100644 --- a/ESPWebDAV.h +++ b/ESPWebDAV.h @@ -2,9 +2,9 @@ #include // debugging -//#define DBG_PRINT(...) { Serial.print(__VA_ARGS__); } -//#define DBG_PRINTLN(...) { Serial.println(__VA_ARGS__); } -// run +// #define DBG_PRINT(...) { Serial.print(__VA_ARGS__); } +// #define DBG_PRINTLN(...) { Serial.println(__VA_ARGS__); } +// production #define DBG_PRINT(...) { } #define DBG_PRINTLN(...) { } @@ -19,15 +19,18 @@ enum DepthType { DEPTH_NONE, DEPTH_CHILD, DEPTH_ALL }; class ESPWebDAV { public: - bool init(int chipSelectPin, int serverPort); - void handleClient(); + bool init(int chipSelectPin, SPISettings spiSettings, int serverPort); + bool isClientWaiting(); + void handleClient(String blank = ""); void rejectClient(String rejectMessage); - protected: + typedef void (ESPWebDAV::*THandlerFunction)(String); + + void processClient(THandlerFunction handler, String message); void handleNotFound(); void handleReject(String rejectMessage); - void handleRequest(); + void handleRequest(String blank); void handleOptions(ResourceType resource); void handleLock(ResourceType resource); void handleUnlock(ResourceType resource); diff --git a/WebDAV.ino b/WebDAV.ino index a6735c9..ba3cb60 100644 --- a/WebDAV.ino +++ b/WebDAV.ino @@ -1,11 +1,15 @@ -// Using the WebDAV server +/* Using the WebDAV server + From windows - + Run: \\HOSTNAME\DavWWWRoot + or Map Network Drive -> Connect to a Website +*/ #include #include "ESPWebDAV.h" #define HOSTNAME "ESPWebDAV" #define SERVER_PORT 80 -#define SD_CARD_CS_PIN 15 +#define SD_CS 15 const char *ssid = "ssid"; @@ -40,7 +44,7 @@ void setup() { Serial.print ("Mode: "); Serial.println(WiFi.getPhyMode()); // start the SD DAV server - if(!dav.init(SD_CARD_CS_PIN, SERVER_PORT)) { + if(!dav.init(SD_CS, SPI_FULL_SPEED, SERVER_PORT)) { statusMessage = "Failed to initialize SD Card"; Serial.print("ERROR: "); Serial.println(statusMessage); initFailed = true; @@ -54,11 +58,13 @@ void setup() { // ------------------------ void loop() { // ------------------------ - // call handle if server was initialized properly - if(!initFailed) + if(dav.isClientWaiting()) { + if(initFailed) + return dav.rejectClient(statusMessage); + + // call handle if server was initialized properly dav.handleClient(); - else - dav.rejectClient(statusMessage); + } } diff --git a/WebSrv.cpp b/WebSrv.cpp index 6170e2a..b253ed6 100644 --- a/WebSrv.cpp +++ b/WebSrv.cpp @@ -77,51 +77,33 @@ String ESPWebDAV::urlToUri(String url) { - - // ------------------------ -void ESPWebDAV::handleClient() { +bool ESPWebDAV::isClientWaiting() { // ------------------------ - // Check if a client has connected - client = server->available(); - if(!client) - return; - - // Wait until the client sends some data - while(!client.available()) - delay(1); - - // reset all variables - _chunked = false; - _responseHeaders = String(); - _contentLength = CONTENT_LENGTH_NOT_SET; - method = String(); - uri = String(); - contentLengthHeader = String(); - depthHeader = String(); - hostHeader = String(); - destinationHeader = String(); - - // extract uri, headers etc - if(parseRequest()) - // invoke the handler for all methods - handleRequest(); - - // finalize the response - if(_chunked) - sendContent(""); - - // send all data before closing connection - client.flush(); - // close the connection - client.stop(); + return server->hasClient(); } +// ------------------------ +void ESPWebDAV::handleClient(String blank) { +// ------------------------ + processClient(&ESPWebDAV::handleRequest, blank); +} + + + // ------------------------ void ESPWebDAV::rejectClient(String rejectMessage) { +// ------------------------ + processClient(&ESPWebDAV::handleReject, rejectMessage); +} + + + +// ------------------------ +void ESPWebDAV::processClient(THandlerFunction handler, String message) { // ------------------------ // Check if a client has connected client = server->available(); @@ -145,8 +127,8 @@ void ESPWebDAV::rejectClient(String rejectMessage) { // extract uri, headers etc if(parseRequest()) - // reject requests with a status message - handleReject(rejectMessage); + // invoke the handler + (this->*handler)(message); // finalize the response if(_chunked) @@ -161,6 +143,7 @@ void ESPWebDAV::rejectClient(String rejectMessage) { + // ------------------------ bool ESPWebDAV::parseRequest() { // ------------------------