blob: f3f2adf64fbb4b8b6dab1f04f7b9fcb4725f9bdf [file] [log] [blame]
Daniel Erat35f65872015-08-17 20:59:29 -06001// 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 Vakulenko039da312015-02-03 08:58:55 -080014
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 Vakulenko039da312015-02-03 08:58:55 -080025#include <libwebserv/export.h>
26
27namespace base {
28class Value;
29} // namespace base
30
Alex Vakulenko039da312015-02-03 08:58:55 -080031namespace libwebserv {
32
Alex Vakulenko31a63792015-02-03 12:44:57 -080033class ProtocolHandler;
Alex Vakulenko039da312015-02-03 08:58:55 -080034
35// Response class is a proxy for HTTP response used by the request handler
36// to provide response HTTP headers and data.
Alex Vakulenko31a63792015-02-03 12:44:57 -080037class LIBWEBSERV_EXPORT Response final {
Alex Vakulenko039da312015-02-03 08:58:55 -080038 public:
39 ~Response();
40
Alex Vakulenko039da312015-02-03 08:58:55 -080041 // 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 Vakulenko31a63792015-02-03 12:44:57 -080080 friend class ProtocolHandler;
81
82 LIBWEBSERV_PRIVATE Response(ProtocolHandler* handler,
83 const std::string& request_id);
Alex Vakulenko039da312015-02-03 08:58:55 -080084
85 LIBWEBSERV_PRIVATE void SendResponse();
86
Alex Vakulenko31a63792015-02-03 12:44:57 -080087 ProtocolHandler* handler_{nullptr};
88 std::string request_id_;
Alex Vakulenko039da312015-02-03 08:58:55 -080089 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 Vakulenko039da312015-02-03 08:58:55 -080094 DISALLOW_COPY_AND_ASSIGN(Response);
95};
96
97} // namespace libwebserv
98
99#endif // WEBSERVER_LIBWEBSERV_RESPONSE_H_