Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

httprequest.cpp

Example: httprequest.cpp application


#include <exception>
#include <iostream>
#include <cxxtools/http/client.h>
#include <cxxtools/http/request.h>
#include <cxxtools/arg.h>
#include <cxxtools/eventloop.h>
#include <cxxtools/loginit.h>
#include <cxxtools/log.h>

log_define("cxxtools.demo.httprequest")

class AsyncRequester : public cxxtools::Connectable
{
    cxxtools::http::Client& _client;
    char** _argv;

    cxxtools::http::Request _request;
    cxxtools::EventLoop _loop;

    std::size_t onBodyAvailable(cxxtools::http::Client& client);
    void onReady(cxxtools::http::Client& client);
    void onError(cxxtools::http::Client& client, const std::exception& e);

  public:
    AsyncRequester(cxxtools::http::Client& client, char** argv)
      : _client(client),
        _argv(argv)
    {
      connect(_client.bodyAvailable, *this, &AsyncRequester::onBodyAvailable);
      connect(_client.replyFinished, *this, &AsyncRequester::onReady);
      connect(_client.errorOccured, *this, &AsyncRequester::onError);
      _client.setSelector(_loop);
    }

    void run()
    {
      if (*_argv)
      {
        _request.url(*_argv++);
        _client.beginExecute(_request);
        _loop.run();
      }
    }
};

std::size_t AsyncRequester::onBodyAvailable(cxxtools::http::Client& client)
{
  char buffer[8192];
  std::streamsize n = client.in().readsome(buffer, sizeof(buffer));
  std::cout.write(buffer, n);

  return n;
}

void AsyncRequester::onReady(cxxtools::http::Client& client)
{
  if (*_argv)
  {
    _request.url(*_argv++);
    client.beginExecute(_request);
  }
  else
  {
    _loop.exit();
  }

}

void AsyncRequester::onError(cxxtools::http::Client& client, const std::exception& e)
{
  log_warn("error occured: " << e.what());
  throw;
}

int main(int argc, char* argv[])
{
  try
  {
    log_init();

    cxxtools::Arg<std::string> user(argc, argv, 'u'); // passed as "username:password"
    cxxtools::Arg<std::string> server(argc, argv, 's', "127.0.0.1");
    cxxtools::Arg<unsigned short int> port(argc, argv, 'p', 80);
    cxxtools::Arg<bool> async(argc, argv, 'a');

    cxxtools::http::Client client(server, port);

    if (user.isSet())
    {
      std::string::size_type p = user.getValue().find(':');
      if (p != std::string::npos)
      {
        client.auth(user.getValue().substr(0, p),
                    user.getValue().substr(p + 1));
      }
      else
      {
        client.auth(user, std::string());
      }
    }

    if (async)
    {
      AsyncRequester ar(client, argv + 1);
      ar.run();
    }
    else
    {
      for (int a = 1; a < argc; ++a)
      {
        std::cout << client.get(argv[a]);
      }
    }
  }
  catch (const std::exception& e)
  {
    std::cerr << "ERROR: " << e.what() << std::endl;
  }
}

Copyright © 2008 The Tntnet Development Team
Tntnet 1.6