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_IMPL_H_
16 #define WEBSERVER_LIBWEBSERV_RESPONSE_IMPL_H_
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include <base/macros.h>
23 #include <brillo/streams/stream.h>
24 #include <libwebserv/response.h>
25 
26 namespace libwebserv {
27 
28 class DBusProtocolHandler;
29 
30 // Implementation of the Response interface.
31 class ResponseImpl final : public Response {
32  public:
33   ~ResponseImpl() override;
34 
35   // Overrides from Response.
36   void AddHeader(const std::string& header_name,
37                  const std::string& value) override;
38   void AddHeaders(
39       const std::vector<std::pair<std::string, std::string>>& headers) override;
40   void Reply(int status_code,
41              brillo::StreamPtr data_stream,
42              const std::string& mime_type) override;
43   void ReplyWithText(int status_code,
44                      const std::string& text,
45                      const std::string& mime_type) override;
46   void ReplyWithJson(int status_code, const base::Value* json) override;
47   void ReplyWithJson(int status_code,
48                      const std::map<std::string, std::string>& json) override;
49   void Redirect(int status_code, const std::string& redirect_url) override;
50   void ReplyWithError(int status_code, const std::string& error_text) override;
51   void ReplyWithErrorNotFound() override;
52 
53  private:
54   friend class DBusProtocolHandler;
55 
56   LIBWEBSERV_PRIVATE ResponseImpl(DBusProtocolHandler* handler,
57                                   const std::string& request_id);
58 
59   LIBWEBSERV_PRIVATE void SendResponse();
60 
61   DBusProtocolHandler* handler_{nullptr};
62   std::string request_id_;
63   int status_code_{0};
64   brillo::StreamPtr data_stream_;
65   std::multimap<std::string, std::string> headers_;
66   bool reply_sent_{false};
67 
68   DISALLOW_COPY_AND_ASSIGN(ResponseImpl);
69 };
70 
71 }  // namespace libwebserv
72 
73 #endif  // WEBSERVER_LIBWEBSERV_RESPONSE_IMPL_H_
74