| // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef WEBSERVER_LIBWEBSERV_RESPONSE_H_ |
| #define WEBSERVER_LIBWEBSERV_RESPONSE_H_ |
| |
| #include <map> |
| #include <memory> |
| #include <string> |
| #include <utility> |
| #include <vector> |
| |
| #include <base/macros.h> |
| #include <libwebserv/export.h> |
| |
| namespace base { |
| class Value; |
| } // namespace base |
| |
| namespace libwebserv { |
| |
| class ProtocolHandler; |
| |
| // Response class is a proxy for HTTP response used by the request handler |
| // to provide response HTTP headers and data. |
| class LIBWEBSERV_EXPORT Response final { |
| public: |
| ~Response(); |
| |
| // Adds a single HTTP response header to the response. |
| void AddHeader(const std::string& header_name, const std::string& value); |
| |
| // Adds number of HTTP response headers to the response. |
| void AddHeaders( |
| const std::vector<std::pair<std::string, std::string>>& headers); |
| |
| // Generic reply method for sending arbitrary binary data response. |
| void Reply(int status_code, |
| const void* data, |
| size_t data_size, |
| const std::string& mime_type); |
| |
| // Reply with text body. |
| void ReplyWithText(int status_code, |
| const std::string& text, |
| const std::string& mime_type); |
| |
| // Reply with JSON object. The content type will be "application/json". |
| void ReplyWithJson(int status_code, const base::Value* json); |
| |
| // Special form for JSON response for simple objects that have a flat |
| // list of key-value pairs of string type. |
| void ReplyWithJson(int status_code, |
| const std::map<std::string, std::string>& json); |
| |
| // Issue a redirect response, so the client browser loads a page at |
| // the URL specified in |redirect_url|. If this is not an external URL, |
| // it must be an absolute path starting at the root "/...". |
| void Redirect(int status_code, const std::string& redirect_url); |
| |
| // Send a plain text response (with no Content-Type header). |
| // Usually used with error responses. |error_text| must be plain text. |
| void ReplyWithError(int status_code, const std::string& error_text); |
| |
| // Send "404 Not Found" response. |
| void ReplyWithErrorNotFound(); |
| |
| private: |
| friend class ProtocolHandler; |
| |
| LIBWEBSERV_PRIVATE Response(ProtocolHandler* handler, |
| const std::string& request_id); |
| |
| LIBWEBSERV_PRIVATE void SendResponse(); |
| |
| ProtocolHandler* handler_{nullptr}; |
| std::string request_id_; |
| int status_code_{0}; |
| std::vector<uint8_t> data_; |
| std::multimap<std::string, std::string> headers_; |
| bool reply_sent_{false}; |
| |
| DISALLOW_COPY_AND_ASSIGN(Response); |
| }; |
| |
| } // namespace libwebserv |
| |
| #endif // WEBSERVER_LIBWEBSERV_RESPONSE_H_ |