Boost logo

Boost-Commit :

From: lists.drrngrvy_at_[hidden]
Date: 2008-05-19 15:03:58


Author: drrngrvy
Date: 2008-05-19 15:03:57 EDT (Mon, 19 May 2008)
New Revision: 45554
URL: http://svn.boost.org/trac/boost/changeset/45554

Log:
Made response, cookie and header parameterised on a char type.
Text files modified:
   sandbox/SOC/2007/cgi/trunk/boost/cgi/cookie.hpp | 32 ++++++++------
   sandbox/SOC/2007/cgi/trunk/boost/cgi/header.hpp | 89 +++++++++++++++------------------------
   sandbox/SOC/2007/cgi/trunk/boost/cgi/response.hpp | 82 +++++++++++++++++++++++-------------
   3 files changed, 106 insertions(+), 97 deletions(-)

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/cookie.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/cookie.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/cookie.hpp 2008-05-19 15:03:57 EDT (Mon, 19 May 2008)
@@ -14,12 +14,13 @@
 #include <boost/tokenizer.hpp>
 
 namespace cgi {
+ namespace common {
 
- template<typename String> struct basic_cookie;
+ template<typename CharT> struct basic_cookie;
   
   // typedefs for common usage
- typedef basic_cookie<std::string> cookie;
- typedef basic_cookie<std::wstring> wcookie;
+ typedef basic_cookie<char> cookie;
+ typedef basic_cookie<wchar_t> wcookie;
 
   /// A `basic_cookie<>` object that can be (out-) streamed
   /**
@@ -32,10 +33,11 @@
    * that takes an HttpCookie?
    * - Add from_string() ?
    */
- template<typename String>
+ template<typename CharT>
   struct basic_cookie
   {
- typedef String string_type;
+ typedef CharT char_type;
+ typedef typename std::basic_string<CharT> string_type;
 
     basic_cookie() {}
 
@@ -119,9 +121,9 @@
     */
 
     /// Make a string out of the cookie
- std::string to_string()
+ string_type to_string()
     {
- std::string str(name + "=" + value);
+ string_type str(name + "=" + value);
       if (!expires.empty()) str += ("; expires=" + expires);
       if (!path.empty() ) str += ("; path=" + path);
       if (!domain.empty() ) str += ("; domain=" + domain);
@@ -131,14 +133,16 @@
     }
   };
 
- template<typename OutStream, typename T>
- OutStream& operator<<(OutStream& os, basic_cookie<T>& ck)
- {
- os<< ck.to_string();
- return os;
- }
-
+ } // namespace common
 } // namespace cgi
 
