summaryrefslogtreecommitdiffstats
path: root/net/websockets/websocket_net_log_params.h
blob: a5041867a2414a79c17b57498441336ea94e4f24 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2010 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.

#ifndef NET_WEBSOCKETS_WEBSOCKET_NET_LOG_PARAMS_H_
#define NET_WEBSOCKETS_WEBSOCKET_NET_LOG_PARAMS_H_
#pragma once

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/ref_counted.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/values.h"
#include "net/base/net_log.h"

namespace net {

class NetLogWebSocketHandshakeParameter : public NetLog::EventParameters {
 public:
  explicit NetLogWebSocketHandshakeParameter(const std::string& headers)
      : headers_(headers) {
  }

  Value* ToValue() const {
    DictionaryValue* dict = new DictionaryValue();
    ListValue* headers = new ListValue();
    std::vector<std::string> lines;
    base::SplitStringDontTrim(headers_, '\n', &lines);
    for (size_t i = 0; i < lines.size(); ++i) {
      if (lines[i] == "\r") {
        headers->Append(new StringValue(""));
        // Dump WebSocket key3
        std::string key;
        i = i + 1;
        for (; i < lines.size(); ++i) {
          for (size_t j = 0; j < lines[i].length(); ++j) {
            key += base::StringPrintf("\\x%02x", lines[i][j] & 0xff);
          }
          key += "\\x0a";
        }
        headers->Append(new StringValue(key));
        break;
      }
      std::string line = lines[i];
      if (line.length() > 0 && line[line.length() - 1] == '\r')
        line = line.substr(0, line.length() - 1);
      headers->Append(new StringValue(line));
    }
    dict->Set("headers", headers);
    return dict;
  }

 private:
  ~NetLogWebSocketHandshakeParameter() {}

  const std::string headers_;

  DISALLOW_COPY_AND_ASSIGN(NetLogWebSocketHandshakeParameter);
};

}  // namespace net

#endif  // NET_WEBSOCKETS_WEBSOCKET_NET_LOG_PARAMS_H_