http_upload.cc revision 5afd60b067a65920ba2e4705ccea71b880cc1607
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#include <windows.h> 328b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner#include <wininet.h> 338b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 348b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner#include <fstream> 358b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 368b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner#include "common/windows/http_upload.h" 378b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 388b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynernamespace google_airbag { 398b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 408b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerusing std::ifstream; 418b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerusing std::ios; 428b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 438b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerstatic const wchar_t kUserAgent[] = L"Airbag/1.0 (Windows)"; 448b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 458b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// Helper class which closes an internet handle when it goes away 468b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerclass HTTPUpload::AutoInternetHandle { 478b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner public: 488b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner explicit AutoInternetHandle(HINTERNET handle) : handle_(handle) {} 498b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner ~AutoInternetHandle() { 508b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (handle_) { 518b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner InternetCloseHandle(handle_); 528b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 538b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 548b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 558b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner HINTERNET get() { return handle_; } 568b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 578b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner private: 588b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner HINTERNET handle_; 598b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner}; 608b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 618b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static 628b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerbool HTTPUpload::SendRequest(const wstring &url, 638b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner const map<wstring, wstring> ¶meters, 648b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner const wstring &upload_file, 658b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner const wstring &file_part_name) { 668b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner // TODO(bryner): support non-ASCII parameter names 678b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (!CheckParameters(parameters)) { 688b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 698b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 708b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 718b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner // Break up the URL and make sure we can handle it 728b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner wchar_t scheme[16], host[256], path[256]; 738b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner URL_COMPONENTS components; 748b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner memset(&components, 0, sizeof(components)); 758b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner components.dwStructSize = sizeof(components); 768b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner components.lpszScheme = scheme; 778b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner components.dwSchemeLength = sizeof(scheme); 788b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner components.lpszHostName = host; 798b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner components.dwHostNameLength = sizeof(host); 808b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner components.lpszUrlPath = path; 818b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner components.dwUrlPathLength = sizeof(path); 828b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (!InternetCrackUrl(url.c_str(), static_cast<DWORD>(url.size()), 838b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 0, &components)) { 848b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 858b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 865afd60b067a65920ba2e4705ccea71b880cc1607mmentovai bool secure = false; 875afd60b067a65920ba2e4705ccea71b880cc1607mmentovai if (wcscmp(scheme, L"https") == 0) { 885afd60b067a65920ba2e4705ccea71b880cc1607mmentovai secure = true; 895afd60b067a65920ba2e4705ccea71b880cc1607mmentovai } else if (wcscmp(scheme, L"http") != 0) { 908b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 918b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 928b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 938b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner AutoInternetHandle internet(InternetOpen(kUserAgent, 945afd60b067a65920ba2e4705ccea71b880cc1607mmentovai INTERNET_OPEN_TYPE_PRECONFIG, 958b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL, // proxy name 968b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL, // proxy bypass 978b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 0)); // flags 988b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (!internet.get()) { 998b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 1008b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 1018b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1028b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner AutoInternetHandle connection(InternetConnect(internet.get(), 1038b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner host, 1048b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner components.nPort, 1058b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL, // user name 1068b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL, // password 1078b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner INTERNET_SERVICE_HTTP, 1088b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 0, // flags 1098b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL)); // context 1108b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (!connection.get()) { 1118b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 1128b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 1138b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1145afd60b067a65920ba2e4705ccea71b880cc1607mmentovai DWORD http_open_flags = secure ? INTERNET_FLAG_SECURE : 0; 1158b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner AutoInternetHandle request(HttpOpenRequest(connection.get(), 1168b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner L"POST", 1178b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner path, 1188b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL, // version 1198b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL, // referer 1208b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL, // agent type 1215afd60b067a65920ba2e4705ccea71b880cc1607mmentovai http_open_flags, 1228b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL)); // context 1238b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (!request.get()) { 1248b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 1258b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 1268b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1278b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner wstring boundary = GenerateMultipartBoundary(); 1288b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner wstring content_type_header = GenerateRequestHeader(boundary); 1298b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner HttpAddRequestHeaders(request.get(), 1308b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner content_type_header.c_str(), 1318b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner -1, HTTP_ADDREQ_FLAG_ADD); 1328b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1338b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner string request_body; 1348b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner GenerateRequestBody(parameters, upload_file, 1358b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner file_part_name, boundary, &request_body); 1368b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1375afd60b067a65920ba2e4705ccea71b880cc1607mmentovai if (!HttpSendRequest(request.get(), NULL, 0, 1385afd60b067a65920ba2e4705ccea71b880cc1607mmentovai const_cast<char *>(request_body.data()), 1395afd60b067a65920ba2e4705ccea71b880cc1607mmentovai static_cast<DWORD>(request_body.size()))) { 1405afd60b067a65920ba2e4705ccea71b880cc1607mmentovai return false; 1415afd60b067a65920ba2e4705ccea71b880cc1607mmentovai } 1425afd60b067a65920ba2e4705ccea71b880cc1607mmentovai 1435afd60b067a65920ba2e4705ccea71b880cc1607mmentovai // The server indicates a successful upload with HTTP status 200. 1445afd60b067a65920ba2e4705ccea71b880cc1607mmentovai wchar_t http_status[4]; 1455afd60b067a65920ba2e4705ccea71b880cc1607mmentovai DWORD http_status_size = sizeof(http_status); 1465afd60b067a65920ba2e4705ccea71b880cc1607mmentovai if (!HttpQueryInfo(request.get(), HTTP_QUERY_STATUS_CODE, 1475afd60b067a65920ba2e4705ccea71b880cc1607mmentovai static_cast<LPVOID>(&http_status), &http_status_size, 1485afd60b067a65920ba2e4705ccea71b880cc1607mmentovai 0)) { 1495afd60b067a65920ba2e4705ccea71b880cc1607mmentovai return false; 1505afd60b067a65920ba2e4705ccea71b880cc1607mmentovai } 1515afd60b067a65920ba2e4705ccea71b880cc1607mmentovai 1525afd60b067a65920ba2e4705ccea71b880cc1607mmentovai return (wcscmp(http_status, L"200") == 0); 1538b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner} 1548b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1558b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static 1568b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerwstring HTTPUpload::GenerateMultipartBoundary() { 1578b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner // The boundary has 27 '-' characters followed by 16 hex digits 1588b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner static const wchar_t kBoundaryPrefix[] = L"---------------------------"; 1598b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner static const int kBoundaryLength = 27 + 16 + 1; 1608b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1618b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner // Generate some random numbers to fill out the boundary 1628b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner int r0 = rand(); 1638b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner int r1 = rand(); 1648b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1658b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner wchar_t temp[kBoundaryLength]; 1668b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner swprintf_s(temp, kBoundaryLength, L"%s%08X%08X", kBoundaryPrefix, r0, r1); 1678b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return wstring(temp); 1688b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner} 1698b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1708b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static 1718b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerwstring HTTPUpload::GenerateRequestHeader(const wstring &boundary) { 1728b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner wstring header = L"Content-Type: multipart/form-data; boundary="; 1738b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner header += boundary; 1748b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return header; 1758b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner} 1768b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1778b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static 1788b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerbool HTTPUpload::GenerateRequestBody(const map<wstring, wstring> ¶meters, 1798b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner const wstring &upload_file, 1808b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner const wstring &file_part_name, 1818b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner const wstring &boundary, 1828b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner string *request_body) { 1838b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner vector<char> contents; 1848b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner GetFileContents(upload_file, &contents); 1858b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (contents.empty()) { 1868b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 1878b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 1888b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1898b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner string boundary_str = WideToUTF8(boundary); 1908b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (boundary_str.empty()) { 1918b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 1928b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 1938b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1948b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner request_body->clear(); 1958b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 1968b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner // Append each of the parameter pairs as a form-data part 1978b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner for (map<wstring, wstring>::const_iterator pos = parameters.begin(); 1988b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner pos != parameters.end(); ++pos) { 1998b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner request_body->append("--" + boundary_str + "\r\n"); 2008b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner request_body->append("Content-Disposition: form-data; name=\"" + 2018b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner WideToUTF8(pos->first) + "\"\r\n\r\n" + 2028b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner WideToUTF8(pos->second) + "\r\n"); 2038b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 2048b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2058b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner // Now append the upload file as a binary (octet-stream) part 2068b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner string filename_utf8 = WideToUTF8(upload_file); 2078b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (filename_utf8.empty()) { 2088b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 2098b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 2108b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2118b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner string file_part_name_utf8 = WideToUTF8(file_part_name); 2128b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (file_part_name_utf8.empty()) { 2138b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 2148b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 2158b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2168b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner request_body->append("--" + boundary_str + "\r\n"); 2178b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner request_body->append("Content-Disposition: form-data; " 2188b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner "name=\"" + file_part_name_utf8 + "\"; " 2198b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner "filename=\"" + filename_utf8 + "\"\r\n"); 2208b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner request_body->append("Content-Type: application/octet-stream\r\n"); 2218b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner request_body->append("\r\n"); 2228b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2238b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner request_body->append(&(contents[0]), contents.size()); 2248b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner request_body->append("\r\n"); 2258b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner request_body->append("--" + boundary_str + "--\r\n"); 2268b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return true; 2278b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner} 2288b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2298b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static 2308b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynervoid HTTPUpload::GetFileContents(const wstring &filename, 2318b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner vector<char> *contents) { 2328b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner ifstream file; 2338b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner file.open(filename.c_str(), ios::binary); 2348b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (file.is_open()) { 2358b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner file.seekg(0, ios::end); 2368b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner int length = file.tellg(); 2378b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner contents->resize(length); 2388b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner file.seekg(0, ios::beg); 2398b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner file.read(&((*contents)[0]), length); 2408b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner file.close(); 2418b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } else { 2428b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner contents->clear(); 2438b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 2448b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner} 2458b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2468b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static 2478b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerstring HTTPUpload::WideToUTF8(const wstring &wide) { 2488b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (wide.length() == 0) { 2498b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return string(); 2508b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 2518b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2528b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner // compute the length of the buffer we'll need 2538b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner int charcount = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, 2548b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL, 0, NULL, NULL); 2558b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (charcount == 0) { 2568b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return string(); 2578b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 2588b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2598b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner // convert 2608b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner char *buf = new char[charcount]; 2618b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, buf, charcount, 2628b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner NULL, NULL); 2638b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2648b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner string result(buf); 2658b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner delete[] buf; 2668b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return result; 2678b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner} 2688b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2698b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner// static 2708b1645d8cdb34035c0b132fe8b574bc5ee48fb62brynerbool HTTPUpload::CheckParameters(const map<wstring, wstring> ¶meters) { 2718b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner for (map<wstring, wstring>::const_iterator pos = parameters.begin(); 2728b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner pos != parameters.end(); ++pos) { 2738b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner const wstring &str = pos->first; 2748b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (str.size() == 0) { 2758b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; // disallow empty parameter names 2768b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 2778b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner for (unsigned int i = 0; i < str.size(); ++i) { 2788b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner wchar_t c = str[i]; 2798b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner if (c < 32 || c == '"' || c > 127) { 2808b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return false; 2818b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 2828b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 2838b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner } 2848b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner return true; 2858b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner} 2868b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner 2878b1645d8cdb34035c0b132fe8b574bc5ee48fb62bryner} // namespace google_airbag 288