| Daniel Erat | 35f6587 | 2015-08-17 20:59:29 -0600 | [diff] [blame] | 1 | // Copyright 2015 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 14 | |
| 15 | #ifndef WEBSERVER_LIBWEBSERV_RESPONSE_H_ |
| 16 | #define WEBSERVER_LIBWEBSERV_RESPONSE_H_ |
| 17 | |
| 18 | #include <map> |
| 19 | #include <memory> |
| 20 | #include <string> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <base/macros.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 25 | #include <libwebserv/export.h> |
| 26 | |
| 27 | namespace base { |
| 28 | class Value; |
| 29 | } // namespace base |
| 30 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 31 | namespace libwebserv { |
| 32 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 33 | class ProtocolHandler; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 34 | |
| 35 | // Response class is a proxy for HTTP response used by the request handler |
| 36 | // to provide response HTTP headers and data. |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 37 | class LIBWEBSERV_EXPORT Response final { |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 38 | public: |
| 39 | ~Response(); |
| 40 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 41 | // Adds a single HTTP response header to the response. |
| 42 | void AddHeader(const std::string& header_name, const std::string& value); |
| 43 | |
| 44 | // Adds number of HTTP response headers to the response. |
| 45 | void AddHeaders( |
| 46 | const std::vector<std::pair<std::string, std::string>>& headers); |
| 47 | |
| 48 | // Generic reply method for sending arbitrary binary data response. |
| 49 | void Reply(int status_code, |
| 50 | const void* data, |
| 51 | size_t data_size, |
| 52 | const std::string& mime_type); |
| 53 | |
| 54 | // Reply with text body. |
| 55 | void ReplyWithText(int status_code, |
| 56 | const std::string& text, |
| 57 | const std::string& mime_type); |
| 58 | |
| 59 | // Reply with JSON object. The content type will be "application/json". |
| 60 | void ReplyWithJson(int status_code, const base::Value* json); |
| 61 | |
| 62 | // Special form for JSON response for simple objects that have a flat |
| 63 | // list of key-value pairs of string type. |
| 64 | void ReplyWithJson(int status_code, |
| 65 | const std::map<std::string, std::string>& json); |
| 66 | |
| 67 | // Issue a redirect response, so the client browser loads a page at |
| 68 | // the URL specified in |redirect_url|. If this is not an external URL, |
| 69 | // it must be an absolute path starting at the root "/...". |
| 70 | void Redirect(int status_code, const std::string& redirect_url); |
| 71 | |
| 72 | // Send a plain text response (with no Content-Type header). |
| 73 | // Usually used with error responses. |error_text| must be plain text. |
| 74 | void ReplyWithError(int status_code, const std::string& error_text); |
| 75 | |
| 76 | // Send "404 Not Found" response. |
| 77 | void ReplyWithErrorNotFound(); |
| 78 | |
| 79 | private: |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 80 | friend class ProtocolHandler; |
| 81 | |
| 82 | LIBWEBSERV_PRIVATE Response(ProtocolHandler* handler, |
| 83 | const std::string& request_id); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 84 | |
| 85 | LIBWEBSERV_PRIVATE void SendResponse(); |
| 86 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 87 | ProtocolHandler* handler_{nullptr}; |
| 88 | std::string request_id_; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 89 | int status_code_{0}; |
| 90 | std::vector<uint8_t> data_; |
| 91 | std::multimap<std::string, std::string> headers_; |
| 92 | bool reply_sent_{false}; |
| 93 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 94 | DISALLOW_COPY_AND_ASSIGN(Response); |
| 95 | }; |
| 96 | |
| 97 | } // namespace libwebserv |
| 98 | |
| 99 | #endif // WEBSERVER_LIBWEBSERV_RESPONSE_H_ |