File: http_basic_state.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (126 lines) | stat: -rw-r--r-- 4,395 bytes parent folder | download | duplicates (6)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/http/http_basic_state.h"

#include <set>
#include <utility>

#include "base/check_op.h"
#include "base/no_destructor.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_endpoint.h"
#include "net/base/load_timing_info.h"
#include "net/base/net_errors.h"
#include "net/http/http_request_info.h"
#include "net/http/http_response_body_drainer.h"
#include "net/http/http_stream_parser.h"
#include "net/http/http_util.h"
#include "net/socket/stream_socket.h"
#include "net/socket/stream_socket_handle.h"
#include "net/ssl/ssl_info.h"
#include "url/gurl.h"

namespace net {

HttpBasicState::HttpBasicState(std::unique_ptr<StreamSocketHandle> connection,
                               bool is_for_get_to_http_proxy)
    : read_buf_(base::MakeRefCounted<GrowableIOBuffer>()),
      connection_(std::move(connection)),
      is_for_get_to_http_proxy_(is_for_get_to_http_proxy) {
  CHECK(connection_) << "StreamSocketHandle passed to HttpBasicState must "
                        "not be NULL. See crbug.com/790776";
}

HttpBasicState::~HttpBasicState() = default;

void HttpBasicState::Initialize(const HttpRequestInfo* request_info,
                                RequestPriority priority,
                                const NetLogWithSource& net_log) {
  DCHECK(!parser_.get());
  traffic_annotation_ = request_info->traffic_annotation;
  parser_ = std::make_unique<HttpStreamParser>(
      connection_->socket(),
      connection_->reuse_type() ==
          StreamSocketHandle::SocketReuseType::kReusedIdle,
      request_info->url, request_info->method, request_info->upload_data_stream,
      read_buf_.get(), net_log);
}

void HttpBasicState::Close(bool not_reusable) {
  // `parser_` is null if the owner of `this` is created by an orphaned
  // HttpStreamFactory::Job in which case InitializeStream() will not have been
  // called. This also protects against null dereference in the case where
  // ReleaseConnection() has been called.
  //
  // TODO(mmenke):  Can these cases be handled a bit more cleanly?
  if (!parser_) {
    return;
  }
  StreamSocket* socket = connection_->socket();
  if (not_reusable && socket) {
    socket->Disconnect();
  }
  parser()->OnConnectionClose();
  connection_->Reset();
}

std::unique_ptr<StreamSocketHandle> HttpBasicState::ReleaseConnection() {
  // The HttpStreamParser object still has a pointer to the connection. Just to
  // be extra-sure it doesn't touch the connection again, delete it here rather
  // than leaving it until the destructor is called.
  parser_.reset();
  return std::move(connection_);
}

scoped_refptr<GrowableIOBuffer> HttpBasicState::read_buf() const {
  return read_buf_;
}

std::string HttpBasicState::GenerateRequestLine() const {
  return HttpUtil::GenerateRequestLine(parser_->method(), parser_->url(),
                                       is_for_get_to_http_proxy_);
}

bool HttpBasicState::IsConnectionReused() const {
  return connection_->reuse_type() ==
             StreamSocketHandle::SocketReuseType::kReusedIdle ||
         connection_->reuse_type() ==
             StreamSocketHandle::SocketReuseType::kUnusedIdle;
}

void HttpBasicState::SetConnectionReused() {
  connection_->set_reuse_type(StreamSocketHandle::SocketReuseType::kReusedIdle);
}

bool HttpBasicState::CanReuseConnection() const {
  return parser_ && connection_->socket() && parser_->CanReuseConnection();
}

bool HttpBasicState::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
  return connection_->GetLoadTimingInfo(IsConnectionReused(), load_timing_info);
}

void HttpBasicState::GetSSLInfo(SSLInfo* ssl_info) {
  CHECK(connection_);
  if (!connection_->socket() || !connection_->socket()->GetSSLInfo(ssl_info)) {
    *ssl_info = SSLInfo();
  }
}

int HttpBasicState::GetRemoteEndpoint(IPEndPoint* endpoint) {
  if (!connection_ || !connection_->socket()) {
    return ERR_SOCKET_NOT_CONNECTED;
  }
  return connection_->socket()->GetPeerAddress(endpoint);
}

const std::set<std::string>& HttpBasicState::GetDnsAliases() const {
  static const base::NoDestructor<std::set<std::string>> emptyset_result;
  return (connection_ && connection_->socket())
             ? connection_->socket()->GetDnsAliases()
             : *emptyset_result;
}

}  // namespace net