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#include <libwebserv/request_utils.h>
16
17#include <base/bind.h>
18#include <brillo/streams/memory_stream.h>
19#include <brillo/streams/stream_utils.h>
20#include <libwebserv/request.h>
21#include <libwebserv/response.h>
22
23namespace libwebserv {
24
25namespace {
26
27struct RequestDataContainer {
28  std::unique_ptr<Request> request;
29  std::unique_ptr<Response> response;
30  GetRequestDataSuccessCallback success_callback;
31  GetRequestDataErrorCallback error_callback;
32  std::vector<uint8_t> data;
33};
34
35void OnCopySuccess(std::shared_ptr<RequestDataContainer> container,
36                   brillo::StreamPtr /* in_stream */,
37                   brillo::StreamPtr out_stream,
38                   uint64_t /* size_copied */) {
39  // Close/release the memory stream so we can work with underlying data buffer.
40  out_stream->CloseBlocking(nullptr);
41  out_stream.reset();
42  container->success_callback.Run(std::move(container->request),
43                                  std::move(container->response),
44                                  std::move(container->data));
45}
46
47void OnCopyError(std::shared_ptr<RequestDataContainer> container,
48                 brillo::StreamPtr /* in_stream */,
49                 brillo::StreamPtr /* out_stream */,
50                 const brillo::Error* error) {
51  container->error_callback.Run(std::move(container->request),
52                                std::move(container->response), error);
53}
54
55}  // anonymous namespace
56
57void GetRequestData(std::unique_ptr<Request> request,
58                    std::unique_ptr<Response> response,
59                    const GetRequestDataSuccessCallback& success_callback,
60                    const GetRequestDataErrorCallback& error_callback) {
61  auto container = std::make_shared<RequestDataContainer>();
62  auto in_stream = request->GetDataStream();
63  auto out_stream =
64      brillo::MemoryStream::CreateRef(&container->data, nullptr);
65  container->request = std::move(request);
66  container->response = std::move(response);
67  container->success_callback = success_callback;
68  container->error_callback = error_callback;
69  brillo::stream_utils::CopyData(std::move(in_stream), std::move(out_stream),
70                                 base::Bind(&OnCopySuccess, container),
71                                 base::Bind(&OnCopyError, container));
72}
73
74}  // namespace libwebserv
75