1 // Copyright (c) 2006, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 // HTTPUpload provides a "nice" API to send a multipart HTTP(S) POST 31 // request using wininet. It currently supports requests that contain 32 // parameters encoded in a JSON string, and a file to upload. 33 34 #ifndef COMMON_WINDOWS_HTTP_UPLOAD_H_ 35 #define COMMON_WINDOWS_HTTP_UPLOAD_H_ 36 37 #pragma warning(push) 38 // Disable exception handler warnings. 39 #pragma warning(disable : 4530) 40 41 #include <windows.h> 42 #include <wininet.h> 43 44 #include <map> 45 46 namespace google_breakpad { 47 48 using std::map; 49 using std::string; 50 using std::wstring; 51 52 class HTTPUpload { 53 public: 54 // Sends a PUT request containing the data in |path| to the given 55 // URL. 56 // Only HTTP(S) URLs are currently supported. Returns true on success. 57 // If the request is successful and response_body is non-NULL, 58 // the response body will be returned in response_body. 59 // If response_code is non-NULL, it will be set to the HTTP response code 60 // received (or 0 if the request failed before getting an HTTP response). 61 static bool SendPutRequest( 62 const wstring& url, 63 const wstring& path, 64 int* timeout_ms, 65 wstring* response_body, 66 int* response_code); 67 68 // Sends a GET request to the given URL. 69 // Only HTTP(S) URLs are currently supported. Returns true on success. 70 // If the request is successful and response_body is non-NULL, 71 // the response body will be returned in response_body. 72 // If response_code is non-NULL, it will be set to the HTTP response code 73 // received (or 0 if the request failed before getting an HTTP response). 74 static bool SendGetRequest( 75 const wstring& url, 76 int* timeout_ms, 77 wstring* response_body, 78 int* response_code); 79 80 // Sends the given sets of parameters and files as a multipart POST 81 // request to the given URL. 82 // Each key in |files| is the name of the file part of the request 83 // (i.e. it corresponds to the name= attribute on an <input type="file">. 84 // Parameters are specified as a JSON-encoded string in |parameters|. 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 string& 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