Browse Source

convert hostnames to lowercase

pull/39/head
Aaron Parecki 8 years ago
parent
commit
1aa2f01d94
2 changed files with 35 additions and 2 deletions
  1. +19
    -2
      lib/helpers.php
  2. +16
    -0
      tests/HelpersTest.php

+ 19
- 2
lib/helpers.php View File

@ -5,9 +5,26 @@ function view($template, $data=[]) {
return $templates->render($template, $data);
}
// Adds slash if no path is in the URL
// Adds slash if no path is in the URL, and convert hostname to lowercase
function normalize_url($url) {
return parse_url($url, PHP_URL_PATH) == '' ? $url.'/' : $url;
$parts = parse_url($url);
if(empty($parts['path']))
$parts['path'] = '/';
$parts['host'] = strtolower($parts['host']);
return build_url($parts);
}
function build_url($parsed_url) {
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "$scheme$user$pass$host$port$path$query$fragment";
}
function should_follow_redirects($url) {

+ 16
- 0
tests/HelpersTest.php View File

@ -0,0 +1,16 @@
<?php
class HelpersTest extends PHPUnit_Framework_TestCase {
public function testLowercaseHostname() {
$url = 'http://Example.com/';
$result = normalize_url($url);
$this->assertEquals('http://example.com/', $result);
}
public function testAddsSlashToBareDomain() {
$url = 'http://example.com';
$result = normalize_url($url);
$this->assertEquals('http://example.com/', $result);
}
}

Loading…
Cancel
Save