getRequestedFileName function and added fileExists function
Now it can load any file!
This commit is contained in:
parent
ed21e01b89
commit
545568ac16
1 changed files with 24 additions and 12 deletions
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue