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 // a set of string parameters (key/value pairs), 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::string;
49 using std::wstring;
50 using std::map;
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   // Parameter names must contain only printable ASCII characters,
85   // and may not contain a quote (") character.
86   // Only HTTP(S) URLs are currently supported.  Returns true on success.
87   // If the request is successful and response_body is non-NULL,
88   // the response body will be returned in response_body.
89   // If response_code is non-NULL, it will be set to the HTTP response code
90   // received (or 0 if the request failed before getting an HTTP response).
91   static bool SendMultipartPostRequest(
92       const wstring& url,
93       const map<wstring, wstring>& parameters,
94       const map<wstring, wstring>& files,
95       int *timeout_ms,
96       wstring *response_body,
97       int *response_code);
98 
99   // Sends a POST request, with the body set to |body|, to the given URL.
100   // Only HTTP(S) URLs are currently supported.  Returns true on success.
101   // If the request is successful and response_body is non-NULL,
102   // the response body will be returned in response_body.
103   // If response_code is non-NULL, it will be set to the HTTP response code
104   // received (or 0 if the request failed before getting an HTTP response).
105   static bool SendSimplePostRequest(
106       const wstring& url,
107       const wstring& body,
108       const wstring& content_type,
109       int *timeout_ms,
110       wstring *response_body,
111       int *response_code);
112 
113  private:
114   // No instances of this class should be created.
115   // Disallow all constructors, destructors, and operator=.
116   HTTPUpload();
117   explicit HTTPUpload(const HTTPUpload&);
118   void operator=(const HTTPUpload&);
119   ~HTTPUpload();
120 };
121 
122 }  // namespace google_breakpad
123 
124 #pragma warning(pop)
125 
126 #endif  // COMMON_WINDOWS_HTTP_UPLOAD_H_
127