fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / websocketpp-0.7.0 / websocketpp / http / response.hpp
1 /*
2  * Copyright (c) 2014, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above copyright
9  *       notice, this list of conditions and the following disclaimer in the
10  *       documentation and/or other materials provided with the distribution.
11  *     * Neither the name of the WebSocket++ Project nor the
12  *       names of its contributors may be used to endorse or promote products
13  *       derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27
28 #ifndef HTTP_PARSER_RESPONSE_HPP
29 #define HTTP_PARSER_RESPONSE_HPP
30
31 #include <iostream>
32 #include <string>
33
34 #include <websocketpp/http/parser.hpp>
35
36 namespace websocketpp {
37 namespace http {
38 namespace parser {
39
40 /// Stores, parses, and manipulates HTTP responses
41 /**
42  * http::response provides the following functionality for working with HTTP
43  * responses.
44  *
45  * - Initialize response via manually setting each element
46  * - Initialize response via reading raw bytes and parsing
47  * - Once initialized, access individual parsed elements
48  * - Once initialized, read entire response as raw bytes
49  *
50  * http::response checks for header completeness separately from the full
51  * response. Once the header is complete, the Content-Length header is read to
52  * determine when to stop reading body bytes. If no Content-Length is present
53  * ready() will never return true. It is the responsibility of the caller to
54  * consume to determine when the response is complete (ie when the connection
55  * terminates, or some other metric).
56  */
57 class response : public parser {
58 public:
59     typedef response type;
60     typedef lib::shared_ptr<type> ptr;
61
62     response()
63       : m_read(0)
64       , m_buf(lib::make_shared<std::string>())
65       , m_status_code(status_code::uninitialized)
66       , m_state(RESPONSE_LINE) {}
67
68     /// Process bytes in the input buffer
69     /**
70      * Process up to len bytes from input buffer buf. Returns the number of
71      * bytes processed. Bytes left unprocessed means bytes left over after the
72      * final header delimiters.
73      *
74      * Consume is a streaming processor. It may be called multiple times on one
75      * response and the full headers need not be available before processing can
76      * begin. If the end of the response was reached during this call to consume
77      * the ready flag will be set. Further calls to consume once ready will be
78      * ignored.
79      *
80      * Consume will throw an http::exception in the case of an error. Typical
81      * error reasons include malformed responses, incomplete responses, and max
82      * header size being reached.
83      *
84      * @param buf Pointer to byte buffer
85      * @param len Size of byte buffer
86      * @return Number of bytes processed.
87      */
88     size_t consume(char const * buf, size_t len);
89
90     /// Process bytes in the input buffer (istream version)
91     /**
92      * Process bytes from istream s. Returns the number of bytes processed. 
93      * Bytes left unprocessed means bytes left over after the final header
94      * delimiters.
95      *
96      * Consume is a streaming processor. It may be called multiple times on one
97      * response and the full headers need not be available before processing can
98      * begin. If the end of the response was reached during this call to consume
99      * the ready flag will be set. Further calls to consume once ready will be
100      * ignored.
101      *
102      * Consume will throw an http::exception in the case of an error. Typical
103      * error reasons include malformed responses, incomplete responses, and max
104      * header size being reached.
105      *
106      * @param buf Pointer to byte buffer
107      * @param len Size of byte buffer
108      * @return Number of bytes processed.
109      */
110     size_t consume(std::istream & s);
111
112     /// Returns true if the response is ready.
113     /**
114      * @note will never return true if the content length header is not present
115      */
116     bool ready() const {
117         return m_state == DONE;
118     }
119
120     /// Returns true if the response headers are fully parsed.
121     bool headers_ready() const {
122         return (m_state == BODY || m_state == DONE);
123     }
124
125     /// Returns the full raw response
126     std::string raw() const;
127
128     /// Set response status code and message
129     /**
130      * Sets the response status code to `code` and looks up the corresponding
131      * message for standard codes. Non-standard codes will be entered as Unknown
132      * use set_status(status_code::value,std::string) overload to set both
133      * values explicitly.
134      *
135      * @param code Code to set
136      * @param msg Message to set
137      */
138     void set_status(status_code::value code);
139
140     /// Set response status code and message
141     /**
142      * Sets the response status code and message to independent custom values.
143      * use set_status(status_code::value) to set the code and have the standard
144      * message be automatically set.
145      *
146      * @param code Code to set
147      * @param msg Message to set
148      */
149     void set_status(status_code::value code, std::string const & msg);
150
151     /// Return the response status code
152     status_code::value get_status_code() const {
153         return m_status_code;
154     }
155
156     /// Return the response status message
157     const std::string& get_status_msg() const {
158         return m_status_msg;
159     }
160 private:
161     /// Helper function for consume. Process response line
162     void process(std::string::iterator begin, std::string::iterator end);
163
164     /// Helper function for processing body bytes
165     size_t process_body(char const * buf, size_t len);
166
167     enum state {
168         RESPONSE_LINE = 0,
169         HEADERS = 1,
170         BODY = 2,
171         DONE = 3
172     };
173
174     std::string                     m_status_msg;
175     size_t                          m_read;
176     lib::shared_ptr<std::string>    m_buf;
177     status_code::value              m_status_code;
178     state                           m_state;
179
180 };
181
182 } // namespace parser
183 } // namespace http
184 } // namespace websocketpp
185
186 #include <websocketpp/http/impl/response.hpp>
187
188 #endif // HTTP_PARSER_RESPONSE_HPP