diff --git a/FileIO.cpp b/FileIO.cpp index 1c5916865626059d907e9d72e182b26a16dd8c19..629362e905675f4b1d7a5562373454c9e179c76f 100755 --- a/FileIO.cpp +++ b/FileIO.cpp @@ -6,7 +6,7 @@ #include "FileIO.h" #include <fstream> #include <string> -#include <iostream> // DEBUG +//#include <iostream> // DEBUG #include "dirent.h" FileIO::FileIO() @@ -17,6 +17,24 @@ FileIO::~FileIO() { } +std::string FileIO::pathAppend(const std::string& p1, const std::string& p2) +{ + char sep = '/'; + std::string tmp = p1; + + #ifdef _WIN32 + sep = '\\'; + #endif + + if (p1[p1.length()] != sep) + { + tmp += sep; + return(tmp + p2); + } + + return(p1 + p2); +} + bool FileIO::getFilesInDirectory(std::vector<std::string> &files, const std::string &directory) { DIR *dir; @@ -58,4 +76,4 @@ bool FileIO::readTextFile(const std::string &filePath, std::string &output) output = contents; my_file.close(); return true; -} \ No newline at end of file +} diff --git a/FileIO.h b/FileIO.h index 4ed3bbe136a0251c73e5b6cad5f3f28c7a421fdd..95a5ef77045bc11a63cfbb98072837a4df54689e 100755 --- a/FileIO.h +++ b/FileIO.h @@ -18,6 +18,15 @@ public: // Destructor ~FileIO(); + /** + * @brief + * Append p2 path to end of p1 + * @param p1 - 'root' directory + * @param p2 - 'child' directory/file + * @return std::string - resultant full path + */ + std::string pathAppend(const std::string& p1, const std::string& p2); + /** * @brief Get the Files In Directory * @@ -38,4 +47,4 @@ public: private: }; -#endif \ No newline at end of file +#endif diff --git a/HTTPD.h b/HTTPD.h index a8052e03af17950aeea277615d66f3b54c5f8ab7..cd4b372b1fec775d5c8b2e6f11f40cb2737036a3 100755 --- a/HTTPD.h +++ b/HTTPD.h @@ -48,6 +48,18 @@ public: private: + // Disable default constructor + HTTPD() : + m_port(0), + m_ServerIntf(0), + m_ready(false), + m_indexCode(0), + m_lenCode(0), + m_lenDescription(0), + m_lenTemplateBodyPlaceholder(0), + m_lenFaviconPlaceholder(0) + {} + /** * @brief * When a client connects, a process is spawned and the child is sent into this @@ -77,16 +89,16 @@ private: void sendResponse(ClientThreadContainer container, statusCodes statusCode, const std::string &html); /** - * @brief + * @brief * Performs a lookup in the m_threadStatus map and updates the status attributed to a thread - * + * * @param uuid - the calling thread's uuid * @param status - the new status */ void updateStatus(std::string uuid, ThreadingStatus status); /** - * @brief + * @brief * Loop through m_clientThreads map and calls join() on threads * @param wait - if true, will call join() regardless of state, if false, will only call join() * on threads that are marked as STOPPED diff --git a/Home.cpp b/Home.cpp deleted file mode 100755 index 13c6f1426e14b88c9f538e6ec2b6fe4ad946a539..0000000000000000000000000000000000000000 --- a/Home.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - Copyright (C) 2021 Embed Creativity LLC - All Rights Reserved -*/ - -#include "Home.h" - -namespace embedcreativity -{ - -const std::string Home::PAGE_CONTENT = "<h1 class=\"welcome\">WiFi Setup</h1>" - "<p class=\"description\">Select your WiFi network. Add your WiFi password to connect your device to the Internet.</p>" - "<div class=\"form-box\" >" - "<form action=\"/connect\">" - "<input type=\"radio\" id=\"ssid_a\" name=\"selected_ssid\" value=\"ssid_a\">" - "<label for=\"ssid_a\">ssid_a</label><br>" - "<input type=\"radio\" id=\"ssid_b\" name=\"selected_ssid\" value=\"ssid_b\">" - "<label for=\"ssid_b\">ssid_b</label><br><br>" - "<label class=\"text-input-label\" for=\"password\">Password</label>" - "<input class=\"text-input\" type=\"text\" id=\"password\" name=\"password\" value=\"\" ><br><br>" - "<input class=\"submit-button\" type=\"submit\" value=\"Connect\">" - "</form>" - "</div>"; - -std::pair<statusCodes, std::string> Home::handleQuery(std::map<std::string, std::string> &arguments) -{ - std::pair<statusCodes, std::string> ret; - ret.first = statusCodes::STATUS_OK; - ret.second = PAGE_CONTENT; - return ret; -} - -} \ No newline at end of file diff --git a/Home.html b/Home.html new file mode 100755 index 0000000000000000000000000000000000000000..a428fc59d5e0985c4182a0fa6d564140d7944523 --- /dev/null +++ b/Home.html @@ -0,0 +1,13 @@ +<h1 class="welcome">WiFi Setup</h1> +<p class="description">Select your WiFi network. Add your WiFi password to connect your device to the Internet.</p> +<div class="form-box" > + <form action="/connect"> + <input type="radio" id="ssid_a" name="selected_ssid" value="ssid_a"> + <label for="ssid_a">ssid_a</label><br> + <input type="radio" id="ssid_b" name="selected_ssid" value="ssid_b"> + <label for="ssid_b">ssid_b</label><br><br> + <label class="text-input-label" for="password">Password</label> + <input class="text-input" type="text" id="password" name="password" value="" ><br><br> + <input class="submit-button" type="submit" value="Connect"> + </form> +</div> diff --git a/Makefile b/Makefile index abf38cf2f0746fc7843afbb11cbcbd8c9dcf52c4..89b703cd2e763fc4ddf9fe9d8e677f554d0baaee 100755 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ -#CXX := g++ Program := ecHTTPD #CXXFLAGS += -Wall -g -std=c++11 CXXFLAGS += -Wall -std=c++11 @@ -7,8 +6,8 @@ LIBS += all: $(Program) -$(Program): HTTPD.o socket.o FileIO.o QueryListener.o QueryPublisher.o main.o Home.o - $(CXX) $(LDFLAGS) HTTPD.o socket.o FileIO.o QueryListener.o QueryPublisher.o main.o Home.o -o $(Program) $(LIBS) +$(Program): HTTPD.o socket.o FileIO.o QueryListener.o QueryPublisher.o main.o StaticPage.o + $(CXX) $(LDFLAGS) HTTPD.o socket.o FileIO.o QueryListener.o QueryPublisher.o main.o StaticPage.o -o $(Program) $(LIBS) HTTPD.o: HTTPD.cpp $(CXX) $(CXXFLAGS) -c HTTPD.cpp @@ -29,8 +28,8 @@ main.o: main.cpp $(CXX) $(CXXFLAGS) -c main.cpp # Pages -Home.o: Home.cpp - $(CXX) $(CXXFLAGS) -c Home.cpp +StaticPage.o: StaticPage.cpp + $(CXX) $(CXXFLAGS) -c StaticPage.cpp # remove object files and executable when user executes "make clean" clean: diff --git a/QueryListener.cpp b/QueryListener.cpp index 45f1ea73f2435db7f08cf350b13a51f7273cf3c9..60058ed675590f7aef71685e23547206483612c6 100755 --- a/QueryListener.cpp +++ b/QueryListener.cpp @@ -4,12 +4,12 @@ */ #include "QueryListener.h" -#include <fstream> +#include "FileIO.h" namespace embedcreativity { -QueryListener::QueryListener(QueryPublisher* publisher, const std::string &query) : +QueryListener::QueryListener(QueryPublisher* publisher, const std::string &query) : m_queryPublisher(publisher) { m_queryPublisher->subscribe(this, query); @@ -23,26 +23,18 @@ QueryListener::~QueryListener() } } -bool QueryListener::assignSourceTemplate(const std::string &templateFile) +bool QueryListener::assignSourceTemplate(const std::string &path, const std::string &templateFile) { - std::ifstream hFile(templateFile); + FileIO fileHandler; + std::string fullPath = fileHandler.pathAppend(path, templateFile); - // ensure buffer is clear - m_templateHTML = ""; - - if (hFile.is_open()) + if (!fileHandler.readTextFile(fullPath, m_templateHTML)) { - // Read file in one character at a time to avoid multiple copies - while (hFile) - { - m_templateHTML += hFile.get(); - } - if (!m_templateHTML.empty()) - { - return true; - } + m_templateHTML = ""; + return false; } - return false; + + return true; } -} \ No newline at end of file +} diff --git a/QueryListener.h b/QueryListener.h index 3ca2d7533d883d2d184100fabe7f5478913a047c..8916063f6a546f2cfda79ba28c47d5b305cf7072 100755 --- a/QueryListener.h +++ b/QueryListener.h @@ -40,15 +40,17 @@ public: * @param[in] templateFile - path to template file * @return True if file was successfully read and contents stored, False otherwise */ - bool assignSourceTemplate(const std::string &templateFile); + bool assignSourceTemplate(const std::string &path, const std::string &templateFile); + +protected: + std::string m_templateHTML; private: // Disable default constructor QueryListener(); QueryPublisher* m_queryPublisher; - std::string m_templateHTML; }; } -#endif \ No newline at end of file +#endif diff --git a/StaticPage.cpp b/StaticPage.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d5006be084de2b2f716a22f28f97a6c34ec405fc --- /dev/null +++ b/StaticPage.cpp @@ -0,0 +1,19 @@ +/* + Copyright (C) 2021 Embed Creativity LLC + All Rights Reserved +*/ + +#include "StaticPage.h" + +namespace embedcreativity +{ + +std::pair<statusCodes, std::string> StaticPage::handleQuery(std::map<std::string, std::string> &arguments) +{ + std::pair<statusCodes, std::string> ret; + ret.first = statusCodes::STATUS_OK; + ret.second = m_templateHTML; + return ret; +} + +} diff --git a/Home.h b/StaticPage.h similarity index 90% rename from Home.h rename to StaticPage.h index 93aea734bae9726bf52c84467d222604f90056c1..6fc734d56fb682e84815bb49021e8afc31f26001 100755 --- a/Home.h +++ b/StaticPage.h @@ -3,8 +3,8 @@ All Rights Reserved */ -#ifndef Home_H_ -#define Home_H_ +#ifndef STATIC_PAGE_H_ +#define STATIC_PAGE_H_ #include "QueryListener.h" #include "HTTPStatusCodes.h" @@ -14,7 +14,7 @@ namespace embedcreativity { -class Home : public QueryListener +class StaticPage : public QueryListener { public: @@ -35,4 +35,4 @@ private: }; } -#endif \ No newline at end of file +#endif diff --git a/main.cpp b/main.cpp index 3152c8538363f47e0491ee4f842579c3e9f480ef..a6b8f29f8a51c614292503bd78faadd16c13c10b 100755 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,7 @@ #include <iostream> #include <cstdlib> #include "HTTPD.h" -#include "Home.h" +#include "StaticPage.h" using namespace embedcreativity; @@ -69,11 +69,12 @@ int main(int argc, char **argv) if (server->ready()) { std::string page = "/"; - Home homePage = Home(server, page); + StaticPage homePage = StaticPage(server, page); + homePage.assignSourceTemplate(templatePath, "Home.html"); // HTTPD will not return on its own from this call server->run(); } return 0; -} \ No newline at end of file +}