Mantid
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Mantid::Kernel::InternetHelper Class Reference

InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS. More...

#include <InternetHelper.h>

Inheritance diagram for Mantid::Kernel::InternetHelper:
Mantid::FrameworkTestHelpers::MockONCatAPI Mantid::Kernel::GitHubApiHelper

Public Types

enum class  HTTPStatus {
  CONTINUE = 100 , SWITCHING_PROTOCOLS = 101 , OK = 200 , CREATED = 201 ,
  ACCEPTED = 202 , NONAUTHORITATIVE = 203 , NO_CONTENT = 204 , RESET_CONTENT = 205 ,
  PARTIAL_CONTENT = 206 , MULTIPLE_CHOICES = 300 , MOVED_PERMANENTLY = 301 , FOUND = 302 ,
  SEE_OTHER = 303 , NOT_MODIFIED = 304 , USEPROXY = 305 , TEMPORARY_REDIRECT = 307 ,
  BAD_REQUEST = 400 , UNAUTHORIZED = 401 , PAYMENT_REQUIRED = 402 , FORBIDDEN = 403 ,
  NOT_FOUND = 404 , METHOD_NOT_ALLOWED = 405 , NOT_ACCEPTABLE = 406 , PROXY_AUTHENTICATION_REQUIRED = 407 ,
  REQUEST_TIMEOUT = 408 , CONFLICT = 409 , GONE = 410 , LENGTH_REQUIRED = 411 ,
  PRECONDITION_FAILED = 412 , REQUESTENTITYTOOLARGE = 413 , REQUESTURITOOLONG = 414 , UNSUPPORTEDMEDIATYPE = 415 ,
  REQUESTED_RANGE_NOT_SATISFIABLE = 416 , EXPECTATION_FAILED = 417 , I_AM_A_TEAPOT = 418 , INTERNAL_SERVER_ERROR = 500 ,
  NOT_IMPLEMENTED = 501 , BAD_GATEWAY = 502 , SERVICE_UNAVAILABLE = 503 , GATEWAY_TIMEOUT = 504 ,
  VERSION_NOT_SUPPORTED = 505
}
 
