本帖最后由 qbit 于 2013-5-27 13:02 编辑
系统升级后,发现有个远程ucenter应用测试老是通信失败,于是一步步调试查找原因,最后发现是因为ucenter中的一个函数dfopen有BUG,他老会自动带上80端口,而有些服务器碰上80端口的请求会自动302重定向到不带端口域名下,即从www.324324.cn:80重定向到www.324324.cn,这时远程调用请求就会返回302响应,造成通信失败。- GET /api/uc.php?code=0690yeurtGqQZ0lp62...... HTTP/1.0
- Accept: */*
- Accept-Language: zh-cn
- User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17
- Host: www.324324.cn:80
- Connection: Close
- Cookie:
复制代码 return data:- <br><br><html>
- <head><title>302 Found</title></head>
- <body bgcolor="white">
- <center><h1>302 Found</h1></center>
- <hr><center>nginx</center>
- </body>
- </html>
复制代码 uc_server/model/msic.php第62到93行,可以看到,默认都加了80端口。- function dfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE , $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE') {
- //error_log("[uc_server]\r\nurl: $url\r\npost: $post\r\n\r\n", 3, 'c:/log/php_fopen.txt');
- $return = '';
- $matches = parse_url($url);
- $host = $matches['host'];
- $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
- $port = !empty($matches['port']) ? $matches['port'] : 80;
- if($post) {
- $out = "POST $path HTTP/1.0\r\n";
- $out .= "Accept: */*\r\n";
- //$out .= "Referer: $boardurl\r\n";
- $out .= "Accept-Language: zh-cn\r\n";
- $boundary = $encodetype == 'URLENCODE' ? '' : ';'.substr($post, 0, trim(strpos($post, "\n")));
- $out .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data$boundary\r\n";
- $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
- $out .= "Host: $host:$port\r\n";
- $out .= 'Content-Length: '.strlen($post)."\r\n";
- $out .= "Connection: Close\r\n";
- $out .= "Cache-Control: no-cache\r\n";
- $out .= "Cookie: $cookie\r\n\r\n";
- $out .= $post;
- } else {
- $out = "GET $path HTTP/1.0\r\n";
- $out .= "Accept: */*\r\n";
- //$out .= "Referer: $boardurl\r\n";
- $out .= "Accept-Language: zh-cn\r\n";
- $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
- $out .= "Host: $host:$port\r\n";
- $out .= "Connection: Close\r\n";
- $out .= "Cookie: $cookie\r\n\r\n";
- }
复制代码 我把其中第78行和第90行的代码:- $out .= "Host: $host:$port\r\n";
复制代码 改为如下:- $out .= "Host: $host".($port==80?"":":$port")."\r\n";
复制代码 就正常了。。。
|