ofxHttpResponse ofxHttpUtils::getUrl(string url){ ofxHttpResponse response; try{ URI uri(url.c_str()); std::string path(uri.getPathAndQuery()); if (path.empty()) path = "/"; HTTPClientSession session(uri.getHost(), uri.getPort()); HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); if(auth.getUsername()!="") auth.authenticate(req); session.setTimeout(Poco::Timespan(timeoutSeconds,0)); session.sendRequest(req); HTTPResponse res; istream& rs = session.receiveResponse(res); response=ofxHttpResponse(res, rs, path); if(response.status>=300 && response.status<400){ Poco::URI uri(req.getURI()); uri.resolve(res.get("Location")); response.location = uri.toString(); } ofNotifyEvent( newResponseEvent, response, this ); //std::cout << res.getStatus() << " " << res.getReason() << std::endl; //StreamCopier::copyStream(rs, std::cout); }catch (Exception& exc){ std::cerr << exc.displayText() << "\n"; } return response; }
int InternetHelper::processRelocation(const HTTPResponse &response, std::ostream &responseStream) { std::string newLocation = response.get("location", ""); if (!newLocation.empty()) { g_log.information() << "url relocated to " << newLocation; return this->sendRequest(newLocation, responseStream); } else { g_log.warning("Apparent relocation did not give new location\n"); return response.getStatus(); } }
// ---------------------------------------------------------------------- ofxHttpResponse ofxHttpUtils::getUrl(string url){ ofxHttpResponse response; try{ URI uri(url.c_str()); std::string path(uri.getPathAndQuery()); if (path.empty()) path = "/"; HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); if(auth.getUsername()!="") auth.authenticate(req); if(sendCookies){ for(unsigned i=0; i<cookies.size(); i++){ NameValueCollection reqCookies; reqCookies.add(cookies[i].getName(),cookies[i].getValue()); req.setCookies(reqCookies); } } HTTPResponse res; ofPtr<HTTPSession> session; istream * rs; if(uri.getScheme()=="https"){ HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context); httpsSession->setTimeout(Poco::Timespan(timeoutSeconds,0)); httpsSession->sendRequest(req); rs = &httpsSession->receiveResponse(res); session = ofPtr<HTTPSession>(httpsSession); }else{ HTTPClientSession * httpSession = new HTTPClientSession(uri.getHost(), uri.getPort()); httpSession->setTimeout(Poco::Timespan(timeoutSeconds,0)); httpSession->sendRequest(req); rs = &httpSession->receiveResponse(res); session = ofPtr<HTTPSession>(httpSession); } response=ofxHttpResponse(res, *rs, url); if(sendCookies){ cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end()); } if(response.status>=300 && response.status<400){ Poco::URI uri(req.getURI()); uri.resolve(res.get("Location")); response.location = uri.toString(); } ofNotifyEvent( newResponseEvent, response, this ); //std::cout << res.getStatus() << " " << res.getReason() << std::endl; //StreamCopier::copyStream(rs, std::cout); }catch (Exception& exc){ ofLogError("ofxHttpUtils") << exc.displayText(); response.status = -1; response.reasonForStatus = exc.displayText(); ofNotifyEvent(newResponseEvent, response, this); } return response; }
// ---------------------------------------------------------------------- ofxHttpResponse ofxHttpUtils::doPostForm(ofxHttpForm & form){ ofxHttpResponse response; try{ URI uri( form.action.c_str() ); std::string path(uri.getPathAndQuery()); if (path.empty()) path = "/"; //HTTPClientSession session(uri.getHost(), uri.getPort()); HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1); if(auth.getUsername()!="") auth.authenticate(req); if(sendCookies){ for(unsigned i=0; i<cookies.size(); i++){ NameValueCollection reqCookies; reqCookies.add(cookies[i].getName(),cookies[i].getValue()); req.setCookies(reqCookies); } } for (unsigned int i = 0; i < form.headerIds.size(); ++i) { const std::string name = form.headerIds[i].c_str(); const std::string val = form.headerValues[i].c_str(); req.set(name, val); } HTTPResponse res; HTMLForm pocoForm; // create the form data to send if(form.formFiles.size()>0) { pocoForm.setEncoding(HTMLForm::ENCODING_MULTIPART); } else { pocoForm.setEncoding(HTMLForm::ENCODING_URL); } // form values for(unsigned i=0; i<form.formIds.size(); i++){ const std::string name = form.formIds[i].c_str(); const std::string val = form.formValues[i].c_str(); pocoForm.set(name, val); } map<string,string>::iterator it; for(it = form.formFiles.begin(); it!=form.formFiles.end(); it++){ string fileName = it->second.substr(it->second.find_last_of('/')+1); ofLogVerbose("ofxHttpUtils") << "adding file: " << fileName << " path: " << it->second; pocoForm.addPart(it->first,new FilePartSource(it->second)); } pocoForm.prepareSubmit(req); ofPtr<HTTPSession> session; istream * rs; if(uri.getScheme()=="https"){ HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context); httpsSession->setTimeout(Poco::Timespan(20,0)); pocoForm.write(httpsSession->sendRequest(req)); rs = &httpsSession->receiveResponse(res); session = ofPtr<HTTPSession>(httpsSession); }else{ HTTPClientSession * httpSession = new HTTPClientSession(uri.getHost(), uri.getPort()); httpSession->setTimeout(Poco::Timespan(20,0)); pocoForm.write(httpSession->sendRequest(req)); rs = &httpSession->receiveResponse(res); session = ofPtr<HTTPSession>(httpSession); } response = ofxHttpResponse(res, *rs, form.action); if(sendCookies){ cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end()); } if(response.status>=300 && response.status<400){ Poco::URI uri(req.getURI()); uri.resolve(res.get("Location")); response.location = uri.toString(); } ofNotifyEvent(newResponseEvent, response, this); }catch (Exception& exc){ ofLogError("ofxHttpUtils") << "ofxHttpUtils error doPostForm -- " << form.action.c_str(); //ofNotifyEvent(notifyNewError, "time out", this); // for now print error, need to broadcast a response ofLogError("ofxHttpUtils") << exc.displayText(); response.status = -1; response.reasonForStatus = exc.displayText(); ofNotifyEvent(newResponseEvent, response, this); } return response; }
// ---------------------------------------------------------------------- ofxHttpResponse ofxHttpUtils::postData(string url, const ofBuffer & data, string contentType){ ofxHttpResponse response; try{ URI uri( url.c_str() ); std::string path(uri.getPathAndQuery()); if (path.empty()) path = "/"; //HTTPClientSession session(uri.getHost(), uri.getPort()); HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1); if(auth.getUsername()!="") auth.authenticate(req); if(sendCookies){ for(unsigned i=0; i<cookies.size(); i++){ NameValueCollection reqCookies; reqCookies.add(cookies[i].getName(),cookies[i].getValue()); req.setCookies(reqCookies); } } if(contentType!=""){ req.setContentType(contentType); } req.setContentLength(data.size()); HTTPResponse res; ofPtr<HTTPSession> session; istream * rs; if(uri.getScheme()=="https"){ HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context); httpsSession->setTimeout(Poco::Timespan(20,0)); httpsSession->sendRequest(req) << data; rs = &httpsSession->receiveResponse(res); session = ofPtr<HTTPSession>(httpsSession); }else{ HTTPClientSession * httpSession = new HTTPClientSession(uri.getHost(), uri.getPort()); httpSession->setTimeout(Poco::Timespan(20,0)); httpSession->sendRequest(req) << data; rs = &httpSession->receiveResponse(res); session = ofPtr<HTTPSession>(httpSession); } response = ofxHttpResponse(res, *rs, url); if(sendCookies){ cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end()); } if(response.status>=300 && response.status<400){ Poco::URI uri(req.getURI()); uri.resolve(res.get("Location")); response.location = uri.toString(); } ofNotifyEvent(newResponseEvent, response, this); }catch (Exception& exc){ ofLogError("ofxHttpUtils") << "ofxHttpUtils error postData --"; //ofNotifyEvent(notifyNewError, "time out", this); // for now print error, need to broadcast a response ofLogError("ofxHttpUtils") << exc.displayText(); response.status = -1; response.reasonForStatus = exc.displayText(); ofNotifyEvent(newResponseEvent, response, this); } return response; }
std::istream* HTTPStreamFactory::open(const URI& uri) { poco_assert (uri.getScheme() == "http"); URI resolvedURI(uri); URI proxyUri; HTTPClientSession* pSession = 0; HTTPResponse res; bool retry = false; bool authorize = false; std::string username; std::string password; try { do { if (!pSession) { pSession = new HTTPClientSession(resolvedURI.getHost(), resolvedURI.getPort()); if (proxyUri.empty()) pSession->setProxy(_proxyHost, _proxyPort); else pSession->setProxy(proxyUri.getHost(), proxyUri.getPort()); pSession->setProxyCredentials(_proxyUsername, _proxyPassword); } std::string path = resolvedURI.getPathAndQuery(); if (path.empty()) path = "/"; HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); if (authorize) { HTTPCredentials::extractCredentials(uri, username, password); HTTPCredentials cred(username, password); cred.authenticate(req, res); } pSession->sendRequest(req); std::istream& rs = pSession->receiveResponse(res); bool moved = (res.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY || res.getStatus() == HTTPResponse::HTTP_FOUND || res.getStatus() == HTTPResponse::HTTP_SEE_OTHER || res.getStatus() == HTTPResponse::HTTP_TEMPORARY_REDIRECT); if (moved) { resolvedURI.resolve(res.get("Location")); if (!username.empty()) { resolvedURI.setUserInfo(username + ":" + password); } throw URIRedirection(resolvedURI.toString()); } else if (res.getStatus() == HTTPResponse::HTTP_OK) { return new HTTPResponseStream(rs, pSession); } else if (res.getStatus() == HTTPResponse::HTTP_USEPROXY && !retry) { // The requested resource MUST be accessed through the proxy // given by the Location field. The Location field gives the // URI of the proxy. The recipient is expected to repeat this // single request via the proxy. 305 responses MUST only be generated by origin servers. // only use for one single request! proxyUri.resolve(res.get("Location")); delete pSession; pSession = 0; retry = true; // only allow useproxy once } else if (res.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED && !authorize) { authorize = true; retry = true; Poco::NullOutputStream null; Poco::StreamCopier::copyStream(rs, null); } else throw HTTPException(res.getReason(), uri.toString()); } while (retry); throw HTTPException("Too many redirects", uri.toString()); } catch (...) { delete pSession; throw; } }