Chromium Code Reviews| 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) { | |
|
mattreynolds
2017/03/09 22:11:30
Add a check for encoded_url == NULL.
iankc
2017/03/09 23:17:50
Done.
| |
| 25 /* | |
| 26 * Sanatize url. | |
|
mattreynolds
2017/03/09 22:11:31
Sanitize URL.
iankc
2017/03/09 23:17:50
Done.
| |
| 27 */ | |
| 28 if (url.empty()) { | |
| 29 return -1; | |
|
mattreynolds
2017/03/09 22:11:31
Define these error codes as constants in eddystone
iankc
2017/03/09 23:17:50
Done.
| |
| 30 } | |
| 31 | |
| 32 GURL gurl(url); | |
| 33 if (!gurl.is_valid()) { | |
| 34 return -2; | |
| 35 } | |
| 36 | |
| 37 if (gurl.HostIsIPAddress() || !gurl.SchemeIsHTTPOrHTTPS()) { | |
| 38 return -3; | |
| 39 } | |
| 40 | |
| 41 /* | |
| 42 * Necessary Declarations. | |
| 43 */ | |
| 44 const char* raw_url_position = url.c_str(); | |
| 45 | |
| 46 static bool prefix_lengths_loaded = false; | |
|
mattreynolds
2017/03/09 22:11:30
How about "lengths_initialized"? (we aren't really
iankc
2017/03/09 23:17:50
Done.
| |
| 47 static size_t prefix_lengths[kNumPrefixes]; | |
| 48 if (!prefix_lengths_loaded) { | |
| 49 for (size_t i = 0; i < kNumPrefixes; i++) { | |
| 50 prefix_lengths[i] = strlen(kPrefixes[i]); | |
| 51 } | |
| 52 prefix_lengths_loaded = true; | |
| 53 } | |
| 54 | |
| 55 static bool suffix_lengths_loaded = false; | |
|
mattreynolds
2017/03/09 22:11:30
You can use the same bool for both array initializ
iankc
2017/03/09 23:17:50
Done.
| |
| 56 static size_t suffix_lengths[kNumSuffixes]; | |
| 57 if (!suffix_lengths_loaded) { | |
| 58 for (size_t i = 0; i < kNumSuffixes; i++) { | |
| 59 suffix_lengths[i] = strlen(kSuffixes[i]); | |
| 60 } | |
| 61 suffix_lengths_loaded = true; | |
| 62 } | |
| 63 /* | |
| 64 * Handle prefix. | |
| 65 */ | |
| 66 for (size_t i = 0; i < kNumPrefixes; i++) { | |
| 67 size_t prefix_len = prefix_lengths[i]; | |
| 68 if (strncmp(raw_url_position, kPrefixes[i], prefix_len) == 0) { | |
| 69 encoded_url->push_back(i); | |
| 70 raw_url_position += prefix_len; | |
| 71 break; | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 /* | |
| 76 * Handle suffixes. | |
| 77 */ | |
| 78 while (*raw_url_position) { | |
| 79 /* check for suffix match */ | |
| 80 size_t i; | |
| 81 for (i = 0; i < kNumSuffixes; i++) { | |
| 82 size_t suffix_len = suffix_lengths[i]; | |
| 83 if (strncmp(raw_url_position, kSuffixes[i], suffix_len) == 0) { | |
| 84 encoded_url->push_back(i); | |
| 85 raw_url_position += suffix_len; | |
| 86 break; /* from the for loop for checking against suffixes */ | |
| 87 } | |
| 88 } | |
| 89 /* This is the default case where we've got an ordinary character which | |
| 90 * doesn't match a suffix. */ | |
| 91 if (i == kNumSuffixes) { | |
| 92 encoded_url->push_back(*raw_url_position++); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 return encoded_url->size(); | |
| 97 } | |
| 98 } | |
| OLD | NEW |