cleanup
This commit is contained in:
24
ws/ws.cpp
24
ws/ws.cpp
@ -33,7 +33,9 @@ int main(int argc, char** argv)
|
||||
bool save = false;
|
||||
bool compress = false;
|
||||
int port = 8080;
|
||||
int connectTimeOutSeconds = 3;
|
||||
int connectTimeOut = 60;
|
||||
int transferTimeout = 1800;
|
||||
int maxRedirects = 5;
|
||||
|
||||
CLI::App* sendApp = app.add_subcommand("send", "Send a file");
|
||||
sendApp->add_option("url", url, "Connection url")->required();
|
||||
@ -68,12 +70,14 @@ int main(int argc, char** argv)
|
||||
httpClientApp->add_option("-F", data, "Form data")->join();
|
||||
httpClientApp->add_option("-H", headers, "Header")->join();
|
||||
httpClientApp->add_option("--output", output, "Output file");
|
||||
httpClientApp->add_flag("-I", headersOnly, "Header");
|
||||
httpClientApp->add_flag("-L", followRedirects, "Header");
|
||||
httpClientApp->add_flag("-I", headersOnly, "Send a HEAD request");
|
||||
httpClientApp->add_flag("-L", followRedirects, "Follow redirects");
|
||||
httpClientApp->add_option("--max-redirects", maxRedirects, "Max Redirects");
|
||||
httpClientApp->add_flag("-v", verbose, "Verbose");
|
||||
httpClientApp->add_flag("-O", save, "Save to disk");
|
||||
httpClientApp->add_flag("--compress", compress, "gzip compression");
|
||||
httpClientApp->add_option("--connect-timeout", connectTimeOutSeconds, "Connection timeout");
|
||||
httpClientApp->add_flag("-O", save, "Save output to disk");
|
||||
httpClientApp->add_flag("--compress", compress, "Enable gzip compression");
|
||||
httpClientApp->add_option("--connect-timeout", connectTimeOut, "Connection timeout");
|
||||
httpClientApp->add_option("--transfer-timeout", transferTimeout, "Transfer timeout");
|
||||
|
||||
CLI11_PARSE(app, argc, argv);
|
||||
|
||||
@ -114,10 +118,10 @@ int main(int argc, char** argv)
|
||||
}
|
||||
else if (app.got_subcommand("curl"))
|
||||
{
|
||||
return ix::ws_http_client_main(url, headers, data,
|
||||
headersOnly, connectTimeOutSeconds,
|
||||
followRedirects, verbose, save, output,
|
||||
compress);
|
||||
return ix::ws_http_client_main(url, headers, data, headersOnly,
|
||||
connectTimeOut, transferTimeout,
|
||||
followRedirects, maxRedirects, verbose,
|
||||
save, output, compress);
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
4
ws/ws.h
4
ws/ws.h
@ -13,8 +13,10 @@ namespace ix
|
||||
const std::string& headers,
|
||||
const std::string& data,
|
||||
bool headersOnly,
|
||||
int timeoutSecs,
|
||||
int connectTimeout,
|
||||
int transferTimeout,
|
||||
bool followRedirects,
|
||||
int maxRedirects,
|
||||
bool verbose,
|
||||
bool save,
|
||||
const std::string& output,
|
||||
|
@ -86,20 +86,27 @@ namespace ix
|
||||
const std::string& headersData,
|
||||
const std::string& data,
|
||||
bool headersOnly,
|
||||
int timeoutSecs,
|
||||
int connectTimeout,
|
||||
int transferTimeout,
|
||||
bool followRedirects,
|
||||
int maxRedirects,
|
||||
bool verbose,
|
||||
bool save,
|
||||
const std::string& output,
|
||||
bool compress)
|
||||
{
|
||||
HttpRequestArgs args;
|
||||
args.url = url;
|
||||
args.extraHeaders = parseHeaders(headersData);
|
||||
args.timeoutSecs = timeoutSecs;
|
||||
args.connectTimeout = connectTimeout;
|
||||
args.transferTimeout = transferTimeout;
|
||||
args.followRedirects = followRedirects;
|
||||
args.maxRedirects = maxRedirects;
|
||||
args.verbose = verbose;
|
||||
args.compress = compress;
|
||||
args.logger = [](const std::string& msg)
|
||||
{
|
||||
std::cout << msg;
|
||||
};
|
||||
|
||||
HttpParameters httpParameters = parsePostParameters(data);
|
||||
|
||||
@ -107,34 +114,40 @@ namespace ix
|
||||
HttpResponse out;
|
||||
if (headersOnly)
|
||||
{
|
||||
out = httpClient.head(args);
|
||||
out = httpClient.head(url, args);
|
||||
}
|
||||
else if (data.empty())
|
||||
{
|
||||
out = httpClient.get(args);
|
||||
out = httpClient.get(url, args);
|
||||
}
|
||||
else
|
||||
{
|
||||
out = httpClient.post(httpParameters, args);
|
||||
out = httpClient.post(url, httpParameters, args);
|
||||
}
|
||||
|
||||
auto errorCode = std::get<0>(out);
|
||||
auto responseHeaders = std::get<1>(out);
|
||||
auto payload = std::get<2>(out);
|
||||
auto errorMsg = std::get<3>(out);
|
||||
auto statusCode = std::get<0>(out);
|
||||
auto errorCode = std::get<1>(out);
|
||||
auto responseHeaders = std::get<2>(out);
|
||||
auto payload = std::get<3>(out);
|
||||
auto errorMsg = std::get<4>(out);
|
||||
auto uploadSize = std::get<5>(out);
|
||||
auto downloadSize = std::get<6>(out);
|
||||
|
||||
for (auto it : responseHeaders)
|
||||
{
|
||||
std::cerr << it.first << ": " << it.second << std::endl;
|
||||
}
|
||||
|
||||
std::cerr << "error code: " << errorCode << std::endl;
|
||||
if (errorCode != 200)
|
||||
std::cerr << "Upload size: " << uploadSize << std::endl;
|
||||
std::cerr << "Download size: " << downloadSize << std::endl;
|
||||
|
||||
std::cerr << "Status: " << statusCode << std::endl;
|
||||
if (errorCode != HttpErrorCode_Ok)
|
||||
{
|
||||
std::cerr << "error message: " << errorMsg << std::endl;
|
||||
}
|
||||
|
||||
if (!headersOnly && errorCode == 200)
|
||||
if (!headersOnly && errorCode == HttpErrorCode_Ok)
|
||||
{
|
||||
if (save || !output.empty())
|
||||
{
|
||||
|
Reference in New Issue
Block a user