Skip to content

Instantly share code, notes, and snippets.

@tommetge
Created June 13, 2012 16:49
Show Gist options
  • Save tommetge/2925221 to your computer and use it in GitHub Desktop.
Save tommetge/2925221 to your computer and use it in GitHub Desktop.
mordor http client
// Copyright (c) 2009 - Decho Corp.
#include "mordor/predef.h"
#include <iostream>
#include <boost/bind.hpp>
#include "mordor/config.h"
#include "mordor/exception.h"
#include "mordor/http/broker.h"
#include "mordor/http/client.h"
#include "mordor/iomanager.h"
#include "mordor/log.h"
#include "mordor/socket.h"
#include "mordor/streams/http.h"
#include "mordor/streams/limited.h"
#include "mordor/streams/socket.h"
#include "mordor/streams/ssl.h"
#include "mordor/streams/std.h"
#include "mordor/streams/transfer.h"
#include "tethys/tritonclient.h"
using namespace Mordor;
using namespace Tethys;
static bool authCallback(std::string &scheme, std::string &username,
std::string &password, size_t attempts, const std::string &user,
const std::string &pass)
{
if (attempts != 0)
return false;
scheme = "Basic";
username = user;
password = pass;
return true;
}
int main(int argc, char **argv)
{
if (argc < 6) {
fprintf(stderr, "usage: get host-uri username password container manifest-filename [decrypt]\n");
return 1;
}
Config::loadFromEnvironment();
IOManager ioManager;
try {
bool decrypt = false;
if (argc == 7 && strcmp(argv[6], "true") == 0)
decrypt = true;
HTTP::RequestBrokerOptions options;
options.ioManager = &ioManager;
options.verifySslCertificateHost = false;
options.getCredentialsDg = boost::bind(&authCallback, _3, _5, _6, _7, argv[2], argv[3]);
options.handleRedirects = false;
HTTP::RequestBroker::ptr requestBroker = HTTP::createRequestBroker(options).first;
TritonClient tritonClient(requestBroker, argv[1], strtoull(argv[4], NULL, 10));
StdoutStream stdoutStream;
HTTPStream::ptr obj = tritonClient.get(argv[5], -1ll, !decrypt);
transferStream(obj, stdoutStream);
std::map<std::string, std::string> properties = TritonClient::getProperties(obj);
std::map<std::string, std::string>::iterator it;
for (it = properties.begin(); it != properties.end(); it++) {
std::cout << it->first << "=";
std::cout << it->second << "\n";
}
} catch (HTTP::InvalidResponseException &ex) {
Stream::ptr response;
std::cerr << ex.request()->response().status;
if (ex.request()->hasResponseBody() && (response = ex.request()->responseStream())) {
std::cerr << ": ";
response.reset(new LimitedStream(response, 80));
StderrStream err;
transferStream(response, err);
}
std::cerr << std::endl;
} catch (...) {
std::cerr << boost::current_exception_diagnostic_information() << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment