From 0a0ed893c3878c82070392bf953ecb16d50734d9 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 6 Dec 2013 13:15:30 -0800 Subject: HTTP client work... --- node/HttpClient.hpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 node/HttpClient.hpp (limited to 'node/HttpClient.hpp') diff --git a/node/HttpClient.hpp b/node/HttpClient.hpp new file mode 100644 index 00000000..da12fb24 --- /dev/null +++ b/node/HttpClient.hpp @@ -0,0 +1,85 @@ +/* + * ZeroTier One - Global Peer to Peer Ethernet + * Copyright (C) 2012-2013 ZeroTier Networks LLC + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +#ifndef ZT_HTTPCLIENT_HPP +#define ZT_HTTPCLIENT_HPP + +#include +#include + +#include "Constants.hpp" + +namespace ZeroTier { + +/** + * HTTP client that does queries in the background + * + * The handler method takes the following arguments: an arbitrary pointer, an + * HTTP response code, the URL queried, whether or not the message body was + * stored on disk, and the message body. + * + * If stored on disk, the body string contains the path and the file must be + * moved or deleted by the receiver when it's done. If an error occurs, the + * response code will be negative and the body will be the error message. + * + * All headers in the returned headers map will have their header names + * converted to lower case, e.g. "content-type". + * + * Currently only the "http" transport is guaranteed to be supported on all + * platforms. + */ +class HttpClient +{ +public: + typedef void * Request; + + /** + * Request a URL using the GET method + */ + static inline Request GET( + const std::string &url, + const std::map &headers, + unsigned int timeout, + void (*handler)(void *,int,const std::string &,bool,const std::string &), + void *arg) + { + return _do("GET",url,headers,timeout,handler,arg); + } + +private: + static Request _do( + const char *method, + const std::string &url, + const std::map &headers, + unsigned int timeout, + void (*handler)(void *,int,const std::string &,bool,const std::string &), + void *arg); +}; + +} // namespace ZeroTier + +#endif -- cgit v1.2.3 From 518410b7e0f8b88ee8822e4449e91f7c52d1022a Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 6 Dec 2013 16:00:12 -0800 Subject: HTTP client works! --- node/HttpClient.cpp | 29 ++++++++++++++++------------- node/HttpClient.hpp | 5 +++++ selftest.cpp | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 13 deletions(-) (limited to 'node/HttpClient.hpp') 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 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::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 ("-H"); curlArgs[argPtr++] = const_cast (h->c_str()); } + curlArgs[argPtr++] = const_cast (_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 diff --git a/node/HttpClient.hpp b/node/HttpClient.hpp index da12fb24..4f5c9bc7 100644 --- a/node/HttpClient.hpp +++ b/node/HttpClient.hpp @@ -57,6 +57,11 @@ class HttpClient public: typedef void * Request; + /** + * Empty map for convenience use + */ + static const std::map NO_HEADERS; + /** * Request a URL using the GET method */ diff --git a/selftest.cpp b/selftest.cpp index ba362bd3..50a14e0f 100644 --- a/selftest.cpp +++ b/selftest.cpp @@ -52,6 +52,7 @@ #include "node/C25519.hpp" #include "node/Poly1305.hpp" #include "node/CertificateOfMembership.hpp" +#include "node/HttpClient.hpp" #ifdef __WINDOWS__ #include @@ -63,6 +64,40 @@ using namespace ZeroTier; static unsigned char fuzzbuf[1048576]; +static Condition webDoneCondition; +static void testHttpHandler(void *arg,int code,const std::string &url,bool onDisk,const std::string &body) +{ + if (code == 200) + std::cout << "got " << body.length() << " bytes, response code " << code << std::endl; + else std::cout << "ERROR " << code << ": " << body << std::endl; + webDoneCondition.signal(); +} + +static int testHttp() +{ + std::cout << "[http] fetching http://download.zerotier.com/dev/1k ... "; std::cout.flush(); + HttpClient::GET("http://download.zerotier.com/dev/1k",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0); + webDoneCondition.wait(); + + std::cout << "[http] fetching http://download.zerotier.com/dev/2k ... "; std::cout.flush(); + HttpClient::GET("http://download.zerotier.com/dev/2k",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0); + webDoneCondition.wait(); + + std::cout << "[http] fetching http://download.zerotier.com/dev/4k ... "; std::cout.flush(); + HttpClient::GET("http://download.zerotier.com/dev/4k",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0); + webDoneCondition.wait(); + + std::cout << "[http] fetching http://download.zerotier.com/dev/8k ... "; std::cout.flush(); + HttpClient::GET("http://download.zerotier.com/dev/8k",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0); + webDoneCondition.wait(); + + std::cout << "[http] fetching http://download.zerotier.com/dev/NOEXIST ... "; std::cout.flush(); + HttpClient::GET("http://download.zerotier.com/dev/NOEXIST",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0); + webDoneCondition.wait(); + + return 0; +} + static int testCrypto() { unsigned char buf1[16384]; @@ -562,6 +597,7 @@ int main(int argc,char **argv) srand((unsigned int)time(0)); + r |= testHttp(); r |= testCrypto(); r |= testPacket(); r |= testOther(); -- cgit v1.2.3