Fawkes API  Fawkes Development Version
reply.h
00001 
00002 /***************************************************************************
00003  *  reply.h - Web request reply
00004  *
00005  *  Created: Wed Oct 22 18:49:35 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 #ifndef __LIBS_WEBVIEW_REPLY_H_
00024 #define __LIBS_WEBVIEW_REPLY_H_
00025 
00026 #include <map>
00027 #include <string>
00028 
00029 namespace fawkes {
00030 #if 0 /* just to make Emacs auto-indent happy */
00031 }
00032 #endif
00033 
00034 class WebReply
00035 {
00036  public:
00037   /** HTTP response code. */
00038   typedef enum {
00039     HTTP_CONTINUE                         = 100, /**< CONTINUE */
00040     HTTP_SWITCHING_PROTOCOLS              = 101, /**< SWITCHING_PROTOCOLS */
00041     HTTP_PROCESSING                       = 102, /**< PROCESSING */
00042 
00043     HTTP_OK                               = 200, /**< OK */
00044     HTTP_CREATED                          = 201, /**< CREATED */
00045     HTTP_ACCEPTED                         = 202, /**< ACCEPTED */
00046     HTTP_NON_AUTHORITATIVE_INFORMATION    = 203, /**< NON_AUTHORITATIVE_INFORMATION */
00047     HTTP_NO_CONTENT                       = 204, /**< NO_CONTENT */
00048     HTTP_RESET_CONTENT                    = 205, /**< RESET_CONTENT */
00049     HTTP_PARTIAL_CONTENT                  = 206, /**< PARTIAL_CONTENT */
00050     HTTP_MULTI_STATUS                     = 207, /**< MULTI_STATUS */
00051 
00052     HTTP_MULTIPLE_CHOICES                 = 300, /**< MULTIPLE_CHOICES */
00053     HTTP_MOVED_PERMANENTLY                = 301, /**< MOVED_PERMANENTLY */
00054     HTTP_FOUND                            = 302, /**< FOUND */
00055     HTTP_SEE_OTHER                        = 303, /**< SEE_OTHER */
00056     HTTP_NOT_MODIFIED                     = 304, /**< NOT_MODIFIED */
00057     HTTP_USE_PROXY                        = 305, /**< USE_PROXY */
00058     HTTP_SWITCH_PROXY                     = 306, /**< SWITCH_PROXY */
00059     HTTP_TEMPORARY_REDIRECT               = 307, /**< TEMPORARY_REDIRECT */
00060 
00061     HTTP_BAD_REQUEST                      = 400, /**< BAD_REQUEST */
00062     HTTP_UNAUTHORIZED                     = 401, /**< UNAUTHORIZED */
00063     HTTP_PAYMENT_REQUIRED                 = 402, /**< PAYMENT_REQUIRED */
00064     HTTP_FORBIDDEN                        = 403, /**< FORBIDDEN */
00065     HTTP_NOT_FOUND                        = 404, /**< NOT_FOUND */
00066     HTTP_METHOD_NOT_ALLOWED               = 405, /**< METHOD_NOT_ALLOWED */
00067     HTTP_METHOD_NOT_ACCEPTABLE            = 406, /**< METHOD_NOT_ACCEPTABLE */
00068     HTTP_PROXY_AUTHENTICATION_REQUIRED    = 407, /**< PROXY_AUTHENTICATION_REQUIRED */
00069     HTTP_REQUEST_TIMEOUT                  = 408, /**< REQUEST_TIMEOUT */
00070     HTTP_CONFLICT                         = 409, /**< CONFLICT */
00071     HTTP_GONE                             = 410, /**< GONE */
00072     HTTP_LENGTH_REQUIRED                  = 411, /**< LENGTH_REQUIRED */
00073     HTTP_PRECONDITION_FAILED              = 412, /**< PRECONDITION_FAILED */
00074     HTTP_REQUEST_ENTITY_TOO_LARGE         = 413, /**< REQUEST_ENTITY_TOO_LARGE */
00075     HTTP_REQUEST_URI_TOO_LONG             = 414, /**< REQUEST_URI_TOO_LONG */
00076     HTTP_UNSUPPORTED_MEDIA_TYPE           = 415, /**< UNSUPPORTED_MEDIA_TYPE */
00077     HTTP_REQUESTED_RANGE_NOT_SATISFIABLE  = 416, /**< REQUESTED_RANGE_NOT_SATISFIABLE */
00078     HTTP_EXPECTATION_FAILED               = 417, /**< EXPECTATION_FAILED */
00079     HTTP_UNPROCESSABLE_ENTITY             = 422, /**< UNPROCESSABLE_ENTITY */
00080     HTTP_LOCKED                           = 423, /**< LOCKED */
00081     HTTP_FAILED_DEPENDENCY                = 424, /**< FAILED_DEPENDENCY */
00082     HTTP_UNORDERED_COLLECTION             = 425, /**< UNORDERED_COLLECTION */
00083     HTTP_UPGRADE_REQUIRED                 = 426, /**< UPGRADE_REQUIRED */
00084     HTTP_RETRY_WITH                       = 449, /**< RETRY_WITH */
00085 
00086     HTTP_INTERNAL_SERVER_ERROR            = 500, /**< INTERNAL_SERVER_ERROR */
00087     HTTP_NOT_IMPLEMENTED                  = 501, /**< NOT_IMPLEMENTED */
00088     HTTP_BAD_GATEWAY                      = 502, /**< BAD_GATEWAY */
00089     HTTP_SERVICE_UNAVAILABLE              = 503, /**< SERVICE_UNAVAILABLE */
00090     HTTP_GATEWAY_TIMEOUT                  = 504, /**< GATEWAY_TIMEOUT */
00091     HTTP_HTTP_VERSION_NOT_SUPPORTED       = 505, /**< HTTP_VERSION_NOT_SUPPORTED */
00092     HTTP_VARIANT_ALSO_NEGOTIATES          = 506, /**< VARIANT_ALSO_NEGOTIATES */
00093     HTTP_INSUFFICIENT_STORAGE             = 507, /**< INSUFFICIENT_STORAGE */
00094     HTTP_BANDWIDTH_LIMIT_EXCEEDED         = 509, /**< BANDWIDTH_LIMIT_EXCEEDED */
00095     HTTP_NOT_EXTENDED                     = 510 /**< NOT_EXTENDED */
00096   } response_code_t;
00097 
00098   /** Map of headers. */
00099   typedef std::map<std::string, std::string> HeaderMap;
00100 
00101   WebReply(response_code_t code);
00102   virtual ~WebReply();
00103 
00104   response_code_t   code() const;
00105   void              add_header(std::string header, std::string content);
00106   void              add_header(std::string header_string);
00107   const HeaderMap & headers() const;
00108   
00109 
00110  private:
00111   response_code_t  __code;
00112   HeaderMap        __headers;
00113 };
00114 
00115 class DynamicWebReply : public WebReply
00116 {
00117  public:
00118   DynamicWebReply(response_code_t code);
00119 
00120   virtual size_t chunk_size();
00121   virtual size_t size() = 0;
00122   virtual size_t next_chunk(size_t pos, char *buffer, size_t buf_max_size) = 0;
00123 };
00124 
00125 class StaticWebReply : public WebReply
00126 {
00127  public:
00128   StaticWebReply(response_code_t code, std::string body = "");
00129 
00130   void append_body(const char *format, ...);
00131   StaticWebReply & operator+=(std::string text);
00132 
00133   virtual const std::string & body();
00134   virtual std::string::size_type body_length();
00135 
00136   virtual void pack();
00137  protected:
00138   /** Body of the reply. */
00139   std::string _body;
00140 };
00141 
00142 } // end namespace fawkes
00143 
00144 #endif