CppCMS
cppcms/http_response.h
00001 
00002 //                                                                             
00003 //  Copyright (C) 2008-2012  Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>     
00004 //                                                                             
00005 //  See accompanying file COPYING.TXT file for licensing details.
00006 //
00008 #ifndef CPPCMS_HTTP_RESPONSE_H
00009 #define CPPCMS_HTTP_RESPONSE_H
00010 
00011 #include <cppcms/defs.h>
00012 #include <booster/noncopyable.h>
00013 #include <booster/hold_ptr.h>
00014 
00015 #include <string>
00016 #include <iostream>
00017 #include <cppcms/cstdint.h>
00018 
00019 namespace cppcms {
00020 class cache_interface;
00021 namespace impl { namespace cgi { class connection;  }}
00022 namespace http {
00023 
00024         class context;
00025         class cookie;
00026 
00031         class CPPCMS_API response : public booster::noncopyable {
00032         public:
00036                 typedef enum {
00037                         continue_transfer = 100,
00038                         switching_protocol = 101,
00039                         ok = 200,
00040                         created = 201,
00041                         accepted = 202,
00042                         non_authoritative_information = 203,
00043                         no_content = 204,
00044                         reset_content = 205,
00045                         partial_content = 206,
00046                         multiple_choices = 300,
00047                         moved_permanently = 301,
00048                         found = 302,
00049                         see_other = 303,
00050                         not_modified = 304,
00051                         use_proxy = 305,
00052                         temporary_redirect = 307,
00053                         bad_request = 400,
00054                         unauthorized = 401,
00055                         payment_required = 402,
00056                         forbidden = 403,
00057                         not_found = 404,
00058                         method_not_allowed = 405,
00059                         not_acceptable = 406,
00060                         proxy_authentication_required = 407,
00061                         request_time_out = 408,
00062                         conflict = 409,
00063                         gone = 410,
00064                         precondition_failed = 412,
00065                         request_entity_too_large = 413,
00066                         request_uri_too_large = 414,
00067                         unsupported_media_type = 415,
00068                         requested_range_not_satisfiable = 416,
00069                         expectation_failed = 417,
00070                         internal_server_error = 500,
00071                         not_implemented = 501,
00072                         bad_gateway = 502,
00073                         service_unavailable = 503,
00074                         gateway_timeout = 504,
00075                         http_version_not_supported = 505
00076                 } status_type;
00077 
00083                 typedef enum {
00084                         normal, 
00085                         nogzip, 
00086                         raw,    
00087 
00088                         asynchronous,
00091                         asynchronous_raw
00093 
00094                 } io_mode_type;
00095 
00096 
00097                 // Standard HTTP Response Headers RFC 2616
00098 
00102                 void accept_ranges(std::string const &);
00106                 void age(unsigned seconds);
00110                 void allow(std::string const &);
00114                 void cache_control(std::string const &);
00120                 void content_encoding(std::string const &);
00124                 void content_language(std::string const &);
00128                 void content_length(unsigned long long len);
00132                 void content_location(std::string const &);
00136                 void content_md5(std::string const &);
00140                 void content_range(std::string const &);
00144                 void content_type(std::string const &);
00148                 void date(time_t);
00152                 void etag(std::string const &);
00156                 void expires(time_t t);
00160                 void last_modified(time_t t);
00164                 void location(std::string const &);
00168                 void pragma(std::string const &);
00172                 void proxy_authenticate(std::string const &);
00176                 void retry_after(std::string const &);
00180                 void retry_after(unsigned);
00184                 void status(int code);
00188                 void status(int code,std::string const &message);
00192                 void trailer(std::string const &);
00196                 void transfer_encoding(std::string const &);
00200                 void vary(std::string const &);
00204                 void via(std::string const &);
00208                 void warning(std::string const &);
00212                 void www_authenticate(std::string const &);
00213 
00214 
00218                 void set_header(std::string const &name,std::string const &value);
00222                 std::string get_header(std::string const &name);
00226                 void erase_header(std::string const &h);
00227 
00232                 void set_content_header(std::string const &content_type);
00233 
00237                 void set_html_header();
00241                 void set_xhtml_header();
00245                 void set_plain_text_header();
00249                 void set_redirect_header(std::string const &location,int status = found);
00253                 void set_cookie(cookie const &);
00254         
00260                 void make_error_response(int stat,std::string const &msg = std::string());
00261 
00265                 io_mode_type io_mode();
00271                 void io_mode(io_mode_type);
00272 
00282                 std::ostream &out();
00283 
00287                 static std::string make_http_time(time_t);
00291                 static char const *status_to_string(int status);
00292 
00297                 bool some_output_was_written();
00302                 void finalize();
00303 
00305                 response(context &);
00306                 ~response();
00308         private:
00309                 friend class impl::cgi::connection;
00310                 friend class ::cppcms::cache_interface;
00311 
00312                 void copy_to_cache();
00313                 std::string copied_data();
00314                 bool need_gzip();
00315 
00316                 std::pair<char const *,size_t> output();
00317 
00318                 void write_http_headers(std::ostream &);
00319                 std::string get_async_chunk();
00320 
00321                 struct _data;
00322                 booster::hold_ptr<_data> d;
00323 
00324                 context &context_;
00325                 std::ostream *stream_;
00326                 io_mode_type io_mode_;
00327 
00328                 uint32_t disable_compression_ : 1;
00329                 uint32_t ostream_requested_ : 1;
00330                 uint32_t copy_to_cache_ : 1;
00331                 uint32_t finalized_ : 1;
00332                 uint32_t reserved_ : 28;
00333         };
00334 
00335 } /* http */
00336 } /* cppcms */
00337 
00338 
00339 #endif