HTTPRequest.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <sstream>
  2. #include <string>
  3. #include <memory>
  4. #include "HTTPRequest.h"
  5. #include "PlaintextConnection.h"
  6. HTTPRequest::HTTPRequest(ConnectionFactory factory)
  7. : factory(factory)
  8. {
  9. }
  10. HTTPSClient::Reply HTTPRequest::request(const HTTPSClient::Request &req)
  11. {
  12. HTTPSClient::Reply reply;
  13. reply.responseCode = 400;
  14. auto info = parseUrl(req.url);
  15. if (!info.valid)
  16. return reply;
  17. std::unique_ptr<Connection> conn;
  18. if (info.schema == "http")
  19. conn.reset(new PlaintextConnection());
  20. else if (info.schema == "https")
  21. conn.reset(factory());
  22. else
  23. throw std::runtime_error("Unknown url schema");
  24. if (!conn->connect(info.hostname, info.port))
  25. return reply;
  26. // Build the request
  27. {
  28. std::stringstream request;
  29. request << (req.method == HTTPSClient::Request::GET ? "GET " : "POST ") << info.query << " HTTP/1.1\r\n";
  30. for (auto &header : req.headers)
  31. request << header.first << ": " << header.second << "\r\n";
  32. request << "Connection: Close\r\n";
  33. request << "Host: " << info.hostname << "\r\n";
  34. if (req.method == HTTPSClient::Request::POST && req.headers.count("Content-Type") == 0)
  35. request << "Content-Type: application/x-www-form-urlencoded\r\n";
  36. if (req.method == HTTPSClient::Request::POST)
  37. request << "Content-Length: " << req.postdata.size() << "\r\n";
  38. request << "\r\n";
  39. if (req.method == HTTPSClient::Request::POST)
  40. request << req.postdata;
  41. // Send it
  42. std::string requestData = request.str();
  43. conn->write(requestData.c_str(), requestData.size());
  44. }
  45. // Now receive the reply
  46. std::stringstream response;
  47. {
  48. char buffer[8192];
  49. while (true)
  50. {
  51. size_t read = conn->read(buffer, sizeof(buffer));
  52. response.write(buffer, read);
  53. if (read == 0)
  54. break;
  55. }
  56. conn->close();
  57. }
  58. reply.responseCode = 500;
  59. // And parse it
  60. {
  61. std::string protocol;
  62. response >> protocol;
  63. if (protocol != "HTTP/1.1")
  64. return reply;
  65. response >> reply.responseCode;
  66. response.ignore(1, '\n');
  67. for (std::string line; getline(response, line, '\n') && line != "\r"; )
  68. {
  69. auto sep = line.find(':');
  70. reply.headers[line.substr(0, sep)] = line.substr(sep+1, line.size()-sep-1);
  71. }
  72. auto begin = std::istreambuf_iterator<char>(response);
  73. auto end = std::istreambuf_iterator<char>();
  74. reply.body = std::string(begin, end);
  75. }
  76. return reply;
  77. }
  78. HTTPRequest::DissectedURL HTTPRequest::parseUrl(const std::string &url)
  79. {
  80. DissectedURL dis;
  81. dis.valid = false;
  82. // Schema
  83. auto schemaStart = 0;
  84. auto schemaEnd = url.find("://");
  85. dis.schema = url.substr(schemaStart, schemaEnd-schemaStart);
  86. // Auth+Hostname+Port
  87. auto connStart = schemaEnd+3;
  88. auto connEnd = url.find('/', connStart);
  89. if (connEnd == std::string::npos)
  90. connEnd = url.size();
  91. // TODO: Auth
  92. if (url.find("@", connStart, connEnd-connStart) != std::string::npos)
  93. return dis;
  94. // Port
  95. auto portStart = url.find(':', connStart);
  96. auto portEnd = connEnd;
  97. if (portStart == std::string::npos || portStart > portEnd)
  98. {
  99. dis.port = dis.schema == "http" ? 80 : 443;
  100. portStart = portEnd;
  101. }
  102. else
  103. dis.port = std::stoi(url.substr(portStart+1, portEnd-portStart-1));
  104. // Hostname
  105. auto hostnameStart = connStart;
  106. auto hostnameEnd = portStart;
  107. dis.hostname = url.substr(hostnameStart, hostnameEnd-hostnameStart);
  108. // And the query
  109. dis.query = url.substr(connEnd);
  110. if (dis.query.size() == 0)
  111. dis.query = "/";
  112. dis.valid = true;
  113. return dis;
  114. }