Planeshift

http_upload.h

Go to the documentation of this file.
00001 // Copyright (c) 2006, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // HTTPUpload provides a "nice" API to send a multipart HTTP(S) POST
00031 // request using wininet.  It currently supports requests that contain
00032 // a set of string parameters (key/value pairs), and a file to upload.
00033 
00034 #ifndef COMMON_WINDOWS_HTTP_UPLOAD_H__
00035 #define COMMON_WINDOWS_HTTP_UPLOAD_H__
00036 
00037 #pragma warning( push )
00038 // Disable exception handler warnings.
00039 #pragma warning( disable : 4530 ) 
00040 
00041 #include <Windows.h>
00042 #include <WinInet.h>
00043 
00044 #include <map>
00045 #include <string>
00046 #include <vector>
00047 
00048 namespace google_breakpad {
00049 
00050 using std::string;
00051 using std::wstring;
00052 using std::map;
00053 using std::vector;
00054 
00055 class HTTPUpload {
00056  public:
00057   // Sends the given set of parameters, along with the contents of
00058   // upload_file, as a multipart POST request to the given URL.
00059   // file_part_name contains the name of the file part of the request
00060   // (i.e. it corresponds to the name= attribute on an <input type="file">.
00061   // Parameter names must contain only printable ASCII characters,
00062   // and may not contain a quote (") character.
00063   // Only HTTP(S) URLs are currently supported.  Returns true on success.
00064   // If the request is successful and response_body is non-NULL,
00065   // the response body will be returned in response_body.
00066   // If response_code is non-NULL, it will be set to the HTTP response code
00067   // received (or 0 if the request failed before getting an HTTP response).
00068   static bool SendRequest(const wstring &url,
00069                           const map<wstring, wstring> &parameters,
00070                           const wstring &upload_file,
00071                           const wstring &file_part_name,
00072                           int *timeout,
00073                           wstring *response_body,
00074                           int *response_code);
00075 
00076  private:
00077   class AutoInternetHandle;
00078 
00079   // Retrieves the HTTP response.  If NULL is passed in for response,
00080   // this merely checks (via the return value) that we were successfully
00081   // able to retrieve exactly as many bytes of content in the response as
00082   // were specified in the Content-Length header.
00083   static bool HTTPUpload::ReadResponse(HINTERNET request, wstring* response);
00084 
00085   // Generates a new multipart boundary for a POST request
00086   static wstring GenerateMultipartBoundary();
00087 
00088   // Generates a HTTP request header for a multipart form submit.
00089   static wstring GenerateRequestHeader(const wstring &boundary);
00090 
00091   // Given a set of parameters, an upload filename, and a file part name,
00092   // generates a multipart request body string with these parameters
00093   // and minidump contents.  Returns true on success.
00094   static bool GenerateRequestBody(const map<wstring, wstring> &parameters,
00095                                   const wstring &upload_file,
00096                                   const wstring &file_part_name,
00097                                   const wstring &boundary,
00098                                   string *request_body);
00099 
00100   // Fills the supplied vector with the contents of filename.
00101   static bool GetFileContents(const wstring &filename, vector<char> *contents);
00102 
00103   // Converts a UTF8 string to UTF16.
00104   static wstring UTF8ToWide(const string &utf8);
00105 
00106   // Converts a UTF16 string to UTF8.
00107   static string WideToUTF8(const wstring &wide);
00108 
00109   // Checks that the given list of parameters has only printable
00110   // ASCII characters in the parameter name, and does not contain
00111   // any quote (") characters.  Returns true if so.
00112   static bool CheckParameters(const map<wstring, wstring> &parameters);
00113 
00114   // No instances of this class should be created.
00115   // Disallow all constructors, destructors, and operator=.
00116   HTTPUpload();
00117   explicit HTTPUpload(const HTTPUpload &);
00118   void operator=(const HTTPUpload &);
00119   ~HTTPUpload();
00120 };
00121 
00122 }  // namespace google_breakpad
00123 
00124 #pragma warning( pop )
00125 
00126 #endif  // COMMON_WINDOWS_HTTP_UPLOAD_H__