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

content / public / test / private_network_access_util.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/test/private_network_access_util.h"

#include "base/functional/bind.h"

namespace content {

DeprecationTrialURLLoaderInterceptor::DeprecationTrialURLLoaderInterceptor()
    : interceptor_(base::BindRepeating(
          &DeprecationTrialURLLoaderInterceptor::HandleRequest,
          base::Unretained(this))) {}

DeprecationTrialURLLoaderInterceptor::~DeprecationTrialURLLoaderInterceptor() =
    default;

bool DeprecationTrialURLLoaderInterceptor::HandleRequest(
    RequestParams* request_params) const {
  const GURL& url = request_params->url_request.url;
  if (url == EnabledUrl()) {
    HandleEnabledUrlRequest(*request_params);
    return true;
  }

  if (url == DisabledUrl()) {
    HandleDisabledUrlRequest(*request_params);
    return true;
  }

  return false;
}

void DeprecationTrialURLLoaderInterceptor::HandleEnabledUrlRequest(
    RequestParams& request_params) const {
  constexpr char kHeaders[] =      //
      "HTTP/1.1 200 OK\n"          //
      "Content-Type: text/html\n"  //
      // Use CSP to make the page `public`, even though it is served with no
      // IP address information. Without this it is treated as `unknown`, and
      // that interferes with its private network request policy.
      "Content-Security-Policy: treat-as-public-address\n"  //
      // This token was generated using:
      //
      //   $ tools/origin_trials/generate_token.py \
      //       --expire-days 5000 \
      //       --version 3 \
      //       http://enabled.test PrivateNetworkAccessNonSecureContextsAllowed
      //
      "Origin-Trial: "
      "A4dgNIB2F3P8qkQQes/oiaobjPNRbfZcaPd5TqdcIHUlpX3/al3rvk5b4f+dnke3WcsXeX"
      "4aMNENL3mg1FM8+wYAAAB1eyJvcmlnaW4iOiAiaHR0cDovL2VuYWJsZWQudGVzdDo4MCIs"
      "ICJmZWF0dXJlIjogIlByaXZhdGVOZXR3b3JrQWNjZXNzTm9uU2VjdXJlQ29udGV4dHNBbG"
      "xvd2VkIiwgImV4cGlyeSI6IDIwNTcxNDYwMzB9"  //
      "\n\n";
  URLLoaderInterceptor::WriteResponse(kHeaders, "",
                                      request_params.client.get());
}

void DeprecationTrialURLLoaderInterceptor::HandleDisabledUrlRequest(
    RequestParams& request_params) const {
  constexpr char kHeaders[] =      //
      "HTTP/1.1 200 OK\n"          //
      "Content-Type: text/html\n"  //
      // See above.
      "Content-Security-Policy: treat-as-public-address\n\n";
  URLLoaderInterceptor::WriteResponse(kHeaders, "",
                                      request_params.client.get());
}

}  // namespace content