1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li
5*1a96fba6SXin Li #include <brillo/http/http_connection_fake.h>
6*1a96fba6SXin Li
7*1a96fba6SXin Li #include <base/bind.h>
8*1a96fba6SXin Li #include <base/logging.h>
9*1a96fba6SXin Li #include <brillo/http/http_request.h>
10*1a96fba6SXin Li #include <brillo/mime_utils.h>
11*1a96fba6SXin Li #include <brillo/streams/memory_stream.h>
12*1a96fba6SXin Li #include <brillo/strings/string_utils.h>
13*1a96fba6SXin Li
14*1a96fba6SXin Li namespace brillo {
15*1a96fba6SXin Li namespace http {
16*1a96fba6SXin Li namespace fake {
17*1a96fba6SXin Li
Connection(const std::string & url,const std::string & method,const std::shared_ptr<http::Transport> & transport)18*1a96fba6SXin Li Connection::Connection(const std::string& url,
19*1a96fba6SXin Li const std::string& method,
20*1a96fba6SXin Li const std::shared_ptr<http::Transport>& transport)
21*1a96fba6SXin Li : http::Connection(transport), request_(url, method) {
22*1a96fba6SXin Li VLOG(1) << "fake::Connection created: " << method;
23*1a96fba6SXin Li }
24*1a96fba6SXin Li
~Connection()25*1a96fba6SXin Li Connection::~Connection() {
26*1a96fba6SXin Li VLOG(1) << "fake::Connection destroyed";
27*1a96fba6SXin Li }
28*1a96fba6SXin Li
SendHeaders(const HeaderList & headers,brillo::ErrorPtr *)29*1a96fba6SXin Li bool Connection::SendHeaders(const HeaderList& headers,
30*1a96fba6SXin Li brillo::ErrorPtr* /* error */) {
31*1a96fba6SXin Li request_.AddHeaders(headers);
32*1a96fba6SXin Li return true;
33*1a96fba6SXin Li }
34*1a96fba6SXin Li
SetRequestData(StreamPtr stream,brillo::ErrorPtr *)35*1a96fba6SXin Li bool Connection::SetRequestData(StreamPtr stream,
36*1a96fba6SXin Li brillo::ErrorPtr* /* error */) {
37*1a96fba6SXin Li request_.SetData(std::move(stream));
38*1a96fba6SXin Li return true;
39*1a96fba6SXin Li }
40*1a96fba6SXin Li
FinishRequest(brillo::ErrorPtr *)41*1a96fba6SXin Li bool Connection::FinishRequest(brillo::ErrorPtr* /* error */) {
42*1a96fba6SXin Li using brillo::string_utils::ToString;
43*1a96fba6SXin Li request_.AddHeaders(
44*1a96fba6SXin Li {{request_header::kContentLength, ToString(request_.GetData().size())}});
45*1a96fba6SXin Li fake::Transport* transport = static_cast<fake::Transport*>(transport_.get());
46*1a96fba6SXin Li CHECK(transport) << "Expecting a fake transport";
47*1a96fba6SXin Li auto handler = transport->GetHandler(request_.GetURL(), request_.GetMethod());
48*1a96fba6SXin Li if (handler.is_null()) {
49*1a96fba6SXin Li LOG(ERROR) << "Received unexpected " << request_.GetMethod()
50*1a96fba6SXin Li << " request at " << request_.GetURL();
51*1a96fba6SXin Li response_.ReplyText(status_code::NotFound,
52*1a96fba6SXin Li "<html><body>Not found</body></html>",
53*1a96fba6SXin Li brillo::mime::text::kHtml);
54*1a96fba6SXin Li } else {
55*1a96fba6SXin Li handler.Run(request_, &response_);
56*1a96fba6SXin Li }
57*1a96fba6SXin Li return true;
58*1a96fba6SXin Li }
59*1a96fba6SXin Li
FinishRequestAsync(const SuccessCallback & success_callback,const ErrorCallback & error_callback)60*1a96fba6SXin Li RequestID Connection::FinishRequestAsync(
61*1a96fba6SXin Li const SuccessCallback& success_callback,
62*1a96fba6SXin Li const ErrorCallback& error_callback) {
63*1a96fba6SXin Li // Make sure the produced Closure holds a reference to the instance of this
64*1a96fba6SXin Li // connection.
65*1a96fba6SXin Li auto connection = std::static_pointer_cast<Connection>(shared_from_this());
66*1a96fba6SXin Li auto callback = [](std::shared_ptr<Connection> connection,
67*1a96fba6SXin Li const SuccessCallback& success_callback,
68*1a96fba6SXin Li const ErrorCallback& error_callback) {
69*1a96fba6SXin Li connection->FinishRequestAsyncHelper(success_callback, error_callback);
70*1a96fba6SXin Li };
71*1a96fba6SXin Li transport_->RunCallbackAsync(FROM_HERE,
72*1a96fba6SXin Li base::Bind(callback,
73*1a96fba6SXin Li base::Passed(&connection),
74*1a96fba6SXin Li success_callback,
75*1a96fba6SXin Li error_callback));
76*1a96fba6SXin Li return 1;
77*1a96fba6SXin Li }
78*1a96fba6SXin Li
FinishRequestAsyncHelper(const SuccessCallback & success_callback,const ErrorCallback & error_callback)79*1a96fba6SXin Li void Connection::FinishRequestAsyncHelper(
80*1a96fba6SXin Li const SuccessCallback& success_callback,
81*1a96fba6SXin Li const ErrorCallback& error_callback) {
82*1a96fba6SXin Li brillo::ErrorPtr error;
83*1a96fba6SXin Li if (!FinishRequest(&error)) {
84*1a96fba6SXin Li error_callback.Run(1, error.get());
85*1a96fba6SXin Li } else {
86*1a96fba6SXin Li std::unique_ptr<Response> response{new Response{shared_from_this()}};
87*1a96fba6SXin Li success_callback.Run(1, std::move(response));
88*1a96fba6SXin Li }
89*1a96fba6SXin Li }
90*1a96fba6SXin Li
GetResponseStatusCode() const91*1a96fba6SXin Li int Connection::GetResponseStatusCode() const {
92*1a96fba6SXin Li return response_.GetStatusCode();
93*1a96fba6SXin Li }
94*1a96fba6SXin Li
GetResponseStatusText() const95*1a96fba6SXin Li std::string Connection::GetResponseStatusText() const {
96*1a96fba6SXin Li return response_.GetStatusText();
97*1a96fba6SXin Li }
98*1a96fba6SXin Li
GetProtocolVersion() const99*1a96fba6SXin Li std::string Connection::GetProtocolVersion() const {
100*1a96fba6SXin Li return response_.GetProtocolVersion();
101*1a96fba6SXin Li }
102*1a96fba6SXin Li
GetResponseHeader(const std::string & header_name) const103*1a96fba6SXin Li std::string Connection::GetResponseHeader(
104*1a96fba6SXin Li const std::string& header_name) const {
105*1a96fba6SXin Li return response_.GetHeader(header_name);
106*1a96fba6SXin Li }
107*1a96fba6SXin Li
ExtractDataStream(brillo::ErrorPtr * error)108*1a96fba6SXin Li StreamPtr Connection::ExtractDataStream(brillo::ErrorPtr* error) {
109*1a96fba6SXin Li // HEAD requests must not return body.
110*1a96fba6SXin Li if (request_.GetMethod() != request_type::kHead) {
111*1a96fba6SXin Li return MemoryStream::OpenRef(response_.GetData(), error);
112*1a96fba6SXin Li } else {
113*1a96fba6SXin Li // Return empty data stream for HEAD requests.
114*1a96fba6SXin Li return MemoryStream::OpenCopyOf(nullptr, 0, error);
115*1a96fba6SXin Li }
116*1a96fba6SXin Li }
117*1a96fba6SXin Li
118*1a96fba6SXin Li } // namespace fake
119*1a96fba6SXin Li } // namespace http
120*1a96fba6SXin Li } // namespace brillo
121