+
+template<typename OutStream, typename T>
+inline OutStream& operator<< (OutStream& os, cgi::common::basic_cookie<T>& ck)
+{
+ os<< ck.to_string();
+ return os;
+}
+
 #endif // CGI_COOKIE_HPP_INCLUDED__
 

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/header.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/header.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/header.hpp 2008-05-19 15:03:57 EDT (Mon, 19 May 2008)
@@ -15,50 +15,34 @@
 namespace cgi {
  namespace common {
 
- //template<typename StringT = std::string>
- struct header
+ template<typename CharT>
+ struct basic_header
   {
- typedef std::string string_type;
+ typedef CharT char_type;
+ typedef typename std::basic_string<CharT> string_type;
     
- header()
+ basic_header()
       : content()
     {
     }
 
-
- /// Templated constructor to allow user-defined types to be converted
- //template<typename T>
- //header(T& t)
- // : content(t.to_string())
- //{
- //}
-
-
- //template<>
- header(const string_type& _content)
+ basic_header(const string_type& _content)
       : content(_content)
     {
     }
 
- header(const string_type& name, const string_type& val)
+ basic_header(const string_type& name, const string_type& val)
       : content(name + ": " + val)
     {
     }
 
- //header(const std::string& name, const std::string& val)
- // : content(name + ": " + val)
- //{
- //}
-
     /// Construct an header from a cookie.
- template<typename T>
- header(const basic_cookie<T>& ck)
+ basic_header(const basic_cookie<char_type>& ck)
       : content("Set-cookie: " + ck.to_string())
     {
     }
 
     string_type content;
-
   };
 
 /*
@@ -77,56 +61,53 @@
 
 
   //{ Some shortcuts, to cut down on typing errors.
- template<typename StringT>
- header
- content_type(StringT str)
+ template<typename CharT, typename StringT> basic_header<CharT>
+ content_type(StringT const& str)
   {
- return header("Content-type", str);
+ return basic_header<CharT>("Content-type", str);
   }
 
- template<typename StringT>
- header
- content_encoding(const StringT& str)
+ template<typename CharT> basic_header<CharT>
+ content_type(const CharT* str)
   {
- return header("Content-encoding", str);
+ return basic_header<CharT>("Content-type", str);
   }
 
- template<typename T>
- header
- content_length(const T& t)
+ template<typename CharT> basic_header<CharT>
+ content_encoding(std::basic_string<CharT> const& str)
   {
- return header("Content-length", boost::lexical_cast<std::string>(t));
+ return basic_header<CharT>("Content-encoding", str);
   }
 
- template<typename T, typename Traits, typename Alloc>
- header
- content_length(const std::basic_string<T, Traits, Alloc>& str)
+ template<typename CharT, typename T> basic_header<CharT>
+ content_length(const T& t)
   {
- return header("Content-length", str);
+ return basic_header<CharT>("Content-length",
+ boost::lexical_cast<std::basic_string<CharT> >(t));
   }
-/*
- template<typename StringT>
- header<StringT>
- location(const StringT& url)
+
+ template<typename CharT> basic_header<CharT>
+ content_length(std::basic_string<CharT> const& str)
   {
- return header<StringT>("Location", url);
+ return basic_header<CharT>("Content-length", str);
   }
 
- header<std::string>
- location(const std::string& url)
+ template<typename CharT> basic_header<CharT>
+ location(const CharT* url)
   {
- return header<std::string>("Location", url);
- }*/
- //template<typename T>
- header location(const std::string& url)
+ return basic_header<CharT>("Location", url);
+ }
+
+ template<typename CharT> basic_header<CharT>
+ location(std::basic_string<CharT> const& url)
   {
- return header("Location", url);
+ return basic_header<CharT>("Location", url);
   }
   //}
 
   // typedefs for typical usage
- //typedef basic_header<std::string> header;
- //typedef basic_header<std::wstring> wheader;
+ typedef basic_header<char> header;
+ typedef basic_header<wchar_t> wheader;
 
  } // namespace common
 } // namespace cgi

Modified: sandbox/SOC/2007/cgi/trunk/boost/cgi/response.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/trunk/boost/cgi/response.hpp (original)
+++ sandbox/SOC/2007/cgi/trunk/boost/cgi/response.hpp 2008-05-19 15:03:57 EDT (Mon, 19 May 2008)
@@ -60,12 +60,15 @@
  namespace common {
 
   /// The response class: a helper for responding to requests.
- class response
+ template<typename T>
+ class basic_response
   {
   public:
- typedef std::ostream ostream_type;
+ typedef T char_type;
+ typedef typename std::basic_string<T> string_type;
+ typedef typename std::basic_ostream<T> ostream_type;
 
- response(http::status_code sc = http::ok)
+ basic_response(http::status_code sc = http::ok)
       : buffer_(new ::cgi::streambuf())
       , ostream_(buffer_.get())
       , http_status_(sc)
@@ -78,14 +81,14 @@
      * Takes the buffer and uses it internally, does nothing with it on
      * destruction.
      */
- response(::cgi::streambuf* buf, http::status_code sc = http::ok)
+ basic_response(::cgi::streambuf* buf, http::status_code sc = http::ok)
       : /*request_(NULL)
           , */ostream_(buf)
       , http_status_(sc)
     {
     }
 
- ~response()
+ ~basic_response()
     {
     }
 
@@ -106,13 +109,13 @@
     }
 
     // provide this too?
- std::size_t write(const char* str, std::size_t len)
+ std::size_t write(const char_type* str, std::size_t len)
     {
       ostream_.write(str, len);
       return len;
     }
 
- std::size_t write(const std::string& str)
+ std::size_t write(const string_type& str)
     {
       return write(str.c_str(), str.size());
     }
@@ -228,7 +231,7 @@
     void async_send(AsyncWriteStream& aws, Handler handler)
     {
       aws.io_service().post(
- boost::bind(&response::do_async_send, aws, handler)
+ boost::bind(&basic_response<char_type>::do_async_send, aws, handler)
       );
     }
 
@@ -256,7 +259,8 @@
     }
 
     /// Set the status code associated with the response.
- response& set_status(const http::status_code& num)
+ basic_response<char_type>&
+ set_status(const http::status_code& num)
     {
       http_status_ = num;
       return *this;
@@ -281,7 +285,8 @@
     }
 
     /// Add a header after appending the CRLF sequence.
