reply.cpp

00001 
00002 /***************************************************************************
00003  *  reply.cpp - Web request reply
00004  *
00005  *  Created: Thu Oct 23 12:01:05 2008
00006  *  Copyright  2006-2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include <webview/reply.h>
00024 
00025 #include <cstdlib>
00026 #include <cstdarg>
00027 #include <cstdio>
00028 
00029 namespace fawkes {
00030 #if 0 /* just to make Emacs auto-indent happy */
00031 }
00032 #endif
00033 
00034 /** @class WebReply <webview/reply.h>
00035  * Basic web reply.
00036  * The base class for all web replies. Though the WebRequestDispatcher expects
00037  * sub-classes of StaticWebReply or DynamicWebReply.
00038  * @author Tim Niemueller
00039  */
00040 
00041 /** Constructor.
00042  * @param code HTTP response code
00043  */
00044 WebReply::WebReply(response_code_t code)
00045 {
00046   __code = code;
00047 }
00048 
00049 
00050 /** Destructor. */
00051 WebReply::~WebReply()
00052 {
00053 }
00054 
00055 
00056 /** Get response code.
00057  * @return HTTP response code
00058  */
00059 WebReply::response_code_t
00060 WebReply::code() const
00061 {
00062   return __code;
00063 }
00064 
00065 
00066 /** Add a HTTP header.
00067  * @param header header entry name
00068  * @param content content of the header field
00069  */
00070 void
00071 WebReply::add_header(std::string header, std::string content)
00072 {
00073   __headers[header] = content;
00074 }
00075 
00076 
00077 /** get headers.
00078  * @return map of header name/content pairs.
00079  */
00080 const WebReply::HeaderMap &
00081 WebReply::headers() const
00082 {
00083   return __headers;
00084 }
00085 
00086 
00087 /** @class DynamicWebReply <webview/reply.h>
00088  * Dynamic web reply.
00089  * A reply of this type is send out in chunks, not all as a whole. It should be
00090  * used for payloads that can get very large, like file transfers.
00091  * @author Tim Niemueller
00092  *
00093  * @fn size_t DynamicWebReply::size() = 0
00094  * Total size of the web reply.
00095  * Return the total size of the reply if known, or 0 if it is not known. In the
00096  * latter case your next_chunk() method has to return -1 at some point to end
00097  * the transfer. If possible by any means return a meaningful value, as it will
00098  * improve the experience of users, especially for long transfers!
00099  * @return total size of reply in bytes
00100  *
00101  * @fn size_t DynamicWebReply::next_chunk(size_t pos, char *buffer, size_t buf_max_size) = 0
00102  * Get data of next chunk.
00103  * @param pos position in the stream. Note that a certain position may be called
00104  * several times.
00105  * @param buffer buffer to store data in
00106  * @param buf_max_size maximum size in bytes of data that can be put into buffer
00107  * @return number of bytes written to buffer, or -1 to immediately stop the
00108  * transfer.
00109  */
00110 
00111 /** Constructor.
00112  * @param code HTTP response code
00113  */
00114 DynamicWebReply::DynamicWebReply(response_code_t code)
00115   : WebReply(code)
00116 {
00117 }
00118 
00119 
00120 /** Chunksize.
00121  * The size that a single chunk should have. A sub-class may override this if a
00122  * specific chunk size is beneficial or even required. The default is 32kb.
00123  * @return chunk size in bytes
00124  */
00125 size_t
00126 DynamicWebReply::chunk_size()
00127 {
00128   // use 32k chunks by default
00129   return 32 * 1024;
00130 }
00131 
00132 
00133 /** @class StaticWebReply <webview/reply.h>
00134  * Static web reply.
00135  * The static web reply is send out as a whole at once and is immediately
00136  * deleted after sending. Use it for regular-sized pages and content.
00137  * @author Tim Niemueller
00138  */
00139 
00140 /** Constructor.
00141  * @param code HTTP response code
00142  * @param body optional initial body
00143  */
00144 StaticWebReply::StaticWebReply(response_code_t code, std::string body)
00145   : WebReply(code)
00146 {
00147   _body = body;
00148 }
00149 
00150 
00151 /** Append to body.
00152  * @param format format of the text to append. Supports the same format as
00153  * printf().
00154  */
00155 void
00156 StaticWebReply::append_body(const char *format, ...)
00157 {
00158   va_list args;
00159   va_start(args, format);
00160   char *s;
00161   if ( vasprintf(&s, format, args) != -1 ) {
00162     _body += s;
00163     free(s);
00164   }
00165   va_end(args);
00166 }
00167 
00168 
00169 /** Append simple text line.
00170  * @param text text to append to body
00171  * @return reference to this instance
00172  */
00173 StaticWebReply &
00174 StaticWebReply::operator+=(std::string text)
00175 {
00176   _body += text;
00177   return *this;
00178 }
00179 
00180 
00181 /** Get body.
00182  * @return reference to body.
00183  */
00184 const std::string &
00185 StaticWebReply::body()
00186 {
00187   return _body;
00188 }
00189 
00190 
00191 /** Get length of body.
00192  * @return body length
00193  */
00194 std::string::size_type
00195 StaticWebReply::body_length()
00196 {
00197   return _body.length();
00198 }
00199 
00200 
00201 /** Pack the data.
00202  * This method is called just before the reply is sent.
00203  * You can implement this method if you need to compose your reply before
00204  * body() and body_length() provide valid output.
00205  */
00206 void
00207 StaticWebReply::pack()
00208 {
00209 }
00210 
00211 } // end namespace fawkes

Generated on 1 Mar 2011 for Fawkes API by  doxygen 1.6.1