Mantid
|
InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS. More...
#include <InternetHelper.h>
Public Member Functions | |
void | addHeader (const std::string &key, const std::string &value) |
Adds a header. More... | |
void | clearHeaders () |
Clears all headers. More... | |
void | clearProxy () |
Clears cached proxy details. More... | |
virtual HTTPStatus | downloadFile (const std::string &urlFile, const std::string &localFilePath="") |
Download a url and fetch it inside the local path given. More... | |
const std::string & | getBody () |
Gets the body set for future requests. More... | |
std::streamsize | getContentLength () |
Gets the content length. More... | |
const std::string & | getContentType () |
Gets the Content Type. More... | |
const std::string & | getHeader (const std::string &key) |
Gets the value of a header. More... | |
const std::string & | getMethod () |
Gets the method. More... | |
Kernel::ProxyInfo & | getProxy (const std::string &url) |
Gets proxy details for a system and a url. More... | |
const std::string & | getResponseReason () |
Gets the body set for future requests. More... | |
HTTPStatus | getResponseStatus () |
Gets the body set for future requests. More... | |
int | getTimeout () |
Gets the timeout in seconds. More... | |
StringToStringMap & | headers () |
Returns a reference to the headers map. More... | |
InternetHelper () | |
Constructor. More... | |
InternetHelper (const Kernel::ProxyInfo &proxy) | |
Constructor. More... | |
void | removeHeader (const std::string &key) |
Removes a header. More... | |
virtual void | reset () |
Resets properties to defaults (except the proxy) More... | |
virtual HTTPStatus | sendRequest (const std::string &url, std::ostream &responseStream) |
Performs a request using http or https depending on the url. More... | |
void | setBody (const std::ostringstream &body) |
Sets the body & content length for future requests, this will also set the method to POST is the body is not empty and GET if it is. More... | |
void | setBody (const std::string &body) |
Sets the body & content length for future requests, this will also set the method to POST is the body is not empty and GET if it is. More... | |
void | setBody (Poco::Net::HTMLForm &form) |
Sets the body & content length for future requests, this will also set the method to POST is the body is not empty and GET if it is. More... | |
void | setContentLength (std::streamsize length) |
Sets the content length. More... | |
void | setContentType (const std::string &contentType) |
Sets the Content Type. More... | |
void | setMethod (const std::string &method) |
Sets the Method. More... | |
void | setProxy (const Kernel::ProxyInfo &proxy) |
sets the proxy details. More... | |
void | setTimeout (int seconds) |
Sets the timeout in seconds. More... | |
virtual | ~InternetHelper () |
Destructor. More... | |
Protected Member Functions | |
void | createRequest (Poco::URI &uri) |
bool | isRelocated (const HTTPStatus &response) |
Checks the HTTP status to decide if this is a relocation. More... | |
void | logDebugRequestSending (const std::string &schemeName, const std::string &url) const |
Helper to log (debug level) the request being sent (careful not to print blatant passwords, etc.). More... | |
virtual HTTPStatus | processErrorStates (const Poco::Net::HTTPResponse &res, std::istream &rs, const std::string &url) |
Process any HTTP errors states. More... | |
InternetHelper::HTTPStatus | processRelocation (const Poco::Net::HTTPResponse &response, std::ostream &responseStream) |
virtual void | processResponseHeaders (const Poco::Net::HTTPResponse &res) |
Process any headers from the response stream Basic implementation does nothing. More... | |
virtual HTTPStatus | sendHTTPRequest (const std::string &url, std::ostream &responseStream) |
Performs a request using http. More... | |
virtual HTTPStatus | sendHTTPSRequest (const std::string &url, std::ostream &responseStream) |
Performs a request using https. More... | |
virtual HTTPStatus | sendRequestAndProcess (Poco::Net::HTTPClientSession &session, Poco::URI &uri, std::ostream &responseStream) |
void | setupProxyOnSession (Poco::Net::HTTPClientSession &session, const std::string &proxyUrl) |
void | throwNotConnected (const std::string &url, const Poco::Net::HostNotFoundException &ex) |
Throw an exception occurs when the computer is not connected to the internet. More... | |
Protected Attributes | |
std::string | m_body |
std::streamsize | m_contentLength |
std::string | m_contentType |
StringToStringMap | m_headers |
bool | m_isProxySet |
bool | m_isTimeoutSet |
std::string | m_method |
Kernel::ProxyInfo | m_proxyInfo |
std::unique_ptr< Poco::Net::HTTPRequest > | m_request |
std::unique_ptr< Poco::Net::HTTPResponse > | m_response |
int | m_timeout |
InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS.
Definition at line 36 of file InternetHelper.h.
using Mantid::Kernel::InternetHelper::StringToStringMap = std::map<std::string, std::string> |
Definition at line 88 of file InternetHelper.h.
|
strong |
Definition at line 38 of file InternetHelper.h.
Mantid::Kernel::InternetHelper::InternetHelper | ( | ) |
Constructor.
Definition at line 91 of file InternetHelper.cpp.
Mantid::Kernel::InternetHelper::InternetHelper | ( | const Kernel::ProxyInfo & | proxy | ) |
Constructor.
Definition at line 99 of file InternetHelper.cpp.
|
virtualdefault |
Destructor.
void Mantid::Kernel::InternetHelper::addHeader | ( | const std::string & | key, |
const std::string & | value | ||
) |
Adds a header.
key | The key to refer to the value |
value | The value in seconds for the timeout |
Definition at line 550 of file InternetHelper.cpp.
References m_headers, and value.
Referenced by Mantid::Kernel::GitHubApiHelper::addAuthenticationToken(), and Mantid::DataHandling::CheckMantidVersion::getVersionsFromGitHub().
void Mantid::Kernel::InternetHelper::clearHeaders | ( | ) |
void Mantid::Kernel::InternetHelper::clearProxy | ( | ) |
Clears cached proxy details.
Definition at line 291 of file InternetHelper.cpp.
References m_isProxySet.
|
protected |
Definition at line 117 of file InternetHelper.cpp.
References m_contentLength, m_contentType, m_headers, m_method, m_request, m_response, and Mantid::Kernel::MantidVersion::version().
Referenced by sendRequestAndProcess(), Mantid::Kernel::GitHubApiHelper::sendRequestAndProcess(), and setBody().
|
virtual |
Download a url and fetch it inside the local path given.
urlFile | Define a valid URL for the file to be downloaded. Eventually, it may give any valid https path. For example: |
url_file = "http://www.google.com"
url_file = "https://mantidweb/repository/README.md"
The result is to connect to the http server, and request the path given.
The answer, will be inserted at the local_file_path.
localFilePath | : Provide the destination of the file downloaded at the url_file. |
Mantid::Kernel::Exception::InternetError | : For any unexpected behaviour. |
Definition at line 389 of file InternetHelper.cpp.
References Mantid::Kernel::Logger::debug(), Mantid::Kernel::DateAndTimeHelpers::g_log, and sendRequest().
Referenced by Mantid::DataHandling::DownloadInstrument::doDownloadFile(), and Mantid::DataHandling::DownloadFile::exec().
const std::string & Mantid::Kernel::InternetHelper::getBody | ( | ) |
Gets the body set for future requests.
Definition at line 532 of file InternetHelper.cpp.
References m_body.
std::streamsize Mantid::Kernel::InternetHelper::getContentLength | ( | ) |
Gets the content length.
Definition at line 484 of file InternetHelper.cpp.
References m_contentLength.
const std::string & Mantid::Kernel::InternetHelper::getContentType | ( | ) |
Gets the Content Type.
Definition at line 474 of file InternetHelper.cpp.
References m_contentType.
const std::string & Mantid::Kernel::InternetHelper::getHeader | ( | const std::string & | key | ) |
Gets the value of a header.
key | The key to refer to the value |
Definition at line 561 of file InternetHelper.cpp.
References m_headers.
const std::string & Mantid::Kernel::InternetHelper::getMethod | ( | ) |
Gets the method.
Definition at line 464 of file InternetHelper.cpp.
References m_method.
Kernel::ProxyInfo & Mantid::Kernel::InternetHelper::getProxy | ( | const std::string & | url | ) |
Gets proxy details for a system and a url.
url | : The url to be called |
Definition at line 281 of file InternetHelper.cpp.
References getProxy(), Mantid::Kernel::SingletonHolder< T >::Instance(), m_isProxySet, m_proxyInfo, and setProxy().
Referenced by getProxy(), and setupProxyOnSession().
const std::string & Mantid::Kernel::InternetHelper::getResponseReason | ( | ) |
Gets the body set for future requests.
Definition at line 544 of file InternetHelper.cpp.
References m_response.
InternetHelper::HTTPStatus Mantid::Kernel::InternetHelper::getResponseStatus | ( | ) |
Gets the body set for future requests.
Definition at line 537 of file InternetHelper.cpp.
References m_response.
int Mantid::Kernel::InternetHelper::getTimeout | ( | ) |
Gets the timeout in seconds.
Definition at line 441 of file InternetHelper.cpp.
References Mantid::Kernel::SingletonHolder< T >::Instance(), m_isTimeoutSet, and m_timeout.
Referenced by sendHTTPRequest(), and sendHTTPSRequest().
std::map< std::string, std::string > & Mantid::Kernel::InternetHelper::headers | ( | ) |
Returns a reference to the headers map.
Definition at line 569 of file InternetHelper.cpp.
References m_headers.
Referenced by Mantid::DataHandling::DownloadInstrument::doDownloadFile().
|
protected |
Checks the HTTP status to decide if this is a relocation.
response | the HTTP status |
Definition at line 422 of file InternetHelper.cpp.
References FOUND, MOVED_PERMANENTLY, SEE_OTHER, and TEMPORARY_REDIRECT.
Referenced by sendRequestAndProcess(), and Mantid::Kernel::GitHubApiHelper::sendRequestAndProcess().
|
protected |
Helper to log (debug level) the request being sent (careful not to print blatant passwords, etc.).
schemeName | Normally "http" or "https" |
url | url being sent (will be logged) |
Definition at line 203 of file InternetHelper.cpp.
References Mantid::Kernel::Logger::debug(), Mantid::Kernel::DateAndTimeHelpers::g_log, and m_method.
Referenced by sendHTTPRequest(), and sendHTTPSRequest().
|
protectedvirtual |
Process any HTTP errors states.
res | : The http response |
rs | : The iutput stream from the response |
url | : The url originally called |
Mantid::Kernel::Exception::InternetError | : Coded for the failure state. |
Definition at line 315 of file InternetHelper.cpp.
References Mantid::Kernel::Logger::debug(), FORBIDDEN, FOUND, Mantid::Kernel::DateAndTimeHelpers::g_log, MOVED_PERMANENTLY, NOT_FOUND, NOT_MODIFIED, and OK.
Referenced by sendRequestAndProcess(), and Mantid::Kernel::GitHubApiHelper::sendRequestAndProcess().
|
protected |
Definition at line 167 of file InternetHelper.cpp.
References Mantid::Kernel::DateAndTimeHelpers::g_log, Mantid::Kernel::Logger::information(), sendRequest(), and Mantid::Kernel::Logger::warning().
Referenced by sendRequestAndProcess(), and Mantid::Kernel::GitHubApiHelper::sendRequestAndProcess().
|
protectedvirtual |
Process any headers from the response stream Basic implementation does nothing.
Reimplemented in Mantid::Kernel::GitHubApiHelper.
Definition at line 304 of file InternetHelper.cpp.
Referenced by sendRequestAndProcess().
void Mantid::Kernel::InternetHelper::removeHeader | ( | const std::string & | key | ) |
Removes a header.
key | The key to refer to the value |
Definition at line 555 of file InternetHelper.cpp.
References m_headers.
Referenced by Mantid::Kernel::GitHubApiHelper::processAnonymousRequest().
|
virtual |
Resets properties to defaults (except the proxy)
Reimplemented in Mantid::Kernel::GitHubApiHelper.
Definition at line 573 of file InternetHelper.cpp.
References m_body, m_contentType, m_headers, m_isTimeoutSet, m_method, m_request, and m_timeout.
Referenced by Mantid::Kernel::GitHubApiHelper::reset().
|
protectedvirtual |
Performs a request using http.
url | the address to the network resource |
responseStream | The stream to fill with the reply on success |
Reimplemented in Mantid::FrameworkTestHelpers::MockONCatAPI.
Definition at line 219 of file InternetHelper.cpp.
References BAD_REQUEST, getTimeout(), logDebugRequestSending(), sendRequestAndProcess(), setupProxyOnSession(), and throwNotConnected().
Referenced by sendRequest().
|
protectedvirtual |
Performs a request using https.
url | the address to the network resource |
responseStream | The stream to fill with the reply on success |
Reimplemented in Mantid::FrameworkTestHelpers::MockONCatAPI.
Definition at line 246 of file InternetHelper.cpp.
References BAD_REQUEST, getTimeout(), Mantid::Kernel::SingletonHolder< T >::Instance(), logDebugRequestSending(), sendRequestAndProcess(), setupProxyOnSession(), and throwNotConnected().
Referenced by sendRequest().
|
virtual |
Performs a request using http or https depending on the url.
url | the address to the network resource |
responseStream | The stream to fill with the reply on success |
Definition at line 183 of file InternetHelper.cpp.
References sendHTTPRequest(), and sendHTTPSRequest().
Referenced by downloadFile(), Mantid::Kernel::GitHubApiHelper::getRateLimitDescription(), Mantid::DataHandling::CheckMantidVersion::getVersionsFromGitHub(), Mantid::Kernel::GitHubApiHelper::processAnonymousRequest(), processRelocation(), Mantid::Kernel::ErrorReporter::sendReport(), Mantid::Kernel::UsageServiceImpl::sendReport(), and Mantid::DataHandling::ISISDataArchive::sendRequest().
|
protectedvirtual |
Reimplemented in Mantid::Kernel::GitHubApiHelper.
Definition at line 142 of file InternetHelper.cpp.
References CREATED, createRequest(), Mantid::Kernel::Logger::debug(), Mantid::Kernel::DateAndTimeHelpers::g_log, isRelocated(), m_body, m_method, m_request, m_response, OK, processErrorStates(), processRelocation(), processResponseHeaders(), and Mantid::Kernel::Logger::warning().
Referenced by sendHTTPRequest(), and sendHTTPSRequest().
void Mantid::Kernel::InternetHelper::setBody | ( | const std::ostringstream & | body | ) |
Sets the body & content length for future requests, this will also set the method to POST is the body is not empty and GET if it is.
body | A stringstream of the body |
Definition at line 506 of file InternetHelper.cpp.
References setBody().
void Mantid::Kernel::InternetHelper::setBody | ( | const std::string & | body | ) |
Sets the body & content length for future requests, this will also set the method to POST is the body is not empty and GET if it is.
body | A string of the body |
Definition at line 491 of file InternetHelper.cpp.
References m_body, m_method, and setContentLength().
Referenced by Mantid::Kernel::ErrorReporter::sendReport(), Mantid::Kernel::UsageServiceImpl::sendReport(), and setBody().
void Mantid::Kernel::InternetHelper::setBody | ( | Poco::Net::HTMLForm & | form | ) |
Sets the body & content length for future requests, this will also set the method to POST is the body is not empty and GET if it is.
form | A HTMLform |
Definition at line 513 of file InternetHelper.cpp.
References createRequest(), m_body, m_request, setContentLength(), setContentType(), and setMethod().
void Mantid::Kernel::InternetHelper::setContentLength | ( | std::streamsize | length | ) |
Sets the content length.
length | The content length in bytes |
Definition at line 479 of file InternetHelper.cpp.
References m_contentLength.
Referenced by setBody().
void Mantid::Kernel::InternetHelper::setContentType | ( | const std::string & | contentType | ) |
Sets the Content Type.
contentType | A string of the content type |
Definition at line 469 of file InternetHelper.cpp.
References m_contentType.
Referenced by setBody().
void Mantid::Kernel::InternetHelper::setMethod | ( | const std::string & | method | ) |
Sets the Method.
method | A string of GET or POST, anything other than POST is considered GET |
Definition at line 453 of file InternetHelper.cpp.
References m_method.
Referenced by setBody().
void Mantid::Kernel::InternetHelper::setProxy | ( | const Kernel::ProxyInfo & | proxy | ) |
sets the proxy details.
proxy | the proxy information to use |
Definition at line 296 of file InternetHelper.cpp.
References m_isProxySet, and m_proxyInfo.
Referenced by getProxy().
void Mantid::Kernel::InternetHelper::setTimeout | ( | int | seconds | ) |
Sets the timeout in seconds.
seconds | The value in seconds for the timeout |
Definition at line 414 of file InternetHelper.cpp.
References m_isTimeoutSet, and m_timeout.
Referenced by Mantid::Kernel::ErrorReporter::sendReport(), and Mantid::Kernel::UsageServiceImpl::sendReport().
|
protected |
Definition at line 109 of file InternetHelper.cpp.
References getProxy().
Referenced by sendHTTPRequest(), and sendHTTPSRequest().
|
protected |
Throw an exception occurs when the computer is not connected to the internet.
url | The url that was use |
ex | The exception generated by Poco |
Definition at line 431 of file InternetHelper.cpp.
Referenced by sendHTTPRequest(), and sendHTTPSRequest().
|
protected |
Definition at line 150 of file InternetHelper.h.
Referenced by getBody(), reset(), sendRequestAndProcess(), Mantid::Kernel::GitHubApiHelper::sendRequestAndProcess(), and setBody().
|
protected |
Definition at line 147 of file InternetHelper.h.
Referenced by createRequest(), getContentLength(), and setContentLength().
|
protected |
Definition at line 149 of file InternetHelper.h.
Referenced by createRequest(), getContentType(), reset(), and setContentType().
|
protected |
Definition at line 151 of file InternetHelper.h.
Referenced by addHeader(), clearHeaders(), createRequest(), getHeader(), headers(), Mantid::Kernel::GitHubApiHelper::isAuthenticated(), removeHeader(), and reset().
|
protected |
Definition at line 144 of file InternetHelper.h.
Referenced by clearProxy(), getProxy(), and setProxy().
|
protected |
Definition at line 146 of file InternetHelper.h.
Referenced by getTimeout(), reset(), and setTimeout().
|
protected |
Definition at line 148 of file InternetHelper.h.
Referenced by createRequest(), getMethod(), logDebugRequestSending(), reset(), sendRequestAndProcess(), Mantid::Kernel::GitHubApiHelper::sendRequestAndProcess(), setBody(), and setMethod().
|
protected |
Definition at line 143 of file InternetHelper.h.
Referenced by getProxy(), and setProxy().
|
protected |
Definition at line 152 of file InternetHelper.h.
Referenced by createRequest(), reset(), sendRequestAndProcess(), Mantid::Kernel::GitHubApiHelper::sendRequestAndProcess(), and setBody().
|
protected |
Definition at line 153 of file InternetHelper.h.
Referenced by createRequest(), getResponseReason(), getResponseStatus(), sendRequestAndProcess(), and Mantid::Kernel::GitHubApiHelper::sendRequestAndProcess().
|
protected |
Definition at line 145 of file InternetHelper.h.
Referenced by getTimeout(), reset(), and setTimeout().