• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 set of parameters, along with the contents of
49   // upload_file, as a multipart POST request to the given URL.
50   // file_part_name contains 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 string &upload_file,
64                           const string &file_part_name,
65                           const string &proxy,
66                           const string &proxy_user_pwd,
67                           const string &ca_certificate_file,
68                           string *response_body,
69                           long *response_code,
70                           string *error_description);
71 
72  private:
73   // Checks that the given list of parameters has only printable
74   // ASCII characters in the parameter name, and does not contain
75   // any quote (") characters.  Returns true if so.
76   static bool CheckParameters(const map<string, string> &parameters);
77 
78   // No instances of this class should be created.
79   // Disallow all constructors, destructors, and operator=.
80   HTTPUpload();
81   explicit HTTPUpload(const HTTPUpload &);
82   void operator=(const HTTPUpload &);
83   ~HTTPUpload();
84 };
85 
86 }  // namespace google_breakpad
87 
88 #endif  // COMMON_LINUX_HTTP_UPLOAD_H__
89