pion  5.0.6
writer.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_HTTP_WRITER_HEADER__
11 #define __PION_HTTP_WRITER_HEADER__
12 
13 #include <vector>
14 #include <string>
15 #include <boost/shared_ptr.hpp>
16 #include <boost/function.hpp>
17 #include <boost/function/function0.hpp>
18 #include <boost/function/function2.hpp>
19 #include <boost/asio.hpp>
20 #include <boost/noncopyable.hpp>
21 #include <pion/config.hpp>
22 #include <pion/logger.hpp>
23 #include <pion/tcp/connection.hpp>
24 #include <pion/http/message.hpp>
25 
26 
27 namespace pion { // begin namespace pion
28 namespace http { // begin namespace http
29 
30 
34 class PION_API writer :
35  private boost::noncopyable
36 {
37 protected:
38 
40  typedef boost::function1<void,const boost::system::error_code&> finished_handler_t;
41 
43  typedef boost::function2<void,const boost::system::error_code&,std::size_t> write_handler_t;
44 
45 
52  writer(const tcp::connection_ptr& tcp_conn, finished_handler_t handler)
53  : m_logger(PION_GET_LOGGER("pion.http.writer")),
54  m_tcp_conn(tcp_conn), m_content_length(0), m_stream_is_empty(true),
55  m_client_supports_chunks(true), m_sending_chunks(false),
56  m_sent_headers(false), m_finished(handler)
57  {}
58 
65  virtual void handle_write(const boost::system::error_code& write_error,
66  std::size_t bytes_written) = 0;
67 
68 
74  virtual void prepare_buffers_for_send(http::message::write_buffers_t& write_buffers) = 0;
75 
77  virtual write_handler_t bind_to_write_handler(void) = 0;
78 
80  inline void finished_writing(const boost::system::error_code& ec) {
81  if (m_finished) m_finished(ec);
82  }
83 
84 
85 public:
86 
88  virtual ~writer() {}
89 
91  inline void clear(void) {
92  m_content_buffers.clear();
93  m_binary_cache.clear();
94  m_text_cache.clear();
95  m_content_stream.str("");
96  m_stream_is_empty = true;
97  m_content_length = 0;
98  }
99 
105  template <typename T>
106  inline void write(const T& data) {
107  m_content_stream << data;
108  if (m_stream_is_empty) m_stream_is_empty = false;
109  }
110 
111  inline void write(std::ostream& (*iomanip)(std::ostream&)) {
112  m_content_stream << iomanip;
113  if (m_stream_is_empty) m_stream_is_empty = false;
114  }
115 
122  inline void write(const void *data, size_t length) {
123  if (length != 0) {
124  flush_content_stream();
125  m_content_buffers.push_back(m_binary_cache.add(data, length));
126  m_content_length += length;
127  }
128  }
129 
137  inline void write_no_copy(const std::string& data) {
138  if (! data.empty()) {
139  flush_content_stream();
140  m_content_buffers.push_back(boost::asio::buffer(data));
141  m_content_length += data.size();
142  }
143  }
144 
152  inline void write_no_copy(void *data, size_t length) {
153  if (length > 0) {
154  flush_content_stream();
155  m_content_buffers.push_back(boost::asio::buffer(data, length));
156  m_content_length += length;
157  }
158  }
159 
160 
166  inline void send(void) {
167  send_more_data(false, bind_to_write_handler());
168  }
169 
179  template <typename SendHandler>
180  inline void send(SendHandler send_handler) {
181  send_more_data(false, send_handler);
182  }
183 
194  template <typename SendHandler>
195  inline void send_chunk(SendHandler send_handler) {
196  m_sending_chunks = true;
197  if (!supports_chunked_messages()) {
198  // sending data in chunks, but the client does not support chunking;
199  // make sure that the connection will be closed when we are all done
200  m_tcp_conn->set_lifecycle(tcp::connection::LIFECYCLE_CLOSE);
201  }
202  // send more data
203  send_more_data(false, send_handler);
204  }
205 
217  template <typename SendHandler>
218  inline void send_final_chunk(SendHandler send_handler) {
219  m_sending_chunks = true;
220  send_more_data(true, send_handler);
221  }
222 
230  inline void send_final_chunk(void) {
231  m_sending_chunks = true;
232  send_more_data(true, bind_to_write_handler());
233  }
234 
235 
237  inline tcp::connection_ptr& get_connection(void) { return m_tcp_conn; }
238 
240  inline size_t get_content_length(void) const { return m_content_length; }
241 
243  inline void supports_chunked_messages(bool b) { m_client_supports_chunks = b; }
244 
246  inline bool supports_chunked_messages() const { return m_client_supports_chunks; }
247 
249  inline bool sending_chunked_message() const { return m_sending_chunks; }
250 
252  inline void set_logger(logger log_ptr) { m_logger = log_ptr; }
253 
255  inline logger get_logger(void) { return m_logger; }
256 
257 
258 private:
259 
266  template <typename SendHandler>
267  inline void send_more_data(const bool send_final_chunk, SendHandler send_handler)
268  {
269  // make sure that we did not lose the TCP connection
270  if (m_tcp_conn->is_open()) {
271  // make sure that the content-length is up-to-date
272  flush_content_stream();
273  // prepare the write buffers to be sent
274  http::message::write_buffers_t write_buffers;
275  prepare_write_buffers(write_buffers, send_final_chunk);
276  // send data in the write buffers
277  m_tcp_conn->async_write(write_buffers, send_handler);
278  } else {
279  finished_writing(boost::asio::error::connection_reset);
280  }
281  }
282 
289  void prepare_write_buffers(http::message::write_buffers_t &write_buffers,
290  const bool send_final_chunk);
291 
293  inline void flush_content_stream(void) {
294  if (! m_stream_is_empty) {
295  std::string string_to_add(m_content_stream.str());
296  if (! string_to_add.empty()) {
297  m_content_stream.str("");
298  m_content_length += string_to_add.size();
299  m_text_cache.push_back(string_to_add);
300  m_content_buffers.push_back(boost::asio::buffer(m_text_cache.back()));
301  }
302  m_stream_is_empty = true;
303  }
304  }
305 
306 
308  class binary_cache_t : public std::vector<std::pair<const char *, size_t> > {
309  public:
310  ~binary_cache_t() {
311  for (iterator i=begin(); i!=end(); ++i) {
312  delete[] i->first;
313  }
314  }
315  inline boost::asio::const_buffer add(const void *ptr, const size_t size) {
316  char *data_ptr = new char[size];
317  memcpy(data_ptr, ptr, size);
318  push_back( std::make_pair(data_ptr, size) );
319  return boost::asio::buffer(data_ptr, size);
320  }
321  };
322 
324  typedef std::list<std::string> text_cache_t;
325 
326 
328  logger m_logger;
329 
331  tcp::connection_ptr m_tcp_conn;
332 
334  http::message::write_buffers_t m_content_buffers;
335 
337  binary_cache_t m_binary_cache;
338 
340  text_cache_t m_text_cache;
341 
343  std::ostringstream m_content_stream;
344 
346  size_t m_content_length;
347 
349  bool m_stream_is_empty;
350 
352  bool m_client_supports_chunks;
353 
355  bool m_sending_chunks;
356 
358  bool m_sent_headers;
359 
361  finished_handler_t m_finished;
362 };
363 
364 
366 typedef boost::shared_ptr<writer> writer_ptr;
367 
368 
370 template <typename T>
371 inline const writer_ptr& operator<<(const writer_ptr& writer, const T& data) {
372  writer->write(data);
373  return writer;
374 }
375 
376 inline const writer_ptr& operator<<(const writer_ptr& writer, std::ostream& (*iomanip)(std::ostream&)) {
377  writer->write(iomanip);
378  return writer;
379 }
380 
381 } // end namespace http
382 } // end namespace pion
383 
384 #endif
void clear(void)
clears out all of the memory buffers used to cache payload content data
Definition: writer.hpp:91
void send(void)
Definition: writer.hpp:166
boost::function2< void, const boost::system::error_code &, std::size_t > write_handler_t
data type for a function that handles write operations
Definition: writer.hpp:43
size_t get_content_length(void) const
returns the length of the payload content (in bytes)
Definition: writer.hpp:240
void write(const void *data, size_t length)
Definition: writer.hpp:122
writer(const tcp::connection_ptr &tcp_conn, finished_handler_t handler)
Definition: writer.hpp:52
logger get_logger(void)
returns the logger currently in use
Definition: writer.hpp:255
void write(const T &data)
Definition: writer.hpp:106
void finished_writing(const boost::system::error_code &ec)
called after we have finished sending the HTTP message
Definition: writer.hpp:80
tcp::connection_ptr & get_connection(void)
returns a shared pointer to the TCP connection
Definition: writer.hpp:237
boost::function1< void, const boost::system::error_code & > finished_handler_t
function called after the HTTP message has been sent
Definition: writer.hpp:40
void send(SendHandler send_handler)
Definition: writer.hpp:180
void supports_chunked_messages(bool b)
sets whether or not the client supports chunked messages
Definition: writer.hpp:243
void write_no_copy(void *data, size_t length)
Definition: writer.hpp:152
void write_no_copy(const std::string &data)
Definition: writer.hpp:137
void set_logger(logger log_ptr)
sets the logger to be used
Definition: writer.hpp:252
void send_final_chunk(SendHandler send_handler)
Definition: writer.hpp:218
virtual ~writer()
default destructor
Definition: writer.hpp:88
void send_final_chunk(void)
Definition: writer.hpp:230
bool sending_chunked_message() const
returns true if we are sending a chunked message to the client
Definition: writer.hpp:249
void send_chunk(SendHandler send_handler)
Definition: writer.hpp:195
std::vector< boost::asio::const_buffer > write_buffers_t
data type for I/O write buffers (these wrap existing data to be sent)
Definition: message.hpp:61
bool supports_chunked_messages() const
returns true if the client supports chunked messages
Definition: writer.hpp:246