-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsyncHttpResponse.cpp
More file actions
147 lines (118 loc) · 3.86 KB
/
Copy pathAsyncHttpResponse.cpp
File metadata and controls
147 lines (118 loc) · 3.86 KB
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
#include "StdAfx.h"
#include <memory>
#include <sstream>
#include <string>
#include <algorithm>
#include "AsyncHttpRequest.h"
#include "AsyncHttpResponse.h"
const DWORD AsyncHttpResponse::m_dwBufSize = 1024;
AsyncHttpResponse::AsyncHttpResponse(AsyncHttpRequest* apAsyncHttpRequest)
: m_pAsyncHttpRequest(apAsyncHttpRequest),
m_spBuf(new char[m_dwBufSize])
{
}
AsyncHttpResponse::~AsyncHttpResponse()
{
}
string AsyncHttpResponse::GetLastResponseInfo(DWORD adwGetLastErrorResult)
{
unique_ptr<char> spBuf(new char[m_dwBufSize]);
DWORD dwMsgLength = FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE,
GetModuleHandle(TEXT("wininet.dll")),
adwGetLastErrorResult,
0,
spBuf.get(),
m_dwBufSize,
NULL);
string result(spBuf.get(), dwMsgLength);
if(adwGetLastErrorResult == ERROR_INTERNET_EXTENDED_ERROR)
{
DWORD dwErrorCode;
DWORD dwErrorMsgLength = 0;
InternetGetLastResponseInfoA(&dwErrorCode, NULL, &dwErrorMsgLength);
unique_ptr<char> spMsg(new char[dwErrorMsgLength]);
if (!InternetGetLastResponseInfoA(&dwErrorCode, spMsg.get(), &dwErrorMsgLength))
{
printf("InternetGetLastResponseInfoA failed. GetLastError()=%p", GetLastError());
return "";
}
result.assign(spMsg.get(), dwErrorMsgLength);
}
return result;
}
int AsyncHttpResponse::ProcessResponse(const INTERNET_ASYNC_RESULT* aAsyncResult)
{
if(!aAsyncResult->dwResult)
{
printf("Request %p failed. dwError=%d Message=\"%s\"", this, aAsyncResult->dwError, GetLastResponseInfo(aAsyncResult->dwError).c_str());
m_pAsyncHttpRequest->m_pHost->OnAsyncHttpRequestComplete(m_pAsyncHttpRequest, aAsyncResult->dwError);
return -1;
}
DWORD dwBytesRead = 0;
do
{
dwBytesRead = 0;
if (!InternetReadFile(m_pAsyncHttpRequest->m_hRequest, m_spBuf.get(), m_dwBufSize, &dwBytesRead))
{
if (GetLastError() != ERROR_IO_PENDING)
{
printf("InternetReadFileEx failed. Request=%p GetLastError()=%d", this, GetLastError());
}
m_pAsyncHttpRequest->m_pHost->OnAsyncHttpRequestComplete(m_pAsyncHttpRequest, GetLastError());
return -1;
}
m_Body.append(m_spBuf.get(), dwBytesRead);
}
while(dwBytesRead > 0);
m_pAsyncHttpRequest->m_pHost->OnAsyncHttpRequestComplete(m_pAsyncHttpRequest, 0);
return 0;
}
string AsyncHttpResponse::GetHeader(string aHeaderName)
{
DWORD dwBufSize = 65535;
wstring HeaderName = Convert_UTF8_UTF16(aHeaderName);
unique_ptr<TCHAR> spBuf(new TCHAR[dwBufSize]);
wcscpy_s(spBuf.get(), dwBufSize, HeaderName.c_str());
BOOL bResult = HttpQueryInfo(m_pAsyncHttpRequest->m_hRequest, HTTP_QUERY_CUSTOM, spBuf.get(), &dwBufSize, NULL);
if(!bResult && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
spBuf.reset(new TCHAR[dwBufSize / sizeof(TCHAR)]);
wcscpy_s(spBuf.get(), dwBufSize, HeaderName.c_str());
bResult = HttpQueryInfo(m_pAsyncHttpRequest->m_hRequest, HTTP_QUERY_CUSTOM, spBuf.get(), &dwBufSize, NULL);
}
if(!bResult)
{
printf("HttpQueryInfo failed with GetLastError()=%p", GetLastError());
return "";
}
wstring result(spBuf.get(), dwBufSize / sizeof(TCHAR));
return Convert_UTF16_UTF8(result);
}
string AsyncHttpResponse::GetContentType()
{
DWORD dwBufSize = 65535;
unique_ptr<TCHAR> spBuf(new TCHAR[dwBufSize]);
if(!HttpQueryInfo(m_pAsyncHttpRequest->m_hRequest, HTTP_QUERY_CONTENT_TYPE, spBuf.get(), &dwBufSize, NULL))
{
printf("HttpQueryInfo failed with GetLastError()=%p", GetLastError());
return "";
}
wstring result(spBuf.get(), dwBufSize / sizeof(TCHAR));
return Convert_UTF16_UTF8(result);
}
DWORD AsyncHttpResponse::GetContentLength()
{
DWORD dwBufSize = 65535;
unique_ptr<TCHAR> spBuf(new TCHAR[dwBufSize]);
if(!HttpQueryInfo(m_pAsyncHttpRequest->m_hRequest, HTTP_QUERY_CONTENT_LENGTH, spBuf.get(), &dwBufSize, NULL))
{
printf("HttpQueryInfo failed with GetLastError()=%p", GetLastError());
return 0;
}
return _tcstoul(spBuf.get(), NULL, 10);
}
string& AsyncHttpResponse::GetBody()
{
return m_Body;
}