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 WebSocketHttpHeaders& extraHeaders,
const HttpParameters& httpParameters,
bool followRedirects,
bool verbose)
{
int code = 0;
@ -188,18 +189,22 @@ namespace ix
}
// Redirect ?
if (code == 301)
if (code == 301 && 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);
}
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:
@ -291,18 +296,35 @@ namespace ix
HttpResponse HttpClient::get(
const std::string& url,
const WebSocketHttpHeaders& extraHeaders,
bool followRedirects,
bool verbose)
{
return request(url, "GET", extraHeaders, HttpParameters(), verbose);
return request(url, "GET", extraHeaders,
HttpParameters(), followRedirects,
verbose);
}
HttpResponse HttpClient::post(
const std::string& url,
const WebSocketHttpHeaders& extraHeaders,
const HttpParameters& httpParameters,
bool followRedirects,
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)

View File

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

View File

@ -20,7 +20,8 @@ namespace ix
int ws_http_client_main(const std::string& url,
const std::string& headers,
const std::string& data,
bool headersOnly);
bool headersOnly,
bool followRedirects);
int ws_ping_pong_main(const std::string& url);
@ -53,6 +54,7 @@ int main(int argc, char** argv)
std::string data;
std::string headers;
bool headersOnly = false;
bool followRedirects = false;
int port = 8080;
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("-H", headers, "Header")->join();
httpClientApp->add_flag("-I", headersOnly, "Header");
httpClientApp->add_flag("-L", followRedirects, "Header");
CLI11_PARSE(app, argc, argv);
@ -129,7 +132,7 @@ int main(int argc, char** argv)
else if (app.got_subcommand("http_client"))
{
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;

View File

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