using StringToStringMap = std::map< std::string, std::string >
 

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::ProxyInfogetProxy (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...
 
StringToStringMapheaders ()
 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
 

Detailed Description

InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS.

Definition at line 36 of file InternetHelper.h.

Member Typedef Documentation

◆ StringToStringMap

using Mantid::Kernel::InternetHelper::StringToStringMap = std::map<std::string, std::string>

Definition at line 88 of file InternetHelper.h.

Member Enumeration Documentation

◆ HTTPStatus

Enumerator
CONTINUE 
SWITCHING_PROTOCOLS 
OK 
CREATED 
ACCEPTED 
NONAUTHORITATIVE 
NO_CONTENT 
RESET_CONTENT 
PARTIAL_CONTENT 
MULTIPLE_CHOICES 
MOVED_PERMANENTLY 
FOUND 
SEE_OTHER 
NOT_MODIFIED 
USEPROXY 
TEMPORARY_REDIRECT 
BAD_REQUEST 
UNAUTHORIZED 
PAYMENT_REQUIRED 
FORBIDDEN 
NOT_FOUND 
METHOD_NOT_ALLOWED 
NOT_ACCEPTABLE 
PROXY_AUTHENTICATION_REQUIRED 
REQUEST_TIMEOUT 
CONFLICT 
GONE 
LENGTH_REQUIRED 
PRECONDITION_FAILED 
REQUESTENTITYTOOLARGE 
REQUESTURITOOLONG 
UNSUPPORTEDMEDIATYPE 
REQUESTED_RANGE_NOT_SATISFIABLE 
EXPECTATION_FAILED 
I_AM_A_TEAPOT 
INTERNAL_SERVER_ERROR 
NOT_IMPLEMENTED 
BAD_GATEWAY 
SERVICE_UNAVAILABLE 
GATEWAY_TIMEOUT 
VERSION_NOT_SUPPORTED 

Definition at line 38 of file InternetHelper.h.

Constructor & Destructor Documentation

◆ InternetHelper() [1/2]

Mantid::Kernel::InternetHelper::InternetHelper ( )

Constructor.

Definition at line 91 of file InternetHelper.cpp.

◆ InternetHelper() [2/2]

Mantid::Kernel::InternetHelper::InternetHelper ( const Kernel::ProxyInfo proxy)

Constructor.

Definition at line 99 of file InternetHelper.cpp.

◆ ~InternetHelper()

Mantid::Kernel::InternetHelper::~InternetHelper ( )
virtualdefault

Destructor.

Member Function Documentation

◆ addHeader()

void Mantid::Kernel::InternetHelper::addHeader ( const std::string &  key,
const std::string &  value 
)

Adds a header.

Parameters
keyThe key to refer to the value
valueThe 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().

◆ clearHeaders()

void Mantid::Kernel::InternetHelper::clearHeaders ( )

Clears all headers.

Definition at line 565 of file InternetHelper.cpp.

References m_headers.

◆ clearProxy()

void Mantid::Kernel::InternetHelper::clearProxy ( )

Clears cached proxy details.

Definition at line 291 of file InternetHelper.cpp.

References m_isProxySet.

◆ createRequest()

void Mantid::Kernel::InternetHelper::createRequest ( Poco::URI &  uri)
protected

◆ downloadFile()

InternetHelper::HTTPStatus Mantid::Kernel::InternetHelper::downloadFile ( const std::string &  urlFile,
const std::string &  localFilePath = "" 
)
virtual

Download a url and fetch it inside the local path given.

Parameters
urlFileDefine 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.

Parameters
localFilePath: Provide the destination of the file downloaded at the url_file.
Exceptions
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().

◆ getBody()

const std::string & Mantid::Kernel::InternetHelper::getBody ( )

Gets the body set for future requests.

Returns
A string of the content type

Definition at line 532 of file InternetHelper.cpp.

References m_body.

◆ getContentLength()

std::streamsize Mantid::Kernel::InternetHelper::getContentLength ( )

Gets the content length.

Returns
The content length in bytes

Definition at line 484 of file InternetHelper.cpp.

References m_contentLength.

◆ getContentType()

const std::string & Mantid::Kernel::InternetHelper::getContentType ( )

Gets the Content Type.

Returns
A string of the content type

Definition at line 474 of file InternetHelper.cpp.

References m_contentType.

◆ getHeader()

const std::string & Mantid::Kernel::InternetHelper::getHeader ( const std::string &  key)

Gets the value of a header.

Parameters
keyThe key to refer to the value
Returns
the value as a string

Definition at line 561 of file InternetHelper.cpp.

References m_headers.

◆ getMethod()

const std::string & Mantid::Kernel::InternetHelper::getMethod ( )

Gets the method.

Returns
either "GET" or "POST"

Definition at line 464 of file InternetHelper.cpp.

References m_method.

◆ getProxy()

Kernel::ProxyInfo & Mantid::Kernel::InternetHelper::getProxy ( const std::string &  url)

Gets proxy details for a system and a url.

Parameters
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().

◆ getResponseReason()

const std::string & Mantid::Kernel::InternetHelper::getResponseReason ( )

Gets the body set for future requests.

Returns
A string of the content type

Definition at line 544 of file InternetHelper.cpp.

References m_response.

◆ getResponseStatus()

InternetHelper::HTTPStatus Mantid::Kernel::InternetHelper::getResponseStatus ( )

Gets the body set for future requests.

Returns
A string of the content type

Definition at line 537 of file InternetHelper.cpp.

References m_response.

◆ getTimeout()

int Mantid::Kernel::InternetHelper::getTimeout ( )

Gets the timeout in seconds.

Returns
The value in seconds for the timeout

Definition at line 441 of file InternetHelper.cpp.

References Mantid::Kernel::SingletonHolder< T >::Instance(), m_isTimeoutSet, and m_timeout.

Referenced by sendHTTPRequest(), and sendHTTPSRequest().

◆ headers()

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().

◆ isRelocated()

bool Mantid::Kernel::InternetHelper::isRelocated ( const HTTPStatus response)
protected

Checks the HTTP status to decide if this is a relocation.

Parameters
responsethe HTTP status
Returns
true if the return code is considered a relocation

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().

◆ logDebugRequestSending()

void Mantid::Kernel::InternetHelper::logDebugRequestSending ( const std::string &  schemeName,
const std::string &  url 
) const
protected

Helper to log (debug level) the request being sent (careful not to print blatant passwords, etc.).

Parameters
schemeNameNormally "http" or "https"
urlurl 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().

◆ processErrorStates()

InternetHelper::HTTPStatus Mantid::Kernel::InternetHelper::processErrorStates ( const Poco::Net::HTTPResponse &  res,
std::istream &  rs,
const std::string &  url 
)
protectedvirtual

Process any HTTP errors states.

Parameters
res: The http response
rs: The iutput stream from the response
url: The url originally called
Exceptions
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().

◆ processRelocation()

InternetHelper::HTTPStatus Mantid::Kernel::InternetHelper::processRelocation ( const Poco::Net::HTTPResponse &  response,
std::ostream &  responseStream 
)
protected

◆ processResponseHeaders()

void Mantid::Kernel::InternetHelper::processResponseHeaders ( const Poco::Net::HTTPResponse &  res)
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().

◆ removeHeader()

void Mantid::Kernel::InternetHelper::removeHeader ( const std::string &  key)

Removes a header.

Parameters
keyThe key to refer to the value

Definition at line 555 of file InternetHelper.cpp.

References m_headers.

Referenced by Mantid::Kernel::GitHubApiHelper::processAnonymousRequest().

◆ reset()

void Mantid::Kernel::InternetHelper::reset ( )
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().

◆ sendHTTPRequest()

InternetHelper::HTTPStatus Mantid::Kernel::InternetHelper::sendHTTPRequest ( const std::string &  url,
std::ostream &  responseStream 
)
protectedvirtual

Performs a request using http.

Parameters
urlthe address to the network resource
responseStreamThe 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().

◆ sendHTTPSRequest()

InternetHelper::HTTPStatus Mantid::Kernel::InternetHelper::sendHTTPSRequest ( const std::string &  url,
std::ostream &  responseStream 
)
protectedvirtual

Performs a request using https.

Parameters
urlthe address to the network resource
responseStreamThe 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().

◆ sendRequest()

InternetHelper::HTTPStatus Mantid::Kernel::InternetHelper::sendRequest ( const std::string &  url,
std::ostream &  responseStream 
)
virtual

◆ sendRequestAndProcess()

InternetHelper::HTTPStatus Mantid::Kernel::InternetHelper::sendRequestAndProcess ( Poco::Net::HTTPClientSession &  session,
Poco::URI &  uri,
std::ostream &  responseStream 
)
protectedvirtual

◆ setBody() [1/3]

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.

Parameters
bodyA stringstream of the body

Definition at line 506 of file InternetHelper.cpp.

References setBody().

◆ setBody() [2/3]

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.

Parameters
bodyA 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().

◆ setBody() [3/3]

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.

Parameters
formA HTMLform

Definition at line 513 of file InternetHelper.cpp.

References createRequest(), m_body, m_request, setContentLength(), setContentType(), and setMethod().

◆ setContentLength()

void Mantid::Kernel::InternetHelper::setContentLength ( std::streamsize  length)

Sets the content length.

Parameters
lengthThe content length in bytes

Definition at line 479 of file InternetHelper.cpp.

References m_contentLength.

Referenced by setBody().

◆ setContentType()

void Mantid::Kernel::InternetHelper::setContentType ( const std::string &  contentType)

Sets the Content Type.

Parameters
contentTypeA string of the content type

Definition at line 469 of file InternetHelper.cpp.

References m_contentType.

Referenced by setBody().

◆ setMethod()

void Mantid::Kernel::InternetHelper::setMethod ( const std::string &  method)

Sets the Method.

Parameters
methodA 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().

◆ setProxy()

void Mantid::Kernel::InternetHelper::setProxy ( const Kernel::ProxyInfo proxy)

sets the proxy details.

Parameters
proxythe proxy information to use

Definition at line 296 of file InternetHelper.cpp.

References m_isProxySet, and m_proxyInfo.

Referenced by getProxy().

◆ setTimeout()

void Mantid::Kernel::InternetHelper::setTimeout ( int  seconds)

Sets the timeout in seconds.

Parameters
secondsThe 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().

◆ setupProxyOnSession()

void Mantid::Kernel::InternetHelper::setupProxyOnSession ( Poco::Net::HTTPClientSession &  session,
const std::string &  proxyUrl 
)
protected

Definition at line 109 of file InternetHelper.cpp.

References getProxy().

Referenced by sendHTTPRequest(), and sendHTTPSRequest().

◆ throwNotConnected()

void Mantid::Kernel::InternetHelper::throwNotConnected ( const std::string &  url,
const Poco::Net::HostNotFoundException &  ex 
)
protected

Throw an exception occurs when the computer is not connected to the internet.

Parameters
urlThe url that was use
exThe exception generated by Poco

Definition at line 431 of file InternetHelper.cpp.

Referenced by sendHTTPRequest(), and sendHTTPSRequest().

Member Data Documentation

◆ m_body

std::string Mantid::Kernel::InternetHelper::m_body
protected

◆ m_contentLength

std::streamsize Mantid::Kernel::InternetHelper::m_contentLength
protected

Definition at line 147 of file InternetHelper.h.

Referenced by createRequest(), getContentLength(), and setContentLength().

◆ m_contentType

std::string Mantid::Kernel::InternetHelper::m_contentType
protected

Definition at line 149 of file InternetHelper.h.

Referenced by createRequest(), getContentType(), reset(), and setContentType().

◆ m_headers

StringToStringMap Mantid::Kernel::InternetHelper::m_headers
protected

◆ m_isProxySet

bool Mantid::Kernel::InternetHelper::m_isProxySet
protected

Definition at line 144 of file InternetHelper.h.

Referenced by clearProxy(), getProxy(), and setProxy().

◆ m_isTimeoutSet

bool Mantid::Kernel::InternetHelper::m_isTimeoutSet
protected

Definition at line 146 of file InternetHelper.h.

Referenced by getTimeout(), reset(), and setTimeout().

◆ m_method

std::string Mantid::Kernel::InternetHelper::m_method
protected

◆ m_proxyInfo

Kernel::ProxyInfo Mantid::Kernel::InternetHelper::m_proxyInfo
protected

Definition at line 143 of file InternetHelper.h.

Referenced by getProxy(), and setProxy().

◆ m_request

std::unique_ptr<Poco::Net::HTTPRequest> Mantid::Kernel::InternetHelper::m_request
protected

◆ m_response

std::unique_ptr<Poco::Net::HTTPResponse> Mantid::Kernel::InternetHelper::m_response
protected

◆ m_timeout

int Mantid::Kernel::InternetHelper::m_timeout
protected

Definition at line 145 of file InternetHelper.h.

Referenced by getTimeout(), reset(), and setTimeout().


The documentation for this class was generated from the following files: