From 545568ac16523ef524c930d7b8c1f14de353457e Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Wed, 19 Jun 2019 23:47:26 +0200 Subject: [PATCH] getRequestedFileName function and added fileExists function Now it can load any file! --- Web_Test/main.cpp | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/Web_Test/main.cpp b/Web_Test/main.cpp index 64fd092..999f3b0 100644 --- a/Web_Test/main.cpp +++ b/Web_Test/main.cpp @@ -14,9 +14,13 @@ int res; std::ostringstream content; std::string l; char buf[DEFAULT_BUFLEN]; - +bool fileExists(std::string path) { + std::fstream file(path); + return file.good(); +} std::string getRequestedFileName(char inBuf[DEFAULT_BUFLEN]) { if (strstr(inBuf,"GET") == NULL) { + //Invalid request return NULL; } std::string result = ""; @@ -25,14 +29,16 @@ std::string getRequestedFileName(char inBuf[DEFAULT_BUFLEN]) { result += inBuf[i]; } //std::cout << result; - /*if (result == "GET / HTTP/1.1") { + if (strstr(inBuf, "GET / HTTP/1.1") != NULL) { result = HOMEPAGE; } else { - // GET / favicon.ico HTTP / 1.1 - result.replace(result.begin(), result.end(), "GET"); - result.replace(result.begin(), result.end(), "HTTP/1.1"); - }*/ + std::stringstream stream(result); + stream >> result; + stream >> result; + result.erase(0, 1); + std::cout << result << std::endl; + } return result; } @@ -110,12 +116,18 @@ int main() { std::cout << buf; } std::string message; - std::cout << getRequestedFileName(buf); - if (strstr(buf, "GET / HTTP/1.1") != NULL) { - message = content.str(); - } - else { - + std::string reqFile = getRequestedFileName(buf); + if (fileExists(reqFile)) { + std::ifstream line(reqFile); + std::ostringstream msg; + while (std::getline(line, l)) + { + msg << l; + } + message = msg.str(); + }else{ + //Error 404 + std::cout << "Error 404!\n"; } res = send(client, message.c_str(), message.size(), 0);