| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef WEBSERVER_LIBWEBSERV_RESPONSE_H_ |
| 6 | #define WEBSERVER_LIBWEBSERV_RESPONSE_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <base/macros.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 15 | #include <libwebserv/export.h> |
| 16 | |
| 17 | namespace base { |
| 18 | class Value; |
| 19 | } // namespace base |
| 20 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 21 | namespace libwebserv { |
| 22 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame^] | 23 | class ProtocolHandler; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 24 | |
| 25 | // Response class is a proxy for HTTP response used by the request handler |
| 26 | // to provide response HTTP headers and data. |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame^] | 27 | class LIBWEBSERV_EXPORT Response final { |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 28 | public: |
| 29 | ~Response(); |
| 30 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 31 | // Adds a single HTTP response header to the response. |
| 32 | void AddHeader(const std::string& header_name, const std::string& value); |
| 33 | |
| 34 | // Adds number of HTTP response headers to the response. |
| 35 | void AddHeaders( |
| 36 | const std::vector<std::pair<std::string, std::string>>& headers); |
| 37 | |
| 38 | // Generic reply method for sending arbitrary binary data response. |
| 39 | void Reply(int status_code, |
| 40 | const void* data, |
| 41 | size_t data_size, |
| 42 | const std::string& mime_type); |
| 43 | |
| 44 | // Reply with text body. |
| 45 | void ReplyWithText(int status_code, |
| 46 | const std::string& text, |
| 47 | const std::string& mime_type); |
| 48 | |
| 49 | // Reply with JSON object. The content type will be "application/json". |
| 50 | void ReplyWithJson(int status_code, const base::Value* json); |
| 51 | |
| 52 | // Special form for JSON response for simple objects that have a flat |
| 53 | // list of key-value pairs of string type. |
| 54 | void ReplyWithJson(int status_code, |
| 55 | const std::map<std::string, std::string>& json); |
| 56 | |
| 57 | // Issue a redirect response, so the client browser loads a page at |
| 58 | // the URL specified in |redirect_url|. If this is not an external URL, |
| 59 | // it must be an absolute path starting at the root "/...". |
| 60 | void Redirect(int status_code, const std::string& redirect_url); |
| 61 | |
| 62 | // Send a plain text response (with no Content-Type header). |
| 63 | // Usually used with error responses. |error_text| must be plain text. |
| 64 | void ReplyWithError(int status_code, const std::string& error_text); |
| 65 | |
| 66 | // Send "404 Not Found" response. |
| 67 | void ReplyWithErrorNotFound(); |
| 68 | |
| 69 | private: |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame^] | 70 | friend class ProtocolHandler; |
| 71 | |
| 72 | LIBWEBSERV_PRIVATE Response(ProtocolHandler* handler, |
| 73 | const std::string& request_id); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 74 | |
| 75 | LIBWEBSERV_PRIVATE void SendResponse(); |
| 76 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame^] | 77 | ProtocolHandler* handler_{nullptr}; |
| 78 | std::string request_id_; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 79 | int status_code_{0}; |
| 80 | std::vector<uint8_t> data_; |
| 81 | std::multimap<std::string, std::string> headers_; |
| 82 | bool reply_sent_{false}; |
| 83 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 84 | DISALLOW_COPY_AND_ASSIGN(Response); |
| 85 | }; |
| 86 | |
| 87 | } // namespace libwebserv |
| 88 | |
| 89 | #endif // WEBSERVER_LIBWEBSERV_RESPONSE_H_ |