OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "components/physical_web/eddystone/eddystone_encoder.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "url/gurl.h" | |
11 | |
12 namespace { | |
13 const char* kPrefixes[] = {"http://www.", "https://www.", "http://", | |
14 "https://"}; | |
15 const size_t kNumPrefixes = sizeof(kPrefixes) / sizeof(char*); | |
16 | |
17 const char* kSuffixes[] = {".com/", ".org/", ".edu/", ".net/", ".info/", | |
18 ".biz/", ".gov/", ".com", ".org", ".edu", | |
19 ".net", ".info", ".biz", ".gov"}; | |
20 const size_t kNumSuffixes = sizeof(kSuffixes) / sizeof(char*); | |
21 } | |
22 | |
23 namespace physical_web { | |
24 int EncodeUrl(const std::string& url, std::vector<uint8_t>* encoded_url) { | |
25 /* | |
26 * Sanitize input. | |
27 */ | |
28 | |
29 GURL gurl(url); | |
Olivier
2017/03/13 11:57:55
nit: move to line 34
iankc
2017/03/13 17:53:01
Done.
| |
30 if (encoded_url == NULL) | |
31 return kNullEncodedUrl; | |
32 if (url.empty()) | |
33 return kEmptyUrl; | |
34 if (!gurl.is_valid()) | |
35 return kInvalidUrl; | |
36 if (gurl.HostIsIPAddress() || !gurl.SchemeIsHTTPOrHTTPS()) | |
37 return kInvalidFormat; | |
38 | |
39 /* | |
40 * Necessary Declarations. | |
41 */ | |
42 const char* raw_url_position = url.c_str(); | |
43 | |
44 static bool lengths_loaded = false; | |
45 static size_t prefix_lengths[kNumPrefixes]; | |
Olivier
2017/03/13 11:57:55
nit: use only one
if(!lengths_loaded) {
...
.
iankc
2017/03/13 17:53:00
Done.
| |
46 if (!lengths_loaded) { | |
47 for (size_t i = 0; i < kNumPrefixes; i++) { | |
48 prefix_lengths[i] = strlen(kPrefixes[i]); | |
49 } | |
50 } | |
51 | |
52 static size_t suffix_lengths[kNumSuffixes]; | |
53 if (!lengths_loaded) { | |
54 for (size_t i = 0; i < kNumSuffixes; i++) { | |
55 suffix_lengths[i] = strlen(kSuffixes[i]); | |
56 } | |
57 lengths_loaded = true; | |
58 } | |
59 | |
60 /* | |
61 * Handle prefix. | |
62 */ | |
63 for (size_t i = 0; i < kNumPrefixes; i++) { | |
64 size_t prefix_len = prefix_lengths[i]; | |
65 if (strncmp(raw_url_position, kPrefixes[i], prefix_len) == 0) { | |
66 encoded_url->push_back(i); | |
67 raw_url_position += prefix_len; | |
68 break; | |
69 } | |
70 } | |
71 | |
72 /* | |
73 * Handle suffixes. | |
74 */ | |
75 while (*raw_url_position) { | |
76 /* check for suffix match */ | |
77 size_t i; | |
78 for (i = 0; i < kNumSuffixes; i++) { | |
79 size_t suffix_len = suffix_lengths[i]; | |
80 if (strncmp(raw_url_position, kSuffixes[i], suffix_len) == 0) { | |
81 encoded_url->push_back(i); | |
82 raw_url_position += suffix_len; | |
83 break; /* from the for loop for checking against suffixes */ | |
84 } | |
85 } | |
86 /* This is the default case where we've got an ordinary character which | |
87 * doesn't match a suffix. */ | |
88 if (i == kNumSuffixes) { | |
89 encoded_url->push_back(*raw_url_position++); | |
90 } | |
91 } | |
92 | |
93 return encoded_url->size(); | |
94 } | |
95 } | |
OLD | NEW |