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. 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> 25 #include <brillo/streams/stream.h> 26 #include <libwebserv/export.h> 27 28 namespace base { 29 class Value; 30 } // namespace base 31 32 namespace libwebserv { 33 34 class ProtocolHandler; 35 36 // Response class is a proxy for HTTP response used by the request handler 37 // to provide response HTTP headers and data. 38 class LIBWEBSERV_EXPORT Response { 39 public: 40 virtual ~Response() = default; 41 42 // Adds a single HTTP response header to the response. 43 virtual void AddHeader(const std::string& header_name, 44 const std::string& value) = 0; 45 46 // Adds number of HTTP response headers to the response. 47 virtual void AddHeaders( 48 const std::vector<std::pair<std::string, std::string>>& headers) = 0; 49 50 // Generic reply method for sending arbitrary binary data response. 51 virtual void Reply(int status_code, 52 brillo::StreamPtr data_stream, 53 const std::string& mime_type) = 0; 54 55 // Reply with text body. 56 virtual void ReplyWithText(int status_code, 57 const std::string& text, 58 const std::string& mime_type) = 0; 59 60 // Reply with JSON object. The content type will be "application/json". 61 virtual void ReplyWithJson(int status_code, const base::Value* json) = 0; 62 63 // Special form for JSON response for simple objects that have a flat 64 // list of key-value pairs of string type. 65 virtual void ReplyWithJson( 66 int status_code, const std::map<std::string, std::string>& json) = 0; 67 68 // Issue a redirect response, so the client browser loads a page at 69 // the URL specified in |redirect_url|. If this is not an external URL, 70 // it must be an absolute path starting at the root "/...". 71 virtual void Redirect(int status_code, const std::string& redirect_url) = 0; 72 73 // Send a plain text response (with no Content-Type header). 74 // Usually used with error responses. |error_text| must be plain text. 75 virtual void ReplyWithError(int status_code, 76 const std::string& error_text) = 0; 77 78 // Send "404 Not Found" response. 79 virtual void ReplyWithErrorNotFound() = 0; 80 }; 81 82 } // namespace libwebserv 83 84 #endif // WEBSERVER_LIBWEBSERV_RESPONSE_H_ 85