- response& set_header(const std::string& value)
+ basic_response<char_type>&
+ set_header(const string_type& value)
     {
       BOOST_ASSERT(!headers_terminated_);
       headers_.push_back(value + "\r\n");
@@ -289,7 +294,8 @@
     }
 
     /// Format and add a header given name and value, appending CRLF.
- response& set_header(const std::string& name, const std::string& value)
+ basic_response<char_type>&
+ set_header(string_type const& name, string_type const& value)
     {
       BOOST_ASSERT(!headers_terminated_);
       headers_.push_back(name + ": " + value + "\r\n");
@@ -312,9 +318,18 @@
     {
       return headers_terminated_;
     }
+
+ // Is this really necessary?
+ void end_headers()
+ {
+ headers_terminated_ = true;
+ }
+
+ /// Get the ostream containing the response body.
+ ostream_type& ostream() { return ostream_; }
   protected:
     // Vector of all the headers, each followed by a CRLF
- std::vector<std::string> headers_;
+ std::vector<string_type> headers_;
 
     // The buffer is a shared_ptr, so you can keep it cached elsewhere.
     boost::shared_ptr<common::streambuf> buffer_;
@@ -326,8 +341,8 @@
     // True if no more headers can be appended.
     bool headers_terminated_;
 
- template<typename T>
- friend response& operator<<(response& resp, const T& t);
+ //template<typename T>
+ //friend response& operator<<(response& resp, const T& t);
 
     //template<typename A, typename B>
     //friend A& operator<<(A& resp, B b);
@@ -348,7 +363,7 @@
 
       //{ Construct a ConstBufferSequence out of the headers we have.
       //std::vector<boost::asio::const_buffer> headers;
- typedef std::vector<std::string>::iterator iter;
+ typedef typename std::vector<string_type>::iterator iter;
       for (iter i(headers_.begin()), e(headers_.end()); i != e; ++i)
       {
         headers.push_back(common::buffer(*i));
@@ -360,11 +375,19 @@
     }
    };
 
+ /// Typedefs for typical usage.
+ typedef basic_response<char> response;
+ typedef basic_response<wchar_t> wresponse; // **FIXME** (untested)
+
+ } // namespace common
+} // namespace cgi
+
   /// Generic ostream template
- template<typename T>
- response& operator<<(response& resp, const T& t)
+ template<typename charT, typename T>
+ inline cgi::common::basic_response<charT>&
+ operator<< (cgi::common::basic_response<charT>& resp, const T& t)
   {
- resp.ostream_<< t;
+ resp.ostream()<< t;
     return resp;
   }
 
@@ -397,15 +420,17 @@
     }
   }*/
 
- template<>
- response& operator<<(response& resp, const ::cgi::common::header& hdr)
+ template<typename charT>
+ inline cgi::common::basic_response<charT>&
+ operator<< (cgi::common::basic_response<charT>& resp
+ , const ::cgi::common::header& hdr)
   {
     if (hdr.content.empty()) {
- resp.headers_terminated_ = true;
+ resp.end_headers();
       return resp;
     }else{
       // We shouldn't allow headers to be sent after they're explicitly ended.
- BOOST_ASSERT(!resp.headers_terminated_);
+ BOOST_ASSERT(!resp.headers_terminated());
       resp.set_header(hdr.content);
       return resp;
     }
@@ -422,24 +447,23 @@
    * http://tinyurl.com/33znkj), but this is outside the scope of this
    * library.
    */
- template<typename T>
- response& operator<<(response& resp, basic_cookie<T> ck)
+ template<typename charT, typename T>
+ inline cgi::common::basic_response<charT>&
+ operator<<(cgi::common::basic_response<charT>& resp, cgi::common::basic_cookie<T> ck)
   {
     BOOST_ASSERT(!resp.headers_terminated());
     resp.set_header("Set-cookie", ck.to_string());
     return resp;
   }
 
- template<typename T>
- response& operator<<(response& resp, http::status_code status)
+ template<typename charT, typename T>
+ inline cgi::common::basic_response<charT>&
+ operator<<(cgi::common::basic_response<charT>& resp, cgi::http::status_code status)
   {
     BOOST_ASSERT(!resp.headers_terminated());
     return resp.set_status(status);
   }
 
- } // namespace common
-} // namespace cgi
-
 #undef BOOST_CGI_ADD_DEFAULT_HEADER
 
 #include "boost/cgi/detail/pop_options.hpp"


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk