/ src / common / linux / http_upload.h
http_upload.h
 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 libcurl.  It currently supports requests that contain
31  // a set of string parameters (key/value pairs), and a file to upload.
32  
33  #ifndef COMMON_LINUX_HTTP_UPLOAD_H__
34  #define COMMON_LINUX_HTTP_UPLOAD_H__
35  
36  #include <map>
37  #include <string>
38  
39  #include "common/using_std_string.h"
40  
41  namespace google_breakpad {
42  
43  using std::map;
44  
45  class HTTPUpload {
46   public:
47    // Sends the given sets of parameters and files as a multipart POST
48    // request to the given URL.
49    // Each key in |files| is the name of the file part of the request
50    // (i.e. it corresponds to the name= attribute on an <input type="file">.
51    // Parameter names must contain only printable ASCII characters,
52    // and may not contain a quote (") character.
53    // Only HTTP(S) URLs are currently supported.  Returns true on success.
54    // If the request is successful and response_body is non-NULL,
55    // the response body will be returned in response_body.
56    // If response_code is non-NULL, it will be set to the HTTP response code
57    // received (or 0 if the request failed before getting an HTTP response).
58    // If the send fails, a description of the error will be
59    // returned in error_description.
60    static bool SendRequest(const string& url,
61                            const map<string, string>& parameters,
62                            const map<string, string>& files,
63                            const string& proxy,
64                            const string& proxy_user_pwd,
65                            const string& ca_certificate_file,
66                            string* response_body,
67                            long* response_code,
68                            string* error_description);
69  
70   private:
71    // Checks that the given list of parameters has only printable
72    // ASCII characters in the parameter name, and does not contain
73    // any quote (") characters.  Returns true if so.
74    static bool CheckParameters(const map<string, string>& parameters);
75  
76    // Checks the curl_lib parameter points to a valid curl lib.
77    static bool CheckCurlLib(void* curl_lib);
78  
79    // No instances of this class should be created.
80    // Disallow all constructors, destructors, and operator=.
81    HTTPUpload();
82    explicit HTTPUpload(const HTTPUpload&);
83    void operator=(const HTTPUpload&);
84    ~HTTPUpload();
85  };
86  
87  }  // namespace google_breakpad
88  
89  #endif  // COMMON_LINUX_HTTP_UPLOAD_H__