Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,212 changes: 643 additions & 569 deletions ESPWebDAV.cpp

Large diffs are not rendered by default.

170 changes: 86 additions & 84 deletions ESPWebDAV.h
Original file line number Diff line number Diff line change
@@ -1,84 +1,86 @@
#include <ESP8266WiFi.h>
#include <SdFat.h>

#define DEBUG

#ifdef DEBUG
#define DBG_PRINT(...) { Serial.print(__VA_ARGS__); }
#define DBG_PRINTLN(...) { Serial.println(__VA_ARGS__); }
#else
#define DBG_PRINT(...) {}
#define DBG_PRINTLN(...) {}
#endif

// constants for WebServer
#define CONTENT_LENGTH_UNKNOWN ((size_t) -1)
#define CONTENT_LENGTH_NOT_SET ((size_t) -2)
#define HTTP_MAX_POST_WAIT 5000

enum ResourceType { RESOURCE_NONE, RESOURCE_FILE, RESOURCE_DIR };
enum DepthType { DEPTH_NONE, DEPTH_CHILD, DEPTH_ALL };


class ESPWebDAV {
public:
bool init(int chipSelectPin, SPISettings spiSettings, int serverPort);
bool initSD(int chipSelectPin, SPISettings spiSettings);
bool startServer();
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(String blank);
void handleOptions(ResourceType resource);
void handleLock(ResourceType resource);
void handleUnlock(ResourceType resource);
void handlePropPatch(ResourceType resource);
void handleProp(ResourceType resource);
void sendPropResponse(boolean recursing, FatFile *curFile);
void handleGet(ResourceType resource, bool isGet);
void handlePut(ResourceType resource);
void handleWriteError(String message, FatFile *wFile);
void handleDirectoryCreate(ResourceType resource);
void handleMove(ResourceType resource);
void handleDelete(ResourceType resource);

// Sections are copied from ESP8266Webserver
String getMimeType(String path);
String urlDecode(const String& text);
String urlToUri(String url);
bool parseRequest();
void sendHeader(const String& name, const String& value, bool first = false);
void send(String code, const char* content_type, const String& content);
void _prepareHeader(String& response, String code, const char* content_type, size_t contentLength);
void sendContent(const String& content);
void sendContent_P(PGM_P content);
void setContentLength(size_t len);
size_t readBytesWithTimeout(uint8_t *buf, size_t bufSize);
size_t readBytesWithTimeout(uint8_t *buf, size_t bufSize, size_t numToRead);


// variables pertaining to current most HTTP request being serviced
WiFiServer *server;
SdFat sd;

WiFiClient client;
String method;
String uri;
String contentLengthHeader;
String depthHeader;
String hostHeader;
String destinationHeader;

String _responseHeaders;
bool _chunked;
int _contentLength;
};

extern ESPWebDAV dav;
#include <ESP8266WiFi.h>
#include <SdFat.h>

#define DEBUG

#ifdef DEBUG
#define DBG_PRINT(...) { Serial.print(__VA_ARGS__); }
#define DBG_PRINTLN(...) { Serial.println(__VA_ARGS__); }
#else
#define DBG_PRINT(...) {}
#define DBG_PRINTLN(...) {}
#endif

// constants for WebServer
#define CONTENT_LENGTH_UNKNOWN ((size_t) -1)
#define CONTENT_LENGTH_NOT_SET ((size_t) -2)
#define CONTENT_RANGE_NOT_SET ((size_t) -1)
#define HTTP_MAX_POST_WAIT 5000

enum ResourceType { RESOURCE_NONE, RESOURCE_FILE, RESOURCE_DIR };
enum DepthType { DEPTH_NONE, DEPTH_CHILD, DEPTH_ALL };


class ESPWebDAV {
public:
bool init(int chipSelectPin, SPISettings spiSettings, int serverPort);
bool initSD(int chipSelectPin, SPISettings spiSettings);
bool startServer();
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(String blank);
void handleOptions(ResourceType resource);
void handleLock(ResourceType resource);
void handleUnlock(ResourceType resource);
void handlePropPatch(ResourceType resource);
void handleProp(ResourceType resource);
void sendPropResponse(boolean recursing, FatFile *curFile);
void handleGet(ResourceType resource, bool isGet);
void handlePut(ResourceType resource);
void handleWriteError(String message, FatFile *wFile);
void handleDirectoryCreate(ResourceType resource);
void handleMove(ResourceType resource);
void handleDelete(ResourceType resource);

// Sections are copied from ESP8266Webserver
String getMimeType(String path);
String urlDecode(const String& text);
String urlToUri(String url);
bool parseRequest();
void sendHeader(const String& name, const String& value, bool first = false);
void send(String code, const char* content_type, const String& content);
void _prepareHeader(String& response, String code, const char* content_type, size_t contentLength);
void sendContent(const String& content);
void sendContent_P(PGM_P content);
void setContentLength(size_t len);
size_t readBytesWithTimeout(uint8_t *buf, size_t bufSize);
size_t readBytesWithTimeout(uint8_t *buf, size_t bufSize, size_t numToRead);


// variables pertaining to current most HTTP request being serviced
WiFiServer *server;
SdFat sd;

WiFiClient client;
String method;
String uri;
String contentLengthHeader;
String contentRangeHeader;
String depthHeader;
String hostHeader;
String destinationHeader;

String _responseHeaders;
bool _chunked;
int _contentLength, _contentRangeStart, _contentRangeEnd;
};

extern ESPWebDAV dav;
18 changes: 10 additions & 8 deletions ESPWebDAV.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ void setup() {
if(config.load() == 1) { // Connected before
if(!network.start()) {
SERIAL_ECHOLN("Connect fail, please check your INI file or set the wifi config and connect again");
SERIAL_ECHOLN("- M50: Set the wifi ssid , 'M50 ssid-name'");
SERIAL_ECHOLN("- M51: Set the wifi password , 'M51 password'");
SERIAL_ECHOLN("- M52: Start to connect the wifi");
SERIAL_ECHOLN("- M53: Check the connection status");
SERIAL_ECHOLN("- M50: Set WiFi SSID");
SERIAL_ECHOLN("- M51: Set WiFi Password");
SERIAL_ECHOLN("- M52: Connect");
SERIAL_ECHOLN("- M53: Connection Status");
SERIAL_ECHOLN("- M54: Set Hostname");
}
}
else {
SERIAL_ECHOLN("Welcome to FYSETC: www.fysetc.com");
SERIAL_ECHOLN("Please set the wifi config first");
SERIAL_ECHOLN("- M50: Set the wifi ssid , 'M50 ssid-name'");
SERIAL_ECHOLN("- M51: Set the wifi password , 'M51 password'");
SERIAL_ECHOLN("- M52: Start to connect the wifi");
SERIAL_ECHOLN("- M53: Check the connection status");
SERIAL_ECHOLN("- M50: Set WiFi SSID");
SERIAL_ECHOLN("- M51: Set WiFi Password");
SERIAL_ECHOLN("- M52: Connect");
SERIAL_ECHOLN("- M53: Connection Status");
SERIAL_ECHOLN("- M54: Set Hostname");
}
}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ GCode can be directly uploaded from the slicer (Cura) to this remote drive, ther

## Dependencies:
1. [ESP8266 Arduino Core version 2.4](https://github.com/esp8266/Arduino)
2. [SdFat library](https://github.com/greiman/SdFat)
2. [SdFat library version 1.0.16](https://github.com/greiman/SdFat)


## Use:
Expand Down
Loading