userver: userver/clients/http/streamed_response.hpp Source File
Loading...
Searching...
No Matches
streamed_response.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/clients/http/streamed_response.hpp
4/// @brief @copybrief clients::http::StreamedResponse
5
6#include <cstddef>
7#include <future>
8#include <memory>
9#include <string>
10
11#include <userver/clients/http/response.hpp>
12#include <userver/concurrent/queue_fwd.hpp>
13#include <userver/engine/deadline.hpp>
14#include <userver/engine/future.hpp>
15#include <userver/utils/fast_pimpl.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace clients::http {
20
21class RequestState;
22
23/// @brief HTTP response for streamed API.
24///
25/// Call Request::async_perform_stream_body()
26/// to get one. You can use it for fast proxying backend response body
27/// to a remote Application.
28class StreamedResponse final {
29public:
30 StreamedResponse(StreamedResponse&&) noexcept;
31 StreamedResponse(const StreamedResponse&) = delete;
32
33 StreamedResponse& operator=(StreamedResponse&&) noexcept;
34 StreamedResponse& operator=(const StreamedResponse&) = delete;
35
36 ~StreamedResponse();
37
38 /// @brief HTTP status code
39 /// @note may suspend the coroutine if the code is not obtained yet.
40 Status StatusCode();
41
42 /// Returns HTTP header value by its name (case-insensitive)
43 /// A missing key results in empty string
44 /// @note may suspend the coroutine if headers are not obtained yet.
45 std::string GetHeader(const std::string& header_name);
46
47 /// Get all HTTP headers as a case-insensitive unordered map
48 /// @note may suspend the coroutine if headers are not obtained yet.
49 const Headers& GetHeaders();
50 const Response::CookiesMap& GetCookies();
51
52 using Queue = concurrent::StringStreamQueue;
53
54 /// Read another HTTP response body part into 'output'. Any previous data in 'output' is dropped.
55 /// @returns true if data was successfully received in 'output' before current task cancellation and 'deadline'.
56 ///
57 /// @note The chunk size is not guaranteed to be exactly
58 /// multipart/form-data chunk size or any other HTTP-related size
59 /// @note may block if the chunk is not obtained yet.
60 bool ReadChunk(std::string& output, engine::Deadline deadline);
61
62 /// @cond
63 StreamedResponse(
64 engine::Future<void>&& headers_future,
65 concurrent::StringStreamQueueConsumer&& queue_consumer,
66 std::shared_ptr<clients::http::RequestState> request_state
67 );
68 /// @endcond
69
70private:
71 std::future_status WaitForHeaders(engine::Deadline);
72
73 void WaitForHeadersOrThrow(engine::Deadline);
74
75 std::shared_ptr<RequestState> request_state_;
76 // re-use sync response's headers & status code storage
77 std::shared_ptr<Response> response_;
78 engine::Deadline deadline_;
79
80 engine::Future<void> headers_future_;
81
82 static constexpr std::size_t kQueueConsumerSize = 3 * sizeof(void*);
83 static constexpr std::size_t kQueueConsumerAlignment = alignof(void*);
84 utils::FastPimpl<concurrent::StringStreamQueueConsumer, kQueueConsumerSize, kQueueConsumerAlignment>
85 queue_consumer_;
86};
87
88} // namespace clients::http
89
90USERVER_NAMESPACE_END