|
1 | 1 | require 'http' |
2 | 2 | require 'json' |
| 3 | +require 'uri' |
3 | 4 |
|
4 | 5 | module NodeInfo |
5 | 6 | # Client for discovering and fetching NodeInfo from Fediverse servers |
@@ -44,7 +45,7 @@ def fetch domain |
44 | 45 | def discover domain |
45 | 46 | url = normalize_url domain, WELL_KNOWN_PATH |
46 | 47 |
|
47 | | - response = http_client.get(url) |
| 48 | + response = http_client.get url |
48 | 49 |
|
49 | 50 | raise DiscoveryError, "HTTP #{response.code}" unless response.status.success? |
50 | 51 |
|
@@ -77,22 +78,19 @@ def http_client |
77 | 78 | end |
78 | 79 |
|
79 | 80 | def normalize_url domain, path |
80 | | - # Remove protocol if present |
81 | | - domain = domain.sub %r{^https?://}, '' |
82 | | - |
83 | | - # Remove trailing slash |
84 | | - domain = domain.chomp '/' |
| 81 | + url = URI.parse domain |
| 82 | + url = URI.parse("https://#{domain}") unless domain.start_with? 'http' |
| 83 | + host = url.host |
85 | 84 |
|
86 | | - # Add https protocol |
87 | | - "https://#{domain}#{path}" |
| 85 | + URI::HTTPS.build(host: host, path: path).to_s |
88 | 86 | end |
89 | 87 |
|
90 | 88 | def parse_well_known body |
91 | | - data = JSON.parse(body) |
| 89 | + data = JSON.parse body |
92 | 90 | links = data['links'] |
93 | 91 |
|
94 | 92 | raise DiscoveryError, 'No links found in well-known document' unless links |
95 | | - raise DiscoveryError, 'Links must be an array' unless links.is_a?(Array) |
| 93 | + raise DiscoveryError, 'Links must be an array' unless links.is_a? Array |
96 | 94 |
|
97 | 95 | links |
98 | 96 | rescue JSON::ParserError => e |
|
0 commit comments