Urdl C++ Library

PrevUpHomeNext

read_stream

The class read_stream supports reading content from a specified URL using synchronous or asynchronous operations.

class read_stream

Member Functions

Name

Description

async_open

Asynchronously opens the specified URL.

async_read_some

Asynchronously reads some data from the stream.

close

Closes the stream.

content_length

Gets the length of the content obtained from the URL.

content_type

Gets the MIME type of the content obtained from the URL.

get_io_service

Gets the io_service associated with the stream.

get_option

Gets the current value of an option that controls the behaviour of the stream.

get_options

Gets the values of all options set on the stream.

headers

Gets the protocol-specific headers obtained from the URL.

is_open

Determines whether the stream is open.

open

Opens the specified URL.

read_some

Reads some data from the stream.

read_stream

Constructs an object of class read_stream.

set_option

Sets an option to control the behaviour of the stream.

set_options

Sets options to control the behaviour of the stream.

Remarks

Currently supported URL protocols are http, https and file.

The class read_stream meets the type requirements for SyncReadStream and AsyncReadStream, as defined in the Boost.Asio documentation. This allows objects of class read_stream to be used with the functions boost::asio::read, boost::asio::async_read, boost::asio::read_until and boost::asio::async_read_until.

Example

To synchronously open the URL, read the content and write it to standard output:

try
{
  boost::asio::io_service io_service;
  urdl::read_stream read_stream(io_service);
  read_stream.open("http://www.boost.org/LICENSE_1_0.txt");
  for (;;)
  {
    char data[1024];
    boost::system::error_code ec;
    std::size_t length = stream.read_some(boost::asio::buffer(data), ec);
    if (ec == boost::asio::error::eof)
      break;
    if (ec)
      throw boost::system::system_error(ec);
    os.write(data, length);
  }
}
catch (std::exception& e)
{
  std::cerr << "Exception: " << e.what() << std::endl;
}

To asynchronously open the URL, read the content and write it to standard output:

boost::asio::io_service io_service;
urdl::read_stream read_stream(io_service)
char data[1024];
...
read_stream.async_open("http://www.boost.org/LICENSE_1_0.txt", open_handler);
...
void open_handler(const boost::system::error_code& ec)
{
  if (!ec)
  {
    read_stream.async_read_some(boost::asio::buffer(data), read_handler);
  }
}
...
void read_handler(const boost::system::error_code& ec, std::size_t length)
{
  if (!ec)
  {
    std::cout.write(data, length);
    read_stream.async_read_some(boost::asio::buffer(data), read_handler);
  }
}

Requirements

Header: <urdl/read_stream.hpp>

Namespace: urdl


PrevUpHomeNext