summaryrefslogtreecommitdiffstats
path: root/net/websockets/websocket_net_log_params.cc
blob: 7c1a2925b208f243de7f3afe7a6ebc3df14dc2d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/websockets/websocket_net_log_params.h"

#include "base/stringprintf.h"
#include "base/values.h"

namespace net {

Value* NetLogWebSocketHandshakeCallback(const std::string* headers,
                                        NetLog::LogLevel /* log_level */) {
  DictionaryValue* dict = new DictionaryValue();
  ListValue* header_list = new ListValue();

  size_t last = 0;
  size_t headers_size = headers->size();
  size_t pos = 0;
  while (pos <= headers_size) {
    if (pos == headers_size ||
        ((*headers)[pos] == '\r' &&
         pos + 1 < headers_size && (*headers)[pos + 1] == '\n')) {
      std::string entry = headers->substr(last, pos - last);
      pos += 2;
      last = pos;

      header_list->Append(new StringValue(entry));

      if (entry.empty()) {
        // Dump WebSocket key3.
        std::string key;
        for (; pos < headers_size; ++pos) {
          key += base::StringPrintf("\\x%02x", (*headers)[pos] & 0xff);
        }
        header_list->Append(new StringValue(key));
        break;
      }
    } else {
      ++pos;
    }
  }

  dict->Set("headers", header_list);
  return dict;
}

}  // namespace net