-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync-winhttp-request.h
190 lines (164 loc) · 5.25 KB
/
async-winhttp-request.h
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
** Òì²½WinHttp´¦ÀíÀà
** author
*/
#ifndef ULT_ASYNCWINHTTPREQUEST_H_
#define ULT_ASYNCWINHTTPREQUEST_H_
#include "./net/winhttp-foundation.h"
namespace ult{
class AsyncWinHttpRequest {
public:
AsyncWinHttpRequest(void) :
callback_setted_(false) {
buffer_ = new char[kBufferLength];
}
virtual ~AsyncWinHttpRequest(void) {
if (buffer_ != NULL) {
delete[] buffer_;
}
Close();
}
void Close(void) {
if (callback_setted_&& handle_.GetHandle() != NULL) {
::WinHttpSetStatusCallback(handle_, NULL, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, NULL);
//::WinHttpSetStatusCallback(handle_, NULL, 0, NULL);
DWORD_PTR p = NULL;
//::WinHttpSetOption(handle_, WINHTTP_OPTION_CONTEXT_VALUE, (LPVOID)p, sizeof (p));
}
handle_.Close();
}
HRESULT Initialize(HINTERNET connection, const wchar_t* verb, const wchar_t* path, int scheme = INTERNET_SCHEME_HTTP) {
DWORD flag = (scheme == INTERNET_SCHEME_HTTPS) ? WINHTTP_FLAG_SECURE : 0;
if (!handle_.Attach(::WinHttpOpenRequest(connection, verb, path, NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES, flag))) {
return HRESULT_FROM_WIN32(::GetLastError());
}
if (WINHTTP_INVALID_STATUS_CALLBACK == ::WinHttpSetStatusCallback(handle_, Callback,
WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, NULL)) {
callback_setted_ = false;
return HRESULT_FROM_WIN32(::GetLastError());
}
callback_setted_ = true;
return S_OK;
}
HRESULT SendRequest(const wchar_t* headers, DWORD headers_len, const void* optional, DWORD optional_len, DWORD total_len) {
if (FALSE == ::WinHttpSendRequest(handle_, headers, headers_len,
const_cast<void*>(optional), optional_len, total_len, reinterpret_cast<DWORD_PTR>(this))) {
return HRESULT_FROM_WIN32(::GetLastError());
}
return S_OK;
}
HRESULT WriteData(const void* data, DWORD length) {
if (FALSE == ::WinHttpWriteData(handle_, data, length, NULL)) {
return HRESULT_FROM_WIN32(::GetLastError());
}
return S_OK;
}
HRESULT RecieveResponse(void) {
if (FALSE == ::WinHttpReceiveResponse(handle_, NULL)) {
return HRESULT_FROM_WIN32(::GetLastError());
}
return S_OK;
}
protected:
virtual HRESULT OnCallback(DWORD code, const void* info, DWORD length) {
return S_OK;
}
virtual HRESULT OnContentLength(DWORD length) {
return S_OK;
}
virtual HRESULT OnReadComplete(const void* info, DWORD length) {
return S_OK;
}
virtual HRESULT OnResponseComplete(HRESULT hr) {
return S_OK;
}
virtual HRESULT OnSendRequestComplete(void) {
return S_OK;
}
virtual HRESULT OnWriteDataComplete(void) {
return S_OK;
}
private:
static void CALLBACK Callback(HINTERNET handle, DWORD_PTR context, DWORD code, void* info, DWORD length) {
AsyncWinHttpRequest* pthis = (AsyncWinHttpRequest*)context;
HRESULT hr = pthis->OnBaseCallback(code, info, length);
if (FAILED(hr)) {
pthis->OnResponseComplete(hr);
}
}
HRESULT OnBaseCallback(DWORD code, const void* info, DWORD length) {
switch (code) {
case WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE:
OnSendRequestComplete();
break;
case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE:
OnWriteDataComplete();
break;
case WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE:
{
DWORD status;
DWORD len = sizeof (status);
if (FALSE == ::WinHttpQueryHeaders(handle_, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
WINHTTP_HEADER_NAME_BY_INDEX, &status, &len, WINHTTP_NO_HEADER_INDEX)) {
return HRESULT_FROM_WIN32(::GetLastError());
}
if (status != HTTP_STATUS_OK) {
return E_FAIL;
}
if (S_OK != QueryDataAvailable()) {
return E_FAIL;
}
DWORD content_length;
len = sizeof (content_length);
if (TRUE == ::WinHttpQueryHeaders(handle_, WINHTTP_QUERY_CONTENT_LENGTH | WINHTTP_QUERY_FLAG_NUMBER,
WINHTTP_HEADER_NAME_BY_INDEX, &content_length, &len, WINHTTP_NO_HEADER_INDEX)) {
OnContentLength(content_length);
}
}
break;
case WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE:
{
DWORD size = *(reinterpret_cast<const DWORD*>(info));
if (size > 0) {
if (FALSE == ::WinHttpReadData(handle_, buffer_, kBufferLength, NULL)) {
return HRESULT_FROM_WIN32(::GetLastError());
}
} else if (size == 0) {
OnResponseComplete(S_OK);
}
}
break;
case WINHTTP_CALLBACK_STATUS_READ_COMPLETE:
if (length > 0) {
OnReadComplete(info, length);
QueryDataAvailable();
}
break;
case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR:
OnResponseComplete(HRESULT_FROM_WIN32(::GetLastError()));
break;
default:
return OnCallback(code, info, length);
break;
}
return S_OK;
}
HRESULT QueryDataAvailable(void) {
if (FALSE == ::WinHttpQueryDataAvailable(handle_, NULL)) {
return HRESULT_FROM_WIN32(::GetLastError());
}
return S_OK;
}
//pritave variable
WinHttpHandle handle_;
void* buffer_;
bool callback_setted_;
//private constant
enum {
kBufferLength = 8 * 1024,
};
}; //class WinHttpRequest
} //namespace ult
#endif