diff --git a/ixwebsocket/IXHttpClient.cpp b/ixwebsocket/IXHttpClient.cpp index 1646a4ba..6704248f 100644 --- a/ixwebsocket/IXHttpClient.cpp +++ b/ixwebsocket/IXHttpClient.cpp @@ -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) diff --git a/ixwebsocket/IXHttpClient.h b/ixwebsocket/IXHttpClient.h index 1e10a92d..fb719af8 100644 --- a/ixwebsocket/IXHttpClient.h +++ b/ixwebsocket/IXHttpClient.h @@ -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); diff --git a/ws/ws.cpp b/ws/ws.cpp index 91020163..cae206aa 100644 --- a/ws/ws.cpp +++ b/ws/ws.cpp @@ -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; diff --git a/ws/ws_http_client.cpp b/ws/ws_http_client.cpp index a456e74d..4716affc 100644 --- a/ws/ws_http_client.cpp +++ b/ws/ws_http_client.cpp @@ -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);