1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/spdy/spdy_log_util.h"
6 
7 #include <utility>
8 
9 #include "base/strings/abseil_string_conversions.h"
10 #include "base/strings/strcat.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "net/http/http_log_util.h"
14 #include "net/log/net_log_values.h"
15 
16 namespace net {
17 
ElideGoAwayDebugDataForNetLog(NetLogCaptureMode capture_mode,base::StringPiece debug_data)18 base::Value ElideGoAwayDebugDataForNetLog(NetLogCaptureMode capture_mode,
19                                           base::StringPiece debug_data) {
20   if (NetLogCaptureIncludesSensitive(capture_mode))
21     return NetLogStringValue(debug_data);
22 
23   return NetLogStringValue(base::StrCat(
24       {"[", base::NumberToString(debug_data.size()), " bytes were stripped]"}));
25 }
26 
ElideHttp2HeaderBlockForNetLog(const spdy::Http2HeaderBlock & headers,NetLogCaptureMode capture_mode)27 base::ListValue ElideHttp2HeaderBlockForNetLog(
28     const spdy::Http2HeaderBlock& headers,
29     NetLogCaptureMode capture_mode) {
30   base::ListValue headers_list;
31   for (const auto& header : headers) {
32     base::StringPiece key = base::StringViewToStringPiece(header.first);
33     base::StringPiece value = base::StringViewToStringPiece(header.second);
34     headers_list.Append(NetLogStringValue(
35         base::StrCat({key, ": ",
36                       ElideHeaderValueForNetLog(capture_mode, key.as_string(),
37                                                 value.as_string())})));
38   }
39   return headers_list;
40 }
41 
Http2HeaderBlockNetLogParams(const spdy::Http2HeaderBlock * headers,NetLogCaptureMode capture_mode)42 base::Value Http2HeaderBlockNetLogParams(const spdy::Http2HeaderBlock* headers,
43                                          NetLogCaptureMode capture_mode) {
44   base::DictionaryValue dict;
45   dict.SetKey("headers",
46               ElideHttp2HeaderBlockForNetLog(*headers, capture_mode));
47   return std::move(dict);
48 }
49 
50 }  // namespace net
51