diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2013-12-06 16:00:12 -0800 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2013-12-06 16:00:12 -0800 |
commit | 518410b7e0f8b88ee8822e4449e91f7c52d1022a (patch) | |
tree | 323d6f71c30170c3689ca48cd61422171af38806 /node/HttpClient.cpp | |
parent | 0a0ed893c3878c82070392bf953ecb16d50734d9 (diff) | |
download | infinitytier-518410b7e0f8b88ee8822e4449e91f7c52d1022a.tar.gz infinitytier-518410b7e0f8b88ee8822e4449e91f7c52d1022a.zip |
HTTP client works!
Diffstat (limited to 'node/HttpClient.cpp')
-rw-r--r-- | node/HttpClient.cpp | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/node/HttpClient.cpp b/node/HttpClient.cpp index a9c40205..1d1624db 100644 --- a/node/HttpClient.cpp +++ b/node/HttpClient.cpp @@ -52,6 +52,8 @@ namespace ZeroTier { +const std::map<std::string,std::string> HttpClient::NO_HEADERS; + #ifdef __UNIX_LIKE__ // The *nix implementation calls 'curl' externally rather than linking to it. @@ -59,6 +61,10 @@ namespace ZeroTier { // provided you don't want to have automatic software updates... or want to // do them via another method. +#ifdef __APPLE__ +// TODO: get proxy configuration +#endif + // Paths where "curl" may be found on the system #define NUM_CURL_PATHS 5 static const char *CURL_PATHS[NUM_CURL_PATHS] = { "/usr/bin/curl","/bin/curl","/usr/local/bin/curl","/usr/sbin/curl","/sbin/curl" }; @@ -117,11 +123,12 @@ public: headerArgs.back().append(h->second); } for(std::vector<std::string>::iterator h(headerArgs.begin());h!=headerArgs.end();++h) { - if (argPtr >= (1024 - 3)) // leave room for terminating NULL + if (argPtr >= (1024 - 4)) // leave room for terminating NULL and URL break; curlArgs[argPtr++] = const_cast <char *>("-H"); curlArgs[argPtr++] = const_cast <char *>(h->c_str()); } + curlArgs[argPtr++] = const_cast <char *>(_url.c_str()); curlArgs[argPtr] = (char *)0; int curlStdout[2]; @@ -166,7 +173,8 @@ public: int n = (int)::read(curlStdout[0],buf,sizeof(buf)); if (n > 0) _body.append(buf,n); - else break; + else if (n < 0) + break; if (_body.length() > CURL_MAX_MESSAGE_LENGTH) { ::kill(pid,SIGKILL); tooLong = true; @@ -215,8 +223,8 @@ public: if (!headers.back().length()) { headers.pop_back(); break; - } - } else if (c != '\r') // \r's shouldn't be present but ignore them if they are + } else headers.push_back(std::string()); + } else if (c != '\r') headers.back().push_back(c); } if (headers.empty()||(!headers.front().length())) { @@ -233,14 +241,6 @@ public: return; } ++scPos; - size_t msgPos = headers.front().find(' ',scPos); - if (msgPos == std::string::npos) - msgPos = headers.front().length(); - if ((msgPos - scPos) != 3) { - _handler(_arg,-1,_url,false,"invalid HTTP response (no response code)"); - delete this; - return; - } unsigned int rcode = Utils::strToUInt(headers.front().substr(scPos,3).c_str()); if ((!rcode)||(rcode > 999)) { _handler(_arg,-1,_url,false,"invalid HTTP response (invalid response code)"); @@ -251,7 +251,9 @@ public: // Serve up the resulting data to the handler if (rcode == 200) _handler(_arg,rcode,_url,false,_body.substr(idx)); - else _handler(_arg,rcode,_url,false,headers.front().substr(scPos+3)); + else if ((scPos + 4) < headers.front().length()) + _handler(_arg,rcode,_url,false,headers.front().substr(scPos+4)); + else _handler(_arg,rcode,_url,false,"(no status message from server)"); } delete this; @@ -284,6 +286,7 @@ HttpClient::Request HttpClient::_do( void (*handler)(void *,int,const std::string &,bool,const std::string &), void *arg) { + return (HttpClient::Request)(new P_Req(method,url,headers,timeout,handler,arg)); } #endif |