// // istream.hpp // ~~~~~~~~~~~ // // Copyright (c) 2009-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef URDL_ISTREAM_HPP #define URDL_ISTREAM_HPP #include #include #include #include "urdl/istreambuf.hpp" #include "urdl/detail/abi_prefix.hpp" namespace urdl { /// The class @c istream supports reading content from a specified URL. /** * @par Remarks * The class stores an object of class @c istreambuf. * * Currently supported URL protocols are @c http, @c https and @c file. * * @par Example * To read the entire content of a resource located by a URL into a string: * @code * urdl::istream is("http://www.boost.org/LICENSE_1_0.txt"); * if (is) * { * std::string content; * if (std::getline(is, content, std::char_traits::eof())) * { * ... * } * } * @endcode * * @par Requirements * @e Header: @c @n * @e Namespace: @c urdl */ class istream : private boost::base_from_member, public std::basic_istream { public: /// Constructs an object of class @c istream. /** * @par Remarks * Initializes the base class with @c std::basic_istream(sb), * where sb is an object of class @c istreambuf stored within the class. */ istream() : std::basic_istream( &this->boost::base_from_member::member) { } /// Constructs an object of class @c istream. /** * @param u The URL to open. * * @par Remarks * Initializes the base class with @c std::basic_istream(sb), * where @c sb is an object of class @c istreambuf stored within the class. It * also opens @c sb by performing @c sb.open(u) and, if that fails (returns a * null pointer), calls @c setstate(failbit). */ explicit istream(const url& u) : std::basic_istream( &this->boost::base_from_member::member) { if (rdbuf()->open(u) == 0) setstate(std::ios_base::failbit); } /// Constructs an object of class @c istream. /** * @param u The URL to open. * * @param options The options to be set on the stream. * * @par Remarks * Initializes the base class with @c std::basic_istream(sb), where * @c sb is an object of class @c istreambuf stored within the class. It also * performs @c rdbuf()->set_options(options), then opens @c sb by performing * @c sb.open(u) and, if that fails (returns a null pointer), calls * @c setstate(failbit). * * @par Example * @code * urdl::option_set options; * options.set_option(urdl::http::max_redirects(1)); * urdl::istream is("http://www.boost.org", options); * @endcode */ explicit istream(const url& u, const option_set& options) : std::basic_istream( &this->boost::base_from_member::member) { rdbuf()->set_options(options); if (rdbuf()->open(u) == 0) setstate(std::ios_base::failbit); } /// Sets an option to control the behaviour of the stream. /** * @param option The option to be set on the stream. * * @par Remarks * Performs @c rdbuf()->set_option(option). Options are uniquely identified by * type. * * @par Example * @code * urdl::istream is; * is.set_option(urdl::http::max_redirects(1)); * @endcode */ template void set_option(const Option& option) { rdbuf()->set_option(option); } /// Sets options to control the behaviour of the stream. /** * @param options The options to be set on the stream. The options in the set * are added on top of any options already set on the stream. * * @par Remarks * Performs @c rdbuf()->set_options(options). * * @par Example * @code * urdl::istream is; * urdl::option_set options; * options.set_option(urdl::http::max_redirects(1)); * options.set_option(urdl::ssl::verify_peer(false)); * stream.set_options(options); * @endcode */ void set_options(const option_set& options) { rdbuf()->set_options(options); } /// Gets the current value of an option that controls the behaviour of the /// stream. /** * @returns The current value of the option. * * @par Remarks * Returns @c rdbuf()->get_option