Mantid
Loading...
Searching...
No Matches
InternetHelper.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2014 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include "MantidKernel/DllConfig.h"
11
12#include <ios>
13#include <map>
14#include <memory>
15#include <string>
16
17namespace Poco {
18// forward declaration
19class URI;
20
21namespace Net {
22// forward declarations
23class HTTPClientSession;
24class HTTPResponse;
25class HTTPRequest;
26class HostNotFoundException;
27class HTMLForm;
28} // namespace Net
29} // namespace Poco
30
31namespace Mantid {
32namespace Kernel {
36class MANTID_KERNEL_DLL InternetHelper {
37public:
38 enum class HTTPStatus {
39 CONTINUE = 100,
40 SWITCHING_PROTOCOLS = 101,
41 OK = 200,
42 CREATED = 201,
43 ACCEPTED = 202,
44 NONAUTHORITATIVE = 203,
45 NO_CONTENT = 204,
46 RESET_CONTENT = 205,
47 PARTIAL_CONTENT = 206,
48 MULTIPLE_CHOICES = 300,
49 MOVED_PERMANENTLY = 301,
50 FOUND = 302,
51 SEE_OTHER = 303,
52 NOT_MODIFIED = 304,
53 USEPROXY = 305,
54 // UNUSED: 306
55 TEMPORARY_REDIRECT = 307,
56 BAD_REQUEST = 400,
57 UNAUTHORIZED = 401,
58 PAYMENT_REQUIRED = 402,
59 FORBIDDEN = 403,
60 NOT_FOUND = 404,
61 METHOD_NOT_ALLOWED = 405,
62 NOT_ACCEPTABLE = 406,
63 PROXY_AUTHENTICATION_REQUIRED = 407,
64 REQUEST_TIMEOUT = 408,
65 CONFLICT = 409,
66 GONE = 410,
67 LENGTH_REQUIRED = 411,
68 PRECONDITION_FAILED = 412,
69 REQUESTENTITYTOOLARGE = 413,
70 REQUESTURITOOLONG = 414,
71 UNSUPPORTEDMEDIATYPE = 415,
72 REQUESTED_RANGE_NOT_SATISFIABLE = 416,
73 EXPECTATION_FAILED = 417,
74 I_AM_A_TEAPOT = 418,
75 INTERNAL_SERVER_ERROR = 500,
76 NOT_IMPLEMENTED = 501,
77 BAD_GATEWAY = 502,
78 SERVICE_UNAVAILABLE = 503,
79 GATEWAY_TIMEOUT = 504,
80 VERSION_NOT_SUPPORTED = 505
81 };
82
85 virtual ~InternetHelper();
86
87 // Convenience typedef
88 using StringToStringMap = std::map<std::string, std::string>;
89
90 // getters and setters
91 void setTimeout(int seconds);
92 int getTimeout();
93
94 void setMethod(const std::string &method);
95 const std::string &getMethod();
96
97 void setContentType(const std::string &contentType);
98 const std::string &getContentType();
99
100 void setContentLength(std::streamsize length);
101 std::streamsize getContentLength();
102
103 void setBody(const std::string &body);
104 void setBody(const std::ostringstream &body);
105 void setBody(Poco::Net::HTMLForm &form);
106 const std::string &getBody();
107
108 HTTPStatus getResponseStatus();
109 const std::string &getResponseReason();
110
111 void addHeader(const std::string &key, const std::string &value);
112 void removeHeader(const std::string &key);
113 const std::string &getHeader(const std::string &key);
114 void clearHeaders();
115 StringToStringMap &headers();
116 virtual void reset();
117
118 // Proxy methods
119 Kernel::ProxyInfo &getProxy(const std::string &url);
120 void clearProxy();
121 void setProxy(const Kernel::ProxyInfo &proxy);
122
123 // Execute call methods
124 virtual HTTPStatus downloadFile(const std::string &urlFile, const std::string &localFilePath = "");
125 virtual HTTPStatus sendRequest(const std::string &url, std::ostream &responseStream);
126
127protected:
128 virtual HTTPStatus sendHTTPSRequest(const std::string &url, std::ostream &responseStream);
129 virtual HTTPStatus sendHTTPRequest(const std::string &url, std::ostream &responseStream);
130 virtual void processResponseHeaders(const Poco::Net::HTTPResponse &res);
131 virtual HTTPStatus processErrorStates(const Poco::Net::HTTPResponse &res, std::istream &rs, const std::string &url);
132 virtual HTTPStatus sendRequestAndProcess(Poco::Net::HTTPClientSession &session, Poco::URI &uri,
133 std::ostream &responseStream);
134
135 void setupProxyOnSession(Poco::Net::HTTPClientSession &session, const std::string &proxyUrl);
136 void createRequest(Poco::URI &uri);
137 InternetHelper::HTTPStatus processRelocation(const Poco::Net::HTTPResponse &response, std::ostream &responseStream);
138 bool isRelocated(const HTTPStatus &response);
139 void throwNotConnected(const std::string &url, const Poco::Net::HostNotFoundException &ex);
140
141 void logDebugRequestSending(const std::string &schemeName, const std::string &url) const;
142
147 std::streamsize m_contentLength;
148 std::string m_method;
149 std::string m_contentType;
150 std::string m_body;
152 std::unique_ptr<Poco::Net::HTTPRequest> m_request;
153 std::unique_ptr<Poco::Net::HTTPResponse> m_response;
154};
155
156} // namespace Kernel
157} // namespace Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS.
std::unique_ptr< Poco::Net::HTTPRequest > m_request
std::map< std::string, std::string > StringToStringMap
virtual ~InternetHelper()
Destructor.
std::unique_ptr< Poco::Net::HTTPResponse > m_response
ProxyInfo : Container for carrying around network proxy information.
Definition: ProxyInfo.h:17
Helper class which provides the Collimation Length for SANS instruments.
Definition: Algorithm.h:30