summaryrefslogtreecommitdiffstats
path: root/net/websockets/websocket_net_log_params.h
blob: 4cd058f0876c489440b7110b5cf29a8ff7087f33 (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
68
69
70
71
72
73
// 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();

    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;

        headers->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);
          }
          headers->Append(new StringValue(key));
          break;
        }
      } else {
        ++pos;
      }
    }

    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_