follow redirects is optional

This commit is contained in:
Benjamin Sergeant 2019-02-25 22:01:04 -08:00
parent 069eccf415
commit 79f601ac65
4 changed files with 55 additions and 11 deletions

View File

@ -38,6 +38,7 @@ namespace ix
const std::string& verb, const std::string& verb,
const WebSocketHttpHeaders& extraHeaders, const WebSocketHttpHeaders& extraHeaders,
const HttpParameters& httpParameters, const HttpParameters& httpParameters,
bool followRedirects,
bool verbose) bool verbose)
{ {
int code = 0; int code = 0;
@ -188,18 +189,22 @@ namespace ix
} }
// Redirect ? // Redirect ?
if (code == 301) if (code == 301 && followRedirects)
{ {
if (headers.find("location") == headers.end()) if (headers.find("location") == headers.end())
{ {
code = 0; // 0 ? code = 0; // 0 ?
std::string errorMsg("Missing location header for redirect"); std::string errorMsg("Missing location header for redirect");
return std::make_tuple(code, headers, payload, errorMsg); return std::make_tuple(code, headers, payload, errorMsg);
} }
std::string location = headers["location"]; std::string location = headers["location"];
return request(location, verb, extraHeaders, httpParameters, verbose); return request(location, verb, extraHeaders, httpParameters, followRedirects, verbose);
}
if (verb == "HEAD")
{
return std::make_tuple(code, headers, payload, std::string());
} }
// Parse response: // Parse response:
@ -291,18 +296,35 @@ namespace ix
HttpResponse HttpClient::get( HttpResponse HttpClient::get(
const std::string& url, const std::string& url,
const WebSocketHttpHeaders& extraHeaders, const WebSocketHttpHeaders& extraHeaders,
bool followRedirects,
bool verbose) bool verbose)
{ {
return request(url, "GET", extraHeaders, HttpParameters(), verbose); return request(url, "GET", extraHeaders,
HttpParameters(), followRedirects,
verbose);
} }
HttpResponse HttpClient::post( HttpResponse HttpClient::post(
const std::string& url, const std::string& url,
const WebSocketHttpHeaders& extraHeaders, const WebSocketHttpHeaders& extraHeaders,
const HttpParameters& httpParameters, const HttpParameters& httpParameters,
bool followRedirects,
bool verbose) bool verbose)
{ {
return request(url, "POST", extraHeaders, httpParameters, verbose); return request(url, "POST", extraHeaders,
httpParameters, followRedirects,
verbose);
}
HttpResponse HttpClient::head(
const std::string& url,
const WebSocketHttpHeaders& extraHeaders,
bool followRedirects,
bool verbose)
{
return request(url, "HEAD", extraHeaders,
HttpParameters(), followRedirects,
verbose);
} }
std::string HttpClient::urlEncode(const std::string& value) std::string HttpClient::urlEncode(const std::string& value)

View File

@ -30,10 +30,18 @@ namespace ix
// Static methods ? // Static methods ?
HttpResponse get(const std::string& url, HttpResponse get(const std::string& url,
const WebSocketHttpHeaders& extraHeaders, const WebSocketHttpHeaders& extraHeaders,
bool followRedirects,
bool verbose); bool verbose);
HttpResponse post(const std::string& url, HttpResponse post(const std::string& url,
const WebSocketHttpHeaders& extraHeaders, const WebSocketHttpHeaders& extraHeaders,
const HttpParameters& httpParameters, const HttpParameters& httpParameters,
bool followRedirects,
bool verbose);
HttpResponse head(const std::string& url,
const WebSocketHttpHeaders& extraHeaders,
bool followRedirects,
bool verbose); bool verbose);
private: private:
@ -41,6 +49,7 @@ namespace ix
const std::string& verb, const std::string& verb,
const WebSocketHttpHeaders& extraHeaders, const WebSocketHttpHeaders& extraHeaders,
const HttpParameters& httpParameters, const HttpParameters& httpParameters,
bool followRedirects,
bool verbose); bool verbose);
std::string urlEncode(const std::string& value); std::string urlEncode(const std::string& value);

View File

@ -20,7 +20,8 @@ namespace ix
int ws_http_client_main(const std::string& url, int ws_http_client_main(const std::string& url,
const std::string& headers, const std::string& headers,
const std::string& data, const std::string& data,
bool headersOnly); bool headersOnly,
bool followRedirects);
int ws_ping_pong_main(const std::string& url); int ws_ping_pong_main(const std::string& url);
@ -53,6 +54,7 @@ int main(int argc, char** argv)
std::string data; std::string data;
std::string headers; std::string headers;
bool headersOnly = false; bool headersOnly = false;
bool followRedirects = false;
int port = 8080; int port = 8080;
CLI::App* sendApp = app.add_subcommand("send", "Send a file"); CLI::App* sendApp = app.add_subcommand("send", "Send a file");
@ -88,6 +90,7 @@ int main(int argc, char** argv)
httpClientApp->add_option("-F", data, "Form data")->join(); httpClientApp->add_option("-F", data, "Form data")->join();
httpClientApp->add_option("-H", headers, "Header")->join(); httpClientApp->add_option("-H", headers, "Header")->join();
httpClientApp->add_flag("-I", headersOnly, "Header"); httpClientApp->add_flag("-I", headersOnly, "Header");
httpClientApp->add_flag("-L", followRedirects, "Header");
CLI11_PARSE(app, argc, argv); CLI11_PARSE(app, argc, argv);
@ -129,7 +132,7 @@ int main(int argc, char** argv)
else if (app.got_subcommand("http_client")) else if (app.got_subcommand("http_client"))
{ {
std::cout << "data: " << data << std::endl; std::cout << "data: " << data << std::endl;
return ix::ws_http_client_main(url, headers, data, headersOnly); return ix::ws_http_client_main(url, headers, data, headersOnly, followRedirects);
} }
return 1; return 1;

View File

@ -68,7 +68,8 @@ namespace ix
int ws_http_client_main(const std::string& url, int ws_http_client_main(const std::string& url,
const std::string& headersData, const std::string& headersData,
const std::string& data, const std::string& data,
bool headersOnly) bool headersOnly,
bool followRedirects)
{ {
HttpParameters httpParameters = parsePostParameters(data); HttpParameters httpParameters = parsePostParameters(data);
WebSocketHttpHeaders headers = parseHeaders(headersData); WebSocketHttpHeaders headers = parseHeaders(headersData);
@ -76,13 +77,22 @@ namespace ix
HttpClient httpClient; HttpClient httpClient;
bool verbose = true; bool verbose = true;
HttpResponse out; HttpResponse out;
if (data.empty()) if (headersOnly)
{ {
out = httpClient.get(url, headers, verbose); out = httpClient.head(url, headers,
followRedirects, verbose);
}
else if (data.empty())
{
out = httpClient.get(url, headers,
followRedirects, verbose);
} }
else else
{ {
out = httpClient.post(url, headers, httpParameters, verbose); out = httpClient.post(url, headers,
httpParameters,
followRedirects,
verbose);
} }
auto errorCode = std::get<0>(out); auto errorCode = std::get<0>(out);
auto responseHeaders = std::get<1>(out); auto responseHeaders = std::get<1>(out);