1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "third_party/mozilla/url_parse_internal.h"
6
7 #include <ctype.h>
8
9 #include "third_party/mozilla/url_parse.h"
10
11 namespace openscreen {
12
13 namespace {
14
15 static const char* g_standard_schemes[] = {
16 kHttpsScheme, kHttpScheme, kFileScheme, kFtpScheme, kWssScheme, kWsScheme,
17 };
18
19 } // namespace
20
IsURLSlash(char ch)21 bool IsURLSlash(char ch) {
22 return ch == '/' || ch == '\\';
23 }
24
ShouldTrimFromURL(char ch)25 bool ShouldTrimFromURL(char ch) {
26 return ch <= ' ';
27 }
28
TrimURL(const char * spec,int * begin,int * len,bool trim_path_end)29 void TrimURL(const char* spec, int* begin, int* len, bool trim_path_end) {
30 // Strip leading whitespace and control characters.
31 while (*begin < *len && ShouldTrimFromURL(spec[*begin])) {
32 (*begin)++;
33 }
34
35 if (trim_path_end) {
36 // Strip trailing whitespace and control characters. We need the >i test
37 // for when the input string is all blanks; we don't want to back past the
38 // input.
39 while (*len > *begin && ShouldTrimFromURL(spec[*len - 1])) {
40 (*len)--;
41 }
42 }
43 }
44
CountConsecutiveSlashes(const char * str,int begin_offset,int str_len)45 int CountConsecutiveSlashes(const char* str, int begin_offset, int str_len) {
46 int count = 0;
47 while ((begin_offset + count) < str_len &&
48 IsURLSlash(str[begin_offset + count])) {
49 ++count;
50 }
51 return count;
52 }
53
CompareSchemeComponent(const char * spec,const Component & component,const char * compare_to)54 bool CompareSchemeComponent(const char* spec,
55 const Component& component,
56 const char* compare_to) {
57 if (!component.is_nonempty()) {
58 return compare_to[0] == 0; // When component is empty, match empty scheme.
59 }
60 for (int i = 0; i < component.len; ++i) {
61 if (tolower(spec[i]) != compare_to[i]) {
62 return false;
63 }
64 }
65 return true;
66 }
67
IsStandard(const char * spec,const Component & component)68 bool IsStandard(const char* spec, const Component& component) {
69 if (!component.is_nonempty()) {
70 return false;
71 }
72
73 constexpr int scheme_count =
74 sizeof(g_standard_schemes) / sizeof(g_standard_schemes[0]);
75 for (int i = 0; i < scheme_count; ++i) {
76 if (CompareSchemeComponent(spec, component, g_standard_schemes[i])) {
77 return true;
78 }
79 }
80 return false;
81 }
82
83 // NOTE: Not implemented because file URLs are currently unsupported.
ParseFileURL(const char * url,int url_len,Parsed * parsed)84 void ParseFileURL(const char* url, int url_len, Parsed* parsed) {}
85
86 } // namespace openscreen
87