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'.
49 /// Any previous data in 'output' is dropped.
50 /// @note The chunk size is not guaranteed to be exactly
51 /// multipart/form-data chunk size or any other HTTP-related size
52 /// @note may block if the chunk is not obtained yet.
53 bool ReadChunk(std::string& output, engine::Deadline);
54
55 /// @cond
56 StreamedResponse(
57 engine::Future<void>&& headers_future,
58 Queue::Consumer&& queue_consumer,
59 std::shared_ptr<clients::http::RequestState> request_state
60 );
61 /// @endcond
62
63private:
64 std::future_status WaitForHeaders(engine::Deadline);
65
66 void WaitForHeadersOrThrow(engine::Deadline);
67
68 std::shared_ptr<RequestState> request_state_;
69 // re-use sync response's headers & status code storage
70 std::shared_ptr<Response> response_;
71 engine::Deadline deadline_;
72
73 engine::Future<void> headers_future_;
74 Queue::Consumer queue_consumer_;
75};
76
77} // namespace clients::http
78
79USERVER_NAMESPACE_END