void HTTPSClientSessionTest::testKeepAlive() { HTTPSTestServer srv; HTTPSClientSession s("localhost", srv.port()); s.setKeepAlive(true); HTTPRequest request(HTTPRequest::HTTP_HEAD, "/keepAlive", HTTPMessage::HTTP_1_1); s.sendRequest(request); HTTPResponse response; std::istream& rs1 = s.receiveResponse(response); assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length()); assert (response.getContentType() == "text/plain"); assert (response.getKeepAlive()); std::ostringstream ostr1; assert (StreamCopier::copyStream(rs1, ostr1) == 0); request.setMethod(HTTPRequest::HTTP_GET); request.setURI("/small"); s.sendRequest(request); std::istream& rs2 = s.receiveResponse(response); assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length()); assert (response.getKeepAlive()); std::ostringstream ostr2; StreamCopier::copyStream(rs2, ostr2); assert (ostr2.str() == HTTPSTestServer::SMALL_BODY); request.setMethod(HTTPRequest::HTTP_GET); request.setURI("/large"); s.sendRequest(request); std::istream& rs3 = s.receiveResponse(response); assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); assert (response.getChunkedTransferEncoding()); assert (response.getKeepAlive()); std::ostringstream ostr3; int n = StreamCopier::copyStream(rs3, ostr3); assert (ostr3.str() == HTTPSTestServer::LARGE_BODY); request.setMethod(HTTPRequest::HTTP_HEAD); request.setURI("/large"); s.sendRequest(request); std::istream& rs4= s.receiveResponse(response); assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length()); assert (response.getContentType() == "text/plain"); assert (!response.getKeepAlive()); std::ostringstream ostr4; assert (StreamCopier::copyStream(rs4, ostr4) == 0); }
TEST(HTTPClientSession, testHead) { HTTPClientSession s("localhost", 80); HTTPRequest request(HTTPRequest::HTTP_HEAD, "/large"); s.sendRequest(request); HTTPResponse response; std::istream& rs = s.receiveResponse(response); EXPECT_TRUE (response.getContentLength() > 0); EXPECT_TRUE (response.getContentType() == "text/plain"); std::ostringstream ostr; EXPECT_TRUE (StreamCopier::copyStream(rs, ostr) == 0); }
void HTTPSClientSessionTest::testHead() { HTTPSTestServer srv; HTTPSClientSession s("localhost", srv.port()); HTTPRequest request(HTTPRequest::HTTP_HEAD, "/large"); s.sendRequest(request); HTTPResponse response; std::istream& rs = s.receiveResponse(response); assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length()); assert (response.getContentType() == "text/plain"); std::ostringstream ostr; assert (StreamCopier::copyStream(rs, ostr) == 0); }
TEST(HTTPClientSession, testGetLarge) { HTTPClientSession s("localhost", 80); HTTPRequest request(HTTPRequest::HTTP_GET, "/index.html"); s.sendRequest(request); HTTPResponse response; std::istream& rs = s.receiveResponse(response); EXPECT_TRUE (response.getContentLength() > 0); EXPECT_TRUE (response.getContentType() == "text/plain"); std::ostringstream ostr; StreamCopier::copyStream(rs, ostr); //EXPECT_TRUE (ostr.str() == HTTPTestServer::LARGE_BODY); }
void HTTPSClientSessionTest::testGetSmall() { HTTPSTestServer srv; HTTPSClientSession s("localhost", srv.port()); HTTPRequest request(HTTPRequest::HTTP_GET, "/small"); s.sendRequest(request); HTTPResponse response; std::istream& rs = s.receiveResponse(response); assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length()); assert (response.getContentType() == "text/plain"); std::ostringstream ostr; StreamCopier::copyStream(rs, ostr); assert (ostr.str() == HTTPSTestServer::SMALL_BODY); }
TEST(HTTPClientSession, testProxy) { HTTPClientSession s("www.somehost.com"); s.setProxy("localhost", 80); HTTPRequest request(HTTPRequest::HTTP_GET, "/large"); s.sendRequest(request); HTTPResponse response; std::istream& rs = s.receiveResponse(response); //EXPECT_TRUE (response.getContentLength() == HTTPTestServer::LARGE_BODY.length()); EXPECT_TRUE (response.getContentType() == "text/plain"); std::ostringstream ostr; StreamCopier::copyStream(rs, ostr); //EXPECT_TRUE (ostr.str() == HTTPTestServer::LARGE_BODY); }
void HTTPSClientSessionTest::testUnknownContentLength() { HTTPSTestServer srv; HTTPSClientSession s("127.0.0.1", srv.port()); HTTPRequest request(HTTPRequest::HTTP_GET, "/nolength"); s.sendRequest(request); HTTPResponse response; std::istream& rs = s.receiveResponse(response); assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); assert (response.getContentType() == "text/plain"); std::ostringstream ostr; StreamCopier::copyStream(rs, ostr); assert (ostr.str() == HTTPSTestServer::SMALL_BODY); }
void HTTPSClientSessionTest::testServerAbort() { HTTPSTestServer srv; HTTPSClientSession s("127.0.0.1", srv.port()); HTTPRequest request(HTTPRequest::HTTP_GET, "/nolength/connection/abort"); s.sendRequest(request); HTTPResponse response; std::istream& rs = s.receiveResponse(response); assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); assert (response.getContentType() == "text/plain"); std::ostringstream ostr; StreamCopier::copyStream(rs, ostr); assert (ostr.str() == HTTPSTestServer::SMALL_BODY); assert ( dynamic_cast<const Poco::Net::SSLConnectionUnexpectedlyClosedException*>( s.networkException()) != NULL ); }
TEST(HTTPClientSession, testProxyAuth) { HTTPClientSession s("www.somehost.com"); s.setProxy("localhost", 80); s.setProxyCredentials("user", "pass"); HTTPRequest request(HTTPRequest::HTTP_GET, "/large"); s.sendRequest(request); HTTPResponse response; std::istream& rs = s.receiveResponse(response); EXPECT_TRUE (response.getContentLength() > 0); EXPECT_TRUE (response.getContentType() == "text/plain"); std::ostringstream ostr; StreamCopier::copyStream(rs, ostr); //EXPECT_TRUE (ostr.str() == HTTPTestServer::LARGE_BODY); //std::string r = srv.lastRequest(); //EXPECT_TRUE (r.find("Proxy-Authorization: Basic dXNlcjpwYXNz\r\n") != std::string::npos); }
bool ofxSimpleHttp::downloadURL(ofxSimpleHttpResponse* resp, bool sendResultThroughEvents, bool beingCalledFromMainThread, bool saveToDisk){ bool ok; ofstream myfile; bool fileIsAlreadyHere = false; //create a file to save the stream to if(saveToDisk){ if (resp->expectedChecksum.length()){ //if user provided a checksum ofFile f; f.open(resp->absolutePath); if (f.exists()){ fileIsAlreadyHere = ofxChecksum::sha1(resp->absolutePath, resp->expectedChecksum); if(fileIsAlreadyHere){ resp->checksumOK = true; resp->status = 0; resp->ok = true; resp->fileWasHere = true; ofLogVerbose() << "ofxSimpleHttp: about to download "<< resp->url << " but a file with same name and correct checksum is already here!"; ofLogVerbose() << "ofxSimpleHttp: skipping download (" << resp->expectedChecksum << ")"; } } f.close(); }else{ if (!onlySkipDownloadIfChecksumMatches){ ofFile f; f.open(resp->absolutePath); if (f.exists() && f.getSize() > 0){ resp->checksumOK = false; resp->status = 0; resp->ok = true; resp->fileWasHere = true; fileIsAlreadyHere = true; ofLogVerbose() << "ofxSimpleHttp: about to download "<< resp->url << " but a file with same name and (size > 0) is already here!"; ofLogVerbose() << "ofxSimpleHttp: skipping download (missing checksum)"; } f.close(); } } } if (!fileIsAlreadyHere){ //if file is not here, download it! myfile.open( resp->absolutePath.c_str(), ios_base::binary ); //myfile.open(ofToDataPath(filename).c_str()); //for not binary? try { ofHttpRequest request(resp->url, resp->url); URI uri(request.url); std::string path(uri.getPathAndQuery()); if (path.empty()) path = "/"; HTTPClientSession * session; if(uri.getScheme()=="https"){ session = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context); }else{ session = new HTTPClientSession(uri.getHost(), uri.getPort()); } //resp->session = &session; HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); req.set( "User-Agent", userAgent.c_str() ); session->setTimeout( Poco::Timespan(timeOut,0) ); session->sendRequest(req); HTTPResponse res; istream& rs = session->receiveResponse(res); resp->status = res.getStatus(); try { resp->timestamp = res.getDate(); } catch (Exception& exc) { resp->timestamp = 0; } resp->reasonForStatus = res.getReasonForStatus( res.getStatus() ); resp->contentType = res.getContentType(); resp->serverReportedSize = res.getContentLength(); resp->timeTakenToDownload = ofGetElapsedTimef(); if (resp->serverReportedSize == -1) ofLogWarning("ofxSimpleHttp", "downloadURL(%s) >> Server doesn't report download size...", resp->fileName.c_str() ); ofLogVerbose("ofxSimpleHttp", "downloadURL() >> about to start download (%s, %d bytes)", resp->fileName.c_str(), res.getContentLength() ); ofLogVerbose("ofxSimpleHttp", "downloadURL() >> server reports request status: (%d-%s)", resp->status, resp->reasonForStatus.c_str() ); //StreamCopier::copyStream(rs, myfile); //write to file here! if(saveToDisk){ streamCopyWithProgress(rs, myfile, resp->serverReportedSize, resp->downloadProgress, resp->downloadSpeed, resp->downloadCanceled); }else{ copyToStringWithProgress(rs, resp->responseBody, resp->serverReportedSize, resp->downloadProgress, resp->downloadSpeed, resp->downloadCanceled); } resp->timeTakenToDownload = ofGetElapsedTimef() - resp->timeTakenToDownload; if (resp->expectedChecksum.length() > 0){ resp->checksumOK = ofxChecksum::sha1(resp->absolutePath, resp->expectedChecksum); if(!resp->checksumOK){ ofLogVerbose() << "ofxSimpleHttp: downloaded OK but Checksum FAILED"; ofLogVerbose() << "ofxSimpleHttp: SHA1 was meant to be: " << resp->expectedChecksum; } } resp->downloadSpeed = 0; resp->avgDownloadSpeed = 0; resp->downloadedBytes = 0; //resp->session = NULL; delete session; session = NULL; if(saveToDisk){ myfile.close(); } if (resp->downloadCanceled){ //delete half-baked download file if (resp->downloadToDisk){ ofFile f; if(f.open(resp->absolutePath)){ if (f.isFile()){ f.remove(); } } } }else{ if(saveToDisk){ ofLogNotice("ofxSimpleHttp", "downloadURL() downloaded to %s", resp->fileName.c_str() ); } if( saveToDisk ){ //ask the filesystem what is the real size of the file ofFile file; try{ file.open(resp->absolutePath.c_str()); resp->downloadedBytes = file.getSize(); file.close(); }catch(Exception& exc){ ofLogError("ofxSimpleHttp", "downloadURL(%s) >> Exception at file.open: %s", resp->fileName.c_str(), exc.displayText().c_str() ); } }else{ resp->downloadedBytes = resp->responseBody.size(); } resp->avgDownloadSpeed = (resp->downloadedBytes / 1024.) / resp->timeTakenToDownload; //kb/sec //check download file size missmatch if ( resp->serverReportedSize > 0 && resp->serverReportedSize != resp->downloadedBytes) { ofLogWarning("ofxSimpleHttp", "downloadURLtoDiskBlocking() >> Download size mismatch (%s) >> Server: %d Downloaded: %d", resp->fileName.c_str(), resp->serverReportedSize, resp->downloadedBytes ); resp->reasonForStatus = "Download size mismatch!"; resp->status = -1; resp->ok = false; }else{ if (resp->status == 200){ resp->ok = true; }else{ resp->ok = false; } } ofLogVerbose() << "ofxSimpleHttp: download finished! " << resp->url << " !"; ok = TRUE; } }catch(Exception& exc){ myfile.close(); ofLogError("ofxSimpleHttp", "downloadURL(%s) >> Exception: %s", resp->fileName.c_str(), exc.displayText().c_str() ); resp->reasonForStatus = exc.displayText(); resp->ok = false; resp->status = -1; ok = false; ofLogError() << "ofxSimpleHttp: failed to download " << resp->url << " !"; } } //enqueue the operation result! if (sendResultThroughEvents ){ if ( resp->notifyOnSuccess ){ if(idleTimeAfterEachDownload > 0.0){ ofSleepMillis(idleTimeAfterEachDownload * 1000); } if (beingCalledFromMainThread){ //we running on main thread, we can just snd the notif from here ofNotifyEvent( httpResponse, *resp, this ); }else{ //we are running from a bg thread if (notifyFromMainThread){ //user wants to get notified form main thread, we need to enqueue the notification if (timeToStop == false){ //see if we have been destructed! dont forward events if so lock(); ofxSimpleHttpResponse tempCopy = *resp; responsesPendingNotification.push(tempCopy); unlock(); } }else{ //user doesnt care about main thread, the notificaiton can come from bg thread so we do it from here ofNotifyEvent( httpResponse, *resp, this ); } } } } return ok; }