| 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> |
| 15 | #include <base/memory/ref_counted.h> |
| 16 | #include <base/memory/scoped_ptr.h> |
| 17 | #include <libwebserv/export.h> |
| 18 | |
| 19 | namespace base { |
| 20 | class Value; |
| 21 | } // namespace base |
| 22 | |
| 23 | struct MHD_Connection; |
| 24 | |
| 25 | namespace libwebserv { |
| 26 | |
| 27 | class Connection; |
| 28 | |
| 29 | // Response class is a proxy for HTTP response used by the request handler |
| 30 | // to provide response HTTP headers and data. |
| 31 | class LIBWEBSERV_EXPORT Response |
| 32 | : public std::enable_shared_from_this<Response> { |
| 33 | public: |
| 34 | ~Response(); |
| 35 | |
| 36 | // Factory constructor method. |
| 37 | static scoped_ptr<Response> Create( |
| 38 | const scoped_refptr<Connection>& connection); |
| 39 | |
| 40 | // Adds a single HTTP response header to the response. |
| 41 | void AddHeader(const std::string& header_name, const std::string& value); |
| 42 | |
| 43 | // Adds number of HTTP response headers to the response. |
| 44 | void AddHeaders( |
| 45 | const std::vector<std::pair<std::string, std::string>>& headers); |
| 46 | |
| 47 | // Generic reply method for sending arbitrary binary data response. |
| 48 | void Reply(int status_code, |
| 49 | const void* data, |
| 50 | size_t data_size, |
| 51 | const std::string& mime_type); |
| 52 | |
| 53 | // Reply with text body. |
| 54 | void ReplyWithText(int status_code, |
| 55 | const std::string& text, |
| 56 | const std::string& mime_type); |
| 57 | |
| 58 | // Reply with JSON object. The content type will be "application/json". |
| 59 | void ReplyWithJson(int status_code, const base::Value* json); |
| 60 | |
| 61 | // Special form for JSON response for simple objects that have a flat |
| 62 | // list of key-value pairs of string type. |
| 63 | void ReplyWithJson(int status_code, |
| 64 | const std::map<std::string, std::string>& json); |
| 65 | |
| 66 | // Issue a redirect response, so the client browser loads a page at |
| 67 | // the URL specified in |redirect_url|. If this is not an external URL, |
| 68 | // it must be an absolute path starting at the root "/...". |
| 69 | void Redirect(int status_code, const std::string& redirect_url); |
| 70 | |
| 71 | // Send a plain text response (with no Content-Type header). |
| 72 | // Usually used with error responses. |error_text| must be plain text. |
| 73 | void ReplyWithError(int status_code, const std::string& error_text); |
| 74 | |
| 75 | // Send "404 Not Found" response. |
| 76 | void ReplyWithErrorNotFound(); |
| 77 | |
| 78 | private: |
| 79 | LIBWEBSERV_PRIVATE explicit Response( |
| 80 | const scoped_refptr<Connection>& connection); |
| 81 | |
| 82 | LIBWEBSERV_PRIVATE void SendResponse(); |
| 83 | |
| 84 | scoped_refptr<Connection> connection_; |
| 85 | int status_code_{0}; |
| 86 | std::vector<uint8_t> data_; |
| 87 | std::multimap<std::string, std::string> headers_; |
| 88 | bool reply_sent_{false}; |
| 89 | |
| 90 | friend class Connection; |
| 91 | friend class ResponseHelper; |
| 92 | DISALLOW_COPY_AND_ASSIGN(Response); |
| 93 | }; |
| 94 | |
| 95 | } // namespace libwebserv |
| 96 | |
| 97 | #endif // WEBSERVER_LIBWEBSERV_RESPONSE_H_ |