svnno****@sourc*****
svnno****@sourc*****
2010年 6月 22日 (火) 11:13:29 JST
Revision: 108 http://sourceforge.jp/projects/frameworkspider/svn/view?view=rev&revision=108 Author: m_nakashima Date: 2010-06-22 11:13:29 +0900 (Tue, 22 Jun 2010) Log Message: ----------- Modified Paths: -------------- current/spider/lib/spider/BuildInformation.class.php current/spider/lib/util/HttpRequest.class.php -------------- next part -------------- Modified: current/spider/lib/spider/BuildInformation.class.php =================================================================== --- current/spider/lib/spider/BuildInformation.class.php 2010-06-22 02:09:58 UTC (rev 107) +++ current/spider/lib/spider/BuildInformation.class.php 2010-06-22 02:13:29 UTC (rev 108) @@ -97,9 +97,10 @@ $virtual_root = APPLICATION_BASE_PATH; if( preg_match('/^[a-zA-Z]\\:/', APPLICATION_BASE_PATH, $regmatch_array ) ) { // Windows対策ドライブをディレクトリとして変換 - $drive_name = substr($regmatch_array[0],0,1); - $virtual_root = preg_replace('/^[a-zA-Z]\\:/' - ,$drive_name.DIRECTORY_SEPARATOR,$virtual_root ); + $driveName = substr($regmatch_array[0],0,1); + $virtual_root = str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, + DIRECTORY_SEPARATOR,preg_replace('/^[a-zA-Z]\\:/' + ,$driveName.DIRECTORY_SEPARATOR,$virtual_root )); } else { $virtual_root = substr($virtual_root,1); } @@ -109,8 +110,9 @@ if( preg_match('/^[a-zA-Z]\\:/', APPLICATION_BASE_PATH, $regmatch_array ) > 0 ) { // Windows対策ドライブをディレクトリとして変換 $driveName = substr($regmatch_array[0],0,1); - $cutPath = preg_replace('/^[a-zA-Z]\\:/' - ,$driveName.DIRECTORY_SEPARATOR, $cutPath ); + $cutPath = str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, + DIRECTORY_SEPARATOR,preg_replace('/^[a-zA-Z]\\:/' + ,$driveName.DIRECTORY_SEPARATOR, $cutPath )); } $virtual_root = preg_replace('/^'.util_CharUtility::escapeRegxStr($cutPath).'/','',$virtual_root); } Modified: current/spider/lib/util/HttpRequest.class.php =================================================================== --- current/spider/lib/util/HttpRequest.class.php 2010-06-22 02:09:58 UTC (rev 107) +++ current/spider/lib/util/HttpRequest.class.php 2010-06-22 02:13:29 UTC (rev 108) @@ -23,7 +23,8 @@ var $requestMethod = 'get'; /** リクエストヘッダハッシュ */ var $requestHeaders = array(); - + /** POSTリクエストデータハッシュ */ + var $postDataHash = array(); /** レスポンスステータスコード */ var $statusCode = 0; /** レスポンスステータスメッセージ */ @@ -69,6 +70,8 @@ } } } + // POST情報をクリア + $this->clearPostData(); } /** * リクエストを送信して結果を取得します @@ -92,13 +95,11 @@ $this->requestUri = '/'.implode('/',$urlElementArray); $this->requestHostName = $fqdn; $this->requestPort = 80; - $connectTarget = $this->requestHostName; // ホスト名整理 if( strpos( $this->requestHostName, ':' ) !== false ) { list( $this->requestHostName, $this->requestPort ) = explode(':',$this->requestHostName); } else if( strtolower($this->requestProtocol) == 'https' ) { $this->requestPort = 443; - $connectTarget = 'ssl://'.$this->requestHostName; } // ホスト存在確認 $address = gethostbyname($this->requestHostName); @@ -106,28 +107,96 @@ $this->errorMessage = 'Target Host is not found!'; return false; } - // リクエスト文字列作成 - $requestStrings = $this->_createRequestStrings( $contents ); - // ソケット通信 - if( $socket = @fsockopen( $address, $this->requestPort, $this->errorNumber, $this->errorMessage, $timeout ) ){ - if( @socket_set_timeout( $socket, $timeout ) ) { - // リクエスト送信 - fwrite( $socket, $requestStrings ); - // レスポンス文字列受信 - $responseText = ''; - while (!feof($socket)) { - $responseText .= fgets($socket, 128); + // 接続先の判断:IPアドレスで接続する + $connectTarget = $address; + if( strtolower($this->requestProtocol) == 'https' ) { + $connectTarget = 'ssl://'.$address; + } + + // PEAR HTTP_Requestの存在確認 + @include_once('HTTP/Request.php'); + if( class_exists('HTTP_Request') ){ + // PEARが存在するなら優先 + $httpRequestOptions = array( + // タイムアウトの秒数指定 + 'timeout' => $timeout, + //"allowRedirects" => true, // リダイレクトの許可設定(true/false) + //"maxRedirects" => 3, // リダイレクトの最大回数 + ); + // httpリクエストオブジェクト作成 + $httpRequest = new HTTP_Request($this->requestUrl, $httpRequestOptions ); + if( 'post' == strtolower($this->requestMethod) ) { + $httpRequest->setMethod(HTTP_REQUEST_METHOD_POST); + } else { + $httpRequest->setMethod(HTTP_REQUEST_METHOD_GET); + } + // headerパラメータ + if( is_array($this->requestHeaders) && count($this->requestHeaders) > 0 ) { + foreach( $this->requestHeaders as $key => $val ) { + $httpRequest->addHeader($key,$val); } - $this->_convertResponse( $responseText ); + } + // bodyコンテンツ + $contentArray = explode('&',$contents); + foreach( $contentArray as $content ) { + list( $key, $val ) = explode('=',$content); + $this->postDataHash[urldecode($key)] = urldecode($val); + } + if( is_array($this->postDataHash) && count($this->postDataHash) > 0 ) { + $httpRequest->addHeader('Content-Type','application/x-www-form-urlencoded'); + foreach( $this->postDataHash as $key => $val ) { + $httpRequest->addPostData($key, $val); + } + } + // リクエスト実行 + $httpResponse = $httpRequest->sendRequest(); + if (!PEAR::isError($httpResponse)) { + // 応答内容の解析 + $this->responseProtocol = $this->requestProtocol; + $this->statusCode = $httpRequest->getResponseCode(); + $this->statusMessage = $httpRequest->getResponseReason(); + $this->responseHeaders = $httpRequest->getResponseHeader(); + $this->responseBody = $httpRequest->getResponseBody(); + return true; } else { - $this->errorMessage = 'Can\'t set connection timeout!'; + // リクエストエラー + $this->errorMessage = $httpResponse->getMessage(); return false; } - @fclose($socket); } else { - return false; + // PEARが存在しない場合 + if( strtolower($this->requestProtocol) == 'https' ) { + // SSL接続の場合 + if( function_exists('get_loaded_extensions') && in_array('openssl', get_loaded_extensions())) { + } else { + // openssl拡張モジュールがないならエラー + $this->errorMessage = 'Requre PEAR:HTTP_Request or openssl extension!'; + return false; + } + } + // リクエスト文字列作成 + $requestStrings = $this->_createRequestStrings( $contents ); + // ソケット通信 + if( $socket = @fsockopen( $connectTarget, $this->requestPort, $this->errorNumber, $this->errorMessage, $timeout ) ){ + if( @socket_set_timeout( $socket, $timeout ) ) { + // リクエスト送信 + fwrite( $socket, $requestStrings ); + // レスポンス文字列受信 + $responseText = ''; + while (!feof($socket)) { + $responseText .= fgets($socket, 128); + } + $this->_convertResponse( $responseText ); + } else { + $this->errorMessage = 'Can\'t set connection timeout!'; + return false; + } + @fclose($socket); + } else { + return false; + } + return true; } - return true; } /** * リクエスト用文字列を作成して取得します @@ -149,6 +218,27 @@ } } $requestStrings .= "Connection: close\r\n"; + + // POSTリクエストデータがあるなら文字列に追加 + if( $this->requestMethod == 'post' && is_array($this->postDataHash) ) { + $addContentsArray = array(); + foreach( $this->postDataHash as $key => $value ) { + if( strlen($key) > 0 ) { + array_push($addContentsArray, urlencode($key).'='.urlencode($value) ); + } + } + if( count($addContentsArray) > 0 ) { + $str = implode('&',$addContentsArray); + if( strlen($contents) == 0 ) { + $contents = $str; + } else if( preg_match('/\\&$/',$contents) == 0 ) { + $contents .= '&'.$str; + } else { + $contents .= $str; + } + } + } + if( strlen($contents) > 0 ) { $requestStrings .= "Content-Type: application/x-www-form-urlencoded\r\n"; $requestStrings .= "Content-Length: ".strlen($contents)."\r\n\r\n"; @@ -156,6 +246,7 @@ } else { $requestStrings .= "\r\n"; } + $this->requestStrings = $requestStrings; return $requestStrings; } /** @@ -205,5 +296,32 @@ $this->responseBody = $responseBody; } } + /** + * POSTで送信するデータを追加・設定します。keyが同じ場合は値を上書きします + * @param $key POSTするキー文字列 + * @param $value POSTする値文字列 + */ + function addPostData( $key, $value ) { + if( strlen($key)>0 ) { + $this->postDataHash[$key] = $value; + } + } + /** + * 現在設定されるPOSTデータをキーを指定して取得します。 + * @param $key POSTするキー文字列 + */ + function getPostData( $key ) { + if( isset( $this->postDataHash[$key] ) ) { + return $this->postDataHash[$key]; + } else { + return null; + } + } + /** + * 現在設定されているPOSTデータをクリアします + */ + function clearPostData() { + $this->postDataHash = array(); + } } ?> \ No newline at end of file