void HttpClient::proxyConnect(PNetworkStream stream, ConstStrA host, ConstStrA authorization) { if (host.find(':') == naturalNull) { return proxyConnect(stream,StringA(host+ConstStrA(":443")),authorization); } PNetworkStream nstream = new BufferedNetworkStream(stream); HttpRequest req(stream.get(), host, mCONNECT, !useHTTP10); if (!authorization.empty()) { req.setHeader(fldProxyAuthorization,authorization); } req.closeOutput(); class ResponseResult: public IHttpResponseCB { public: virtual void storeStatus(natural statusCode, ConstStrA statusMessage) { status = statusCode; statusMsg.append(statusMessage); } virtual void storeHeaderLine(ConstStrA, ConstStrA) { } natural status; AutoArray<char, SmallAlloc<32> > statusMsg; }; ResponseResult res; HttpResponse resp(stream.get(),res,HttpResponse::readHeadersNow); if (status != 200) throw HttpStatusException(THISLOCATION,host,res.status, res.statusMsg); }
void HTTPSClientSession::connect(const SocketAddress& address) { if (getProxyHost().empty() || bypassProxy()) { SecureStreamSocket sss(socket()); if (sss.getPeerHostName().empty()) { sss.setPeerHostName(getHost()); } if (_pContext->sessionCacheEnabled()) { sss.useSession(_pSession); } HTTPSession::connect(address); if (_pContext->sessionCacheEnabled()) { _pSession = sss.currentSession(); } } else { StreamSocket proxySocket(proxyConnect()); SecureStreamSocket secureSocket = SecureStreamSocket::attach(proxySocket, getHost(), _pContext, _pSession); attachSocket(secureSocket); if (_pContext->sessionCacheEnabled()) { _pSession = secureSocket.currentSession(); } } }
void FileTransferHandler::ft_accepted() { d->offset = d->ft->offset(); d->length = d->ft->length(); d->c = d->ft->bsConnection(); if (dynamic_cast<S5BConnection*>(d->c)) { connect(d->c, SIGNAL(proxyQuery()), SLOT(s5b_proxyQuery())); connect(d->c, SIGNAL(proxyResult(bool)), SLOT(s5b_proxyResult(bool))); connect(d->c, SIGNAL(requesting()), SLOT(s5b_requesting())); connect(d->c, SIGNAL(accepted()), SLOT(s5b_accepted())); connect(d->c, SIGNAL(tryingHosts(const StreamHostList &)), SLOT(s5b_tryingHosts(const StreamHostList &))); connect(d->c, SIGNAL(proxyConnect()), SLOT(s5b_proxyConnect())); connect(d->c, SIGNAL(waitingForActivation()), SLOT(s5b_waitingForActivation())); }
void HttpClient::createRequest(ConstStrA url, Method method) { connectionReused = false; TextParser<char, SmallAlloc<1024> > parser; if (parser("http%(?)[s]1://%(*)*2%%[a-zA-Z0-9-_.:]3/%(*)4",url)) { ConstStrA s = parser[1].str(); ConstStrA auth = parser[2].str(); ConstStrA domain_port = parser[3].str(); ConstStrA path = parser[4].str(); path = url.tail(path.length()+1); if (!s.empty() && s != "s") throw InvalidUrlException(THISLOCATION,url,"unknown protocol"); if (!auth.empty() && s.tail(1)[0] != '@') throw InvalidUrlException(THISLOCATION,url,"auth need '@' as separator"); if (domain_port.tail(1)[0] == ':') throw InvalidUrlException(THISLOCATION,url,"missing port number"); bool tls = !s.empty(); IHttpProxyProvider::Result proxyInfo; if (proxyProvider) { proxyInfo = proxyProvider->getProxy(domain_port); } if (canReuseConnection(domain_port, tls)) { try { response->skipRemainBody(); connectionReused = true; } catch (const NetworkException &) { response = nil; nstream = nil; } } if (!connectionReused) { if (tls) { if (httpsProvider == 0) { throw InvalidUrlException(THISLOCATION,url,"Https is not configured"); } PNetworkStream stream; if (proxyInfo.defined) { stream = connectSite(proxyInfo.proxyAddr,8080); proxyConnect(stream, domain_port, proxyInfo.authorization); } else { stream = connectSite(domain_port, 443); } stream = httpsProvider->connectTLS(stream,domain_port); nstream = new BufferedNetworkStream(stream); currentDomain = domain_port; currentTls = true; } else { if (proxyInfo.defined) { PNetworkStream stream; stream = connectSite(proxyInfo.proxyAddr,8080); nstream = new BufferedNetworkStream(stream); path = url; currentDomain = proxyInfo.proxyAddr; } else { PNetworkStream stream; stream = connectSite(domain_port,80); nstream = new BufferedNetworkStream(stream); currentDomain = domain_port; } } } response = nil; request = new(pool) HttpRequest(nstream.get(),path,method,!useHTTP10); if (proxyInfo.defined && !tls && !proxyInfo.authorization.empty()) { request->setHeader(fldProxyAuthorization, proxyInfo.authorization); } request->setHeader(fldHost, domain_port); request->setHeader(fldUserAgent, userAgent); if (keepAlive == false) { request->setHeader(fldConnection,"close"); } else if (useHTTP10) { request->setHeader(fldConnection,"keep-alive"); } } else { throw InvalidUrlException(THISLOCATION,url,"Parser rejected"); } }
int main(void) { unsigned int j; redisReply *reply; proxyContext *p; redisAddr addrs[SERVER_COUNT] = { { "127.0.0.1", 2000 }, { "127.0.0.1", 2001 }, { "127.0.0.1", 2002 } }; p = proxyConnect( addrs, SERVER_COUNT ); if (!p) { printf("Connection error:\n" ); exit(1); } reply = proxyCommand(p,"AUTH %s","1234"); printf("AUTH: %s\n", reply->str); freeReplyObject(reply); /* PING server */ reply = proxyCommand(p,"PING"); printf("PING: %s\n", reply->str); freeReplyObject(reply); reply = proxyCommand(p,"FLUSHALL"); printf("FLUSHALL: %s\n", reply->str); freeReplyObject(reply); int i; /* Set a key using binary safe API */ for( i = 0; i < 1000000; i++ ) { reply = proxyCommand( p,"SET %s%d %s", "foo", i, "hello world"); if( reply ){ printf("SET: %s%d %s\n", "foo", i, reply->str); freeReplyObject(reply); } } for( i = 0; i < 1000; i++ ) { reply = proxyCommand( p,"GET %s%d", "foo", i); if( reply ){ printf("GET: %s%d %s\n", "foo", i, reply->str); freeReplyObject(reply); } } reply = proxyCommand( p, "SET %b %b", "bar", 3, "hello", 5); printf("SET (binary API): %s\n", reply->str); freeReplyObject(reply); reply = proxyCommand(p,"dbsize"); printf("%lld\n", reply->integer); freeReplyObject(reply); reply = proxyCommand(p,"TOP123 foo bar"); printf("%s\n", reply->str); freeReplyObject(reply); reply = redisCommand(p->contexts[0],"TOP123 foo bar"); printf("%s\n", reply->str); freeReplyObject(reply); for (j = 0; j < 10; j++) { char buf[64]; snprintf(buf,64,"%d",j); reply = proxyCommand(p,"LPUSH mylist element-%s", buf); freeReplyObject(reply); } /* Let's check what we have inside the list */ reply = proxyCommand(p,"LRANGE mylist 0 -1"); if (reply->type == REDIS_REPLY_ARRAY) { for (j = 0; j < reply->elements; j++) { printf("%u) %s\n", j, reply->element[j]->str); } } freeReplyObject(reply); reply = proxyCommand(p ,"mset foo 123 bar 123 key 123"); printf("%s\n", reply->str); freeReplyObject(reply); reply = proxyCommand(p ,"mget foo bar key asdf"); if (reply->type == REDIS_REPLY_ARRAY) { for (j = 0; j < reply->elements; j++) { printf("%u) %s\n", j, reply->element[j]->str); } } destroyProxyContext(p); return 0; }
void HTTPClientSession::proxyTunnel() { StreamSocket ss = proxyConnect(); attachSocket(ss); }