summaryrefslogtreecommitdiff
path: root/chromium/net/url_request/url_request_job_factory.cc
blob: 68b4dae4b2e71d66a6ae71f6cd19cf3286281d99 (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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2011 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/url_request/url_request_job_factory.h"

#include "base/containers/contains.h"
#include "net/base/net_errors.h"
#include "net/net_buildflags.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_error_job.h"
#include "net/url_request/url_request_http_job.h"
#include "net/url_request/url_request_interceptor.h"
#include "url/gurl.h"
#include "url/url_constants.h"

namespace net {

namespace {

URLRequestInterceptor* g_interceptor_for_testing = nullptr;

// TODO(mmenke): Look into removing this class and
// URLRequestJobFactory::ProtocolHandlers completely. The only other subclass
// is iOS-only.
class HttpProtocolHandler : public URLRequestJobFactory::ProtocolHandler {
 public:
  // URLRequest::is_for_websockets() must match `is_for_websockets`, or requests
  // will be failed. This is so that attempts to fetch WebSockets requests
  // fails, and attempts to use HTTP URLs for WebSockets also fail.
  explicit HttpProtocolHandler(bool is_for_websockets)
      : is_for_websockets_(is_for_websockets) {}

  HttpProtocolHandler(const HttpProtocolHandler&) = delete;
  HttpProtocolHandler& operator=(const HttpProtocolHandler&) = delete;
  ~HttpProtocolHandler() override = default;

  std::unique_ptr<URLRequestJob> CreateJob(URLRequest* request) const override {
    if (request->is_for_websockets() != is_for_websockets_) {
      return std::make_unique<URLRequestErrorJob>(request,
                                                  ERR_UNKNOWN_URL_SCHEME);
    }
    return URLRequestHttpJob::Create(request);
  }

  const bool is_for_websockets_;
};

}  // namespace

URLRequestJobFactory::ProtocolHandler::~ProtocolHandler() = default;

bool URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(
    const GURL& location) const {
  return true;
}

URLRequestJobFactory::URLRequestJobFactory() {
  SetProtocolHandler(url::kHttpScheme, std::make_unique<HttpProtocolHandler>(
                                           /*is_for_websockets=*/false));
  SetProtocolHandler(url::kHttpsScheme, std::make_unique<HttpProtocolHandler>(
                                            /*is_for_websockets=*/false));
#if BUILDFLAG(ENABLE_WEBSOCKETS)
  SetProtocolHandler(url::kWsScheme, std::make_unique<HttpProtocolHandler>(
                                         /*is_for_websockets=*/true));
  SetProtocolHandler(url::kWssScheme, std::make_unique<HttpProtocolHandler>(
                                          /*is_for_websockets=*/true));
#endif  // BUILDFLAG(ENABLE_WEBSOCKETS)
}

URLRequestJobFactory::~URLRequestJobFactory() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}

bool URLRequestJobFactory::SetProtocolHandler(
    const std::string& scheme,
    std::unique_ptr<ProtocolHandler> protocol_handler) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  if (!protocol_handler) {
    auto it = protocol_handler_map_.find(scheme);
    if (it == protocol_handler_map_.end())
      return false;

    protocol_handler_map_.erase(it);
    return true;
  }

  if (base::Contains(protocol_handler_map_, scheme))
    return false;
  protocol_handler_map_[scheme] = std::move(protocol_handler);
  return true;
}

std::unique_ptr<URLRequestJob> URLRequestJobFactory::CreateJob(
    URLRequest* request) const {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  // If we are given an invalid URL, then don't even try to inspect the scheme.
  if (!request->url().is_valid())
    return std::make_unique<URLRequestErrorJob>(request, ERR_INVALID_URL);

  if (g_interceptor_for_testing) {
    std::unique_ptr<URLRequestJob> job(
        g_interceptor_for_testing->MaybeInterceptRequest(request));
    if (job)
      return job;
  }

  auto it = protocol_handler_map_.find(request->url().scheme());
  if (it == protocol_handler_map_.end()) {
    return std::make_unique<URLRequestErrorJob>(request,
                                                ERR_UNKNOWN_URL_SCHEME);
  }

  return it->second->CreateJob(request);
}

bool URLRequestJobFactory::IsSafeRedirectTarget(const GURL& location) const {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  if (!location.is_valid()) {
    // Error cases are safely handled.
    return true;
  }
  auto it = protocol_handler_map_.find(location.scheme());
  if (it == protocol_handler_map_.end()) {
    // Unhandled cases are safely handled.
    return true;
  }
  return it->second->IsSafeRedirectTarget(location);
}

void URLRequestJobFactory::SetInterceptorForTesting(
    URLRequestInterceptor* interceptor) {
  DCHECK(!interceptor || !g_interceptor_for_testing);

  g_interceptor_for_testing = interceptor;
}

}  // namespace net