Skip to content

Commit

Permalink
re #11069 ScriptRepo now works with the new internetHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
NickDraper committed Feb 18, 2015
1 parent 1b8b39e commit 1b3e513
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 208 deletions.
13 changes: 7 additions & 6 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/InternetHelper.h
Expand Up @@ -6,8 +6,6 @@
#include "MantidKernel/ProxyInfo.h"

#include <map>
#include <iostream>
#include <sstream>

namespace Poco {
// forward declaration
Expand Down Expand Up @@ -79,9 +77,11 @@ class MANTID_KERNEL_DLL InternetHelper {
void setBody(const std::string& body);
void setBody(const std::ostringstream& body);
void setBody(Poco::Net::HTMLForm& form);
const std::string getBody();


const std::string& getBody();

int getResponseStatus();
const std::string& getResponseReason();

void addHeader(const std::string& key, const std::string& value);
void removeHeader (const std::string& key);
const std::string& getHeader (const std::string& key);
Expand Down Expand Up @@ -125,9 +125,10 @@ class MANTID_KERNEL_DLL InternetHelper {
std::streamsize m_contentLength;
std::string m_method;
std::string m_contentType;
std::ostringstream m_body;
std::string m_body;
StringToStringMap m_headers;
Poco::Net::HTTPRequest *m_request;
Poco::Net::HTTPResponse *m_response;
};

} // namespace Kernel
Expand Down
70 changes: 45 additions & 25 deletions Code/Mantid/Framework/Kernel/src/InternetHelper.cpp
Expand Up @@ -34,6 +34,7 @@

// std
#include <fstream>
#include <sstream>

namespace Mantid {
namespace Kernel {
Expand Down Expand Up @@ -75,22 +76,25 @@ bool isRelocated(const int response) {
InternetHelper::InternetHelper()
: m_proxyInfo(), m_isProxySet(false), m_timeout(30), m_contentLength(0),
m_method(HTTPRequest::HTTP_GET), m_contentType("application/json"),
m_body(), m_headers(), m_request(NULL) {}
m_body(), m_headers(), m_request(NULL),m_response(NULL) {}

//----------------------------------------------------------------------------------------------
/** Constructor
*/
InternetHelper::InternetHelper(const Kernel::ProxyInfo &proxy)
: m_proxyInfo(proxy), m_isProxySet(true), m_timeout(30),
m_method(HTTPRequest::HTTP_GET), m_contentType("application/json"),
m_body(), m_headers(), m_request(NULL) {}
m_body(), m_headers(), m_request(NULL),m_response(NULL) {}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
InternetHelper::~InternetHelper() {
if (m_request != NULL) {
delete m_request;
}
if (m_response != NULL) {
delete m_response;
}
}

Expand All @@ -107,9 +111,13 @@ void InternetHelper::createRequest(Poco::URI &uri) {
if (m_request != NULL) {
delete m_request;
}
if (m_response != NULL) {
delete m_response;
}

m_request =
new HTTPRequest(m_method, uri.getPathAndQuery(), HTTPMessage::HTTP_1_1);
m_response = new HTTPResponse();
if (!m_contentType.empty()) {
m_request->setContentType(m_contentType);
}
Expand All @@ -136,10 +144,10 @@ int InternetHelper::sendRequestAndProcess(HTTPClientSession &session,
this->createRequest(uri);
session.sendRequest(*m_request) << m_body;

HTTPResponse res;
std::istream &rs = session.receiveResponse(res);
int retStatus = res.getStatus();
g_log.debug() << "Answer from web: " << retStatus << " " << res.getReason()

std::istream &rs = session.receiveResponse(*m_response);
int retStatus = m_response->getStatus();
g_log.debug() << "Answer from web: " << retStatus << " " << m_response->getReason()
<< std::endl;

if (retStatus == HTTPResponse::HTTP_OK ||
Expand All @@ -148,9 +156,10 @@ int InternetHelper::sendRequestAndProcess(HTTPClientSession &session,
Poco::StreamCopier::copyStream(rs, responseStream);
return retStatus;
} else if (isRelocated(retStatus)) {
return this->processRelocation(res, responseStream);
return this->processRelocation(*m_response, responseStream);
} else {
return processErrorStates(res, rs, uri.toString());
Poco::StreamCopier::copyStream(rs, responseStream);
return processErrorStates(*m_response, rs, uri.toString());
}
}

Expand Down Expand Up @@ -352,6 +361,7 @@ int InternetHelper::processErrorStates(const Poco::Net::HTTPResponse &res,
// show the error
info << res.getReason();
info << ss.str();
g_log.debug() << ss.str();
}
throw Exception::InternetError(info.str() + ss.str(), retStatus);
}
Expand Down Expand Up @@ -393,6 +403,12 @@ int InternetHelper::downloadFile(const std::string &urlFile,

// if there have been no errors move it to the final location, and turn off
// automatic deletion.
//clear the way if the target file path is already in use
Poco::File file(localFilePath);
if (file.exists()) {
file.remove();
}

tempFile.moveTo(localFilePath);
tempFile.keep();

Expand All @@ -417,7 +433,7 @@ void InternetHelper::setMethod(const std::string& method) {
if (method == "POST") {
m_method = method;
} else {
m_method = "GET";
m_method = "GET";
}
}

Expand Down Expand Up @@ -456,15 +472,13 @@ std::streamsize InternetHelper::getContentLength() { return m_contentLength; }
* @param body A string of the body
**/
void InternetHelper::setBody(const std::string& body) {
//clear the old body
m_body.str("") ;
if (body.empty()) {
setMethod("GET");
m_body = body;
if (m_body.empty()) {
m_method="GET";
} else {
setMethod("POST");
m_body << body;
m_method="POST";
}
setContentLength(body.size());
setContentLength(m_body.size());
}

/** Sets the body & content length for future requests, this will also
Expand All @@ -473,7 +487,7 @@ void InternetHelper::setBody(const std::string& body) {
* @param body A stringstream of the body
**/
void InternetHelper::setBody(const std::ostringstream& body) {
setBody(body.str());
setBody(body.str());
}

/** Sets the body & content length for future requests, this will also
Expand All @@ -490,22 +504,28 @@ void InternetHelper::setBody(Poco::Net::HTMLForm& form) {
}
form.prepareSubmit(*m_request);
setContentType(m_request->getContentType());
//clear the old body
m_body.str("") ;

form.write(m_body);
setContentLength(m_body.tellp());
std::ostringstream ss;
form.write(ss);
m_body = ss.str();
setContentLength(m_body.size());
}

/** Gets the body set for future requests
* @returns A string of the content type
**/
const std::string& InternetHelper::getBody() {return m_body; }


/** Gets the body set for future requests
* @returns A string of the content type
**/
const std::string InternetHelper::getBody() {return m_body.str(); }
int InternetHelper::getResponseStatus() {return m_response->getStatus(); }

void setBody(const std::string& body);
const std::string getBody();
/** Gets the body set for future requests
* @returns A string of the content type
**/
const std::string& InternetHelper::getResponseReason() {return m_response->getReason(); }

/** Adds a header
* @param key The key to refer to the value
Expand Down Expand Up @@ -545,7 +565,7 @@ std::map<std::string, std::string>& InternetHelper::headers() {
void InternetHelper::reset() {
m_headers.clear();
m_timeout = 30;
m_body.str("");
m_body = "";
m_method = HTTPRequest::HTTP_GET;
m_contentType = "application/json";
m_request = NULL ;
Expand Down
Expand Up @@ -167,7 +167,6 @@ class SCRIPT_DLL_EXPORT ScriptRepositoryImpl : public ScriptRepository {

std::string ignoreregex;

bool getProxyConfig(std::string &, unsigned short &);

std::string getParentFolder(const std::string &entry);
};
Expand Down

0 comments on commit 1b3e513

Please sign in to comment.