userver: userver/clients/http/streamed_response.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 {
25 public:
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.
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(engine::Future<void>&& headers_future,
57 Queue::Consumer&& queue_consumer,
58 std::shared_ptr<clients::http::RequestState> request_state);
59 /// @endcond
60
61 private:
62 std::future_status WaitForHeaders(engine::Deadline);
63
64 void WaitForHeadersOrThrow(engine::Deadline);
65
66 std::shared_ptr<RequestState> request_state_;
67 // re-use sync response's headers & status code storage
68 std::shared_ptr<Response> response_;
69 engine::Deadline deadline_;
70
71 engine::Future<void> headers_future_;
72 Queue::Consumer queue_consumer_;
73};
74
75} // namespace clients::http
76
77USERVER_NAMESPACE_END