summaryrefslogtreecommitdiff
path: root/src/mongo/util/net/httpclient.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/util/net/httpclient.cpp')
-rw-r--r--src/mongo/util/net/httpclient.cpp238
1 files changed, 118 insertions, 120 deletions
diff --git a/src/mongo/util/net/httpclient.cpp b/src/mongo/util/net/httpclient.cpp
index 4f7e9260555..a3aa63651da 100644
--- a/src/mongo/util/net/httpclient.cpp
+++ b/src/mongo/util/net/httpclient.cpp
@@ -41,158 +41,156 @@
namespace mongo {
- using std::string;
- using std::stringstream;
+using std::string;
+using std::stringstream;
- //#define HD(x) cout << x << endl;
+//#define HD(x) cout << x << endl;
#define HD(x)
- int HttpClient::get( const std::string& url , Result * result ) {
- return _go( "GET" , url , 0 , result );
+int HttpClient::get(const std::string& url, Result* result) {
+ return _go("GET", url, 0, result);
+}
+
+int HttpClient::post(const std::string& url, const std::string& data, Result* result) {
+ return _go("POST", url, data.c_str(), result);
+}
+
+int HttpClient::_go(const char* command, string url, const char* body, Result* result) {
+ bool ssl = false;
+ if (url.find("https://") == 0) {
+ ssl = true;
+ url = url.substr(8);
+ } else {
+ uassert(10271, "invalid url", url.find("http://") == 0);
+ url = url.substr(7);
}
- int HttpClient::post( const std::string& url , const std::string& data , Result * result ) {
- return _go( "POST" , url , data.c_str() , result );
+ string host, path;
+ if (url.find("/") == string::npos) {
+ host = url;
+ path = "/";
+ } else {
+ host = url.substr(0, url.find("/"));
+ path = url.substr(url.find("/"));
}
- int HttpClient::_go( const char * command , string url , const char * body , Result * result ) {
- bool ssl = false;
- if ( url.find( "https://" ) == 0 ) {
- ssl = true;
- url = url.substr( 8 );
- }
- else {
- uassert( 10271 , "invalid url" , url.find( "http://" ) == 0 );
- url = url.substr( 7 );
- }
- string host , path;
- if ( url.find( "/" ) == string::npos ) {
- host = url;
- path = "/";
- }
- else {
- host = url.substr( 0 , url.find( "/" ) );
- path = url.substr( url.find( "/" ) );
- }
+ HD("host [" << host << "]");
+ HD("path [" << path << "]");
+ string server = host;
+ int port = ssl ? 443 : 80;
- HD( "host [" << host << "]" );
- HD( "path [" << path << "]" );
-
- string server = host;
- int port = ssl ? 443 : 80;
+ string::size_type idx = host.find(":");
+ if (idx != string::npos) {
+ server = host.substr(0, idx);
+ string t = host.substr(idx + 1);
+ port = atoi(t.c_str());
+ }
- string::size_type idx = host.find( ":" );
- if ( idx != string::npos ) {
- server = host.substr( 0 , idx );
- string t = host.substr( idx + 1 );
- port = atoi( t.c_str() );
+ HD("server [" << server << "]");
+ HD("port [" << port << "]");
+
+ string req;
+ {
+ stringstream ss;
+ ss << command << " " << path << " HTTP/1.1\r\n";
+ ss << "Host: " << host << "\r\n";
+ ss << "Connection: Close\r\n";
+ ss << "User-Agent: mongodb http client\r\n";
+ if (body) {
+ ss << "Content-Length: " << strlen(body) << "\r\n";
}
-
- HD( "server [" << server << "]" );
- HD( "port [" << port << "]" );
-
- string req;
- {
- stringstream ss;
- ss << command << " " << path << " HTTP/1.1\r\n";
- ss << "Host: " << host << "\r\n";
- ss << "Connection: Close\r\n";
- ss << "User-Agent: mongodb http client\r\n";
- if ( body ) {
- ss << "Content-Length: " << strlen( body ) << "\r\n";
- }
- ss << "\r\n";
- if ( body ) {
- ss << body;
- }
-
- req = ss.str();
+ ss << "\r\n";
+ if (body) {
+ ss << body;
}
- SockAddr addr( server.c_str() , port );
- uassert( 15000 , "server socket addr is invalid" , addr.isValid() );
- HD( "addr: " << addr.toString() );
+ req = ss.str();
+ }
- Socket sock;
- if ( ! sock.connect( addr ) )
- return -1;
-
- if ( ssl ) {
+ SockAddr addr(server.c_str(), port);
+ uassert(15000, "server socket addr is invalid", addr.isValid());
+ HD("addr: " << addr.toString());
+
+ Socket sock;
+ if (!sock.connect(addr))
+ return -1;
+
+ if (ssl) {
#ifdef MONGO_CONFIG_SSL
- // pointer to global singleton instance
- SSLManagerInterface* mgr = getSSLManager();
+ // pointer to global singleton instance
+ SSLManagerInterface* mgr = getSSLManager();
- sock.secure(mgr, "");
+ sock.secure(mgr, "");
#else
- uasserted( 15862 , "no ssl support" );
+ uasserted(15862, "no ssl support");
#endif
- }
-
- {
- const char * out = req.c_str();
- int toSend = req.size();
- sock.send( out , toSend, "_go" );
- }
-
- char buf[4097];
- int got = sock.unsafe_recv( buf , 4096 );
- buf[got] = 0;
-
- int rc;
- char version[32];
- verify( sscanf( buf , "%s %d" , version , &rc ) == 2 );
- HD( "rc: " << rc );
-
- StringBuilder sb;
- if ( result )
- sb << buf;
-
- // SERVER-8864, unsafe_recv will throw when recv returns 0 indicating closed socket.
- try {
- while ( ( got = sock.unsafe_recv( buf , 4096 ) ) > 0) {
- buf[got] = 0;
- if ( result )
- sb << buf;
- }
- } catch (const SocketException&) {}
+ }
+ {
+ const char* out = req.c_str();
+ int toSend = req.size();
+ sock.send(out, toSend, "_go");
+ }
- if ( result ) {
- result->_init( rc , sb.str() );
+ char buf[4097];
+ int got = sock.unsafe_recv(buf, 4096);
+ buf[got] = 0;
+
+ int rc;
+ char version[32];
+ verify(sscanf(buf, "%s %d", version, &rc) == 2);
+ HD("rc: " << rc);
+
+ StringBuilder sb;
+ if (result)
+ sb << buf;
+
+ // SERVER-8864, unsafe_recv will throw when recv returns 0 indicating closed socket.
+ try {
+ while ((got = sock.unsafe_recv(buf, 4096)) > 0) {
+ buf[got] = 0;
+ if (result)
+ sb << buf;
}
+ } catch (const SocketException&) {
+ }
- return rc;
+
+ if (result) {
+ result->_init(rc, sb.str());
}
- void HttpClient::Result::_init( int code , string entire ) {
- _code = code;
- _entireResponse = entire;
+ return rc;
+}
- while ( true ) {
- size_t i = entire.find( '\n' );
- if ( i == string::npos ) {
- // invalid
- break;
- }
+void HttpClient::Result::_init(int code, string entire) {
+ _code = code;
+ _entireResponse = entire;
- string h = entire.substr( 0 , i );
- entire = entire.substr( i + 1 );
+ while (true) {
+ size_t i = entire.find('\n');
+ if (i == string::npos) {
+ // invalid
+ break;
+ }
- if ( h.size() && h[h.size()-1] == '\r' )
- h = h.substr( 0 , h.size() - 1 );
+ string h = entire.substr(0, i);
+ entire = entire.substr(i + 1);
- if ( h.size() == 0 )
- break;
+ if (h.size() && h[h.size() - 1] == '\r')
+ h = h.substr(0, h.size() - 1);
- i = h.find( ':' );
- if ( i != string::npos )
- _headers[h.substr(0,i)] = str::ltrim(h.substr(i+1));
- }
+ if (h.size() == 0)
+ break;
- _body = entire;
+ i = h.find(':');
+ if (i != string::npos)
+ _headers[h.substr(0, i)] = str::ltrim(h.substr(i + 1));
}
+ _body = entire;
+}
}