|
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 #include <string> |
|
46 #include <vector> |
|
47 |
|
48 namespace google_breakpad { |
|
49 |
|
50 using std::string; |
|
51 using std::wstring; |
|
52 using std::map; |
|
53 using std::vector; |
|
54 |
|
55 class HTTPUpload { |
|
56 public: |
|
57 // Sends the given set of parameters, along with the contents of |
|
58 // upload_file, as a multipart POST request to the given URL. |
|
59 // file_part_name contains the name of the file part of the request |
|
60 // (i.e. it corresponds to the name= attribute on an <input type="file">. |
|
61 // Parameter names must contain only printable ASCII characters, |
|
62 // and may not contain a quote (") character. |
|
63 // Only HTTP(S) URLs are currently supported. Returns true on success. |
|
64 // If the request is successful and response_body is non-NULL, |
|
65 // the response body will be returned in response_body. |
|
66 // If response_code is non-NULL, it will be set to the HTTP response code |
|
67 // received (or 0 if the request failed before getting an HTTP response). |
|
68 static bool SendRequest(const wstring &url, |
|
69 const map<wstring, wstring> ¶meters, |
|
70 const wstring &upload_file, |
|
71 const wstring &file_part_name, |
|
72 int *timeout, |
|
73 wstring *response_body, |
|
74 int *response_code); |
|
75 |
|
76 private: |
|
77 class AutoInternetHandle; |
|
78 |
|
79 // Retrieves the HTTP response. If NULL is passed in for response, |
|
80 // this merely checks (via the return value) that we were successfully |
|
81 // able to retrieve exactly as many bytes of content in the response as |
|
82 // were specified in the Content-Length header. |
|
83 static bool HTTPUpload::ReadResponse(HINTERNET request, wstring* response); |
|
84 |
|
85 // Generates a new multipart boundary for a POST request |
|
86 static wstring GenerateMultipartBoundary(); |
|
87 |
|
88 // Generates a HTTP request header for a multipart form submit. |
|
89 static wstring GenerateRequestHeader(const wstring &boundary); |
|
90 |
|
91 // Given a set of parameters, an upload filename, and a file part name, |
|
92 // generates a multipart request body string with these parameters |
|
93 // and minidump contents. Returns true on success. |
|
94 static bool GenerateRequestBody(const map<wstring, wstring> ¶meters, |
|
95 const wstring &upload_file, |
|
96 const wstring &file_part_name, |
|
97 const wstring &boundary, |
|
98 string *request_body); |
|
99 |
|
100 // Fills the supplied vector with the contents of filename. |
|
101 static bool GetFileContents(const wstring &filename, vector<char> *contents); |
|
102 |
|
103 // Converts a UTF8 string to UTF16. |
|
104 static wstring UTF8ToWide(const string &utf8); |
|
105 |
|
106 // Converts a UTF16 string to UTF8. |
|
107 static string WideToUTF8(const wstring &wide); |
|
108 |
|
109 // Checks that the given list of parameters has only printable |
|
110 // ASCII characters in the parameter name, and does not contain |
|
111 // any quote (") characters. Returns true if so. |
|
112 static bool CheckParameters(const map<wstring, wstring> ¶meters); |
|
113 |
|
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__ |