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