1 // Copyright 2014 The Crashpad Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef CRASHPAD_UTIL_NET_HTTP_TRANSPORT_H_ 16 #define CRASHPAD_UTIL_NET_HTTP_TRANSPORT_H_ 17 18 #include <memory> 19 #include <string> 20 21 #include "base/files/file_path.h" 22 #include "base/macros.h" 23 #include "util/net/http_headers.h" 24 25 namespace crashpad { 26 27 class HTTPBodyStream; 28 29 //! \brief HTTPTransport executes a HTTP request using the specified URL, HTTP 30 //! method, headers, and body. This class can only issue a synchronous 31 //! HTTP request. 32 //! 33 //! This class cannot be instantiated directly. A concrete subclass must be 34 //! instantiated instead, which provides an implementation to execute the 35 //! request that is appropriate for the host operating system. 36 class HTTPTransport { 37 public: 38 virtual ~HTTPTransport(); 39 40 //! \brief Instantiates a concrete HTTPTransport class for the current 41 //! operating system. 42 //! 43 //! \return A new caller-owned HTTPTransport object. 44 static std::unique_ptr<HTTPTransport> Create(); 45 46 //! \brief Sets URL to which the request will be made. 47 //! 48 //! \param[in] url The request URL. 49 void SetURL(const std::string& url); 50 51 //! \brief Sets the HTTP method to execute. E.g., GET, POST, etc. The default 52 //! method is `"POST"`. 53 //! 54 //! \param[in] http_method The HTTP method. 55 void SetMethod(const std::string& http_method); 56 57 //! \brief Sets a HTTP header-value pair. 58 //! 59 //! \param[in] header The HTTP header name. Any previous value set at this 60 //! name will be overwritten. 61 //! \param[in] value The value to set for the header. 62 void SetHeader(const std::string& header, const std::string& value); 63 64 //! \brief Sets the stream object from which to generate the HTTP body. 65 //! 66 //! \param[in] stream A HTTPBodyStream, of which this class will take 67 //! ownership. 68 void SetBodyStream(std::unique_ptr<HTTPBodyStream> stream); 69 70 //! \brief Sets the timeout for the HTTP request. The default is 15 seconds. 71 //! 72 //! \param[in] timeout The request timeout, in seconds. 73 void SetTimeout(double timeout); 74 75 //! \brief Sets a certificate file to be used in lieu of the system CA cert 76 //! bundle. 77 //! 78 //! This is exposed primarily for testing with a self-signed certificate, and 79 //! it isn't necessary to set it in normal use. 80 //! 81 //! \param[in] cert The filename of a file in PEM format containing the CA 82 //! cert to be used for TLS connections. 83 void SetRootCACertificatePath(const base::FilePath& cert); 84 85 //! \brief Performs the HTTP request with the configured parameters and waits 86 //! for the execution to complete. 87 //! 88 //! \param[out] response_body On success, this will be set to the HTTP 89 //! response body. This parameter is optional and may be set to `nullptr` 90 //! if the response body is not required. 91 //! 92 //! \return Whether or not the request was successful, defined as returning 93 //! a HTTP status code in the range 200-203 (inclusive). 94 virtual bool ExecuteSynchronously(std::string* response_body) = 0; 95 96 protected: 97 HTTPTransport(); 98 url()99 const std::string& url() const { return url_; } method()100 const std::string& method() const { return method_; } headers()101 const HTTPHeaders& headers() const { return headers_; } body_stream()102 HTTPBodyStream* body_stream() const { return body_stream_.get(); } timeout()103 double timeout() const { return timeout_; } root_ca_certificate_path()104 const base::FilePath& root_ca_certificate_path() const { 105 return root_ca_certificate_path_; 106 } 107 108 private: 109 std::string url_; 110 std::string method_; 111 base::FilePath root_ca_certificate_path_; 112 HTTPHeaders headers_; 113 std::unique_ptr<HTTPBodyStream> body_stream_; 114 double timeout_; 115 116 DISALLOW_COPY_AND_ASSIGN(HTTPTransport); 117 }; 118 119 } // namespace crashpad 120 121 #endif // CRASHPAD_UTIL_NET_HTTP_TRANSPORT_H_ 122