Skip to content

Commit

Permalink
Merge pull request abcdlzy#6 from PandaShare/develop
Browse files Browse the repository at this point in the history
修改了 fix_request_url 的逻辑
  • Loading branch information
abcdlzy authored Apr 14, 2018
2 parents e6f4f38 + 1f0869b commit b59fc2c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
18 changes: 16 additions & 2 deletions phpproxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,23 @@ public static function hook($url,$response) {
return $response;
}

/**
* update : 2018-04-14 12:36:34
* 改了一下 fix_request_url 的逻辑, 测试参考 test/url_format.php
* 使用必须要求 url 是由 http/https 开头
*
* @$url 输入参数
* @return 如果格式化成功返回 http[s]://prefix.domainname.org/req/page.html
*/
public static function fix_request_url($url){
$url=preg_replace('/http:\/\/\//i','http://',$url);
$url=preg_replace('/https:\/\/\//i','https://',$url);
// $url=preg_replace('/http:\/\/\//i','http://',$url);
// $url=preg_replace('/https:\/\/\//i','https://',$url);
// return $url;
if (empty($url)) return '';
$url_detail = array();
$url = urldecode($url);
if (!preg_match('/^(https|http):\/{0,}(.*)$/', $url, $url_detail) || count($url_detail) != 3) return '';
$url = $url_detail[1] . '://' . preg_replace('/\/{2,}/', '/', $url_detail[2]);
return $url;
}

Expand Down
39 changes: 39 additions & 0 deletions test/url_format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

$list = array(
'http://normal.com',
'https://normals.com',
'http:////a.com/',
'https:////a.com////',
'http:/a.com//////',
'http://normal.com/test/page.html',
'https://normals.com/test/page.html?req=hello',
'http:////a.com/test/123/page/__f.aspx?req=test',
'https:////a.com////test/123/page/__f.aspx?req=test',
'http:/a.com//////test/123/page/__f.aspx?req=test',
);

function formatURL($url) {
if (empty($url)) return '';
$url_detail = array();
$url = urldecode($url);
if (!preg_match('/^(https|http):\/{0,}(.*)$/', $url, $url_detail) || count($url_detail) != 3) return '';
$url = $url_detail[1] . '://' . preg_replace('/\/{2,}/', '/', $url_detail[2]);
return $url;
}

foreach ($list as $val) {
foreach ($list as $val) {
echo
'origin : ' . $val . '<br />' .
'new : ' . formatURL($val) . '<br />';

$encode = urlencode($val);
echo
'origin : ' . $encode . '<br />' .
'new : ' . formatURL($encode) . '<br />';

echo '<br /><br /><br /><br /><br /><br />';
}
}
?>

0 comments on commit b59fc2c

Please sign in to comment.