HTTPRequest.cpp 3.4 KB

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