blob: 1919c0966a1c1e2667c93a8e43b64c9bf9d6ca73 [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 Vakulenko330a10f2015-09-23 16:51:02 -070025#include <chromeos/streams/stream.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080026#include <libwebserv/export.h>
27
28namespace base {
29class Value;
30} // namespace base
31
Alex Vakulenko039da312015-02-03 08:58:55 -080032namespace libwebserv {
33
Alex Vakulenko31a63792015-02-03 12:44:57 -080034class ProtocolHandler;
Alex Vakulenko039da312015-02-03 08:58:55 -080035
36// Response class is a proxy for HTTP response used by the request handler
37// to provide response HTTP headers and data.
Alex Vakulenko31a63792015-02-03 12:44:57 -080038class LIBWEBSERV_EXPORT Response final {
Alex Vakulenko039da312015-02-03 08:58:55 -080039 public:
40 ~Response();
41
Alex Vakulenko039da312015-02-03 08:58:55 -080042 // Adds a single HTTP response header to the response.
43 void AddHeader(const std::string& header_name, const std::string& value);
44
45 // Adds number of HTTP response headers to the response.
46 void AddHeaders(
47 const std::vector<std::pair<std::string, std::string>>& headers);
48
49 // Generic reply method for sending arbitrary binary data response.
50 void Reply(int status_code,
Alex Vakulenko330a10f2015-09-23 16:51:02 -070051 chromeos::StreamPtr data_stream,
Alex Vakulenko039da312015-02-03 08:58:55 -080052 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};
Alex Vakulenko330a10f2015-09-23 16:51:02 -070090 chromeos::StreamPtr data_stream_;
Alex Vakulenko039da312015-02-03 08:58:55 -080091 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_