18b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// Copyright (c) 2006, Google Inc.
28b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// All rights reserved.
38b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner//
48b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// Redistribution and use in source and binary forms, with or without
58b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// modification, are permitted provided that the following conditions are
68b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// met:
78b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner//
88b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner//     * Redistributions of source code must retain the above copyright
98b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// notice, this list of conditions and the following disclaimer.
108b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner//     * Redistributions in binary form must reproduce the above
118b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// copyright notice, this list of conditions and the following disclaimer
128b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// in the documentation and/or other materials provided with the
138b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// distribution.
148b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner//     * Neither the name of Google Inc. nor the names of its
158b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// contributors may be used to endorse or promote products derived from
168b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// this software without specific prior written permission.
178b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner//
188b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
198b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
208b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
218b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
228b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
238b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
248b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
258b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
268b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
288b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
308b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner#include <assert.h>
318b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
3212a5245bfe93d4a0ccb692afefc4ecf1435d97f4mmentovai// Disable exception handler warnings.
33f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com#pragma warning(disable:4530)
3412a5245bfe93d4a0ccb692afefc4ecf1435d97f4mmentovai
358b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner#include <fstream>
368b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
3773cd14b4af906e77f3d8b019962fb9979ff12620mmentovai#include "common/windows/string_utils-inl.h"
3873cd14b4af906e77f3d8b019962fb9979ff12620mmentovai
398b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner#include "common/windows/http_upload.h"
408b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
41e5dc60822e5938fea2ae892ccddb906641ba174emmentovainamespace google_breakpad {
428b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
438b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerusing std::ifstream;
448b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerusing std::ios;
458b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
46e5dc60822e5938fea2ae892ccddb906641ba174emmentovaistatic const wchar_t kUserAgent[] = L"Breakpad/1.0 (Windows)";
478b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
488b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// Helper class which closes an internet handle when it goes away
498b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerclass HTTPUpload::AutoInternetHandle {
508b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner public:
518b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  explicit AutoInternetHandle(HINTERNET handle) : handle_(handle) {}
528b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  ~AutoInternetHandle() {
538b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    if (handle_) {
548b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner      InternetCloseHandle(handle_);
558b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    }
568b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
578b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
588b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  HINTERNET get() { return handle_; }
598b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
608b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner private:
618b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  HINTERNET handle_;
628b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner};
638b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
648b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static
658b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerbool HTTPUpload::SendRequest(const wstring &url,
668b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                             const map<wstring, wstring> &parameters,
67996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org                             const wstring &upload_file,
68996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org                             const wstring &file_part_name,
693366749ee7fd0e6c94906efa817d257917e3bf82nealsid                             int *timeout,
70d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist                             wstring *response_body,
71d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist                             int *response_code) {
72d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist  if (response_code) {
73d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist    *response_code = 0;
74d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist  }
757fde9a879dbf38aa6adcf7691139dc32cd567089erikwright@chromium.org
768b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  // TODO(bryner): support non-ASCII parameter names
778b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  if (!CheckParameters(parameters)) {
788b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    return false;
798b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
808b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
818b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  // Break up the URL and make sure we can handle it
828b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  wchar_t scheme[16], host[256], path[256];
838b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  URL_COMPONENTS components;
848b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  memset(&components, 0, sizeof(components));
858b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  components.dwStructSize = sizeof(components);
868b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  components.lpszScheme = scheme;
87bbd9b472916ce10b16f59d4e56c9ed7959f9d95emmentovai  components.dwSchemeLength = sizeof(scheme) / sizeof(scheme[0]);
888b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  components.lpszHostName = host;
89bbd9b472916ce10b16f59d4e56c9ed7959f9d95emmentovai  components.dwHostNameLength = sizeof(host) / sizeof(host[0]);
908b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  components.lpszUrlPath = path;
91bbd9b472916ce10b16f59d4e56c9ed7959f9d95emmentovai  components.dwUrlPathLength = sizeof(path) / sizeof(path[0]);
928b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  if (!InternetCrackUrl(url.c_str(), static_cast<DWORD>(url.size()),
937fde9a879dbf38aa6adcf7691139dc32cd567089erikwright@chromium.org                        0, &components)) {
948b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    return false;
958b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
965afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  bool secure = false;
975afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  if (wcscmp(scheme, L"https") == 0) {
985afd60b067a65920ba2e4705ccea71b880cc1607mmentovai    secure = true;
995afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  } else if (wcscmp(scheme, L"http") != 0) {
1008b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    return false;
1018b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
1028b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
1038b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  AutoInternetHandle internet(InternetOpen(kUserAgent,
1045afd60b067a65920ba2e4705ccea71b880cc1607mmentovai                                           INTERNET_OPEN_TYPE_PRECONFIG,
1058b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                           NULL,  // proxy name
1068b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                           NULL,  // proxy bypass
1078b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                           0));   // flags
1088b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  if (!internet.get()) {
1098b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    return false;
1108b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
1118b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
1128b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  AutoInternetHandle connection(InternetConnect(internet.get(),
1138b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                                host,
1148b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                                components.nPort,
1158b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                                NULL,    // user name
1168b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                                NULL,    // password
1178b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                                INTERNET_SERVICE_HTTP,
1188b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                                0,       // flags
1198b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                                NULL));  // context
1208b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  if (!connection.get()) {
1218b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    return false;
1228b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
1238b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
1245afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  DWORD http_open_flags = secure ? INTERNET_FLAG_SECURE : 0;
1253b7d8ee3621e1f70dc1097258743e35309823ed7ted.mielczarek  http_open_flags |= INTERNET_FLAG_NO_COOKIES;
1268b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  AutoInternetHandle request(HttpOpenRequest(connection.get(),
1278b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                             L"POST",
1288b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                             path,
1298b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                             NULL,    // version
1308b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                             NULL,    // referer
1318b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                             NULL,    // agent type
1325afd60b067a65920ba2e4705ccea71b880cc1607mmentovai                                             http_open_flags,
1338b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                             NULL));  // context
1348b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  if (!request.get()) {
1358b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    return false;
1368b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
1378b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
1388b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  wstring boundary = GenerateMultipartBoundary();
1398b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  wstring content_type_header = GenerateRequestHeader(boundary);
1408b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  HttpAddRequestHeaders(request.get(),
1418b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                        content_type_header.c_str(),
142f2fa084fd229b60cc3350bed66c4b7d95b10d61ammentovai                        static_cast<DWORD>(-1),
143f2fa084fd229b60cc3350bed66c4b7d95b10d61ammentovai                        HTTP_ADDREQ_FLAG_ADD);
1448b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
1458b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  string request_body;
146996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  if (!GenerateRequestBody(parameters, upload_file,
147996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org                           file_part_name, boundary, &request_body)) {
148684649a57172532274e2b8b1307af6fa11fdabceted.mielczarek    return false;
149684649a57172532274e2b8b1307af6fa11fdabceted.mielczarek  }
1508b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
1513366749ee7fd0e6c94906efa817d257917e3bf82nealsid  if (timeout) {
1523366749ee7fd0e6c94906efa817d257917e3bf82nealsid    if (!InternetSetOption(request.get(),
1533366749ee7fd0e6c94906efa817d257917e3bf82nealsid                           INTERNET_OPTION_SEND_TIMEOUT,
1543366749ee7fd0e6c94906efa817d257917e3bf82nealsid                           timeout,
15518360b876d4901d2df58b0a9c6eb9797533b4151ted.mielczarek                           sizeof(*timeout))) {
1567fde9a879dbf38aa6adcf7691139dc32cd567089erikwright@chromium.org      fwprintf(stderr, L"Could not unset send timeout, continuing...\n");
1573366749ee7fd0e6c94906efa817d257917e3bf82nealsid    }
1583366749ee7fd0e6c94906efa817d257917e3bf82nealsid
1593366749ee7fd0e6c94906efa817d257917e3bf82nealsid    if (!InternetSetOption(request.get(),
1603366749ee7fd0e6c94906efa817d257917e3bf82nealsid                           INTERNET_OPTION_RECEIVE_TIMEOUT,
1613366749ee7fd0e6c94906efa817d257917e3bf82nealsid                           timeout,
16218360b876d4901d2df58b0a9c6eb9797533b4151ted.mielczarek                           sizeof(*timeout))) {
1633366749ee7fd0e6c94906efa817d257917e3bf82nealsid      fwprintf(stderr, L"Could not unset receive timeout, continuing...\n");
1643366749ee7fd0e6c94906efa817d257917e3bf82nealsid    }
1653366749ee7fd0e6c94906efa817d257917e3bf82nealsid  }
166f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com
1675afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  if (!HttpSendRequest(request.get(), NULL, 0,
1685afd60b067a65920ba2e4705ccea71b880cc1607mmentovai                       const_cast<char *>(request_body.data()),
1695afd60b067a65920ba2e4705ccea71b880cc1607mmentovai                       static_cast<DWORD>(request_body.size()))) {
1705afd60b067a65920ba2e4705ccea71b880cc1607mmentovai    return false;
1715afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  }
1725afd60b067a65920ba2e4705ccea71b880cc1607mmentovai
1735afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  // The server indicates a successful upload with HTTP status 200.
1745afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  wchar_t http_status[4];
1755afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  DWORD http_status_size = sizeof(http_status);
1765afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  if (!HttpQueryInfo(request.get(), HTTP_QUERY_STATUS_CODE,
1775afd60b067a65920ba2e4705ccea71b880cc1607mmentovai                     static_cast<LPVOID>(&http_status), &http_status_size,
1785afd60b067a65920ba2e4705ccea71b880cc1607mmentovai                     0)) {
1795afd60b067a65920ba2e4705ccea71b880cc1607mmentovai    return false;
1805afd60b067a65920ba2e4705ccea71b880cc1607mmentovai  }
1815afd60b067a65920ba2e4705ccea71b880cc1607mmentovai
182d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist  int http_response = wcstol(http_status, NULL, 10);
183d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist  if (response_code) {
184d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist    *response_code = http_response;
185d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist  }
186d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist
187d4e527b7eea7628e305e6ca93cdcb9796060d65aincrementalist  bool result = (http_response == 200);
188dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist
189dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  if (result) {
190dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist    result = ReadResponse(request.get(), response_body);
191dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  }
192dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist
193dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  return result;
194dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist}
195dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist
196dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist// static
197dd7c38baae8c9a08d3568217d3cad6309add576dincrementalistbool HTTPUpload::ReadResponse(HINTERNET request, wstring *response) {
1981de8bed899cc08b3453753132d137b8a953a4e8dmmentovai  bool has_content_length_header = false;
199dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  wchar_t content_length[32];
200dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  DWORD content_length_size = sizeof(content_length);
20128c645f3ee1d33c20e6ad7314c1454e8254809f2bryner  DWORD claimed_size = 0;
2021de8bed899cc08b3453753132d137b8a953a4e8dmmentovai  string response_body;
2031de8bed899cc08b3453753132d137b8a953a4e8dmmentovai
2041de8bed899cc08b3453753132d137b8a953a4e8dmmentovai  if (HttpQueryInfo(request, HTTP_QUERY_CONTENT_LENGTH,
2051de8bed899cc08b3453753132d137b8a953a4e8dmmentovai                    static_cast<LPVOID>(&content_length),
2061de8bed899cc08b3453753132d137b8a953a4e8dmmentovai                    &content_length_size, 0)) {
2071de8bed899cc08b3453753132d137b8a953a4e8dmmentovai    has_content_length_header = true;
2081de8bed899cc08b3453753132d137b8a953a4e8dmmentovai    claimed_size = wcstol(content_length, NULL, 10);
2091de8bed899cc08b3453753132d137b8a953a4e8dmmentovai    response_body.reserve(claimed_size);
210dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  }
211dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist
2121de8bed899cc08b3453753132d137b8a953a4e8dmmentovai
2131de8bed899cc08b3453753132d137b8a953a4e8dmmentovai  DWORD bytes_available;
214dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  DWORD total_read = 0;
2152a4698319ae9043b4479bf04381ad67c17922761nealsid  BOOL return_code;
2162a4698319ae9043b4479bf04381ad67c17922761nealsid
2172a4698319ae9043b4479bf04381ad67c17922761nealsid  while (((return_code = InternetQueryDataAvailable(request, &bytes_available,
218f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com      0, 0)) != 0) && bytes_available > 0) {
2191de8bed899cc08b3453753132d137b8a953a4e8dmmentovai    vector<char> response_buffer(bytes_available);
2201de8bed899cc08b3453753132d137b8a953a4e8dmmentovai    DWORD size_read;
2211de8bed899cc08b3453753132d137b8a953a4e8dmmentovai
2222a4698319ae9043b4479bf04381ad67c17922761nealsid    return_code = InternetReadFile(request,
2232a4698319ae9043b4479bf04381ad67c17922761nealsid                                   &response_buffer[0],
2242a4698319ae9043b4479bf04381ad67c17922761nealsid                                   bytes_available, &size_read);
2252a4698319ae9043b4479bf04381ad67c17922761nealsid
2262a4698319ae9043b4479bf04381ad67c17922761nealsid    if (return_code && size_read > 0) {
2271de8bed899cc08b3453753132d137b8a953a4e8dmmentovai      total_read += size_read;
2281de8bed899cc08b3453753132d137b8a953a4e8dmmentovai      response_body.append(&response_buffer[0], size_read);
2291de8bed899cc08b3453753132d137b8a953a4e8dmmentovai    } else {
2301de8bed899cc08b3453753132d137b8a953a4e8dmmentovai      break;
2311de8bed899cc08b3453753132d137b8a953a4e8dmmentovai    }
2321de8bed899cc08b3453753132d137b8a953a4e8dmmentovai  }
2331de8bed899cc08b3453753132d137b8a953a4e8dmmentovai
2341de8bed899cc08b3453753132d137b8a953a4e8dmmentovai  bool succeeded = return_code && (!has_content_length_header ||
2351de8bed899cc08b3453753132d137b8a953a4e8dmmentovai                                   (total_read == claimed_size));
236dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  if (succeeded && response) {
2371de8bed899cc08b3453753132d137b8a953a4e8dmmentovai    *response = UTF8ToWide(response_body);
238dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  }
239dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist
240dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  return succeeded;
2418b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner}
2428b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
2438b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static
2448b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerwstring HTTPUpload::GenerateMultipartBoundary() {
2458b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  // The boundary has 27 '-' characters followed by 16 hex digits
2468b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  static const wchar_t kBoundaryPrefix[] = L"---------------------------";
2478b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  static const int kBoundaryLength = 27 + 16 + 1;
2488b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
2498b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  // Generate some random numbers to fill out the boundary
2508b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  int r0 = rand();
2518b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  int r1 = rand();
2528b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
2538b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  wchar_t temp[kBoundaryLength];
254aec44489da7da17b5babd9343850257558741fa2mmentovai  swprintf(temp, kBoundaryLength, L"%s%08X%08X", kBoundaryPrefix, r0, r1);
25530fc9ce1c0e671d7a4234ea70f7ed3906c627659mmentovai
25630fc9ce1c0e671d7a4234ea70f7ed3906c627659mmentovai  // remove when VC++7.1 is no longer supported
25730fc9ce1c0e671d7a4234ea70f7ed3906c627659mmentovai  temp[kBoundaryLength - 1] = L'\0';
25830fc9ce1c0e671d7a4234ea70f7ed3906c627659mmentovai
2598b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  return wstring(temp);
2608b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner}
2618b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
2628b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static
2638b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerwstring HTTPUpload::GenerateRequestHeader(const wstring &boundary) {
2648b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  wstring header = L"Content-Type: multipart/form-data; boundary=";
2658b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  header += boundary;
2668b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  return header;
2678b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner}
2688b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
2698b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static
2708b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerbool HTTPUpload::GenerateRequestBody(const map<wstring, wstring> &parameters,
271996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org                                     const wstring &upload_file,
272996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org                                     const wstring &file_part_name,
2738b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                     const wstring &boundary,
2748b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                     string *request_body) {
275996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  vector<char> contents;
276996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  if (!GetFileContents(upload_file, &contents)) {
277996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org    return false;
278996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  }
279996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org
2808b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  string boundary_str = WideToUTF8(boundary);
2818b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  if (boundary_str.empty()) {
2828b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    return false;
2838b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
2848b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
2858b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  request_body->clear();
2868b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
2878b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  // Append each of the parameter pairs as a form-data part
2888b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  for (map<wstring, wstring>::const_iterator pos = parameters.begin();
2898b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner       pos != parameters.end(); ++pos) {
2908b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    request_body->append("--" + boundary_str + "\r\n");
2918b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    request_body->append("Content-Disposition: form-data; name=\"" +
2928b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                         WideToUTF8(pos->first) + "\"\r\n\r\n" +
2938b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                         WideToUTF8(pos->second) + "\r\n");
2948b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
2958b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
296996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  // Now append the upload file as a binary (octet-stream) part
297996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  string filename_utf8 = WideToUTF8(upload_file);
298996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  if (filename_utf8.empty()) {
299996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org    return false;
300996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  }
3014cf2c88144ca7634880be3de59420b107f774edbted.mielczarek@gmail.com
302996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  string file_part_name_utf8 = WideToUTF8(file_part_name);
303996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  if (file_part_name_utf8.empty()) {
304996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org    return false;
305996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  }
3068b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
307996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  request_body->append("--" + boundary_str + "\r\n");
308996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  request_body->append("Content-Disposition: form-data; "
309996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org                       "name=\"" + file_part_name_utf8 + "\"; "
310996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org                       "filename=\"" + filename_utf8 + "\"\r\n");
311996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  request_body->append("Content-Type: application/octet-stream\r\n");
312996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  request_body->append("\r\n");
3138b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
314996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  if (!contents.empty()) {
315c297c50f83344a35712c69563f75a70a3e3182e1bryner      request_body->append(&(contents[0]), contents.size());
316c297c50f83344a35712c69563f75a70a3e3182e1bryner  }
317996f24efb31578c6d617f2f1565078350ac16521mark@chromium.org  request_body->append("\r\n");
3188b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  request_body->append("--" + boundary_str + "--\r\n");
3198b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  return true;
3208b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner}
3218b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
3228b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static
3237405ecd04629d759e6525c134ea375aa3aad83c0ted.mielczarek@gmail.combool HTTPUpload::GetFileContents(const wstring &filename,
3248b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                 vector<char> *contents) {
325f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com  bool rv = false;
32673cd14b4af906e77f3d8b019962fb9979ff12620mmentovai  // The "open" method on pre-MSVC8 ifstream implementations doesn't accept a
32773cd14b4af906e77f3d8b019962fb9979ff12620mmentovai  // wchar_t* filename, so use _wfopen directly in that case.  For VC8 and
32873cd14b4af906e77f3d8b019962fb9979ff12620mmentovai  // later, _wfopen has been deprecated in favor of _wfopen_s, which does
32973cd14b4af906e77f3d8b019962fb9979ff12620mmentovai  // not exist in earlier versions, so let the ifstream open the file itself.
330de9fe36b16a45972571b9d650633fe5cb156b536ted.mielczarek@gmail.com  // GCC doesn't support wide file name and opening on FILE* requires ugly
331de9fe36b16a45972571b9d650633fe5cb156b536ted.mielczarek@gmail.com  // hacks, so fallback to multi byte file.
332de9fe36b16a45972571b9d650633fe5cb156b536ted.mielczarek@gmail.com#ifdef _MSC_VER
3338b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  ifstream file;
3348b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  file.open(filename.c_str(), ios::binary);
335de9fe36b16a45972571b9d650633fe5cb156b536ted.mielczarek@gmail.com#else // GCC
336de9fe36b16a45972571b9d650633fe5cb156b536ted.mielczarek@gmail.com  ifstream file(WideToMBCP(filename, CP_ACP).c_str(), ios::binary);
33773cd14b4af906e77f3d8b019962fb9979ff12620mmentovai#endif  // _MSC_VER >= 1400
3388b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  if (file.is_open()) {
3398b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    file.seekg(0, ios::end);
3402a4698319ae9043b4479bf04381ad67c17922761nealsid    std::streamoff length = file.tellg();
341f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com    // Check for loss of data when converting lenght from std::streamoff into
342f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com    // std::vector<char>::size_type
343f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com    std::vector<char>::size_type vector_size =
344f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com        static_cast<std::vector<char>::size_type>(length);
345f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com    if (static_cast<std::streamoff>(vector_size) == length) {
346f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com      contents->resize(vector_size);
347f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com      if (length != 0) {
348f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com        file.seekg(0, ios::beg);
349f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com        file.read(&((*contents)[0]), length);
350f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com      }
351f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com      rv = true;
352c297c50f83344a35712c69563f75a70a3e3182e1bryner    }
3538b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    file.close();
3548b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
355f7838a8665eb5e46f2eb136679b27707d6adb523ivan.penkov@gmail.com  return rv;
3568b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner}
3578b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
3588b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static
359dd7c38baae8c9a08d3568217d3cad6309add576dincrementalistwstring HTTPUpload::UTF8ToWide(const string &utf8) {
360dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  if (utf8.length() == 0) {
361dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist    return wstring();
362dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  }
363dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist
364dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  // compute the length of the buffer we'll need
365dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  int charcount = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
366dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist
367dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  if (charcount == 0) {
368dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist    return wstring();
369dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  }
370dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist
371dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  // convert
372dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  wchar_t* buf = new wchar_t[charcount];
373dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, buf, charcount);
374dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  wstring result(buf);
375dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  delete[] buf;
376dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist  return result;
377dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist}
378dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist
379dd7c38baae8c9a08d3568217d3cad6309add576dincrementalist// static
380de9fe36b16a45972571b9d650633fe5cb156b536ted.mielczarek@gmail.comstring HTTPUpload::WideToMBCP(const wstring &wide, unsigned int cp) {
3818b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  if (wide.length() == 0) {
3828b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    return string();
3838b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
3848b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
3858b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  // compute the length of the buffer we'll need
386de9fe36b16a45972571b9d650633fe5cb156b536ted.mielczarek@gmail.com  int charcount = WideCharToMultiByte(cp, 0, wide.c_str(), -1,
3878b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                                      NULL, 0, NULL, NULL);
3888b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  if (charcount == 0) {
3898b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    return string();
3908b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
3918b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
3928b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  // convert
3938b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  char *buf = new char[charcount];
394de9fe36b16a45972571b9d650633fe5cb156b536ted.mielczarek@gmail.com  WideCharToMultiByte(cp, 0, wide.c_str(), -1, buf, charcount,
3958b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner                      NULL, NULL);
3968b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
3978b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  string result(buf);
3988b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  delete[] buf;
3998b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  return result;
4008b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner}
4018b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
4028b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static
4038b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerbool HTTPUpload::CheckParameters(const map<wstring, wstring> &parameters) {
4048b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  for (map<wstring, wstring>::const_iterator pos = parameters.begin();
4058b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner       pos != parameters.end(); ++pos) {
4068b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    const wstring &str = pos->first;
4078b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    if (str.size() == 0) {
4088b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner      return false;  // disallow empty parameter names
4098b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    }
4108b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    for (unsigned int i = 0; i < str.size(); ++i) {
4118b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner      wchar_t c = str[i];
4128b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner      if (c < 32 || c == '"' || c > 127) {
4138b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner        return false;
4148b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner      }
4158b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner    }
4168b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  }
4178b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner  return true;
4188b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner}
4198b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner
420e5dc60822e5938fea2ae892ccddb906641ba174emmentovai}  // namespace google_breakpad
421