Some minor improvements to the http request demo.

--HG--
branch : dev
This commit is contained in:
Martín Lucas Golini
2019-04-24 21:56:44 -03:00
parent 8acc2bb39c
commit ece833dd43
4 changed files with 4430 additions and 247 deletions

View File

@@ -1,12 +1,34 @@
#include <eepp/ee.hpp>
#include <args/args.hxx>
EE_MAIN_FUNC int main (int argc, char * argv []) {
args::ArgumentParser parser("HTTP request program example");
args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
args::ValueFlag<std::string> output(parser, "file", "Write to file instead of stdout", {'o', "output"} );
args::Positional<std::string> url(parser, "url", "The url to request");
args::Flag verbose(parser, "verbose", "Prints the request response headers", {'v',"verbose"} );
try {
parser.ParseCLI(argc, argv);
} catch (const args::Help&) {
std::cout << parser;
return 0;
} catch (const args::ParseError& e) {
std::cerr << e.what() << std::endl;
std::cerr << parser;
return 1;
} catch (args::ValidationError& e) {
std::cerr << e.what() << std::endl;
std::cerr << parser;
return 1;
}
{
// Create a new HTTP client
Http http;
Http::Request request;
if ( argc < 2 ) {
if ( !url ) {
// We'll work on en.wikipedia.org
if ( SSLSocket::isSupported() ) {
http.setHost("https://en.wikipedia.org");
@@ -31,7 +53,11 @@ EE_MAIN_FUNC int main (int argc, char * argv []) {
}, asyncRequest, Seconds( 5 ) );
} else {
// If the user provided the URI, creates an instance of URI to parse it.
URI uri( argv[1] );
URI uri( url.Get() );
if ( uri.getScheme().empty() ) {
uri = URI( "http://" + url.Get() );
}
// Set the host and port from the URI
http.setHost( uri.getHost(), uri.getPort() );
@@ -39,31 +65,38 @@ EE_MAIN_FUNC int main (int argc, char * argv []) {
// Set the path and query parts for the request
request.setUri( uri.getPathEtc() );
// Send the request
Http::Response response = http.sendRequest(request);
if ( !output ) {
// Send the request
Http::Response response = http.sendRequest(request);
// Check the status code and display the result
Http::Response::Status status = response.getStatus();
// Check the status code and display the result
Http::Response::Status status = response.getStatus();
if ( status == Http::Response::Ok ) {
Http::Response::FieldTable headers = response.getHeaders();
if ( status == Http::Response::Ok ) {
if ( verbose ) {
Http::Response::FieldTable headers = response.getHeaders();
std::cout << "Headers: " << std::endl;
std::cout << "Headers: " << std::endl;
for ( auto head = headers.begin(); head != headers.end(); ++head ) {
std::cout << "\t" << head->first << ": " << head->second << std::endl;
for ( auto&& head : headers ) {
std::cout << "\t" << head.first << ": " << head.second << std::endl;
}
std::cout << std::endl << "Body: " << std::endl;
}
std::cout << response.getBody() << std::endl;
} else {
std::cout << "Error " << status << std::endl;
}
std::cout << std::endl << "Body: " << std::endl;
std::cout << response.getBody() << std::endl;
} else {
std::cout << "Error " << status << std::endl;
http.downloadRequest(request, output.Get(), Seconds(5));
}
}
}
MemoryManager::showResults();
if ( verbose )
MemoryManager::showResults();
return EXIT_SUCCESS;
}