blob: 6dcf39ae9c72ae78efc3d68b03115535a2de7acb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#pragma once
#include "query.h"
#include <library/cpp/deprecated/atomic/atomic.h>
#include <library/cpp/http/client/fetch/fetch_request.h>
#include <library/cpp/http/client/fetch/fetch_result.h>
#include <library/cpp/http/client/cookies/cookiestore.h>
#include <util/generic/intrlist.h>
#include <util/system/condvar.h>
#include <util/system/spinlock.h>
namespace NHttp {
class TFetchRequest: public TAtomicRefCount<TFetchRequest> {
public:
TFetchRequest(const TString& url, const TFetchOptions& options);
TFetchRequest(const TString& url, TVector<TString> headers, const TFetchOptions& options);
/// Returns reference to request object.
static TFetchRequestRef FromQuery(const TFetchQuery& query) {
return query.ConstructRequest();
}
/// Cancel the request.
void Cancel();
/// Is the current request is still valid?
bool IsValid() const;
/// Is the current request cancelled?
bool IsCancelled() const;
/// Makes request in the format of underlining fetch subsystem.
NHttpFetcher::TRequestRef GetRequestImpl() const;
/// Whether new connection should been established.
bool GetForceReconnect() const;
/// Unique identifier of the request.
ui64 GetId() const {
return Id_;
}
/// Returns url of original request.
TString GetUrl() const {
return Url_;
}
/// Makes final result.
NHttpFetcher::TResultRef MakeResult() const;
void SetException(std::exception_ptr ptr);
void SetCallback(NHttpFetcher::TCallBack cb);
void SetOnFail(TOnFail cb);
void SetOnRedirect(TOnRedirect cb);
void SetOnPartialRead(NHttpFetcher::TNeedDataCallback cb);
/// Waits for completion of the request no more than given timeout.
///
/// \return true if fetching is finished.
/// \return false if timed out.
bool WaitT(TDuration timeout);
/// Response has been gotten.
TDuration OnResponse(NHttpFetcher::TResultRef result);
private:
bool IsValidNoLock() const;
void Reply(NHttpFetcher::TResultRef result);
private:
/// State of redirects processing.
struct TRedirects: public std::vector<NHttpFetcher::TResultRef> {
THolder<NHttp::TCookieStore> CookieStore;
explicit TRedirects(bool parseCookie);
size_t Level() const;
void ParseCookies(const TString& url, const THttpHeaders& headers);
};
struct TWaitState : TIntrusiveListItem<TWaitState> {
inline TWaitState(TCondVar* c)
: C_(c)
{
}
inline void Signal() {
C_->Signal();
}
TCondVar* const C_;
};
const TString Url_;
const TFetchOptions Options_;
TVector<TString> Headers_;
TAtomic Id_;
ui32 RetryAttempts_;
TDuration RetryDelay_;
NHttpFetcher::TCallBack
Cb_;
TOnFail OnFail_;
TOnRedirect OnRedirect_;
NHttpFetcher::TNeedDataCallback OnPartialRead_;
std::exception_ptr Exception_;
NHttpFetcher::TResultRef
Result_;
TMutex Lock_;
TIntrusiveList<TWaitState>
Awaitings_;
TAtomic Cancel_;
/// During following through redirects the url is changing.
/// So, this is actual url for the current step.
TString CurrentUrl_;
THolder<TRedirects> Redirects_;
};
}
|