/thirdparty/breakpad/common/windows/http_upload.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 126 lines · 45 code · 21 blank · 60 comment · 0 complexity · 5d2165e884f0b35384c839a93951e946 MD5 · raw file

  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. // HTTPUpload provides a "nice" API to send a multipart HTTP(S) POST
  30. // request using wininet. It currently supports requests that contain
  31. // a set of string parameters (key/value pairs), and a file to upload.
  32. #ifndef COMMON_WINDOWS_HTTP_UPLOAD_H__
  33. #define COMMON_WINDOWS_HTTP_UPLOAD_H__
  34. #pragma warning( push )
  35. // Disable exception handler warnings.
  36. #pragma warning( disable : 4530 )
  37. #include <Windows.h>
  38. #include <WinInet.h>
  39. #include <map>
  40. #include <string>
  41. #include <vector>
  42. namespace google_breakpad {
  43. using std::string;
  44. using std::wstring;
  45. using std::map;
  46. using std::vector;
  47. class HTTPUpload {
  48. public:
  49. // Sends the given set of parameters, along with the contents of
  50. // upload_file, as a multipart POST request to the given URL.
  51. // file_part_name contains the name of the file part of the request
  52. // (i.e. it corresponds to the name= attribute on an <input type="file">.
  53. // Parameter names must contain only printable ASCII characters,
  54. // and may not contain a quote (") character.
  55. // Only HTTP(S) URLs are currently supported. Returns true on success.
  56. // If the request is successful and response_body is non-NULL,
  57. // the response body will be returned in response_body.
  58. // If response_code is non-NULL, it will be set to the HTTP response code
  59. // received (or 0 if the request failed before getting an HTTP response).
  60. static bool SendRequest(const wstring &url,
  61. const map<wstring, wstring> &parameters,
  62. const wstring &upload_file,
  63. const wstring &file_part_name,
  64. int *timeout,
  65. wstring *response_body,
  66. int *response_code);
  67. private:
  68. class AutoInternetHandle;
  69. // Retrieves the HTTP response. If NULL is passed in for response,
  70. // this merely checks (via the return value) that we were successfully
  71. // able to retrieve exactly as many bytes of content in the response as
  72. // were specified in the Content-Length header.
  73. static bool HTTPUpload::ReadResponse(HINTERNET request, wstring* response);
  74. // Generates a new multipart boundary for a POST request
  75. static wstring GenerateMultipartBoundary();
  76. // Generates a HTTP request header for a multipart form submit.
  77. static wstring GenerateRequestHeader(const wstring &boundary);
  78. // Given a set of parameters, an upload filename, and a file part name,
  79. // generates a multipart request body string with these parameters
  80. // and minidump contents. Returns true on success.
  81. static bool GenerateRequestBody(const map<wstring, wstring> &parameters,
  82. const wstring &upload_file,
  83. const wstring &file_part_name,
  84. const wstring &boundary,
  85. string *request_body);
  86. // Fills the supplied vector with the contents of filename.
  87. static bool GetFileContents(const wstring &filename, vector<char> *contents);
  88. // Converts a UTF8 string to UTF16.
  89. static wstring UTF8ToWide(const string &utf8);
  90. // Converts a UTF16 string to UTF8.
  91. static string WideToUTF8(const wstring &wide);
  92. // Checks that the given list of parameters has only printable
  93. // ASCII characters in the parameter name, and does not contain
  94. // any quote (") characters. Returns true if so.
  95. static bool CheckParameters(const map<wstring, wstring> &parameters);
  96. // No instances of this class should be created.
  97. // Disallow all constructors, destructors, and operator=.
  98. HTTPUpload();
  99. explicit HTTPUpload(const HTTPUpload &);
  100. void operator=(const HTTPUpload &);
  101. ~HTTPUpload();
  102. };
  103. } // namespace google_breakpad
  104. #pragma warning( pop )
  105. #endif // COMMON_WINDOWS_HTTP_UPLOAD_H__