HTTPRequest.cpp 3.4 KB

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