Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "request.hh"
- #include "types.hh"
- #include <string>
- #include <ctime>
- #include <chrono>
- #include <filesystem>
- #include <sstream>
- #include "vhost/vhost.hh"
- #include "../config/config.hh"
- #include "response.hh"
- namespace http
- {
- Response::Response(Request& requete, STATUS_CODE c, std::shared_ptr<VHost> vost)
- {
- std::string full_response;
- std::string bdy;
- int g = (int) c;
- g++;
- if (requete.Method == GET || requete.Method == POST)
- {
- std::string outputstring;
- std::ifstream o;
- outputstring.append(vost->conf_get().root);
- outputstring.append(requete.URI);
- o.open(outputstring);
- if (requete.responseRequest == UPGRADE_REQUIRED)
- {
- requete.responseRequest = UPGRADE_REQUIRED;
- }
- else if (requete.responseRequest == BAD_REQUEST)
- {
- requete.responseRequest = BAD_REQUEST;
- }
- else if (!std::filesystem::exists(outputstring))
- {
- requete.responseRequest = NOT_FOUND;
- }
- else if (o.fail())
- {
- requete.responseRequest = FORBIDDEN;
- }
- else
- {
- std::stringstream buffer;
- buffer << o.rdbuf();
- bdy.append(buffer.str());
- }
- }
- full_response.append(requete.Version);
- full_response += ' ';
- full_response.append(std::to_string((int) requete.responseRequest));
- full_response += ' ';
- full_response.append(statusCode(requete.responseRequest).second);
- addCRLF(full_response);
- if (is_finit_request(requete.responseRequest))
- {
- server_response = full_response;
- return;
- }
- //ADDING THE HEADERS;
- auto end = std::chrono::system_clock::now();
- std::time_t end_time = std::chrono::system_clock::to_time_t(end);
- full_response.append("Date: ");
- std::string cdate = std::string(std::ctime(&end_time));
- cdate.pop_back();
- cdate.pop_back();
- full_response.append(cdate);
- addCRLF(full_response);
- if (requete.responseRequest == FORBIDDEN)
- {
- server_response = full_response;
- return;
- }
- full_response.append("Server: Spider Web Server");
- addCRLF(full_response);
- addCRLF(full_response);
- if (requete.Method != HEAD)
- full_response.append(bdy);
- server_response = full_response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement