Do HttpClient refactoring for Windows.

This commit is contained in:
Adam Ierymenko 2014-08-18 14:34:04 -07:00
parent f5cbb45ab9
commit 95d123cfc2
3 changed files with 88 additions and 31 deletions

View File

@ -356,16 +356,24 @@ private:
class HttpClient_Private_Request : NonCopyable class HttpClient_Private_Request : NonCopyable
{ {
public: public:
HttpClient_Private_Request(const char *method,const std::string &url,const std::map<std::string,std::string> &headers,unsigned int timeout,void (*handler)(void *,int,const std::string &,const std::string &),void *arg) : HttpClient_Private_Request(HttpClient *parent,const char *method,const std::string &url,const std::map<std::string,std::string> &headers,unsigned int timeout,void (*handler)(void *,int,const std::string &,const std::string &),void *arg) :
_url(url), _url(url),
_headers(headers), _headers(headers),
_timeout(timeout), _timeout(timeout),
_handler(handler), _handler(handler),
_arg(arg) _arg(arg),
_parent(parent),
_hRequest((HINTERNET)0)
{ {
_myThread = Thread::start(this); _myThread = Thread::start(this);
} }
~HttpClient_Private_Request()
{
Mutex::Lock _l(_parent->_requests_m);
_parent->_requests.erase((HttpClient::Request)this);
}
void threadMain() void threadMain()
{ {
HINTERNET hSession = (HINTERNET)0; HINTERNET hSession = (HINTERNET)0;
@ -373,9 +381,9 @@ public:
HINTERNET hRequest = (HINTERNET)0; HINTERNET hRequest = (HINTERNET)0;
try { try {
hSession = WinHttpOpen(L"ZeroTier One HttpClient/1.0",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0); hSession = WinHttpOpen(L"ZeroTier One HttpClient/1.0 (WinHttp)",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);
if (!hSession) { if (!hSession) {
_handler(_arg,-1,_url,false,"WinHttpOpen() failed"); _handler(_arg,-1,_url,"WinHttpOpen() failed");
goto closeAndReturnFromHttp; goto closeAndReturnFromHttp;
} }
int timeoutMs = (int)_timeout * 1000; int timeoutMs = (int)_timeout * 1000;
@ -392,11 +400,11 @@ public:
uc.dwUrlPathLength = -1; uc.dwUrlPathLength = -1;
uc.dwExtraInfoLength = -1; uc.dwExtraInfoLength = -1;
if (!WinHttpCrackUrl(wurl.c_str(),(DWORD)wurl.length(),0,&uc)) { if (!WinHttpCrackUrl(wurl.c_str(),(DWORD)wurl.length(),0,&uc)) {
_handler(_arg,-1,_url,false,"unable to parse URL: WinHttpCrackUrl() failed"); _handler(_arg,-1,_url,"unable to parse URL: WinHttpCrackUrl() failed");
goto closeAndReturnFromHttp; goto closeAndReturnFromHttp;
} }
if ((!uc.lpszHostName)||(!uc.lpszUrlPath)||(!uc.lpszScheme)||(uc.dwHostNameLength <= 0)||(uc.dwUrlPathLength <= 0)||(uc.dwSchemeLength <= 0)) { if ((!uc.lpszHostName)||(!uc.lpszUrlPath)||(!uc.lpszScheme)||(uc.dwHostNameLength <= 0)||(uc.dwUrlPathLength <= 0)||(uc.dwSchemeLength <= 0)) {
_handler(_arg,-1,_url,false,"unable to parse URL: missing scheme, host name, or path"); _handler(_arg,-1,_url,"unable to parse URL: missing scheme, host name, or path");
goto closeAndReturnFromHttp; goto closeAndReturnFromHttp;
} }
std::wstring urlScheme(uc.lpszScheme,uc.dwSchemeLength); std::wstring urlScheme(uc.lpszScheme,uc.dwSchemeLength);
@ -406,25 +414,29 @@ public:
urlPath.append(uc.lpszExtraInfo,uc.dwExtraInfoLength); urlPath.append(uc.lpszExtraInfo,uc.dwExtraInfoLength);
if (urlScheme != L"http") { if (urlScheme != L"http") {
_handler(_arg,-1,_url,false,"only 'http' scheme is supported"); _handler(_arg,-1,_url,"only 'http' scheme is supported");
goto closeAndReturnFromHttp; goto closeAndReturnFromHttp;
} }
hConnect = WinHttpConnect(hSession,urlHostName.c_str(),((uc.nPort > 0) ? uc.nPort : 80),0); hConnect = WinHttpConnect(hSession,urlHostName.c_str(),((uc.nPort > 0) ? uc.nPort : 80),0);
if (!hConnect) { if (!hConnect) {
_handler(_arg,-1,_url,false,"connection failed"); _handler(_arg,-1,_url,"connection failed");
goto closeAndReturnFromHttp; goto closeAndReturnFromHttp;
} }
hRequest = WinHttpOpenRequest(hConnect,L"GET",urlPath.c_str(),NULL,WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,0); {
if (!hRequest) { Mutex::Lock _rl(_hRequest_m);
_handler(_arg,-1,_url,false,"error sending request (1)"); _hRequest = WinHttpOpenRequest(hConnect,L"GET",urlPath.c_str(),NULL,WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,0);
if (!_hRequest) {
_handler(_arg,-1,_url,"error sending request (1)");
goto closeAndReturnFromHttp; goto closeAndReturnFromHttp;
} }
if (!WinHttpSendRequest(hRequest,WINHTTP_NO_ADDITIONAL_HEADERS,0,WINHTTP_NO_REQUEST_DATA,0,0,0)) { if (!WinHttpSendRequest(_hRequest,WINHTTP_NO_ADDITIONAL_HEADERS,0,WINHTTP_NO_REQUEST_DATA,0,0,0)) {
_handler(_arg,-1,_url,false,"error sending request (2)"); _handler(_arg,-1,_url,"error sending request (2)");
goto closeAndReturnFromHttp; goto closeAndReturnFromHttp;
} }
hRequest = _hRequest;
}
if (WinHttpReceiveResponse(hRequest,NULL)) { if (WinHttpReceiveResponse(hRequest,NULL)) {
DWORD dwStatusCode = 0; DWORD dwStatusCode = 0;
@ -435,38 +447,65 @@ public:
do { do {
dwSize = 0; dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest,&dwSize)) { if (!WinHttpQueryDataAvailable(hRequest,&dwSize)) {
_handler(_arg,-1,_url,false,"receive error (1)"); _handler(_arg,-1,_url,"receive error (1)");
goto closeAndReturnFromHttp; goto closeAndReturnFromHttp;
} }
{
Mutex::Lock _rl(_hRequest_m);
if (!_hRequest) {
_handler(_arg,-1,_url,"request cancelled");
goto closeAndReturnFromHttp;
}
}
char *outBuffer = new char[dwSize]; char *outBuffer = new char[dwSize];
DWORD dwRead = 0; DWORD dwRead = 0;
if (!WinHttpReadData(hRequest,(LPVOID)outBuffer,dwSize,&dwRead)) { if (!WinHttpReadData(hRequest,(LPVOID)outBuffer,dwSize,&dwRead)) {
_handler(_arg,-1,_url,false,"receive error (2)"); _handler(_arg,-1,_url,"receive error (2)");
goto closeAndReturnFromHttp;
}
{
Mutex::Lock _rl(_hRequest_m);
if (!_hRequest) {
_handler(_arg,-1,_url,"request cancelled");
goto closeAndReturnFromHttp; goto closeAndReturnFromHttp;
} }
_body.append(outBuffer,dwRead); _body.append(outBuffer,dwRead);
delete [] outBuffer; delete [] outBuffer;
if (_body.length() > WIN_MAX_MESSAGE_LENGTH) { if (_body.length() > WIN_MAX_MESSAGE_LENGTH) {
_handler(_arg,-1,_url,false,"result too large"); _handler(_arg,-1,_url,"result too large");
goto closeAndReturnFromHttp; goto closeAndReturnFromHttp;
} }
} while (dwSize > 0);
_handler(_arg,dwStatusCode,_url,false,_body);
} else {
_handler(_arg,-1,_url,false,"receive response failed");
} }
} catch (std::bad_alloc &exc) { } while ((dwSize > 0)&&(_hRequest));
_handler(_arg,-1,_url,false,"insufficient memory");
{
Mutex::Lock _rl(_hRequest_m);
if (!_hRequest) {
_handler(_arg,-1,_url,"request cancelled");
goto closeAndReturnFromHttp;
}
_handler(_arg,dwStatusCode,_url,_body);
}
} else {
_handler(_arg,-1,_url,"receive response failed");
}
} catch ( ... ) { } catch ( ... ) {
_handler(_arg,-1,_url,false,"unexpected exception"); _handler(_arg,-1,_url,"unexpected exception");
} }
closeAndReturnFromHttp: closeAndReturnFromHttp:
if (hRequest) {
WinHttpCloseHandle(hRequest); Mutex::Lock _rl(_hRequest_m);
if (_hRequest) {
WinHttpCloseHandle(_hRequest);
_hRequest = (HINTERNET)0;
}
}
if (hConnect) if (hConnect)
WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hConnect);
if (hSession) if (hSession)
@ -475,12 +514,24 @@ closeAndReturnFromHttp:
return; return;
} }
inline void cancel()
{
Mutex::Lock _rl(_hRequest_m);
if (_hRequest) {
WinHttpCloseHandle(_hRequest);
_hRequest = (HINTERNET)0;
}
}
const std::string _url; const std::string _url;
std::string _body; std::string _body;
std::map<std::string,std::string> _headers; std::map<std::string,std::string> _headers;
unsigned int _timeout; unsigned int _timeout;
void (*_handler)(void *,int,const std::string &,const std::string &); void (*_handler)(void *,int,const std::string &,const std::string &);
void *_arg; void *_arg;
HttpClient *_parent;
HINTERNET _hRequest;
Mutex _hRequest_m;
Thread _myThread; Thread _myThread;
}; };

