1 // Copyright 2006 Google LLC 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google LLC nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 // HTTPUpload provides a "nice" API to send a multipart HTTP(S) POST 30 // request using wininet. It currently supports requests that contain 31 // a set of string parameters (key/value pairs), and a file to upload. 32 33 #ifndef COMMON_WINDOWS_HTTP_UPLOAD_H_ 34 #define COMMON_WINDOWS_HTTP_UPLOAD_H_ 35 36 #pragma warning(push) 37 // Disable exception handler warnings. 38 #pragma warning(disable : 4530) 39 40 #include <windows.h> 41 #include <wininet.h> 42 43 #include <map> 44 45 namespace google_breakpad { 46 47 using std::string; 48 using std::wstring; 49 using std::map; 50 51 class HTTPUpload { 52 public: 53 // Sends a PUT request containing the data in |path| to the given 54 // URL. 55 // Only HTTP(S) URLs are currently supported. Returns true on success. 56 // If the request is successful and response_body is non-NULL, 57 // the response body will be returned in response_body. 58 // If response_code is non-NULL, it will be set to the HTTP response code 59 // received (or 0 if the request failed before getting an HTTP response). 60 static bool SendPutRequest( 61 const wstring& url, 62 const wstring& path, 63 int* timeout_ms, 64 wstring* response_body, 65 int* response_code); 66 67 // Sends a GET request to the given URL. 68 // Only HTTP(S) URLs are currently supported. Returns true on success. 69 // If the request is successful and response_body is non-NULL, 70 // the response body will be returned in response_body. 71 // If response_code is non-NULL, it will be set to the HTTP response code 72 // received (or 0 if the request failed before getting an HTTP response). 73 static bool SendGetRequest( 74 const wstring& url, 75 int* timeout_ms, 76 wstring* response_body, 77 int* response_code); 78 79 // Sends the given sets of parameters and files as a multipart POST 80 // request to the given URL. 81 // Each key in |files| is the name of the file part of the request 82 // (i.e. it corresponds to the name= attribute on an <input type="file">. 83 // Parameter names must contain only printable ASCII characters, 84 // and may not contain a quote (") character. 85 // Only HTTP(S) URLs are currently supported. Returns true on success. 86 // If the request is successful and response_body is non-NULL, 87 // the response body will be returned in response_body. 88 // If response_code is non-NULL, it will be set to the HTTP response code 89 // received (or 0 if the request failed before getting an HTTP response). 90 static bool SendMultipartPostRequest( 91 const wstring& url, 92 const map<wstring, wstring>& parameters, 93 const map<wstring, wstring>& files, 94 int *timeout_ms, 95 wstring *response_body, 96 int *response_code); 97 98 // Sends a POST request, with the body set to |body|, to the given URL. 99 // Only HTTP(S) URLs are currently supported. Returns true on success. 100 // If the request is successful and response_body is non-NULL, 101 // the response body will be returned in response_body. 102 // If response_code is non-NULL, it will be set to the HTTP response code 103 // received (or 0 if the request failed before getting an HTTP response). 104 static bool SendSimplePostRequest( 105 const wstring& url, 106 const wstring& body, 107 const wstring& content_type, 108 int *timeout_ms, 109 wstring *response_body, 110 int *response_code); 111 112 private: 113 // No instances of this class should be created. 114 // Disallow all constructors, destructors, and operator=. 115 HTTPUpload(); 116 explicit HTTPUpload(const HTTPUpload&); 117 void operator=(const HTTPUpload&); 118 ~HTTPUpload(); 119 }; 120 121 } // namespace google_breakpad 122 123 #pragma warning(pop) 124 125 #endif // COMMON_WINDOWS_HTTP_UPLOAD_H_ 126