summaryrefslogtreecommitdiffstats
path: root/net/http/http_net_log_params.h
blob: 4b419da7ee047d9dc22c37d41bb7c8014775978c (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
74
75
76
77
78
79
80
81
82
83
84
85
// 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_HTTP_HTTP_NET_LOG_PARAMS_H_
#define NET_HTTP_HTTP_NET_LOG_PARAMS_H_
#pragma once

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/ref_counted.h"
#include "base/values.h"
#include "net/base/net_log.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"

namespace net {

class NetLogHttpRequestParameter : public NetLog::EventParameters {
 public:
  NetLogHttpRequestParameter(const std::string& line,
                             const HttpRequestHeaders& headers)
      : line_(line) {
    headers_.CopyFrom(headers);
  }

  Value* ToValue() const {
    DictionaryValue* dict = new DictionaryValue();
    dict->SetString("line", line_);
    ListValue* headers = new ListValue();
    HttpRequestHeaders::Iterator iterator(headers_);
    while (iterator.GetNext()) {
      headers->Append(
          new StringValue(StringPrintf("%s: %s",
                                       iterator.name().c_str(),
                                       iterator.value().c_str())));
    }
    dict->Set("headers", headers);
    return dict;
  }

 private:
  ~NetLogHttpRequestParameter() {}

  const std::string line_;
  HttpRequestHeaders headers_;

  DISALLOW_COPY_AND_ASSIGN(NetLogHttpRequestParameter);
};

class NetLogHttpResponseParameter : public NetLog::EventParameters {
 public:
  explicit NetLogHttpResponseParameter(
      const scoped_refptr<HttpResponseHeaders>& headers)
      : headers_(headers) {}

  Value* ToValue() const {
    DictionaryValue* dict = new DictionaryValue();
    ListValue* headers = new ListValue();
    headers->Append(new StringValue(headers_->GetStatusLine()));
    void* iterator = NULL;
    std::string name;
    std::string value;
    while (headers_->EnumerateHeaderLines(&iterator, &name, &value)) {
      headers->Append(
          new StringValue(StringPrintf("%s: %s", name.c_str(), value.c_str())));
    }
    dict->Set("headers", headers);
    return dict;
  }

 private:
  ~NetLogHttpResponseParameter() {}

  const scoped_refptr<HttpResponseHeaders> headers_;

  DISALLOW_COPY_AND_ASSIGN(NetLogHttpResponseParameter);
};

}  // namespace net

#endif  // NET_HTTP_HTTP_NET_LOG_PARAMS_H_