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