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