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