|
| 1 | +#ifndef BOOST_NETWORK_PROTOCOL_HTTP_RESPONSE_RESPONSE_IPP_20111206 |
| 2 | +#define BOOST_NETWORK_PROTOCOL_HTTP_RESPONSE_RESPONSE_IPP_20111206 |
| 3 | + |
| 4 | +// Copyright 2011 Dean Michael Berris <dberris@google.com>. |
| 5 | +// Copyright 2011 Google, Inc. |
| 6 | +// Distributed under the Boost Software License, Version 1.0. |
| 7 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 8 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 9 | + |
| 10 | +#include <boost/network/protocol/http/response/response.hpp> |
| 11 | +#include <boost/optional/optional.hpp> |
| 12 | +#include <boost/utility/in_place_factory.hpp> |
| 13 | + |
| 14 | +namespace boost { namespace network { namespace http { |
| 15 | + |
| 16 | +struct response_pimpl { |
| 17 | + response_pimpl() {} |
| 18 | + response_pimpl * clone() { |
| 19 | + response_pimpl * new_pimpl = new (std::nothrow) response_pimpl; |
| 20 | + new_pimpl->source_future_ = source_future_; |
| 21 | + new_pimpl->destination_future_ = destination_future_; |
| 22 | + new_pimpl->headers_future_ = headers_future_; |
| 23 | + new_pimpl->status_future_ = status_future_; |
| 24 | + new_pimpl->status_message_future_ = status_message_future_; |
| 25 | + new_pimpl->version_future_ = version_future_; |
| 26 | + new_pimpl->body_future_ = body_future_; |
| 27 | + return new_pimpl; |
| 28 | + } |
| 29 | + |
| 30 | + void set_destination(std::string const &destination) { |
| 31 | + promise<std::string> destination_promise; |
| 32 | + unique_future<std::string> tmp_future = destination_promise.get_future(); |
| 33 | + destination_future_ = move(tmp_future); |
| 34 | + destination_promise.set_value(destination); |
| 35 | + } |
| 36 | + |
| 37 | + void get_destination(std::string &destination) { |
| 38 | + if (!destination_future_) { |
| 39 | + destination = ""; |
| 40 | + } else { |
| 41 | + destination = destination_future_->get(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + void set_source(std::string const &source) { |
| 46 | + promise<std::string> source_promise; |
| 47 | + unique_future<std::string> tmp_future = source_promise.get_future(); |
| 48 | + source_future_ = move(tmp_future); |
| 49 | + source_promise.set_value(source); |
| 50 | + } |
| 51 | + |
| 52 | + void get_source(std::string &source) { |
| 53 | + if (!source_future_) { |
| 54 | + source = ""; |
| 55 | + } else { |
| 56 | + source = source_future_->get(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + void append_header(std::string const & name, |
| 61 | + std::string const & value) { |
| 62 | + // FIXME do something! |
| 63 | + } |
| 64 | + |
| 65 | + void remove_headers(std::string const &name) { |
| 66 | + // FIXME do something! |
| 67 | + } |
| 68 | + |
| 69 | + void remove_headers() { |
| 70 | + // FIXME do something! |
| 71 | + } |
| 72 | + |
| 73 | + void get_headers( |
| 74 | + function<void(std::string const &, std::string const &)> inserter) { /* FIXME: Do something! */ } |
| 75 | + void get_headers( |
| 76 | + std::string const & name, |
| 77 | + function<void(std::string const &, std::string const &)> inserter) { /* FIXME: Do something! */ } |
| 78 | + void get_headers( |
| 79 | + function<bool(std::string const &, std::string const &)> predicate, |
| 80 | + function<void(std::string const &, std::string const &)> inserter) { /* FIXME: Do something! */ } |
| 81 | + |
| 82 | + void set_body(std::string const &body) { |
| 83 | + promise<std::string> body_promise; |
| 84 | + unique_future<std::string> tmp_future = body_promise.get_future(); |
| 85 | + body_future_ = move(tmp_future); |
| 86 | + body_promise.set_value(body); |
| 87 | + } |
| 88 | + |
| 89 | + void append_body(std::string const & data) { /* FIXME: Do something! */ } |
| 90 | + |
| 91 | + void get_body(std::string &body) { |
| 92 | + if (!body_future_) { |
| 93 | + body = ""; |
| 94 | + } else { |
| 95 | + body = body_future_->get(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + void get_body( |
| 100 | + function<void(iterator_range<char const *>)> chunk_reader, |
| 101 | + size_t size) { /* FIXME: Do something! */ } |
| 102 | + |
| 103 | + void set_status(boost::uint16_t status) { |
| 104 | + promise<boost::uint16_t> status_promise; |
| 105 | + unique_future<boost::uint16_t> tmp_future = status_promise.get_future(); |
| 106 | + status_future_ = move(tmp_future); |
| 107 | + status_promise.set_value(status); |
| 108 | + } |
| 109 | + |
| 110 | + void get_status(boost::uint16_t &status) { |
| 111 | + if (!status_future_) { |
| 112 | + status = 0u; |
| 113 | + } else { |
| 114 | + status = status_future_->get(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + void set_status_message(std::string const &status_message) { |
| 119 | + promise<std::string> status_message_promise_; |
| 120 | + unique_future<std::string> tmp_future = status_message_promise_.get_future(); |
| 121 | + status_message_future_ = move(tmp_future); |
| 122 | + status_message_promise_.set_value(status_message); |
| 123 | + } |
| 124 | + |
| 125 | + void get_status_message(std::string &status_message) { |
| 126 | + if (!status_message_future_) { |
| 127 | + status_message = ""; |
| 128 | + } else { |
| 129 | + status_message = status_message_future_->get(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + void set_version(std::string const &version) { |
| 134 | + promise<std::string> version_promise; |
| 135 | + unique_future<std::string> tmp_future = version_promise.get_future(); |
| 136 | + version_future_ = move(tmp_future); |
| 137 | + version_promise.set_value(version); |
| 138 | + } |
| 139 | + |
| 140 | + void get_version(std::string &version) { |
| 141 | + if (!version_future_) { |
| 142 | + version = ""; |
| 143 | + } else { |
| 144 | + version = version_future_->get(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + promise<std::string> get_source_promise() { |
| 149 | + promise<std::string> promise_; |
| 150 | + unique_future<std::string> tmp_future = promise_.get_future(); |
| 151 | + source_future_ = move(tmp_future); |
| 152 | + return move(promise_); |
| 153 | + } |
| 154 | + |
| 155 | + promise<std::string> get_destination_promise() { |
| 156 | + promise<std::string> promise_; |
| 157 | + unique_future<std::string> tmp_future = promise_.get_future(); |
| 158 | + destination_future_ = move(tmp_future); |
| 159 | + return move(promise_); |
| 160 | + } |
| 161 | + |
| 162 | + promise<std::multimap<std::string, std::string> > get_headers_promise() { |
| 163 | + promise<std::multimap<std::string, std::string> > promise_; |
| 164 | + unique_future<std::multimap<std::string, std::string> > tmp_future = promise_.get_future(); |
| 165 | + headers_future_ = move(tmp_future); |
| 166 | + return promise_; |
| 167 | + } |
| 168 | + |
| 169 | + promise<boost::uint16_t> get_status_promise() { |
| 170 | + promise<boost::uint16_t> promise_; |
| 171 | + unique_future<boost::uint16_t> tmp_future = promise_.get_future(); |
| 172 | + status_future_ = move(tmp_future); |
| 173 | + return promise_; |
| 174 | + } |
| 175 | + |
| 176 | + promise<std::string> get_status_message_promise() { |
| 177 | + promise<std::string> promise_; |
| 178 | + unique_future<std::string> tmp_future = promise_.get_future(); |
| 179 | + status_message_future_ = move(tmp_future); |
| 180 | + return promise_; |
| 181 | + } |
| 182 | + |
| 183 | + promise<std::string> get_version_promise() { |
| 184 | + promise<std::string> promise_; |
| 185 | + unique_future<std::string> tmp_future = promise_.get_future(); |
| 186 | + version_future_ = move(tmp_future); |
| 187 | + return promise_; |
| 188 | + } |
| 189 | + |
| 190 | + promise<std::string> get_body_promise() { |
| 191 | + promise<std::string> promise_; |
| 192 | + unique_future<std::string> tmp_future = promise_.get_future(); |
| 193 | + body_future_ = move(tmp_future); |
| 194 | + return promise_; |
| 195 | + } |
| 196 | + |
| 197 | + private: |
| 198 | + optional<shared_future<std::string> > source_future_; |
| 199 | + optional<shared_future<std::string> > destination_future_; |
| 200 | + optional<shared_future<std::multimap<std::string, std::string> > > |
| 201 | + headers_future_; |
| 202 | + optional<shared_future<boost::uint16_t> > status_future_; |
| 203 | + optional<shared_future<std::string> > status_message_future_; |
| 204 | + optional<shared_future<std::string> > version_future_; |
| 205 | + optional<shared_future<std::string> > body_future_; |
| 206 | +}; |
| 207 | + |
| 208 | +response::response() |
| 209 | +: pimpl_(new (std::nothrow) response_pimpl) |
| 210 | +{} |
| 211 | + |
| 212 | +response::response(response const & other) |
| 213 | +: pimpl_(other.pimpl_->clone()) |
| 214 | +{} |
| 215 | + |
| 216 | +response& response::operator=(response rhs) { |
| 217 | + rhs.swap(*this); |
| 218 | + return *this; |
| 219 | +} |
| 220 | + |
| 221 | +void response::swap(response &other) { |
| 222 | + other.pimpl_.swap(pimpl_); |
| 223 | +} |
| 224 | + |
| 225 | +void response::set_destination(std::string const &destination) { |
| 226 | + pimpl_->set_destination(destination); |
| 227 | +} |
| 228 | + |
| 229 | +void response::set_source(std::string const &source) { |
| 230 | + pimpl_->set_source(source); |
| 231 | +} |
| 232 | + |
| 233 | +void response::append_header(std::string const &name, |
| 234 | + std::string const &value) { |
| 235 | + pimpl_->append_header(name, value); |
| 236 | +} |
| 237 | + |
| 238 | +void response::remove_headers(std::string const &name) { |
| 239 | + pimpl_->remove_headers(name); |
| 240 | +} |
| 241 | + |
| 242 | +void response::remove_headers() { |
| 243 | + pimpl_->remove_headers(); |
| 244 | +} |
| 245 | + |
| 246 | +void response::set_body(std::string const &body) { |
| 247 | + pimpl_->set_body(body); |
| 248 | +} |
| 249 | + |
| 250 | +void response::append_body(std::string const &data) { |
| 251 | + pimpl_->append_body(data); |
| 252 | +} |
| 253 | + |
| 254 | +void response::get_destination(std::string &destination) const { |
| 255 | + pimpl_->get_destination(destination); |
| 256 | +} |
| 257 | + |
| 258 | +void response::get_source(std::string &source) const { |
| 259 | + pimpl_->get_source(source); |
| 260 | +} |
| 261 | + |
| 262 | +void response::get_headers(function<void(std::string const &, std::string const &)> inserter) const { |
| 263 | + pimpl_->get_headers(inserter); |
| 264 | +} |
| 265 | + |
| 266 | +void response::get_headers(std::string const &name, |
| 267 | + function<void(std::string const &, std::string const &)> inserter) const { |
| 268 | + pimpl_->get_headers(name, inserter); |
| 269 | +} |
| 270 | + |
| 271 | +void response::get_headers(function<bool(std::string const &, std::string const &)> predicate, |
| 272 | + function<void(std::string const &, std::string const &)> inserter) const { |
| 273 | + pimpl_->get_headers(predicate, inserter); |
| 274 | +} |
| 275 | + |
| 276 | +void response::get_body(std::string &body) const { |
| 277 | + pimpl_->get_body(body); |
| 278 | +} |
| 279 | + |
| 280 | +void response::get_body(function<void(iterator_range<char const *>)> chunk_reader, size_t size) const { |
| 281 | + pimpl_->get_body(chunk_reader, size); |
| 282 | +} |
| 283 | + |
| 284 | +void response::set_status(boost::uint16_t new_status) { |
| 285 | + pimpl_->set_status(new_status); |
| 286 | +} |
| 287 | + |
| 288 | +void response::set_status_message(std::string const &new_status_message) { |
| 289 | + pimpl_->set_status_message(new_status_message); |
| 290 | +} |
| 291 | + |
| 292 | +void response::set_version(std::string const &new_version) { |
| 293 | + pimpl_->set_version(new_version); |
| 294 | +} |
| 295 | + |
| 296 | +void response::get_status(boost::uint16_t &status) const { |
| 297 | + pimpl_->get_status(status); |
| 298 | +} |
| 299 | + |
| 300 | +void response::get_status_message(std::string &status_message) const { |
| 301 | + pimpl_->get_status_message(status_message); |
| 302 | +} |
| 303 | + |
| 304 | +void response::get_version(std::string &version) const { |
| 305 | + pimpl_->get_version(version); |
| 306 | +} |
| 307 | + |
| 308 | +response::~response() {} |
| 309 | + |
| 310 | +promise<std::string> response::get_version_promise() { |
| 311 | + return pimpl_->get_version_promise(); |
| 312 | +} |
| 313 | + |
| 314 | +promise<boost::uint16_t> response::get_status_promise() { |
| 315 | + return pimpl_->get_status_promise(); |
| 316 | +} |
| 317 | + |
| 318 | +promise<std::string> response::get_status_message_promise() { |
| 319 | + return pimpl_->get_status_message_promise(); |
| 320 | +} |
| 321 | + |
| 322 | +promise<std::multimap<std::string, std::string> > response::get_headers_promise() { |
| 323 | + return pimpl_->get_headers_promise(); |
| 324 | +} |
| 325 | + |
| 326 | +promise<std::string> response::get_source_promise() { |
| 327 | + return pimpl_->get_source_promise(); |
| 328 | +} |
| 329 | + |
| 330 | +promise<std::string> response::get_destination_promise() { |
| 331 | + return pimpl_->get_destination_promise(); |
| 332 | +} |
| 333 | + |
| 334 | +promise<std::string> response::get_body_promise() { |
| 335 | + return pimpl_->get_body_promise(); |
| 336 | +} |
| 337 | + |
| 338 | +} // namespace http |
| 339 | + |
| 340 | +} // namespace network |
| 341 | + |
| 342 | +} // namespace boost |
| 343 | + |
| 344 | +#endif // BOOST_NETWORK_PROTOCOL_HTTP_RESPONSE_RESPONSE_IPP_20111206 |
0 commit comments