HTTPRequest.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. std::string method = req.method;
  32. bool hasData = req.postdata.length() > 0;
  33. if (method.length() == 0)
  34. method = hasData ? "POST" : "GET";
  35. request << method << " " << info.query << " HTTP/1.1\r\n";
  36. for (auto &header : req.headers)
  37. request << header.first << ": " << header.second << "\r\n";
  38. request << "Connection: Close\r\n";
  39. request << "Host: " << info.hostname << "\r\n";
  40. if (hasData)
  41. request << "Content-Length: " << req.postdata.size() << "\r\n";
  42. request << "\r\n";
  43. if (hasData)
  44. request << req.postdata;
  45. // Send it
  46. std::string requestData = request.str();
  47. conn->write(requestData.c_str(), requestData.size());
  48. }
  49. // Now receive the reply
  50. std::stringstream response;
  51. {
  52. char buffer[8192];
  53. while (true)
  54. {
  55. size_t read = conn->read(buffer, sizeof(buffer));
  56. response.write(buffer, read);
  57. if (read == 0)
  58. break;
  59. }
  60. conn->close();
  61. }
  62. reply.responseCode = 500;
  63. // And parse it
  64. {
  65. std::string protocol;
  66. response >> protocol;
  67. if (protocol != "HTTP/1.1")
  68. return reply;
  69. response >> reply.responseCode;
  70. response.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  71. for (std::string line; getline(response, line, '\n') && line != "\r"; )
  72. {
  73. auto sep = line.find(':');
  74. reply.headers[line.substr(0, sep)] = line.substr(sep+1, line.size()-sep-1);
  75. }
  76. auto begin = std::istreambuf_iterator<char>(response);
  77. auto end = std::istreambuf_iterator<char>();
  78. reply.body = std::string(begin, end);
  79. }
  80. return reply;
  81. }
  82. HTTPRequest::DissectedURL HTTPRequest::parseUrl(const std::string &url)
  83. {
  84. DissectedURL dis;
  85. dis.valid = false;
  86. // Schema
  87. auto schemaStart = 0;
  88. auto schemaEnd = url.find("://");
  89. dis.schema = url.substr(schemaStart, schemaEnd-schemaStart);
  90. // Auth+Hostname+Port
  91. auto connStart = schemaEnd+3;
  92. auto connEnd = url.find('/', connStart);
  93. if (connEnd == std::string::npos)
  94. connEnd = url.size();
  95. // TODO: Auth
  96. if (url.find("@", connStart, connEnd-connStart) != std::string::npos)
  97. return dis;
  98. // Port
  99. auto portStart = url.find(':', connStart);
  100. auto portEnd = connEnd;
  101. if (portStart == std::string::npos || portStart > portEnd)
  102. {
  103. dis.port = dis.schema == "http" ? 80 : 443;
  104. portStart = portEnd;
  105. }
  106. else
  107. dis.port = std::stoi(url.substr(portStart+1, portEnd-portStart-1));
  108. // Hostname
  109. auto hostnameStart = connStart;
  110. auto hostnameEnd = portStart;
  111. dis.hostname = url.substr(hostnameStart, hostnameEnd-hostnameStart);
  112. // And the query
  113. dis.query = url.substr(connEnd);
  114. if (dis.query.size() == 0)
  115. dis.query = "/";
  116. dis.valid = true;
  117. return dis;
  118. }