File: url_utils.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 (142 lines) | stat: -rw-r--r-- 4,325 bytes parent folder | download | duplicates (3)
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
141
142
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/dom_distiller/core/url_utils.h"

#include <string>
#include <string_view>

#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/uuid.h"
#include "components/dom_distiller/core/url_constants.h"
#include "components/grit/components_resources.h"
#include "crypto/sha2.h"
#include "net/base/url_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"
#include "url/url_util.h"

namespace dom_distiller {

namespace url_utils {

namespace {

const char kDummyInternalUrlPrefix[] = "chrome-distiller-internal://dummy/";
const char kSeparator[] = "_";

std::string SHA256InHex(std::string_view str) {
  std::string sha256 = crypto::SHA256HashString(str);
  return base::ToLowerASCII(base::HexEncode(sha256));
}

}  // namespace

const GURL GetDistillerViewUrlFromEntryId(const std::string& scheme,
                                          const std::string& entry_id) {
  GURL url(scheme + "://" + base::Uuid::GenerateRandomV4().AsLowercaseString());
  return net::AppendOrReplaceQueryParameter(url, kEntryIdKey, entry_id);
}

const GURL GetDistillerViewUrlFromUrl(const std::string& scheme,
                                      const GURL& url,
                                      const std::string& title,
                                      int64_t start_time_ms) {
  GURL view_url(scheme + "://" +
                base::Uuid::GenerateRandomV4().AsLowercaseString() +
                kSeparator + SHA256InHex(url.spec()));
  view_url = net::AppendOrReplaceQueryParameter(view_url, kTitleKey, title);
  if (start_time_ms > 0) {
    view_url = net::AppendOrReplaceQueryParameter(
        view_url, kTimeKey, base::NumberToString(start_time_ms));
  }
  return net::AppendOrReplaceQueryParameter(view_url, kUrlKey, url.spec());
}

GURL GetOriginalUrlFromDistillerUrl(const GURL& url) {
  if (!IsUrlDistilledFormat(url))
    return url;

  std::string original_url_str;
  net::GetValueForKeyInQuery(url, kUrlKey, &original_url_str);

  // Make sure kDomDistillerScheme is considered standard scheme for
  // |GURL::host_piece()| to work correctly.
  DCHECK(url::IsStandard(kDomDistillerScheme));
  std::vector<std::string_view> pieces =
      base::SplitStringPiece(url.host_piece(), kSeparator,
                             base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  if (pieces.size() != 2)
    return GURL();
  if (SHA256InHex(original_url_str) != pieces[1])
    return GURL();
  const GURL original_url(original_url_str);
  if (!IsUrlDistillable(original_url))
    return GURL();

  return original_url;
}

int64_t GetTimeFromDistillerUrl(const GURL& url) {
  if (!IsDistilledPage(url))
    return 0;

  std::string time_str;
  if (!net::GetValueForKeyInQuery(url, kTimeKey, &time_str))
    return 0;

  int64_t time_int = 0;
  if (!base::StringToInt64(time_str, &time_int))
    return 0;

  return time_int;
}

std::string GetTitleFromDistillerUrl(const GURL& url) {
  if (!IsDistilledPage(url))
    return "";

  std::string title;
  if (!net::GetValueForKeyInQuery(url, kTitleKey, &title))
    return "";

  return title;
}

std::string GetValueForKeyInUrl(const GURL& url, const std::string& key) {
  if (!url.is_valid())
    return "";
  std::string value;
  if (net::GetValueForKeyInQuery(url, key, &value)) {
    return value;
  }
  return "";
}

std::string GetValueForKeyInUrlPathQuery(const std::string& path,
                                         const std::string& key) {
  // Tools for retrieving a value in a query only works with full GURLs, so
  // using a dummy scheme and host to create a fake URL which can be parsed.
  GURL dummy_url(kDummyInternalUrlPrefix + path);
  return GetValueForKeyInUrl(dummy_url, key);
}

bool IsUrlDistillable(const GURL& url) {
  return url.is_valid() && url.SchemeIsHTTPOrHTTPS();
}

bool IsDistilledPage(const GURL& url) {
  return IsUrlDistilledFormat(url) &&
         GetOriginalUrlFromDistillerUrl(url).is_valid();
}

bool IsUrlDistilledFormat(const GURL& url) {
  return url.is_valid() && url.scheme() == kDomDistillerScheme;
}

}  // namespace url_utils

}  // namespace dom_distiller