xref: /aosp_15_r20/external/google-breakpad/src/common/windows/sym_upload_v2_protocol.cc (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2022 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>  // Must come first
31 #endif
32 
33 #include "common/windows/sym_upload_v2_protocol.h"
34 
35 #include <cstdio>
36 
37 #include "common/windows/http_upload.h"
38 #include "common/windows/symbol_collector_client.h"
39 
40 using google_breakpad::CompleteUploadResult;
41 using google_breakpad::HTTPUpload;
42 using google_breakpad::SymbolCollectorClient;
43 using google_breakpad::SymbolStatus;
44 using google_breakpad::UploadUrlResponse;
45 using std::wstring;
46 
47 namespace google_breakpad {
48 
SymUploadV2ProtocolSend(const wchar_t * api_url,const wchar_t * api_key,int * timeout_ms,const wstring & debug_file,const wstring & debug_id,const wstring & symbol_filename,const wstring & symbol_type,const wstring & product_name,bool force)49 static bool SymUploadV2ProtocolSend(const wchar_t* api_url,
50                                     const wchar_t* api_key,
51                                     int* timeout_ms,
52                                     const wstring& debug_file,
53                                     const wstring& debug_id,
54                                     const wstring& symbol_filename,
55                                     const wstring& symbol_type,
56                                     const wstring& product_name,
57                                     bool force) {
58   wstring url(api_url);
59   wstring key(api_key);
60 
61   if (!force) {
62     SymbolStatus symbolStatus = SymbolCollectorClient::CheckSymbolStatus(
63         url, key, timeout_ms, debug_file, debug_id);
64     if (symbolStatus == SymbolStatus::Found) {
65       wprintf(
66           L"Symbol file already exists, upload aborted."
67           L" Use \"-f\" to overwrite.\n");
68       return true;
69     } else if (symbolStatus == SymbolStatus::Unknown) {
70       wprintf(L"Failed to get check for existing symbol.\n");
71       return false;
72     }
73   }
74 
75   UploadUrlResponse uploadUrlResponse;
76   if (!SymbolCollectorClient::CreateUploadUrl(url, key, timeout_ms,
77                                               &uploadUrlResponse)) {
78     wprintf(L"Failed to create upload URL.\n");
79     return false;
80   }
81 
82   wstring signed_url = uploadUrlResponse.upload_url;
83   wstring upload_key = uploadUrlResponse.upload_key;
84   wstring response;
85   int response_code;
86   bool success = HTTPUpload::SendPutRequest(
87       signed_url, symbol_filename, timeout_ms, &response, &response_code);
88   if (!success) {
89     wprintf(L"Failed to send symbol file.\n");
90     wprintf(L"Response code: %ld\n", response_code);
91     wprintf(L"Response:\n");
92     wprintf(L"%s\n", response.c_str());
93     return false;
94   } else if (response_code == 0) {
95     wprintf(L"Failed to send symbol file: No response code\n");
96     return false;
97   } else if (response_code != 200) {
98     wprintf(L"Failed to send symbol file: Response code %ld\n", response_code);
99     wprintf(L"Response:\n");
100     wprintf(L"%s\n", response.c_str());
101     return false;
102   }
103 
104   CompleteUploadResult completeUploadResult =
105       SymbolCollectorClient::CompleteUpload(url, key, timeout_ms, upload_key,
106                                             debug_file, debug_id, symbol_type,
107                                             product_name);
108   if (completeUploadResult == CompleteUploadResult::Error) {
109     wprintf(L"Failed to complete upload.\n");
110     return false;
111   } else if (completeUploadResult == CompleteUploadResult::DuplicateData) {
112     wprintf(
113         L"Uploaded file checksum matched existing file checksum,"
114         L" no change necessary.\n");
115   } else {
116     wprintf(L"Successfully sent the symbol file.\n");
117   }
118 
119   return true;
120 }
121 
122 }  // namespace google_breakpad
123