View File

@ -20,7 +20,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\ext\lz4\lz4.c" /> <ClCompile Include="..\..\ext\lz4\lz4.c" />
<ClCompile Include="..\..\main.cpp" /> <ClCompile Include="..\..\main.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\node\C25519.cpp" /> <ClCompile Include="..\..\node\C25519.cpp" />
<ClCompile Include="..\..\node\CertificateOfMembership.cpp" /> <ClCompile Include="..\..\node\CertificateOfMembership.cpp" />
<ClCompile Include="..\..\node\Defaults.cpp" /> <ClCompile Include="..\..\node\Defaults.cpp" />
@ -54,6 +56,7 @@
<ClCompile Include="..\..\osnet\WindowsEthernetTap.cpp" /> <ClCompile Include="..\..\osnet\WindowsEthernetTap.cpp" />
<ClCompile Include="..\..\osnet\WindowsEthernetTapFactory.cpp" /> <ClCompile Include="..\..\osnet\WindowsEthernetTapFactory.cpp" />
<ClCompile Include="..\..\osnet\WindowsRoutingTable.cpp" /> <ClCompile Include="..\..\osnet\WindowsRoutingTable.cpp" />
<ClCompile Include="..\..\selftest.cpp" />
<ClCompile Include="ServiceBase.cpp" /> <ClCompile Include="ServiceBase.cpp" />
<ClCompile Include="ServiceInstaller.cpp" /> <ClCompile Include="ServiceInstaller.cpp" />
<ClCompile Include="ZeroTierOneService.cpp" /> <ClCompile Include="ZeroTierOneService.cpp" />

View File

@ -129,6 +129,9 @@
<ClCompile Include="..\..\main.cpp"> <ClCompile Include="..\..\main.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\selftest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\ext\lz4\lz4.h"> <ClInclude Include="..\..\ext\lz4\lz4.h">