IXWebSocket/ixwebsocket/IXHttpClient.cpp

414 lines
12 KiB
C++
Raw Normal View History

/*
* IXHttpClient.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#include "IXHttpClient.h"
2019-02-15 05:11:42 +01:00
#include "IXUrlParser.h"
#include "IXWebSocketHttpHeaders.h"
#include "IXSocketFactory.h"
#include <iostream>
2019-02-15 05:11:42 +01:00
#include <sstream>
#include <iomanip>
2019-02-15 05:11:42 +01:00
#include <vector>
2019-02-28 03:02:45 +01:00
#include <zlib.h>
namespace ix
{
HttpClient::HttpClient()
{
}
HttpClient::~HttpClient()
{
}
HttpResponse HttpClient::request(
const std::string& verb,
const std::string& body,
HttpRequestArgs args)
{
int code = 0;
WebSocketHttpHeaders headers;
std::string payload;
2019-02-15 05:11:42 +01:00
std::string protocol, host, path, query;
int port;
if (!parseUrl(args.url, protocol, host, path, query, port))
2019-02-15 05:11:42 +01:00
{
std::stringstream ss;
ss << "Cannot parse url: " << args.url;
return std::make_tuple(code, headers, payload, ss.str());
2019-02-15 05:11:42 +01:00
}
if (protocol != "http" && protocol != "https")
{
std::stringstream ss;
ss << "Invalid protocol: " << protocol
<< " for url " << args.url
<< " . Supported protocols are http and https";
return std::make_tuple(code, headers, payload, ss.str());
}
bool tls = protocol == "https";
std::string errorMsg;
_socket = createSocket(tls, errorMsg);
if (!_socket)
{
return std::make_tuple(code, headers, payload, errorMsg);
}
// Build request string
2019-02-15 05:11:42 +01:00
std::stringstream ss;
ss << verb << " " << path << " HTTP/1.1\r\n";
2019-02-15 05:11:42 +01:00
ss << "Host: " << host << "\r\n";
ss << "User-Agent: ixwebsocket/1.0.0" << "\r\n";
ss << "Accept: */*" << "\r\n";
2019-02-26 02:17:05 +01:00
2019-02-28 03:02:45 +01:00
if (args.compress)
{
ss << "Accept-Encoding: gzip" << "\r\n";
}
// Append extra headers
for (auto&& it : args.extraHeaders)
2019-02-26 02:17:05 +01:00
{
ss << it.first << ": " << it.second << "\r\n";
2019-02-26 02:17:05 +01:00
}
if (verb == "POST")
{
ss << "Content-Length: " << body.size() << "\r\n";
// Set default Content-Type if unspecified
if (args.extraHeaders.find("Content-Type") == args.extraHeaders.end())
{
ss << "Content-Type: application/x-www-form-urlencoded" << "\r\n";
}
ss << "\r\n";
ss << body;
}
else
{
ss << "\r\n";
}
std::string req(ss.str());
2019-02-15 05:11:42 +01:00
std::string errMsg;
std::atomic<bool> requestInitCancellation(false);
auto isCancellationRequested =
makeCancellationRequestWithTimeout(args.timeoutSecs, requestInitCancellation);
2019-02-15 05:11:42 +01:00
bool success = _socket->connect(host, port, errMsg, isCancellationRequested);
if (!success)
{
std::stringstream ss;
ss << "Cannot connect to url: " << args.url;
return std::make_tuple(code, headers, payload, ss.str());
2019-02-15 05:11:42 +01:00
}
if (args.verbose)
2019-02-15 05:11:42 +01:00
{
std::cerr << "Sending " << verb << " request "
<< "to " << host << ":" << port << std::endl
<< "request size: " << req.size() << " bytes" << std::endl
<< "=============" << std::endl
<< req
<< "=============" << std::endl
2019-02-15 05:11:42 +01:00
<< std::endl;
}
if (!_socket->writeBytes(req, isCancellationRequested))
{
2019-02-15 05:11:42 +01:00
std::string errorMsg("Cannot send request");
return std::make_tuple(code, headers, payload, errorMsg);
}
2019-02-15 05:11:42 +01:00
auto lineResult = _socket->readLine(isCancellationRequested);
auto lineValid = lineResult.first;
auto line = lineResult.second;
2019-02-15 05:11:42 +01:00
if (!lineValid)
{
std::string errorMsg("Cannot retrieve status line");
return std::make_tuple(code, headers, payload, errorMsg);
}
2019-02-15 05:11:42 +01:00
if (sscanf(line.c_str(), "HTTP/1.1 %d", &code) != 1)
{
std::string errorMsg("Cannot parse response code from status line");
return std::make_tuple(code, headers, payload, errorMsg);
}
2019-02-15 05:11:42 +01:00
auto result = parseHttpHeaders(_socket, isCancellationRequested);
auto headersValid = result.first;
headers = result.second;
if (!headersValid)
{
code = 0; // 0 ?
std::string errorMsg("Cannot parse http headers");
return std::make_tuple(code, headers, payload, errorMsg);
}
// Redirect ?
// FIXME wrong conditional
if ((code == 301 || code == 308) && args.followRedirects)
{
if (headers.find("location") == headers.end())
{
code = 0; // 0 ?
std::string errorMsg("Missing location header for redirect");
return std::make_tuple(code, headers, payload, errorMsg);
}
// Recurse
std::string location = headers["location"];
return request(verb, body, args);
2019-02-26 07:01:04 +01:00
}
if (verb == "HEAD")
{
return std::make_tuple(code, headers, payload, std::string());
}
2019-02-15 05:11:42 +01:00
// Parse response:
// http://bryce-thomas.blogspot.com/2012/01/technical-parsing-http-to-extract.html
if (headers.find("content-length") != headers.end())
2019-02-15 05:11:42 +01:00
{
ssize_t contentLength = -1;
ss.str("");
ss << headers["content-length"];
ss >> contentLength;
2019-02-15 05:11:42 +01:00
payload.reserve(contentLength);
// FIXME: very inefficient way to read bytes, but it works...
for (int i = 0; i < contentLength; ++i)
{
char c;
if (!_socket->readByte(&c, isCancellationRequested))
{
ss.str("");
ss << "Cannot read byte";
2019-02-28 03:02:45 +01:00
return std::make_tuple(-1, headers, payload, "Cannot read byte");
}
2019-02-15 05:11:42 +01:00
payload += c;
}
2019-02-28 03:02:45 +01:00
std::cout << "I WAS HERE" << std::endl;
}
else if (headers.find("transfer-encoding") != headers.end() &&
headers["transfer-encoding"] == "chunked")
2019-02-15 05:11:42 +01:00
{
std::stringstream ss;
while (true)
2019-02-15 05:11:42 +01:00
{
lineResult = _socket->readLine(isCancellationRequested);
line = lineResult.second;
if (!lineResult.first)
{
code = 0; // 0 ?
std::string errorMsg("Cannot read http body");
return std::make_tuple(code, headers, payload, errorMsg);
}
uint64_t chunkSize;
2019-02-15 05:11:42 +01:00
ss.str("");
ss << std::hex << line;
ss >> chunkSize;
if (args.verbose)
{
std::cerr << "Reading " << chunkSize << " bytes"
<< std::endl;
}
payload.reserve(payload.size() + chunkSize);
// Read another line
for (uint64_t i = 0; i < chunkSize; ++i)
{
char c;
if (!_socket->readByte(&c, isCancellationRequested))
{
ss.str("");
ss << "Cannot read byte";
return std::make_tuple(-1, headers, payload, ss.str());
}
payload += c;
}
lineResult = _socket->readLine(isCancellationRequested);
2019-02-15 05:11:42 +01:00
if (!lineResult.first)
{
code = 0; // 0 ?
std::string errorMsg("Cannot read http body");
return std::make_tuple(code, headers, payload, errorMsg);
}
if (chunkSize == 0) break;
}
}
else if (code == 204)
{
; // 204 is NoContent response code
}
else
{
code = 0; // 0 ?
std::string errorMsg("Cannot read http body");
2019-02-28 03:02:45 +01:00
return std::make_tuple(-1, headers, payload, errorMsg);
}
// If the content was compressed with gzip, decode it
if (headers["Content-Encoding"] == "gzip")
{
if (args.verbose) std::cout << "Decoding gzip..." << std::endl;
std::string decompressedPayload;
if (!gzipInflate(payload, decompressedPayload))
{
std::string errorMsg("Error decompressing payload");
return std::make_tuple(-1, headers, payload, errorMsg);
}
payload = decompressedPayload;
2019-02-15 05:11:42 +01:00
}
return std::make_tuple(code, headers, payload, "");
}
HttpResponse HttpClient::get(HttpRequestArgs args)
{
return request("GET", std::string(), args);
}
HttpResponse HttpClient::head(HttpRequestArgs args)
2019-02-26 07:01:04 +01:00
{
return request("HEAD", std::string(), args);
2019-02-26 07:01:04 +01:00
}
HttpResponse HttpClient::post(const HttpParameters& httpParameters,
HttpRequestArgs args)
{
return request("POST", serializeHttpParameters(httpParameters), args);
}
HttpResponse HttpClient::post(const std::string& body,
HttpRequestArgs args)
{
return request("POST", body, args);
}
std::string HttpClient::urlEncode(const std::string& value)
{
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (std::string::const_iterator i = value.begin(), n = value.end();
i != n; ++i)
{
std::string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
{
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << std::uppercase;
escaped << '%' << std::setw(2) << int((unsigned char) c);
escaped << std::nouppercase;
}
return escaped.str();
}
std::string HttpClient::serializeHttpParameters(const HttpParameters& httpParameters)
{
std::stringstream ss;
size_t count = httpParameters.size();
size_t i = 0;
for (auto&& it : httpParameters)
{
ss << urlEncode(it.first)
<< "="
<< urlEncode(it.second);
if (i++ < (count-1))
{
ss << "&";
}
}
return ss.str();
}
2019-02-28 03:02:45 +01:00
bool HttpClient::gzipInflate(
const std::string& in,
std::string& out)
{
z_stream inflateState;
memset(&inflateState, 0, sizeof(inflateState));
inflateState.zalloc = Z_NULL;
inflateState.zfree = Z_NULL;
inflateState.opaque = Z_NULL;
inflateState.avail_in = 0;
inflateState.next_in = Z_NULL;
if (inflateInit2(&inflateState, 16+MAX_WBITS) != Z_OK)
{
return false;
}
inflateState.avail_in = (uInt) in.size();
inflateState.next_in = (unsigned char *)(const_cast<char *>(in.data()));
const int kBufferSize = 1 << 14;
std::unique_ptr<unsigned char[]> compressBuffer =
std::make_unique<unsigned char[]>(kBufferSize);
do
{
inflateState.avail_out = (uInt) kBufferSize;
inflateState.next_out = compressBuffer.get();
int ret = inflate(&inflateState, Z_SYNC_FLUSH);
if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR)
{
inflateEnd(&inflateState);
return false;
}
out.append(
reinterpret_cast<char *>(compressBuffer.get()),
kBufferSize - inflateState.avail_out
);
} while (inflateState.avail_out == 0);
inflateEnd(&inflateState);
return true;
}
}