XE의 FileHandler::getRemoteFile을 PEAR::HTTPReuqest를 이용하도록 수정.

FileHandler::getRemoteResource를 FileHandler::getRemoteFile 에서 다시 사용하도록 하고 config.inc.php에서 __PROXY_SERVER__ 라는 상수를 통해 특정 서버를 거쳐서 외부 요청을 하는 기능 추가


git-svn-id: http://xe-core.googlecode.com/svn/sandbox@6037 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
zero 2009-04-07 02:43:14 +00:00
parent 1ce3a67992
commit a1f49ed545
4 changed files with 69 additions and 75 deletions

View file

@ -266,51 +266,60 @@
return sprintf("%0.2fMB",$size / (1024*1024));
}
/**
* @brief 원격파일을 다운받아 return
**/
function getRemoteResource($url, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array()) {
set_include_path(_XE_PATH_."libs/PEAR");
require_once('PEAR.php');
require_once('HTTP/Request.php');
if(__PROXY_SERVER__!==null) {
$oRequest = new HTTP_Request(__PROXY_SERVER__);
$oRequest->setMethod('POST');
$oRequest->_timeout = $timeout;
$oRequest->addPostData('arg', serialize(array('Destination'=>$url, 'method'=>$method, 'body'=>$body, 'content_type'=>$content_type, "headers"=>$headers)));
} else {
$oRequest = new HTTP_Request($url);
if(!$content_type) $oRequest->addHeader('Content-Type', 'text/html');
else $oRequest->addHeader('Content-Type', $content_type);
if(count($headers)) {
foreach($headers as $key => $val) {
$oRequest->addHeader($key, $val);
}
}
$oRequest->setMethod($method);
if($body) $oRequest->setBody($body);
$oRequest->_timeout = $timeout;
}
$oResponse = $oRequest->sendRequest();
if(PEAR::isError($oResponse)) return;
$code = $oRequest->getResponseCode();
$header = $oRequest->getResponseHeader();
$body = $oRequest->getResponseBody();
if($code == 301) {
$url = $header['location'];
if($url) return FileHandler::getRemoteResource($url, $body, $timeout, $method, $content_type, $headers);
else return;
}
if($code != 200) return;
return $body;
}
/**
* @brief 원격파일을 다운받아서 특정 위치에 저장
**/
function getRemoteFile($url, $target_filename) {
function getRemoteFile($url, $target_filename, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array()) {
$body = FileHandler::getRemoteResource($url, $body, $timeout, $method, $content_type, $headers);
if(!$body) return;
$target_filename = FileHandler::getRealPath($target_filename);
$url_info = parse_url($url);
if(!$url_info['port']) $url_info['port'] = 80;
if(!$url_info['path']) $url_info['path'] = '/';
$fp = @fsockopen($url_info['host'], $url_info['port']);
if(!$fp) return;
// 한글 파일이 있으면 한글파일 부분만 urlencode하여 처리 (iconv 필수)
/*
$path = $url_info['path'];
if(preg_match('/[\xEA-\xED][\x80-\xFF]{2}/', $path)&&function_exists('iconv')) {
$path_list = explode('/',$path);
$cnt = count($path_list);
$filename = $path_list[$cnt-1];
$filename = urlencode(iconv("UTF-8","EUC-KR",$filename));
$path_list[$cnt-1] = $filename;
$path = implode('/',$path_list);
$url_info['path'] = $path;
}
*/
$header = sprintf("GET %s%s HTTP/1.0\r\nHost: %s\r\nAccept-Charset: utf-8;q=0.7,*;q=0.7\r\nReferer: %s://%s\r\nRequestUrl: %s\r\nConnection: Close\r\n\r\n", $url_info['path'], $url_info['query']?'?'.$url_info['query']:'', $url_info['host'], $url_info['scheme'], $url_info['host'], Context::getRequestUri());
@fwrite($fp, $header);
$ft = @fopen($target_filename, 'w');
if(!$ft) return;
$begin = false;
while(!feof($fp)) {
$str = fgets($fp, 1024);
if($begin) @fwrite($ft, $str);
if(!trim($str)) $begin = true;
}
@fclose($ft);
@fclose($fp);
@chmod($target_filename, 0644);
FileHandler::writeFile($target_filename, $body);
return true;
}

View file

@ -273,7 +273,7 @@
}
$output = $widget_content_header . $widget_content_body . $widget_content_footer;
//if($widget=='widgetBox')debugPrint($output);
// 위젯 결과물 생성 시간을 debug 정보에 추가
if(__DEBUG__==3) $GLOBALS['__widget_excute_elapsed__'] += getMicroTime() - $start;
// 결과 return

View file

@ -31,6 +31,8 @@
* define('__DEBUG_DB_OUTPUT__', 0);
* define('__LOG_SLOW_QUERY__', 0);
* define('__OB_GZHANDLER_ENABLE__', 1);
*
* define('__PROXY_SERVER__', 'http://domain:port/path');
* ?>
*/
if(file_exists(_XE_PATH_.'config/config.user.inc.php')) {
@ -85,6 +87,12 @@
**/
if(!defined('__OB_GZHANDLER_ENABLE__')) define('__OB_GZHANDLER_ENABLE__', 1);
/**
* @brief __PROXY_SERVER__ 대상 서버를 거쳐서 외부 요청을 하도록 하는 서버의 정보를 가지고 있음
* FileHandler::getRemoteResource 에서 상수를 사용함
**/
if(!defined('__PROXY_SERVER__')) define('__PROXY_SERVER__', null);
/**
* @brief Firebug 콘솔 출력 사용시 관련 파일 require
**/

View file

@ -3,11 +3,9 @@
var $user_id = null;
var $user_key = null;
var $api_url = 'http://me2day.net:80';
var $application_key = '537a368d9049d9e86b2b169d75a2a4c3';
var $api_host = 'me2day.net';
var $api_port = 80;
function me2api($user_id, $user_key) {
$this->user_id = $user_id;
$this->user_key = $user_key;
@ -29,9 +27,7 @@
}
function _getContent($method, $user_id = null, $params = null) {
$host = $this->api_host;
$path = $this->_getPath($method, $user_id);
$port = $this->api_port;
$url = $this->api_url.$this->_getPath($method, $user_id);
$auth = base64_encode($this->user_id.':'.$this->_getAuthKey());
$arr_content = array();
@ -39,35 +35,16 @@
foreach($params as $key => $val) {
$arr_content[] = sprintf('%s=%s', $key, urlencode($val));
}
$content = implode('&',$arr_content);
$body = implode('&',$arr_content);
}
$header = sprintf(
"POST %s HTTP/1.0\r\n".
"Host: %s\r\n".
"me2_application_key: %s\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Authorization: Basic %s\r\n".
"Content-Length: %d\r\n\r\n",
$path,
$host,
$this->application_key,
$auth,
strlen($content)
);
if($content) $header.=$content."\r\n\r\n";
$fp = fsockopen($host, $port);
if(!$fp) return null;
fwrite($fp, $header);
$started = false;
while(!feof($fp)) {
$str = fgets($fp, 1024);
if(!trim($str)) $started = true;
if($started) $buff .= $str;
}
fclose($fp);
return trim($buff);
$buff = FileHandler::getRemoteResource($url, $body, 3, 'POST', 'application/x-www-form-urlencoded',
array(
'me2_application_key'=>$this->application_key,
'Authorization'=>'Basic '.$auth,
)
);
return $buff;
}
function chkNoop() {