From a6ce671c56679c8b20ca79ad56bad50b5d96dc66 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sun, 3 Mar 2013 17:34:41 -0600 Subject: [PATCH] moves body to parser rather than response since requests can have bodies too. --- websocketpp/http/impl/parser.hpp | 12 ++++++++++++ websocketpp/http/impl/request.hpp | 2 +- websocketpp/http/impl/response.hpp | 14 -------------- websocketpp/http/parser.hpp | 19 ++++++++++++++++++- websocketpp/http/response.hpp | 17 ----------------- 5 files changed, 31 insertions(+), 33 deletions(-) diff --git a/websocketpp/http/impl/parser.hpp b/websocketpp/http/impl/parser.hpp index f86c291bb0..bbb535146e 100644 --- a/websocketpp/http/impl/parser.hpp +++ b/websocketpp/http/impl/parser.hpp @@ -108,6 +108,18 @@ inline void parser::remove_header(const std::string &key) { m_headers.erase(key); } +inline void parser::set_body(const std::string& value) { + if (value.size() == 0) { + remove_header("Content-Length"); + m_body = ""; + return; + } + + std::stringstream foo; + foo << value.size(); + replace_header("Content-Length", foo.str()); + m_body = value; +} inline bool parser::parse_headers(std::istream& s) { std::string header; diff --git a/websocketpp/http/impl/request.hpp b/websocketpp/http/impl/request.hpp index 7e7d10b5b4..d4b697b8e2 100644 --- a/websocketpp/http/impl/request.hpp +++ b/websocketpp/http/impl/request.hpp @@ -136,7 +136,7 @@ inline std::string request::raw() { std::stringstream raw; raw << m_method << " " << m_uri << " " << get_version() << "\r\n"; - raw << raw_headers() << "\r\n"; + raw << raw_headers() << "\r\n" << m_body; return raw.str(); } diff --git a/websocketpp/http/impl/response.hpp b/websocketpp/http/impl/response.hpp index 839f67c692..b3e0a18f4c 100644 --- a/websocketpp/http/impl/response.hpp +++ b/websocketpp/http/impl/response.hpp @@ -185,20 +185,6 @@ inline void response::set_status(status_code::value code, const std::string& m_status_code = code; m_status_msg = msg; } - -inline void response::set_body(const std::string& value) { - if (value.size() == 0) { - remove_header("Content-Length"); - m_body = ""; - return; - } - - std::stringstream foo; - foo << value.size(); - replace_header("Content-Length", foo.str()); - m_body = value; -} - inline void response::process(std::string::iterator begin, std::string::iterator end) diff --git a/websocketpp/http/parser.hpp b/websocketpp/http/parser.hpp index e6add109c5..ddf5439833 100644 --- a/websocketpp/http/parser.hpp +++ b/websocketpp/http/parser.hpp @@ -355,6 +355,11 @@ public: */ const std::string& get_header(const std::string& key) const; + /// Get the body string + const std::string& get_body() const { + return m_body; + } + /// Get the HTTP header with name `key` /** * @@ -392,6 +397,17 @@ public: /// Remove a header void remove_header(const std::string &key); + + /// Set body content + /** + * Set the body content of the HTTP response to the parameter string. Note + * set_body will also set the Content-Length HTTP header to the appropriate + * value. If you want the Content-Length header to be something else, do so + * via replace_header("Content-Length") after calling set_body() + * + * @param value String data to include as the body content. + */ + void set_body(const std::string& value); protected: /// DEPRECATED Read headers out of an istream bool parse_headers(std::istream& s); @@ -401,9 +417,10 @@ protected: /// Return headers in raw string form. std::string raw_headers() const; -private: + std::string m_version; header_list m_headers; + std::string m_body; }; } // namespace parser diff --git a/websocketpp/http/response.hpp b/websocketpp/http/response.hpp index e989b4305d..c31ef199b3 100644 --- a/websocketpp/http/response.hpp +++ b/websocketpp/http/response.hpp @@ -123,17 +123,6 @@ public: */ void set_status(status_code::value code, const std::string& msg); - /// Set response body content - /** - * Set the body content of the HTTP response to the parameter string. Note - * set_body will also set the Content-Length HTTP header to the appropriate - * value. If you want the Content-Length header to be something else set it - * to something else after calling set_body - * - * @param value String data to include as the body content. - */ - void set_body(const std::string& value); - /// Return the response status code status_code::value get_status_code() const { return m_status_code; @@ -143,11 +132,6 @@ public: const std::string& get_status_msg() const { return m_status_msg; } - - /// Return the body string - const std::string& get_body() const { - return m_body; - } private: /// Helper function for consume. Process response line void process(std::string::iterator begin, std::string::iterator end); @@ -163,7 +147,6 @@ private: }; std::string m_status_msg; - std::string m_body; size_t m_read; lib::shared_ptr m_buf; status_code::value m_status_code;