8000 Sync 2017-09-18 · Tomato1107/esp32-snippets@b91f16b · GitHub
[go: up one dir, main page]

Skip to content

Commit b91f16b

Browse files
committed
Sync 2017-09-18
1 parent 01bfcfe commit b91f16b

File tree

10 files changed

+111
-55
lines changed

10 files changed

+111
-55
lines changed

cpp_utils/File.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,5 @@ bool File::isDirectory() {
126126
if (rc != 0) {
127127
return false;
128128
}
129-
if (S_ISDIR(buf.st_mode)) {
130-
return true;
131-
}
132-
return false;
129+
return S_ISDIR(buf.st_mode);
133130
} // isDirectory

cpp_utils/FileSystem.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <dirent.h>
1212
#include <errno.h>
13+
#include <string.h>
1314
#include <sys/stat.h>
1415
#include <unistd.h>
1516

@@ -74,15 +75,31 @@ std::vector<File> FileSystem::getDirectoryContents(std::string path) {
7475
} // getDirectoryContents
7576

7677

78+
/**
79+
* @brief Does the path refer to a directory?
80+
* @param [in] path The path to the directory.
81+
*/
82+
bool FileSystem::isDirectory(std::string path) {
83+
struct stat statBuf;
84+
int rc = stat(path.c_str(), &statBuf);
85+
if (rc != 0) {
86+
return false;
87+
}
88+
return S_ISDIR(statBuf.st_mode);
89+
} // isDirectory
90+
91+
92+
7793
/**
7894
* @brief Create a directory
7995
* @param [in] path The directory to create.
8096
* @return N/A.
8197
*/
8298
int FileSystem::mkdir(std::string path) {
99+
ESP_LOGD(LOG_TAG, ">> mkdir: %s", path.c_str());
83100
int rc = ::mkdir(path.c_str(), 0);
84101
if (rc != 0) {
85-
ESP_LOGE(LOG_TAG, "mkdir: errno=%d", errno);
102+
ESP_LOGE(LOG_TAG, "mkdir: errno=%d %s", errno, strerror(errno));
86103
rc = errno;
87104
}
88105
return rc;

cpp_utils/FileSystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class FileSystem {
1717
public:
1818
static std::vector<File> getDirectoryContents(std:: string path);
1919
static void dumpDirectory(std::string path);
20+
static bool isDirectory(std::string path);
2021
static int mkdir(std::string path);
2122
static std::vector<std::string> pathSplit(std::string path);
2223
static int remove(std::string path);

cpp_utils/HttpResponse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const int HttpResponse::HTTP_STATUS_SERVICE_UNAVAILABLE = 503;
2727
static std::string lineTerminator = "\r\n";
2828
HttpResponse::HttpResponse(HttpRequest *request) {
2929
m_request = request;
30-
m_status = 0;
30+
m_status = 200;
3131
m_headerCommitted = false; // We have not yet sent a header.
3232
}
3333

cpp_utils/HttpServer.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <esp_log.h>
1313
#include "HttpRequest.h"
1414
#include "HttpResponse.h"
15+
#include "FileSystem.h"
1516
#include "WebSocket.h"
1617
static const char* LOG_TAG = "HttpServer";
1718

@@ -20,11 +21,13 @@ static const char* LOG_TAG = "HttpServer";
2021
* Constructor for HTTP Server
2122
*/
2223
HttpServer::HttpServer() {
23-
m_portNumber = 80; // The default port number.
24-
m_rootPath = "/"; // The default path.
25-
m_useSSL = false;
24+
m_portNumber = 80; // The default port number.
25+
m_rootPath = "/"; // The default path.
26+
m_useSSL = false; // Default SSL is no.
27+
setDirectoryListing(false); // Default directory listing is no.
2628
} // HttpServer
2729

30+
2831
HttpServer::~HttpServer() {
2932
ESP_LOGD(LOG_TAG, "~HttpServer");
3033
}
@@ -88,6 +91,10 @@ class HttpServerTask: public Task {
8891
// Serve up the content from the file on the file system ... if found ...
8992
std::ifstream ifStream;
9093
std::string fileName = m_pHttpServer->getRootPath() + request.getPath(); // Build the absolute file name to read.
94+
if (FileSystem::isDirectory(fileName)) {
95+
ESP_LOGD(LOG_TAG, "Path is a directory");
96+
return;
97+
}
9198
ESP_LOGD("HttpServerTask", "Opening file: %s", fileName.c_str());
9299
ifStream.open(fileName, std::ifstream::in | std::ifstream::binary); // Attempt to open the file for reading.
93100

@@ -131,7 +138,7 @@ class HttpServerTask: public Task {
131138

132139
Socket clientSocket = sockServ.waitForNewClient(); // Block waiting for a new external client connection.
133140

134-
ESP_LOGD("HttpServerTask", "HttpServer listening on port %d received a new client connection", m_pHttpServer->getPort());
141+
ESP_LOGD("HttpServerTask", "HttpServer listening on port %d received a new client connection; sockFd=%d", m_pHttpServer->getPort(), clientSocket.getFD());
135142

136143

137144
HttpRequest request(clientSocket); // Build the HTTP Request from the socket.
@@ -197,6 +204,16 @@ bool HttpServer::getSSL() {
197204
return m_useSSL;
198205
} // getSSL
199206

207+
208+
/**
209+
* @brief Set whether or not we will list directories.
210+
* @param [in] use Set to true to enable directory listing.
211+
*/
212+
void HttpServer::setDirectoryListing(bool use) {
213+
m_directoryListing = use;
214+
} // setDirectoryListening
215+
216+
200217
/**
201218
* @brief Set the root path for URL file mapping.
202219
*

cpp_utils/HttpServer.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ class HttpServerTask;
2525
class PathHandler {
2626
public:
2727
PathHandler(
28-
std::string method, // The method in the request to be matched.
29-
std::string pathPattern, // The pattern in the request to be matched
30-
void (*pWebServerRequestHandler) // The handler function to be invoked upon a match.
28+
std::string method, // The method in the request to be matched.
29+
std::string pathPattern, // The pattern in the request to be matched
30+
void (*pWebServerRequestHandler) // The handler function to be invoked upon a match.
3131
(
3232
HttpRequest* pHttpRequest,
3333
HttpResponse* pHttpResponse)
3434
);
35-
bool match(std::string method, std::string path); // Does the request method and pattern match?
35+
bool match(std::string method, std::string path); // Does the request method and pattern match?
3636
void invokePathHandler(HttpRequest* request, HttpResponse* response);
3737
private:
3838
std::string m_method;
@@ -55,18 +55,20 @@ class HttpServer {
5555
HttpRequest* pHttpRequest,
5656
HttpResponse* pHttpResponse)
5757
);
58-
uint16_t getPort(); // Get the port on which the Http server is listening.
59-
std::string getRootPath(); // Get the root of the file system path.
60-
bool getSSL(); // Are we using SSL?
61-
void setRootPath(std::string path); // Set the root of the file system path.
58+
uint16_t getPort(); // Get the port on which the Http server is listening.
59+
std::string getRootPath(); // Get the root of the file system path.
60+
bool getSSL(); // Are we using SSL?
61+
void setDirectoryListing(bool use); // Should we list the content of directories?
62+
void setRootPath(std::string path); // Set the root of the file system path.
6263
void start(uint16_t portNumber, bool useSSL=false);
6364
private:
6465
friend class HttpServerTask;
6566
friend class WebSocket;
66-
uint16_t m_portNumber;
67-
std::vector<PathHandler> m_pathHandlers;
68-
std::string m_rootPath; // Root path into the file system.
69-
bool m_useSSL;
67+
uint16_t m_portNumber; // Port number on which server is listening.
68+
std::vector<PathHandler> m_pathHandlers; // Vector of path handlers.
69+
std::string m_rootPath; // Root path into the file system.
70+
bool m_useSSL; // Is this server listening on an HTTPS port?
71+
bool m_directoryListing; // Should we list directory content?
7072
}; // HttpServer
7173

7274
#endif /* COMPONENTS_CPP_UTILS_HTTPSERVER_H_ */

cpp_utils/SSLUtils.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ char* SSLUtils::m_certificate = nullptr;
1313
char* SSLUtils::m_key = nullptr;
1414

1515
SSLUtils::SSLUtils() {
16-
// TODO Auto-generated constructor stub
17-
1816
}
1917

2018
SSLUtils::~SSLUtils() {
21-
// TODO Auto-generated destructor stub
2219
}
2320

2421
void SSLUtils::setCertificate(std::string certificate) {

cpp_utils/SockServ.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ void SockServ::acceptTask(void *data) {
4949

5050
SockServ* pSockServ = (SockServ*)data;
5151
while(1) {
52-
Socket tempSock = pSockServ->m_serverSocket.accept(true);
52+
Socket tempSock = pSockServ->m_serverSocket.accept(pSockServ->getSSL());
5353
if (!tempSock.isValid()) {
5454
continue;
5555
}
5656

5757
pSockServ->m_clientSet.insert(tempSock);
5858
xQueueSendToBack(pSockServ->m_acceptQueue, &tempSock, portMAX_DELAY);
5959
pSockServ->m_clientSemaphore.give();
60-
FreeRTOS::sleep(9999999);
6160
}
6261
} // acceptTask
6362

@@ -82,6 +81,14 @@ void SockServ::disconnect(Socket s) {
8281
} // disconnect
8382

8483

84+
/**
85+
* Get the SSL status.
86+
*/
87+
bool SockServ::getSSL() {
88+
return m_useSSL;
89+
} // getSSL
90+
91+
8592
/**
8693
* @brief Wait for data
8794
* @param [in] pData Pointer to buffer to hold the data.
@@ -198,12 +205,12 @@ Socket SockServ::waitForData(std::set<Socket>& socketSet) {
198205
* or can return immediately is there is already a client connection in existence.
199206
*/
200207
Socket SockServ::waitForNewClient() {
201-
202-
m_clientSemaphore.wait("waitForClient");
208+
ESP_LOGD(LOG_TAG, ">> waitForNewClient")
209+
m_clientSemaphore.wait("waitForNewClient");
203210
Socket tempSocket;
204211
xQueueReceive(m_acceptQueue, &tempSocket,portMAX_DELAY);
205-
ESP_LOGD(LOG_TAG, "<< waitForClient");
212+
ESP_LOGD(LOG_TAG, "<< waitForNewClient");
206213
return tempSocket;
207-
} // waitForClient
214+
} // waitForNewClient
208215

209216

cpp_utils/SockServ.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SockServ {
4444
SockServ();
4545
int connectedCount();
4646
void disconnect(Socket s);
47+
bool getSSL();
4748
size_t receiveData(Socket s, void* pData, size_t maxData);
4849
void sendData(uint8_t* data, size_t length);
4950
void sendData(std::string str);

0 commit comments

Comments
 (0)
0