Skip to content

Commit

Permalink
re #11067 moved http status codes to internethelper class
Browse files Browse the repository at this point in the history
  • Loading branch information
NickDraper committed Feb 16, 2015
1 parent 4db79ac commit 779c1b1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 36 deletions.
Expand Up @@ -9,7 +9,6 @@
#include <Poco/DirectoryIterator.h>
#include <Poco/Path.h>
#include <Poco/File.h>
#include <Poco/Net/HTTPResponse.h>
// Visual Studio complains with the inclusion of Poco/FileStream
// disabling this warning.
#if defined(_WIN32) || defined(_WIN64)
Expand Down Expand Up @@ -153,7 +152,7 @@ DownloadInstrument::StringToStringMap DownloadInstrument::processRepository() {
try {
doDownloadFile(gitHubInstrumentRepoUrl, gitHubJson.toString(), headers);
} catch (Exception::InternetError &ex) {
if (ex.errorCode() == HTTPResponse::HTTP_NOT_MODIFIED) {
if (ex.errorCode() == InternetHelper::HTTP_NOT_MODIFIED) {
// No changes since last time
return fileMap;
} else {
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/DataHandling/src/SNSDataArchive.cpp
Expand Up @@ -15,7 +15,6 @@
#include "Poco/SAX/InputSource.h"
#include <Poco/DOM/NodeList.h>
#include <Poco/DOM/NodeIterator.h>
#include <Poco/NET/HTTPResponse.h>
#include <boost/algorithm/string/predicate.hpp>
#include "Poco/DOM/AutoPtr.h"

Expand Down Expand Up @@ -74,7 +73,7 @@ SNSDataArchive::getArchivePath(const std::set<std::string> &filenames,

// Everything went fine, return the XML document.
// Otherwise look for an error message in the XML document.
if (status == Poco::Net::HTTPResponse::HTTP_OK) {
if (status == Kernel::InternetHelper::HTTP_OK) {
std::string location;
Poco::AutoPtr<Poco::XML::NodeList> pList =
pDoc->getElementsByTagName("location");
Expand Down
53 changes: 50 additions & 3 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/InternetHelper.h
Expand Up @@ -12,12 +12,11 @@ namespace Poco {
class URI;

namespace Net {
// forward declaration
// forward declarations
class HTTPClientSession;
// forward declaration
class HTTPResponse;
// forward declaration
class HTTPRequest;
class HostNotFoundException;
}
}

Expand Down Expand Up @@ -52,6 +51,51 @@ class Logger;
*/
class MANTID_KERNEL_DLL InternetHelper {
public:
enum HTTPStatus
{
HTTP_CONTINUE = 100,
HTTP_SWITCHING_PROTOCOLS = 101,
HTTP_OK = 200,
HTTP_CREATED = 201,
HTTP_ACCEPTED = 202,
HTTP_NONAUTHORITATIVE = 203,
HTTP_NO_CONTENT = 204,
HTTP_RESET_CONTENT = 205,
HTTP_PARTIAL_CONTENT = 206,
HTTP_MULTIPLE_CHOICES = 300,
HTTP_MOVED_PERMANENTLY = 301,
HTTP_FOUND = 302,
HTTP_SEE_OTHER = 303,
HTTP_NOT_MODIFIED = 304,
HTTP_USEPROXY = 305,
// UNUSED: 306
HTTP_TEMPORARY_REDIRECT = 307,
HTTP_BAD_REQUEST = 400,
HTTP_UNAUTHORIZED = 401,
HTTP_PAYMENT_REQUIRED = 402,
HTTP_FORBIDDEN = 403,
HTTP_NOT_FOUND = 404,
HTTP_METHOD_NOT_ALLOWED = 405,
HTTP_NOT_ACCEPTABLE = 406,
HTTP_PROXY_AUTHENTICATION_REQUIRED = 407,
HTTP_REQUEST_TIMEOUT = 408,
HTTP_CONFLICT = 409,
HTTP_GONE = 410,
HTTP_LENGTH_REQUIRED = 411,
HTTP_PRECONDITION_FAILED = 412,
HTTP_REQUESTENTITYTOOLARGE = 413,
HTTP_REQUESTURITOOLONG = 414,
HTTP_UNSUPPORTEDMEDIATYPE = 415,
HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
HTTP_EXPECTATION_FAILED = 417,
HTTP_INTERNAL_SERVER_ERROR = 500,
HTTP_NOT_IMPLEMENTED = 501,
HTTP_BAD_GATEWAY = 502,
HTTP_SERVICE_UNAVAILABLE = 503,
HTTP_GATEWAY_TIMEOUT = 504,
HTTP_VERSION_NOT_SUPPORTED = 505
};

InternetHelper();
InternetHelper(const Kernel::ProxyInfo &proxy);
virtual ~InternetHelper();
Expand Down Expand Up @@ -91,6 +135,9 @@ class MANTID_KERNEL_DLL InternetHelper {
Poco::URI &uri, std::ostream &responseStream);
int processRelocation(const Poco::Net::HTTPResponse &response,
std::ostream &responseStream);
bool isRelocated(const int response);
void throwNotConnected(const std::string &url,
const Poco::Net::HostNotFoundException &ex);
Kernel::ProxyInfo m_proxyInfo;
bool m_isProxySet;
int m_timeout;
Expand Down
62 changes: 33 additions & 29 deletions Code/Mantid/Framework/Kernel/src/InternetHelper.cpp
Expand Up @@ -43,29 +43,8 @@ using std::string;

namespace {
// anonymous namespace for some utility functions

/// static Logger object
Logger g_log("InternetHelper");

/// Throw an exception occurs when the computer
/// is not connected to the internet
void throwNotConnected(const std::string &url,
const HostNotFoundException &ex) {
std::stringstream info;
info << "Failed to download " << url
<< " because there is no connection to the host " << ex.message()
<< ".\nHint: Check your connection following this link: <a href=\""
<< url << "\">" << url << "</a> ";
throw Exception::InternetError(info.str() + ex.displayText());
}

/// @returns true if the return code is considered a relocation
bool isRelocated(const int response) {
return ((response == HTTPResponse::HTTP_FOUND) ||
(response == HTTPResponse::HTTP_MOVED_PERMANENTLY) ||
(response == HTTPResponse::HTTP_TEMPORARY_REDIRECT) ||
(response == HTTPResponse::HTTP_SEE_OTHER));
}
}

//----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -129,8 +108,8 @@ int InternetHelper::sendRequestAndProcess(HTTPClientSession &session,
g_log.debug() << "Answer from web: " << retStatus << " " << res.getReason()
<< std::endl;

if (retStatus == HTTPResponse::HTTP_OK ||
(retStatus == HTTPResponse::HTTP_CREATED &&
if (retStatus == HTTP_OK ||
(retStatus == HTTP_CREATED &&
m_method == HTTPRequest::HTTP_POST)) {
Poco::StreamCopier::copyStream(rs, responseStream);
return retStatus;
Expand Down Expand Up @@ -315,24 +294,24 @@ int InternetHelper::processErrorStates(const Poco::Net::HTTPResponse &res,
rateLimitRemaining = -1;
}

if (retStatus == HTTPResponse::HTTP_OK) {
if (retStatus == HTTP_OK) {
throw Exception::InternetError("Response was ok, processing should never "
"have entered processErrorStates",
retStatus);
} else if (retStatus == HTTPResponse::HTTP_FOUND) {
} else if (retStatus == HTTP_FOUND) {
throw Exception::InternetError("Response was HTTP_FOUND, processing should "
"never have entered processErrorStates",
retStatus);
} else if (retStatus == HTTPResponse::HTTP_MOVED_PERMANENTLY) {
} else if (retStatus == HTTP_MOVED_PERMANENTLY) {
throw Exception::InternetError("Response was HTTP_MOVED_PERMANENTLY, "
"processing should never have entered "
"processErrorStates",
retStatus);
} else if (retStatus == HTTPResponse::HTTP_NOT_MODIFIED) {
} else if (retStatus == HTTP_NOT_MODIFIED) {
throw Exception::InternetError("Not modified since provided date" +
rateLimitReset.toSimpleString(),
retStatus);
} else if ((retStatus == HTTPResponse::HTTP_FORBIDDEN) &&
} else if ((retStatus == HTTP_FORBIDDEN) &&
(rateLimitRemaining == 0)) {
throw Exception::InternetError(
"The Github API rate limit has been reached, try again after " +
Expand All @@ -342,7 +321,7 @@ int InternetHelper::processErrorStates(const Poco::Net::HTTPResponse &res,
std::stringstream info;
std::stringstream ss;
Poco::StreamCopier::copyStream(rs, ss);
if (retStatus == HTTPResponse::HTTP_NOT_FOUND)
if (retStatus == HTTP_NOT_FOUND)
info << "Failed to download " << url << " with the link "
<< "<a href=\"" << url << "\">.\n"
<< "Hint. Check that link is correct</a>";
Expand Down Expand Up @@ -403,5 +382,30 @@ int InternetHelper::downloadFile(const std::string &urlFile,
**/
void InternetHelper::setTimeout(int seconds) { m_timeout = seconds; }

/// Checks the HTTP status to decide if this is a relocation
/// @response the HTTP status
/// @returns true if the return code is considered a relocation
bool InternetHelper::isRelocated(const int response) {
return ((response == HTTP_FOUND) ||
(response == HTTP_MOVED_PERMANENTLY) ||
(response == HTTP_TEMPORARY_REDIRECT) ||
(response == HTTP_SEE_OTHER));
}

/// Throw an exception occurs when the computer
/// is not connected to the internet
/// @param url The url that was use
/// @param ex The exception generated by Poco
void InternetHelper::throwNotConnected(const std::string &url,
const HostNotFoundException &ex) {
std::stringstream info;
info << "Failed to access " << url
<< " because there is no connection to the host " << ex.message()
<< ".\nHint: Check your connection following this link: <a href=\""
<< url << "\">" << url << "</a> ";
throw Exception::InternetError(info.str() + ex.displayText());
}


} // namespace Kernel
} // namespace Mantid

0 comments on commit 779c1b1

Please sign in to comment.