Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include "request.hh"
  2. #include "types.hh"
  3. #include <string>
  4. #include <ctime>
  5. #include <chrono>
  6. #include <filesystem>
  7. #include <sstream>
  8. #include "vhost/vhost.hh"
  9. #include "../config/config.hh"
  10. #include "response.hh"
  11.  
  12. namespace http
  13. {
  14. Response::Response(Request& requete, STATUS_CODE c, std::shared_ptr<VHost> vost)
  15. {
  16. std::string full_response;
  17. std::string bdy;
  18. int g = (int) c;
  19. g++;
  20. if (requete.Method == GET || requete.Method == POST)
  21. {
  22. std::string outputstring;
  23. std::ifstream o;
  24.  
  25. outputstring.append(vost->conf_get().root);
  26. outputstring.append(requete.URI);
  27. o.open(outputstring);
  28. if (requete.responseRequest == UPGRADE_REQUIRED)
  29. {
  30. requete.responseRequest = UPGRADE_REQUIRED;
  31. }
  32. else if (requete.responseRequest == BAD_REQUEST)
  33. {
  34. requete.responseRequest = BAD_REQUEST;
  35. }
  36. else if (!std::filesystem::exists(outputstring))
  37. {
  38. requete.responseRequest = NOT_FOUND;
  39. }
  40. else if (o.fail())
  41. {
  42. requete.responseRequest = FORBIDDEN;
  43. }
  44. else
  45. {
  46. std::stringstream buffer;
  47. buffer << o.rdbuf();
  48. bdy.append(buffer.str());
  49. }
  50. }
  51. full_response.append(requete.Version);
  52. full_response += ' ';
  53. full_response.append(std::to_string((int) requete.responseRequest));
  54. full_response += ' ';
  55. full_response.append(statusCode(requete.responseRequest).second);
  56. addCRLF(full_response);
  57.  
  58. if (is_finit_request(requete.responseRequest))
  59. {
  60. server_response = full_response;
  61. return;
  62. }
  63.  
  64. //ADDING THE HEADERS;
  65. auto end = std::chrono::system_clock::now();
  66. std::time_t end_time = std::chrono::system_clock::to_time_t(end);
  67. full_response.append("Date: ");
  68. std::string cdate = std::string(std::ctime(&end_time));
  69. cdate.pop_back();
  70. cdate.pop_back();
  71. full_response.append(cdate);
  72. addCRLF(full_response);
  73. if (requete.responseRequest == FORBIDDEN)
  74. {
  75. server_response = full_response;
  76. return;
  77. }
  78.  
  79. full_response.append("Server: Spider Web Server");
  80. addCRLF(full_response);
  81.  
  82.  
  83. addCRLF(full_response);
  84.  
  85. if (requete.Method != HEAD)
  86. full_response.append(bdy);
  87. server_response = full_response;
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement