From 449bd063f0ec62d6a1d9295a856964c10293c1e2 Mon Sep 17 00:00:00 2001 From: hashimMH Date: Fri, 19 Apr 2024 23:06:04 +0400 Subject: [PATCH 01/10] Fix(CGI):just push clean update --- .resources/cgi_bin/file.py | 2 + .resources/cgi_bin/file.sh | 3 + configs/default.conf | 2 +- curl.sh | 2 +- sources/CGI/Cgi.cpp | 120 +-------------------- sources/CGI/Cgi.hpp | 5 +- sources/http/handler/GetRequestHandler.cpp | 2 +- 7 files changed, 15 insertions(+), 121 deletions(-) create mode 100755 .resources/cgi_bin/file.py create mode 100755 .resources/cgi_bin/file.sh diff --git a/.resources/cgi_bin/file.py b/.resources/cgi_bin/file.py new file mode 100755 index 0000000..b263f32 --- /dev/null +++ b/.resources/cgi_bin/file.py @@ -0,0 +1,2 @@ +#!/usr/bin/python3 +print("hello worled") \ No newline at end of file diff --git a/.resources/cgi_bin/file.sh b/.resources/cgi_bin/file.sh new file mode 100755 index 0000000..f0a6caa --- /dev/null +++ b/.resources/cgi_bin/file.sh @@ -0,0 +1,3 @@ +#!/bin/sh +echo "Hello, World Bash!" +sleep 10 \ No newline at end of file diff --git a/configs/default.conf b/configs/default.conf index 191ec1b..c4efdae 100644 --- a/configs/default.conf +++ b/configs/default.conf @@ -4,7 +4,7 @@ http { client_max_body_size 1M; server_name localhost; - root ./.resources/sample_pages; + root ./.resources/cgi_bin; index index.html index.htm; location / { diff --git a/curl.sh b/curl.sh index b1d0020..fead08c 100755 --- a/curl.sh +++ b/curl.sh @@ -1,4 +1,4 @@ -curl -X GET http://localhost:8080/.resources/sample_pages/index.html \ +curl -X GET http://localhost:8080/file.sh \ -H "Host: Linode.com" \ -H "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) Gecko/20091102 Firefox/3.5.5" \ -H "Accept-Encoding: gzip,deflate" \ diff --git a/sources/CGI/Cgi.cpp b/sources/CGI/Cgi.cpp index bd9b22a..e176601 100644 --- a/sources/CGI/Cgi.cpp +++ b/sources/CGI/Cgi.cpp @@ -6,18 +6,12 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:44:51 by hmohamed #+# #+# */ -/* Updated: 2024/04/16 01:59:01 by hmohamed ### ########.fr */ +/* Updated: 2024/04/19 22:37:01 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ #include "Cgi.hpp" - -// Cgi::Cgi(char* filePath, char** arguments, char** environment): filePath(filePath), arguments(arguments), environment(environment) -// { - -// } - char **headersToEnv(vsp &headers) { std::vector envVector; @@ -91,7 +85,7 @@ string getStingQuery(string res) } -Cgi::Cgi(const Request &request) +Cgi::Cgi(const Request &request, const ServerConfig &config) { string res; @@ -99,7 +93,7 @@ Cgi::Cgi(const Request &request) headers = request.get_headers(); environment = headersToEnv(headers); //filePath = const_cast(geturi(request.get_resource())->c_str()); - filePath = (geturi(res)); + filePath = config.serverRoot + (geturi(res)); queryString = getStingQuery(res); //filePath = const_cast (request.get_resource().c_str()); // // Check if the Python script exists @@ -130,44 +124,6 @@ Cgi::~Cgi() delete[] arguments; } -// void Cgi::execute() -// { -// int fd[2]; -// int id; -// char *res_body; -// int a; - -// res_body = (char *)malloc(100); -// res_body[99] = '\0'; -// pipe(fd); -// id = fork(); -// if (id == 0) -// { -// // Child process -// dup2(fd[1], STDOUT_FILENO); -// close(fd[0]); -// close(fd[1]); - -// if (execve(filePath, arguments, environment) == -1) -// { -// std::cerr << "Error executing execve: " << strerror(errno) << std::endl; -// _exit(EXIT_FAILURE); -// } -// } - -// // Wait for the child process to finish -// int status; -// waitpid(id, &status, 0); -// a = read(fd[0], res_body, 90); -// res_body[a] = '\0'; -// cout << res_body << endl; -// close(fd[0]); -// close(fd[1]); - - -// } - - void Cgi::execute(const std::string& outputFile) { int fd[2]; @@ -235,7 +191,7 @@ string Cgi::execute(void) std::cerr << "Error creating pipe: " << strerror(errno) << std::endl; return NULL; } - + cout<< "FBB" << filePath << endl; // Fork the process id = fork(); if (id == -1) @@ -271,75 +227,7 @@ string Cgi::execute(void) { res_body.append(buffer, bytesRead); } - // Save the response to a file - // std::ofstream outFile(outputFile.c_str()); - // if (outFile.is_open()) - // { - // outFile << res_body; - // outFile.close(); - // std::cout << "Response saved to: " << outputFile << std::endl; - // } - // else - // { - // std::cerr << "Error opening output file: " << strerror(errno) << std::endl; - // } close(fd[0]); return (res_body); } - -// std::string Cgi::execute(void) -// { -// int fd[2]; -// int id; -// std::string res_body; - -// // Create a pipe for communication -// if (pipe(fd) == -1) { -// std::cerr << "Error creating pipe: " << strerror(errno) << std::endl; -// return (NULL); -// } - -// // Fork the process -// id = fork(); -// if (id == -1) { -// std::cerr << "Error forking process: " << strerror(errno) << std::endl; -// return (NULL); -// } else if (id == 0) { -// // Child process -// dup2(fd[1], STDOUT_FILENO); -// close(fd[0]); -// close(fd[1]); - -// if (execve(filePath, arguments, environment) == -1) { -// std::cerr << "Error executing execve: " << strerror(errno) << std::endl; -// _exit(EXIT_FAILURE); -// } -// } - -// // Parent process -// close(fd[1]); // Close the write end of the pipe - -// // Wait for the child process to finish -// int status; -// waitpid(id, &status, 0); - -// // Read the response from the pipe -// char buffer[91]; -// ssize_t bytesRead; -// while ((bytesRead = read(fd[0], buffer, 90)) > 0) { -// res_body.append(buffer, bytesRead); -// } -// // Save the response to a file -// // std::ofstream outFile(outputFile); -// // if (outFile.is_open()) { -// // outFile << res_body; -// // outFile.close(); -// // std::cout << "Response saved to: " << outputFile << std::endl; -// // } else { -// // std::cerr << "Error opening output file: " << strerror(errno) << std::endl; -// // } - -// close(fd[0]); -// return(res_body); -// } \ No newline at end of file diff --git a/sources/CGI/Cgi.hpp b/sources/CGI/Cgi.hpp index 27ee2e0..268987f 100644 --- a/sources/CGI/Cgi.hpp +++ b/sources/CGI/Cgi.hpp @@ -6,7 +6,7 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:42:45 by hmohamed #+# #+# */ -/* Updated: 2024/04/16 01:44:42 by hmohamed ### ########.fr */ +/* Updated: 2024/04/18 14:03:46 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,11 +21,12 @@ #include #include "webserv.hpp" #include "Request.hpp" +#include "Server.hpp" class Cgi { public: // Cgi(char* filePath, char** arguments, char** environment); - Cgi(const Request &request); + Cgi(const Request &request, const ServerConfig &config); ~Cgi(); void execute(const std::string &outputFile); string execute(void); diff --git a/sources/http/handler/GetRequestHandler.cpp b/sources/http/handler/GetRequestHandler.cpp index 27ba007..b14b9fe 100644 --- a/sources/http/handler/GetRequestHandler.cpp +++ b/sources/http/handler/GetRequestHandler.cpp @@ -86,7 +86,7 @@ const vector GetRequestHandler::get_resource(const Request &request, { if(check_cgi_request(resource)) { - Cgi cgi(request); + Cgi cgi(request, config); string strv; strv = cgi.execute(); From 75c0d17f170f97d1b79ace883c75d95a8063a005 Mon Sep 17 00:00:00 2001 From: hashimMH Date: Sat, 20 Apr 2024 03:05:02 +0400 Subject: [PATCH 02/10] fix(GetRequestHandler):return the body size --- .resources/notes/CGI_ref | 1 + sources/CGI/Cgi.cpp | 26 ++++++++++++++++++---- sources/CGI/Cgi.hpp | 3 ++- sources/http/handler/GetRequestHandler.cpp | 16 +++++++++++-- sources/http/request/Request.cpp | 5 +++++ sources/http/request/Request.hpp | 1 + 6 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 .resources/notes/CGI_ref diff --git a/.resources/notes/CGI_ref b/.resources/notes/CGI_ref new file mode 100644 index 0000000..2090d27 --- /dev/null +++ b/.resources/notes/CGI_ref @@ -0,0 +1 @@ +https://www.oreilly.com/library/view/cgi-programming-on/9781565921689/04_chapter-01.html //good aticla for CGi \ No newline at end of file diff --git a/sources/CGI/Cgi.cpp b/sources/CGI/Cgi.cpp index e176601..8084204 100644 --- a/sources/CGI/Cgi.cpp +++ b/sources/CGI/Cgi.cpp @@ -6,7 +6,7 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:44:51 by hmohamed #+# #+# */ -/* Updated: 2024/04/19 22:37:01 by hmohamed ### ########.fr */ +/* Updated: 2024/04/20 02:23:34 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,24 @@ char **headersToEnv(vsp &headers) envVector.push_back(envEntry); } + envVector.push_back("GATEWAY_INTERFACE=CGI/1.1"); + + + // "REQUEST_METHOD" + // "CONTENT_LENGTH" + // "CONTENT_TYPE" + // "PATH_INFO" + // "PATH_TRANSLATED" + // "QUERY_STRING" + // "REMOTEaddr" + // "REMOTE_IDENT" + // "REMOTE_USER" + // "REQUEST_URI" + // "SERVER_NAME" + // "SERVER_PORT" + // "SERVER_PROTOCOL" = "HTTP/1.1"; + // "SERVER_SOFTWARE" + // Allocate memory for char* array char **envp = new char *[envVector.size() + 1]; @@ -85,7 +103,7 @@ string getStingQuery(string res) } -Cgi::Cgi(const Request &request, const ServerConfig &config) +Cgi::Cgi(const Request &request, const ServerConfig &config): body(request.get_body()) { string res; @@ -221,9 +239,9 @@ string Cgi::execute(void) waitpid(id, &status, 0); // Read the response from the pipe - char buffer[91]; + char buffer[1024]; ssize_t bytesRead; - while ((bytesRead = read(fd[0], buffer, 90)) > 0) + while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) { res_body.append(buffer, bytesRead); } diff --git a/sources/CGI/Cgi.hpp b/sources/CGI/Cgi.hpp index 268987f..0e8c14e 100644 --- a/sources/CGI/Cgi.hpp +++ b/sources/CGI/Cgi.hpp @@ -6,7 +6,7 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:42:45 by hmohamed #+# #+# */ -/* Updated: 2024/04/18 14:03:46 by hmohamed ### ########.fr */ +/* Updated: 2024/04/20 01:58:16 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,7 @@ class Cgi { vsp headers; string queryString; string filePath; + string body; char **arguments; char **environment; diff --git a/sources/http/handler/GetRequestHandler.cpp b/sources/http/handler/GetRequestHandler.cpp index b14b9fe..c176878 100644 --- a/sources/http/handler/GetRequestHandler.cpp +++ b/sources/http/handler/GetRequestHandler.cpp @@ -91,12 +91,22 @@ const vector GetRequestHandler::get_resource(const Request &request, strv = cgi.execute(); body = vector(strv.begin(), strv.end()); + // content type + string resource_type = find_resource_type(resource); + if (resource_type.length() != 0) + add_header(std::make_pair("Content-Type", resource_type)); + // content length + resource_file.seekg(0, std::ios_base::end); + resource_size = strv.size(); + add_header( + std::make_pair("Content-Length", ws_itoa(resource_size))); + resource_file.seekg(0, std::ios_base::beg); } else + { body = vector((std::istreambuf_iterator(resource_file)), std::istreambuf_iterator()); - - // content type + // content type string resource_type = find_resource_type(resource); if (resource_type.length() != 0) add_header(std::make_pair("Content-Type", resource_type)); @@ -106,6 +116,8 @@ const vector GetRequestHandler::get_resource(const Request &request, add_header( std::make_pair("Content-Length", ws_itoa(resource_size))); resource_file.seekg(0, std::ios_base::beg); + } + } } /* authentication function goes here for the requested resource */ diff --git a/sources/http/request/Request.cpp b/sources/http/request/Request.cpp index bb421f0..bf07b85 100644 --- a/sources/http/request/Request.cpp +++ b/sources/http/request/Request.cpp @@ -42,6 +42,11 @@ const string &Request::get_resource() const return resource; } +const string &Request::get_body() const +{ + return body; +} + std::ostream &operator<<(ostream &os, const Request &r) { os << "Method: " << enumToString(r.method) << std::endl; diff --git a/sources/http/request/Request.hpp b/sources/http/request/Request.hpp index 5f1b06d..e1f9d16 100644 --- a/sources/http/request/Request.hpp +++ b/sources/http/request/Request.hpp @@ -13,6 +13,7 @@ class Request Request(const Request &other); METHOD get_method() const; const string &get_resource() const; + const string &get_body() const; const vsp &get_headers() const; friend ostream &operator<<(ostream &os, const Request &r); From ff0bb16a7934e50e3ee3281bfd7a32965b7cb80a Mon Sep 17 00:00:00 2001 From: hashimtop Date: Sat, 20 Apr 2024 22:24:43 +0400 Subject: [PATCH 03/10] commit --- .resources/{cgi_bin => cgi-bin}/file.py | 0 .resources/{cgi_bin => cgi-bin}/file.sh | 0 configs/cgitest.config | 19 +++++++ configs/default.conf | 2 +- curl.sh | 2 +- sources/CGI/Cgi.cpp | 58 +++++++++++----------- sources/CGI/Cgi.hpp | 4 +- sources/http/handler/GetRequestHandler.cpp | 27 +++++++--- 8 files changed, 73 insertions(+), 39 deletions(-) rename .resources/{cgi_bin => cgi-bin}/file.py (100%) rename .resources/{cgi_bin => cgi-bin}/file.sh (100%) create mode 100644 configs/cgitest.config diff --git a/.resources/cgi_bin/file.py b/.resources/cgi-bin/file.py similarity index 100% rename from .resources/cgi_bin/file.py rename to .resources/cgi-bin/file.py diff --git a/.resources/cgi_bin/file.sh b/.resources/cgi-bin/file.sh similarity index 100% rename from .resources/cgi_bin/file.sh rename to .resources/cgi-bin/file.sh diff --git a/configs/cgitest.config b/configs/cgitest.config new file mode 100644 index 0000000..19ec707 --- /dev/null +++ b/configs/cgitest.config @@ -0,0 +1,19 @@ +http { + server { + listen 8080; + client_max_body_size 1M; + server_name localhost; + + root ./.resources; + index index.html index.htm; + + location / { + root ./.resources/sample_pages; + } + + error_page 404 /404.html; + location = /404.html { + root ./.resources/sample_pages; + } + } +} \ No newline at end of file diff --git a/configs/default.conf b/configs/default.conf index c4efdae..191ec1b 100644 --- a/configs/default.conf +++ b/configs/default.conf @@ -4,7 +4,7 @@ http { client_max_body_size 1M; server_name localhost; - root ./.resources/cgi_bin; + root ./.resources/sample_pages; index index.html index.htm; location / { diff --git a/curl.sh b/curl.sh index fead08c..897246f 100755 --- a/curl.sh +++ b/curl.sh @@ -1,4 +1,4 @@ -curl -X GET http://localhost:8080/file.sh \ +curl -X GET http://localhost:8080/cgi-bin/file.sh \ -H "Host: Linode.com" \ -H "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) Gecko/20091102 Firefox/3.5.5" \ -H "Accept-Encoding: gzip,deflate" \ diff --git a/sources/CGI/Cgi.cpp b/sources/CGI/Cgi.cpp index 8084204..de5aec7 100644 --- a/sources/CGI/Cgi.cpp +++ b/sources/CGI/Cgi.cpp @@ -6,7 +6,7 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:44:51 by hmohamed #+# #+# */ -/* Updated: 2024/04/20 02:23:34 by hmohamed ### ########.fr */ +/* Updated: 2024/04/20 21:50:25 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,10 +25,11 @@ char **headersToEnv(vsp &headers) envVector.push_back(envEntry); } - envVector.push_back("GATEWAY_INTERFACE=CGI/1.1"); + // std::string gateway_interface = "GATEWAY_INTERFACE=CGI/1.1"; + // envVector.push_back(const_cast(gateway_interface.c_str())); - // "REQUEST_METHOD" + // "REQUEST_METHOD" // "CONTENT_LENGTH" // "CONTENT_TYPE" // "PATH_INFO" @@ -56,23 +57,6 @@ char **headersToEnv(vsp &headers) return envp; } -// string *geturi(string res) -// { -// char *result; -// string *resn; -// size_t qu; - -// result = NULL; -// qu = res.find('?', 0); -// resn = new string(res.substr(0, qu)); -// //result = const_cast(res.substr(0,qu).c_str()); -// cout << *resn << endl; -// cout<< "test" << qu <(resn->c_str()); -// cout<< "result : " << result < 0) + res_body.append(buffer, bytesRead); + close(fd[0]); + }); + // Wait for the child process to finish int status; waitpid(id, &status, 0); - // Read the response from the pipe - char buffer[1024]; - ssize_t bytesRead; - while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) - { - res_body.append(buffer, bytesRead); - } + // Join the read thread + readThread.join(); - close(fd[0]); + // // Wait for the child process to finish + // int status; + // waitpid(id, &status, 0); + + // // Read the response from the pipe + // char buffer[1024]; + // ssize_t bytesRead; + // while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) + // { + // res_body.append(buffer, bytesRead); + // } + + //close(fd[0]); return (res_body); } diff --git a/sources/CGI/Cgi.hpp b/sources/CGI/Cgi.hpp index 0e8c14e..294eab2 100644 --- a/sources/CGI/Cgi.hpp +++ b/sources/CGI/Cgi.hpp @@ -6,7 +6,7 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:42:45 by hmohamed #+# #+# */ -/* Updated: 2024/04/20 01:58:16 by hmohamed ### ########.fr */ +/* Updated: 2024/04/20 21:50:36 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #define CGI_HPP #include +#include #include #include #include @@ -38,7 +39,6 @@ class Cgi { string body; char **arguments; char **environment; - }; #endif diff --git a/sources/http/handler/GetRequestHandler.cpp b/sources/http/handler/GetRequestHandler.cpp index c176878..e6af1ed 100644 --- a/sources/http/handler/GetRequestHandler.cpp +++ b/sources/http/handler/GetRequestHandler.cpp @@ -26,15 +26,28 @@ inline const string find_resource_type(const string &resource) return (fileTypes.find(file_extension)->second); } -bool check_cgi_request(const string &res) +// bool check_cgi_request(const string &res) +// { +// string filetype; + +// filetype = find_resource_type(res); +// if(filetype == "bash" || filetype == "python") +// return true; +// else +// return false; +// } + +bool check_cgi_request(string res) { - string filetype; + // Find the position of the word in the string + size_t pos = res.find("cgi-bin"); + + if (pos != std::string::npos) + return(true); + else + return(false); + - filetype = find_resource_type(res); - if(filetype == "bash" || filetype == "python") - return true; - else - return false; } From 3041c1c07358b1f33a4b891445169640d4a3490e Mon Sep 17 00:00:00 2001 From: hashimtop Date: Sat, 20 Apr 2024 22:55:09 +0400 Subject: [PATCH 04/10] compilation fix --- sources/CGI/Cgi.cpp | 34 +++++++++------------------------- sources/CGI/Cgi.hpp | 3 +-- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/sources/CGI/Cgi.cpp b/sources/CGI/Cgi.cpp index de5aec7..339f0ea 100644 --- a/sources/CGI/Cgi.cpp +++ b/sources/CGI/Cgi.cpp @@ -6,7 +6,7 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:44:51 by hmohamed #+# #+# */ -/* Updated: 2024/04/20 21:50:25 by hmohamed ### ########.fr */ +/* Updated: 2024/04/20 22:53:33 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ @@ -220,34 +220,18 @@ string Cgi::execute(void) // Parent process close(fd[1]); // Close the write end of the pipe - // Read the response from the pipe using a thread - std::thread readThread([&]() { - char buffer[1024]; - ssize_t bytesRead; - while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) - res_body.append(buffer, bytesRead); - close(fd[0]); - }); - // Wait for the child process to finish int status; waitpid(id, &status, 0); - // Join the read thread - readThread.join(); - - // // Wait for the child process to finish - // int status; - // waitpid(id, &status, 0); - - // // Read the response from the pipe - // char buffer[1024]; - // ssize_t bytesRead; - // while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) - // { - // res_body.append(buffer, bytesRead); - // } + // Read the response from the pipe + char buffer[1024]; + ssize_t bytesRead; + while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) + { + res_body.append(buffer, bytesRead); + } - //close(fd[0]); + close(fd[0]); return (res_body); } diff --git a/sources/CGI/Cgi.hpp b/sources/CGI/Cgi.hpp index 294eab2..a02f03a 100644 --- a/sources/CGI/Cgi.hpp +++ b/sources/CGI/Cgi.hpp @@ -6,7 +6,7 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:42:45 by hmohamed #+# #+# */ -/* Updated: 2024/04/20 21:50:36 by hmohamed ### ########.fr */ +/* Updated: 2024/04/20 22:53:43 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ #define CGI_HPP #include -#include #include #include #include From 98d2b6881c0a4ef2ce1af8fab57f55161f0fadaf Mon Sep 17 00:00:00 2001 From: hashimtop Date: Sun, 21 Apr 2024 00:43:58 +0400 Subject: [PATCH 05/10] Fix(CGI):fix the marge --- curl.sh | 7 +++ sources/CGI/Cgi.cpp | 5 +- sources/http/handler/GetRequestHandler.cpp | 59 +++++----------------- 3 files changed, 24 insertions(+), 47 deletions(-) create mode 100755 curl.sh diff --git a/curl.sh b/curl.sh new file mode 100755 index 0000000..897246f --- /dev/null +++ b/curl.sh @@ -0,0 +1,7 @@ +curl -X GET http://localhost:8080/cgi-bin/file.sh \ + -H "Host: Linode.com" \ + -H "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) Gecko/20091102 Firefox/3.5.5" \ + -H "Accept-Encoding: gzip,deflate" \ + -H "Accept-Charset: ISO-8859-1,utf-8" \ + -H "Cache-Control: no-cache" \ + -d $'body\r\n' \ No newline at end of file diff --git a/sources/CGI/Cgi.cpp b/sources/CGI/Cgi.cpp index bc35102..ab1cff0 100644 --- a/sources/CGI/Cgi.cpp +++ b/sources/CGI/Cgi.cpp @@ -6,7 +6,7 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:44:51 by hmohamed #+# #+# */ -/* Updated: 2024/04/20 22:59:00 by hmohamed ### ########.fr */ +/* Updated: 2024/04/20 23:45:03 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ @@ -91,11 +91,12 @@ Cgi::Cgi(const Request &request, const ServerConfig &config): body(request.get_b { string res; + (void)config; res = const_cast(request.get_resource().c_str()); headers = request.get_headers(); environment = headersToEnv(headers); //filePath = const_cast(geturi(request.get_resource())->c_str()); - filePath = config.serverRoot + (geturi(res)); + filePath = (geturi(res)); queryString = getStingQuery(res); //filePath = const_cast (request.get_resource().c_str()); // // Check if the Python script exists diff --git a/sources/http/handler/GetRequestHandler.cpp b/sources/http/handler/GetRequestHandler.cpp index a9920e5..595e532 100644 --- a/sources/http/handler/GetRequestHandler.cpp +++ b/sources/http/handler/GetRequestHandler.cpp @@ -51,13 +51,13 @@ bool check_cgi_request(string res) } -const vector GetRequestHandler::get_resource(const Request &request, - const CachedPages *cachedPages, - const ServerConfig &config) +// TODO resource handling for get-requests, is broken +const vector GetRequestHandler::get_resource(const Request& request, + const CachedPages* cachedPages, const ServerConfig& config) { vsp requestHeaders = request.get_headers(); string resource = request.get_resource(); - string defaultPage = config.serverRoot + "/"; + string defaultPage = config.serverRoot + "/"; vector body; @@ -69,8 +69,8 @@ const vector GetRequestHandler::get_resource(const Request &request, status = OK; if (resource == defaultPage) { body = cachedPages->home.data; - add_header(make_pair("Content-Type", - cachedPages->home.contentType)); + add_header( + make_pair("Content-Type", cachedPages->home.contentType)); add_header(make_pair("Content-Length", ws_itoa(cachedPages->home.contentLength))); @@ -85,67 +85,36 @@ const vector GetRequestHandler::get_resource(const Request &request, ws_itoa(cachedPages->notFound.contentLength))); body = cachedPages->notFound.data; } - else - { - if(check_cgi_request(resource)) - { - Cgi cgi(request, config); - string strv; - - strv = cgi.execute(); - body = vector(strv.begin(), strv.end()); - // content type - string resource_type = find_resource_type(resource); - if (resource_type.length() != 0) - add_header(std::make_pair("Content-Type", resource_type)); - // content length - resource_file.seekg(0, std::ios_base::end); - resource_size = strv.size(); - add_header( - std::make_pair("Content-Length", ws_itoa(resource_size))); - resource_file.seekg(0, std::ios_base::beg); - } - else - { - body = vector((std::istreambuf_iterator(resource_file)), - std::istreambuf_iterator()); - // content type + else { string resource_type = find_resource_type(resource); if (resource_type.length() != 0) add_header(make_pair("Content-Type", resource_type)); - if (resource_type == "bash" || resource_type == "python") { - Cgi cgi(request); + if (check_cgi_request(resource)) { + Cgi cgi(request, config); string result; result = cgi.execute(); body = vector(result.begin(), result.end()); - add_header(make_pair("Content-Length", - ws_itoa(body.size()))); + add_header( + make_pair("Content-Length", ws_itoa(body.size()))); } else { body = vector((std::istreambuf_iterator(resource_file)), std::istreambuf_iterator()); resource_file.seekg(0, std::ios_base::end); resource_size = resource_file.tellg(); - add_header(make_pair("Content-Length", - ws_itoa(resource_size))); + add_header( + make_pair("Content-Length", ws_itoa(resource_size))); resource_file.seekg(0, std::ios_base::beg); } // content type // content length - resource_file.seekg(0, std::ios_base::end); - resource_size = resource_file.tellg(); - add_header( - std::make_pair("Content-Length", ws_itoa(resource_size))); - resource_file.seekg(0, std::ios_base::beg); - } - } } /* authentication function goes here for the requested resource */ - + DEBUG_MSG("Resource '" + resource + "' : [" + ws_itoa(resource_size) + "]", W); /* caching control */ /* compression/encoding From ca8d2626c0353c421cb69711607a24d5a50350a4 Mon Sep 17 00:00:00 2001 From: hashimtop Date: Sun, 21 Apr 2024 04:12:51 +0400 Subject: [PATCH 06/10] Fix(server.cpp/.hpp):CGI none blocking --- .resources/cgi-bin/file.sh | 2 +- sources/server/Server.cpp | 47 +++++++++++++++++++++++++++++++++----- sources/server/Server.hpp | 1 + 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/.resources/cgi-bin/file.sh b/.resources/cgi-bin/file.sh index f0a6caa..d859a7f 100755 --- a/.resources/cgi-bin/file.sh +++ b/.resources/cgi-bin/file.sh @@ -1,3 +1,3 @@ #!/bin/sh echo "Hello, World Bash!" -sleep 10 \ No newline at end of file +sleep 20 \ No newline at end of file diff --git a/sources/server/Server.cpp b/sources/server/Server.cpp index e1d6c94..5daed80 100644 --- a/sources/server/Server.cpp +++ b/sources/server/Server.cpp @@ -26,6 +26,17 @@ Server& Server::get_instance(const ServerConfig& config, int backLog) return instance; } +bool Server::check_cgi_request(string res) +{ + // Find the position of the word in the string + size_t pos = res.find("cgi-bin"); + + if (pos != std::string::npos) + return (true); + else + return (false); +} + /** * @brief Constructor for the Server class. * @@ -58,6 +69,8 @@ Server::~Server() void Server::handle_connection(fd incoming, fd_set& activeSockets) { + int id; + try { ConnectionManager::check_connection(incoming); Request& r = ConnectionManager::add_connection(incoming, activeSockets); @@ -65,13 +78,35 @@ void Server::handle_connection(fd incoming, fd_set& activeSockets) if (!r.parse_request(config)) return; - IRequestHandler* handler = - RequestHandlerFactory::MakeRequestHandler(r.get_method()); - Response response = handler->handle_request(r, cachedPages, config); - - response.send_response(incoming); + if(check_cgi_request(r.get_resource())) + { + id = fork(); + if (id == -1) { + std::cerr << "Error forking process: " << strerror(errno) << std::endl; + } + else if (id == 0) { + // Child process + IRequestHandler* handler = + RequestHandlerFactory::MakeRequestHandler(r.get_method()); + Response response = handler->handle_request(r, cachedPages, config); + + response.send_response(incoming); + r.setCompleted(); + delete handler; + exit(0); + } + }else{ + IRequestHandler* handler = + RequestHandlerFactory::MakeRequestHandler(r.get_method()); + Response response = handler->handle_request(r, cachedPages, config); + + response.send_response(incoming); + // r.setCompleted(); + delete handler; + } r.setCompleted(); - delete handler; + + } catch (std::ios_base::failure& f) { DEBUG_MSG(ERR_PARSE, R); } catch (std::exception& e) { diff --git a/sources/server/Server.hpp b/sources/server/Server.hpp index e96eefd..b5f210f 100644 --- a/sources/server/Server.hpp +++ b/sources/server/Server.hpp @@ -26,6 +26,7 @@ class Server { int backLog = DEFAULT_BACKLOG); ~Server(); void start(enum polling_strat); + bool check_cgi_request(string res); protected: Server(const ServerConfig& config, int backLog); From d299b65e7a86fccb266a113079208c33905d2752 Mon Sep 17 00:00:00 2001 From: hashimtop Date: Sun, 21 Apr 2024 04:15:32 +0400 Subject: [PATCH 07/10] ref --- .resources/notes/CGI_ref | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.resources/notes/CGI_ref b/.resources/notes/CGI_ref index 2090d27..133afd6 100644 --- a/.resources/notes/CGI_ref +++ b/.resources/notes/CGI_ref @@ -1 +1,2 @@ -https://www.oreilly.com/library/view/cgi-programming-on/9781565921689/04_chapter-01.html //good aticla for CGi \ No newline at end of file +https://www.oreilly.com/library/view/cgi-programming-on/9781565921689/04_chapter-01.html //good aticla for CGi +http://www.mnuwer.dbasedeveloper.co.uk/dlearn/web/session01.htm \ No newline at end of file From 0704176b1e66469830de96bc3c58f56314416150 Mon Sep 17 00:00:00 2001 From: hashimMH Date: Thu, 25 Apr 2024 16:26:43 +0400 Subject: [PATCH 08/10] build(cgi.cpp): Timeout control --- curl.sh | 2 +- sources/CGI/Cgi.cpp | 187 +++++++++++++-------- sources/CGI/Cgi.hpp | 6 +- sources/http/handler/GetRequestHandler.cpp | 25 ++- 4 files changed, 142 insertions(+), 78 deletions(-) diff --git a/curl.sh b/curl.sh index 897246f..8a66f7f 100755 --- a/curl.sh +++ b/curl.sh @@ -1,4 +1,4 @@ -curl -X GET http://localhost:8080/cgi-bin/file.sh \ +curl -X GET http://localhost:8080/cgi-bin/file.sh?name=hashim \ -H "Host: Linode.com" \ -H "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) Gecko/20091102 Firefox/3.5.5" \ -H "Accept-Encoding: gzip,deflate" \ diff --git a/sources/CGI/Cgi.cpp b/sources/CGI/Cgi.cpp index ab1cff0..1fd6de5 100644 --- a/sources/CGI/Cgi.cpp +++ b/sources/CGI/Cgi.cpp @@ -6,98 +6,54 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:44:51 by hmohamed #+# #+# */ -/* Updated: 2024/04/20 23:45:03 by hmohamed ### ########.fr */ +/* Updated: 2024/04/25 14:04:56 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ #include "Cgi.hpp" -char **headersToEnv(vsp &headers) -{ - std::vector envVector; - - // Iterate through headers - for (vsp::iterator it = headers.begin(); it != headers.end(); ++it) - { - size_t len = it->first.size() + it->second.size() + 2; - char *envEntry = new char[len]; - std::snprintf(envEntry, len, "%s=%s", it->first.c_str(), it->second.c_str()); - envVector.push_back(envEntry); - } - - // std::string gateway_interface = "GATEWAY_INTERFACE=CGI/1.1"; - // envVector.push_back(const_cast(gateway_interface.c_str())); - - - // "REQUEST_METHOD" - // "CONTENT_LENGTH" - // "CONTENT_TYPE" - // "PATH_INFO" - // "PATH_TRANSLATED" - // "QUERY_STRING" - // "REMOTEaddr" - // "REMOTE_IDENT" - // "REMOTE_USER" - // "REQUEST_URI" - // "SERVER_NAME" - // "SERVER_PORT" - // "SERVER_PROTOCOL" = "HTTP/1.1"; - // "SERVER_SOFTWARE" - - // Allocate memory for char* array - char **envp = new char *[envVector.size() + 1]; - - // Copy pointers from vector to char* array - for (size_t i = 0; i < envVector.size(); ++i) - { - envp[i] = envVector[i]; - } - envp[envVector.size()] = NULL; - - return envp; -} - -string geturi(string res) +string getStingQuery(string res) { string resn; size_t qu; + size_t length; qu = res.find('?', 0); - resn = res.substr(0, qu); + length = res.length(); + cout<< "the ? :" << qu <(res.substr(0,qu).c_str()); cout << resn << endl; - cout<< "test" << qu <(res.substr(0,qu).c_str()); cout << resn << endl; + cout<< "test" << qu <(request.get_resource().c_str()); + filePath = (geturi(res)); headers = request.get_headers(); - environment = headersToEnv(headers); + environment = headersToEnv(request, res, config); + timer = request.timer; //filePath = const_cast(geturi(request.get_resource())->c_str()); - filePath = (geturi(res)); - queryString = getStingQuery(res); + //queryString = getStingQuery(res); //filePath = const_cast (request.get_resource().c_str()); // // Check if the Python script exists // if (access(filePath, X_OK) == -1) @@ -125,6 +81,72 @@ Cgi::~Cgi() delete[] arguments; } +char ** Cgi::headersToEnv(const Request &request, const string res, const ServerConfig &config) +{ + std::vector envVector; + + (void)config; + (void)request; + + string queryString = getStingQuery(res);; + // Iterate through headers + for (vsp::iterator it = headers.begin(); it != headers.end(); ++it) + { + size_t len = it->first.size() + it->second.size() + 2; + char *envEntry = new char[len]; + std::snprintf(envEntry, len, "%s=%s", it->first.c_str(), it->second.c_str()); + envVector.push_back(envEntry); + } + + // std::string gateway_interface = "GATEWAY_INTERFACE=CGI/1.1"; + // envVector.push_back(const_cast(gateway_interface.c_str())); + + envVector.push_back(strdup("GATEWAY_INTERFACE=CGI/1.1")); + envVector.push_back(strdup("SERVER_PROTOCOL=HTTP/1.1")); + string val; + val = "SERVER_NAME=" + config.serverName; + envVector.push_back(strdup(val.c_str())); + val = "SERVER_SOFTWARE=" + config.serverName + "/1.0"; + envVector.push_back(strdup(val.c_str())); + val = "SERVER_PORT=" + std::to_string(config.listenerPort); + envVector.push_back(strdup(val.c_str())); + val = "REQUEST_URI=" + request.get_resource(); + envVector.push_back(strdup(val.c_str())); + queryString = getStingQuery(res); + // val = "QUERY_STRING=" + queryString; + // envVector.push_back(strdup(val.c_str())); + // "REQUEST_METHOD" + // "PATH_INFO" + // "PATH_TRANSLATED" + // "QUERY_STRING" + // "REMOTEaddr" + // "REMOTE_IDENT" + // "REMOTE_USER" + + + char* token = strtok(const_cast(queryString.c_str()), "&"); + + //cout << token +3 < 0) - { - res_body.append(buffer, bytesRead); + // Check if the child process has finished + if (waitpid(id, &status, WNOHANG) == id) { + // Child process has finished + // Read from the pipe + char buffer[1024]; + ssize_t bytesRead = read(fd[0], buffer, sizeof(buffer)); + if (bytesRead > 0) { + res_body.append(buffer, bytesRead); + } else if (bytesRead == 0) { + break; + } else { + // Error while reading from the pipe + if (errno == EAGAIN || errno == EWOULDBLOCK) { + // No data available yet, continue waiting + usleep(100000); + continue; + } else { + std::cerr << "Error reading from pipe: " << strerror(errno) << std::endl; + break; + } + } + break; + } + usleep(100000); } + close(fd[0]); return (res_body); diff --git a/sources/CGI/Cgi.hpp b/sources/CGI/Cgi.hpp index f5fae1b..dbc2c31 100644 --- a/sources/CGI/Cgi.hpp +++ b/sources/CGI/Cgi.hpp @@ -6,7 +6,7 @@ /* By: hmohamed +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 12:42:45 by hmohamed #+# #+# */ -/* Updated: 2024/04/20 22:59:34 by hmohamed ### ########.fr */ +/* Updated: 2024/04/25 13:04:14 by hmohamed ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,9 @@ #include "Request.hpp" #include "webserv.hpp" #include "Request.hpp" +#include "TimeOut.hpp" #include "Server.hpp" +#include "signal.h" class Cgi { public: @@ -25,6 +27,8 @@ class Cgi { ~Cgi(); void execute(const std::string &outputFile); string execute(void); + char **headersToEnv(const Request &request, const string res, const ServerConfig &config); + TimeOut timer; private: vsp headers; diff --git a/sources/http/handler/GetRequestHandler.cpp b/sources/http/handler/GetRequestHandler.cpp index 595e532..2b1265a 100644 --- a/sources/http/handler/GetRequestHandler.cpp +++ b/sources/http/handler/GetRequestHandler.cpp @@ -50,6 +50,20 @@ bool check_cgi_request(string res) } +//fuction to get url without the query string +string geturis(string res) +{ + string resn; + size_t qu; + + qu = res.find('?', 0); + resn = res.substr(0, qu); + //result = const_cast(res.substr(0,qu).c_str()); + cout << resn << endl; + cout<< "test" << qu < GetRequestHandler::get_resource(const Request& request, @@ -59,15 +73,15 @@ const vector GetRequestHandler::get_resource(const Request& request, string resource = request.get_resource(); string defaultPage = config.serverRoot + "/"; vector body; + string plain_res = geturis(resource); add_header(make_pair("Server", config.serverName)); - ifstream resource_file; size_t resource_size = 0; status = OK; - if (resource == defaultPage) { + if (plain_res == defaultPage) { body = cachedPages->home.data; add_header( make_pair("Content-Type", cachedPages->home.contentType)); @@ -76,7 +90,7 @@ const vector GetRequestHandler::get_resource(const Request& request, ws_itoa(cachedPages->home.contentLength))); } else { - resource_file.open(resource.c_str(), std::ios_base::binary); + resource_file.open(plain_res.c_str(), std::ios_base::binary); if (resource_file.fail()) { status = NOT_FOUND; add_header(make_pair("Content-Type", @@ -86,11 +100,10 @@ const vector GetRequestHandler::get_resource(const Request& request, body = cachedPages->notFound.data; } else { - string resource_type = find_resource_type(resource); + string resource_type = find_resource_type(plain_res); if (resource_type.length() != 0) add_header(make_pair("Content-Type", resource_type)); - - if (check_cgi_request(resource)) { + if (check_cgi_request(plain_res)) { Cgi cgi(request, config); string result; From f0c4011c222195e74893734fcc4c9c73d8c6df58 Mon Sep 17 00:00:00 2001 From: Taanviir <66136914+Taanviir@users.noreply.github.com> Date: Fri, 26 Apr 2024 18:40:42 +0400 Subject: [PATCH 09/10] fix: compile fixes --- .github/workflows/compile-check.yml | 7 +- sources/CGI/Cgi.cpp | 92 +++++++++---------- .../test_files/location_check/normal.conf | 19 ++++ test.conf | 21 +++++ 4 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 sources/parser/.tests/test_files/location_check/normal.conf create mode 100644 test.conf diff --git a/.github/workflows/compile-check.yml b/.github/workflows/compile-check.yml index 20e48ff..aff5a69 100644 --- a/.github/workflows/compile-check.yml +++ b/.github/workflows/compile-check.yml @@ -15,7 +15,7 @@ jobs: uses: actions/checkout@v3 - name: Set up C++ environment - uses: aminya/setup-cpp@v1 + uses: egor-tensin/setup-gcc@v1.3 with: compiler: gcc @@ -30,10 +30,5 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - - name: Set up C++ environment - uses: aminya/setup-cpp@v1 - with: - compiler: gcc - - name: Build project run: make diff --git a/sources/CGI/Cgi.cpp b/sources/CGI/Cgi.cpp index 8320dd6..a3c7cc5 100644 --- a/sources/CGI/Cgi.cpp +++ b/sources/CGI/Cgi.cpp @@ -279,49 +279,49 @@ string Cgi::execute(void) return (res_body); } -static char** headers_to_env(const vsp& headers) -{ - vector envVector; - - // Iterate through headers - vsp::const_iterator end = headers.end(); - for (vsp::const_iterator it = headers.begin(); it != end; ++it) { - size_t len = it->first.size() + it->second.size() + 2; - char* envEntry = new char[len]; - std::snprintf(envEntry, len, "%s=%s", it->first.c_str(), it->second.c_str()); - envVector.push_back(envEntry); - } - - // Allocate memory for char* array - char** envp = new char*[envVector.size() + 1]; - - // Copy pointers from vector to char* array - for (size_t i = 0; i < envVector.size(); ++i) - envp[i] = envVector[i]; - envp[envVector.size()] = NULL; - - return envp; -} - -static string get_uri(string res) -{ - string resn; - size_t qu; - - qu = res.find('?', 0); - resn = res.substr(0, qu); - // result = const_cast(res.substr(0,qu).c_str()); - return (resn); -} - -static string get_query_string(string res) -{ - string resn; - size_t qu; - size_t length; - - qu = res.find('?', 0); - length = res.length(); - resn = res.substr(qu + 1, length); - return (resn); -} +// static char** headers_to_env(const vsp& headers) +// { +// vector envVector; + +// // Iterate through headers +// vsp::const_iterator end = headers.end(); +// for (vsp::const_iterator it = headers.begin(); it != end; ++it) { +// size_t len = it->first.size() + it->second.size() + 2; +// char* envEntry = new char[len]; +// std::snprintf(envEntry, len, "%s=%s", it->first.c_str(), it->second.c_str()); +// envVector.push_back(envEntry); +// } + +// // Allocate memory for char* array +// char** envp = new char*[envVector.size() + 1]; + +// // Copy pointers from vector to char* array +// for (size_t i = 0; i < envVector.size(); ++i) +// envp[i] = envVector[i]; +// envp[envVector.size()] = NULL; + +// return envp; +// } + +// static string get_uri(string res) +// { +// string resn; +// size_t qu; + +// qu = res.find('?', 0); +// resn = res.substr(0, qu); +// // result = const_cast(res.substr(0,qu).c_str()); +// return (resn); +// } + +// static string get_query_string(string res) +// { +// string resn; +// size_t qu; +// size_t length; + +// qu = res.find('?', 0); +// length = res.length(); +// resn = res.substr(qu + 1, length); +// return (resn); +// } diff --git a/sources/parser/.tests/test_files/location_check/normal.conf b/sources/parser/.tests/test_files/location_check/normal.conf new file mode 100644 index 0000000..91f16fe --- /dev/null +++ b/sources/parser/.tests/test_files/location_check/normal.conf @@ -0,0 +1,19 @@ +http { + server { + listen 8080; + client_max_body_size 1M; + server_name localhost; + + root ./.resources/sample_pages; + index index.html index.htm; + + location { + root ./.resources/sample_pages; + } + + error_page 404 /404.html; + location = /404.html { + root ./.resources/sample_pages; + } + } +} \ No newline at end of file diff --git a/test.conf b/test.conf new file mode 100644 index 0000000..1fd6a4e --- /dev/null +++ b/test.conf @@ -0,0 +1,21 @@ +events { +} +http { + # server { + # listen 8080; + # client_max_body_size 1M; + # server_name localhost; + + # root ./.resources/sample_pages; + # index index.html index.htm; + + # location { + # root ./.resources/sample_pages; + # } + + # # error_page 404 /404.html; + # # location = /404.html { + # # root ./.resources/sample_pages; + # # } + # } +} From 26527ed09f8b4ea3eddae2ab8a75b8c1cf5c4ede Mon Sep 17 00:00:00 2001 From: Taanviir <66136914+Taanviir@users.noreply.github.com> Date: Fri, 26 Apr 2024 18:42:46 +0400 Subject: [PATCH 10/10] fix: fixed mac compile issue caused by make_pair --- sources/http/handler/GetRequestHandler.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sources/http/handler/GetRequestHandler.cpp b/sources/http/handler/GetRequestHandler.cpp index 2b1265a..c3aad63 100644 --- a/sources/http/handler/GetRequestHandler.cpp +++ b/sources/http/handler/GetRequestHandler.cpp @@ -76,7 +76,7 @@ const vector GetRequestHandler::get_resource(const Request& request, string plain_res = geturis(resource); - add_header(make_pair("Server", config.serverName)); + add_header(make_pair("Server", config.serverName.c_str())); ifstream resource_file; size_t resource_size = 0; @@ -84,7 +84,7 @@ const vector GetRequestHandler::get_resource(const Request& request, if (plain_res == defaultPage) { body = cachedPages->home.data; add_header( - make_pair("Content-Type", cachedPages->home.contentType)); + make_pair("Content-Type", cachedPages->home.contentType.c_str())); add_header(make_pair("Content-Length", ws_itoa(cachedPages->home.contentLength))); @@ -94,7 +94,7 @@ const vector GetRequestHandler::get_resource(const Request& request, if (resource_file.fail()) { status = NOT_FOUND; add_header(make_pair("Content-Type", - cachedPages->notFound.contentType)); + cachedPages->notFound.contentType.c_str())); add_header(make_pair("Content-Length", ws_itoa(cachedPages->notFound.contentLength))); body = cachedPages->notFound.data; @@ -102,7 +102,7 @@ const vector GetRequestHandler::get_resource(const Request& request, else { string resource_type = find_resource_type(plain_res); if (resource_type.length() != 0) - add_header(make_pair("Content-Type", resource_type)); + add_header(make_pair("Content-Type", resource_type.c_str())); if (check_cgi_request(plain_res)) { Cgi cgi(request, config); string result;