Adding thrift as a local library with TLS support enabled
Project: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/repo Commit: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/commit/89236650 Tree: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/tree/89236650 Diff: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/diff/89236650 Branch: refs/heads/master Commit: 8923665031044afe99d9ea31bd86fbcc7d97fb23 Parents: f4514f2 Author: Supun Nakandala <[email protected]> Authored: Sat Sep 5 17:19:46 2015 +0530 Committer: Supun Nakandala <[email protected]> Committed: Sat Sep 5 17:19:46 2015 +0530 ---------------------------------------------------------------------- app/config/pga_config.php.template | 14 +- app/controllers/ExperimentController.php | 2 +- app/libraries/AdminUtilities.php | 2 +- .../Airavata/AiravataServiceProvider.php | 14 + app/libraries/ExperimentUtilities.php | 6 +- app/libraries/Thrift/Base/TBase.php | 367 ++++++++++ .../Thrift/ClassLoader/ThriftClassLoader.php | 223 ++++++ .../Thrift/Exception/TApplicationException.php | 72 ++ app/libraries/Thrift/Exception/TException.php | 369 ++++++++++ .../Thrift/Exception/TProtocolException.php | 50 ++ .../Thrift/Exception/TTransportException.php | 41 ++ .../Thrift/Factory/TBinaryProtocolFactory.php | 43 ++ .../Thrift/Factory/TCompactProtocolFactory.php | 39 ++ .../Thrift/Factory/TJSONProtocolFactory.php | 41 ++ .../Thrift/Factory/TProtocolFactory.php | 35 + .../Thrift/Factory/TStringFuncFactory.php | 63 ++ .../Thrift/Factory/TTransportFactory.php | 16 + .../Thrift/Protocol/JSON/BaseContext.php | 39 ++ .../Thrift/Protocol/JSON/ListContext.php | 52 ++ .../Thrift/Protocol/JSON/LookaheadReader.php | 54 ++ .../Thrift/Protocol/JSON/PairContext.php | 60 ++ .../Thrift/Protocol/TBinaryProtocol.php | 396 +++++++++++ .../Protocol/TBinaryProtocolAccelerated.php | 62 ++ .../Thrift/Protocol/TCompactProtocol.php | 669 ++++++++++++++++++ app/libraries/Thrift/Protocol/TJSONProtocol.php | 694 +++++++++++++++++++ .../Thrift/Protocol/TMultiplexedProtocol.php | 86 +++ app/libraries/Thrift/Protocol/TProtocol.php | 340 +++++++++ .../Thrift/Protocol/TProtocolDecorator.php | 284 ++++++++ .../Thrift/Serializer/TBinarySerializer.php | 79 +++ app/libraries/Thrift/Server/TForkingServer.php | 119 ++++ app/libraries/Thrift/Server/TServer.php | 101 +++ app/libraries/Thrift/Server/TServerSocket.php | 98 +++ .../Thrift/Server/TServerTransport.php | 54 ++ app/libraries/Thrift/Server/TSimpleServer.php | 57 ++ app/libraries/Thrift/StringFunc/Core.php | 38 + app/libraries/Thrift/StringFunc/Mbstring.php | 45 ++ app/libraries/Thrift/StringFunc/TStringFunc.php | 27 + app/libraries/Thrift/TMultiplexedProcessor.php | 136 ++++ .../Thrift/Transport/TBufferedTransport.php | 170 +++++ app/libraries/Thrift/Transport/TCurlClient.php | 222 ++++++ .../Thrift/Transport/TFramedTransport.php | 183 +++++ app/libraries/Thrift/Transport/THttpClient.php | 221 ++++++ .../Thrift/Transport/TMemoryBuffer.php | 89 +++ .../Thrift/Transport/TNullTransport.php | 50 ++ app/libraries/Thrift/Transport/TPhpStream.php | 114 +++ app/libraries/Thrift/Transport/TSocket.php | 328 +++++++++ app/libraries/Thrift/Transport/TSocketPool.php | 295 ++++++++ app/libraries/Thrift/Transport/TTransport.php | 93 +++ app/libraries/Thrift/Type/TConstant.php | 51 ++ app/libraries/Thrift/Type/TMessageType.php | 33 + app/libraries/Thrift/Type/TType.php | 46 ++ composer.json | 3 +- 52 files changed, 6772 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/config/pga_config.php.template ---------------------------------------------------------------------- diff --git a/app/config/pga_config.php.template b/app/config/pga_config.php.template index ae0ef43..ee84f13 100644 --- a/app/config/pga_config.php.template +++ b/app/config/pga_config.php.template @@ -69,14 +69,14 @@ return array( 'service-url' => 'https://idp.scigap.org:7443/', /** - * Path to the server certificate file + * Enable HTTPS server verification */ - 'cafile-path' => app_path() . '/resources/security/idp_scigap_org.pem', + 'verify-peer' => true, /** - * Enable HTTPS server verification + * Path to the server certificate file */ - 'verify-peer' => true, + 'cafile-path' => app_path() . '/resources/security/idp_scigap_org.pem', /** * Allow self signed server certificates @@ -91,10 +91,12 @@ return array( * ***************************************************************** */ 'airavata' => [ + /** - * Airavata API server location + * Airavata API server location. Use tls:// as the protocol to + * connect TLS enabled Airavata */ - 'airavata-server' => 'gw111.iu.xsede.org', + 'airavata-server' => 'gw56.iu.xsede.org', /** * Airavata API server port http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/controllers/ExperimentController.php ---------------------------------------------------------------------- diff --git a/app/controllers/ExperimentController.php b/app/controllers/ExperimentController.php index 85ce991..5627624 100755 --- a/app/controllers/ExperimentController.php +++ b/app/controllers/ExperimentController.php @@ -7,7 +7,7 @@ class ExperimentController extends BaseController * Limit used in fetching paginated results * @var int */ - var $limit = 10; + var $limit = 20; /** * Instantiate a new ExperimentController Instance http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/AdminUtilities.php ---------------------------------------------------------------------- diff --git a/app/libraries/AdminUtilities.php b/app/libraries/AdminUtilities.php index 1e99090..3313b6f 100644 --- a/app/libraries/AdminUtilities.php +++ b/app/libraries/AdminUtilities.php @@ -61,7 +61,7 @@ class AdminUtilities $expContainer = array(); $expNum = 0; foreach ($experiments as $experiment) { - $expValue = ExperimentUtilities::get_experiment_summary_values($experiment, ProjectUtilities::get_project($experiment->projectId), true); + $expValue = ExperimentUtilities::get_experiment_summary_values($experiment, true); $expContainer[$expNum]['experiment'] = $experiment; $expValue["editable"] = false; $expContainer[$expNum]['expValue'] = $expValue; http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Airavata/AiravataServiceProvider.php ---------------------------------------------------------------------- diff --git a/app/libraries/Airavata/AiravataServiceProvider.php b/app/libraries/Airavata/AiravataServiceProvider.php index 53a8c39..3bcd120 100755 --- a/app/libraries/Airavata/AiravataServiceProvider.php +++ b/app/libraries/Airavata/AiravataServiceProvider.php @@ -38,6 +38,20 @@ class AiravataServiceProvider extends ServiceProvider { $this->app['airavata'] = $this->app->share(function($app) { try{ +// $options = array(); +// if(Config::get('pga_config.airavata')['verify-peer']){ +// $options = array( +// 'verify-peer'=>Config::get('pga_config.airavata')['verify-peer'], +// 'certfile'=>Config::get('pga_config.airavata')['cafile-path'], +// 'selfsign'=> Config::get('pga_config.airavata')['allow-self-signed-cert'] +// ); +// } +// +// $transport = new TSSLSocket( +// Config::get('pga_config.airavata')['airavata-server'], +// Config::get('pga_config.airavata')['airavata-port'], +// $options +// ); $transport = new TSocket( Config::get('pga_config.airavata')['airavata-server'], Config::get('pga_config.airavata')['airavata-port'] http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/ExperimentUtilities.php ---------------------------------------------------------------------- diff --git a/app/libraries/ExperimentUtilities.php b/app/libraries/ExperimentUtilities.php index 499b78f..83f9141 100644 --- a/app/libraries/ExperimentUtilities.php +++ b/app/libraries/ExperimentUtilities.php @@ -620,7 +620,7 @@ class ExperimentUtilities echo "Experiment hasn't completed. Experiment Status is : " . $expStatus; } - public static function get_experiment_summary_values($experimentSummary, $project, $forSearch = false) + public static function get_experiment_summary_values($experimentSummary, $forSearch = false) { // var_dump( $experimentSummary); exit; $expVal = array(); @@ -840,7 +840,7 @@ class ExperimentUtilities $expContainer = array(); $expNum = 0; foreach ($experiments as $experiment) { - $expValue = ExperimentUtilities::get_experiment_summary_values($experiment, ProjectUtilities::get_project($experiment->projectId), true); + $expValue = ExperimentUtilities::get_experiment_summary_values($experiment, true); $expContainer[$expNum]['experiment'] = $experiment; if ($expValue["experimentStatusString"] == "FAILED") $expValue["editable"] = false; @@ -943,7 +943,7 @@ class ExperimentUtilities $expContainer = array(); $expNum = 0; foreach ($experiments as $experiment) { - $expValue = ExperimentUtilities::get_experiment_summary_values($experiment, ProjectUtilities::get_project($experiment->projectId), true); + $expValue = ExperimentUtilities::get_experiment_summary_values($experiment, true); $expContainer[$expNum]['experiment'] = $experiment; if ($expValue["experimentStatusString"] == "FAILED") $expValue["editable"] = false; http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Base/TBase.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Base/TBase.php b/app/libraries/Thrift/Base/TBase.php new file mode 100644 index 0000000..3d5b526 --- /dev/null +++ b/app/libraries/Thrift/Base/TBase.php @@ -0,0 +1,367 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift + */ + +namespace Thrift\Base; + +use Thrift\Type\TType; + +/** + * Base class from which other Thrift structs extend. This is so that we can + * cut back on the size of the generated code which is turning out to have a + * nontrivial cost just to load thanks to the wondrously abysmal implementation + * of PHP. Note that code is intentionally duplicated in here to avoid making + * function calls for every field or member of a container.. + */ +abstract class TBase { + + static $tmethod = array(TType::BOOL => 'Bool', + TType::BYTE => 'Byte', + TType::I16 => 'I16', + TType::I32 => 'I32', + TType::I64 => 'I64', + TType::DOUBLE => 'Double', + TType::STRING => 'String'); + + abstract function read($input); + + abstract function write($output); + + public function __construct($spec=null, $vals=null) { + if (is_array($spec) && is_array($vals)) { + foreach ($spec as $fid => $fspec) { + $var = $fspec['var']; + if (isset($vals[$var])) { + $this->$var = $vals[$var]; + } + } + } + } + + public function __wakeup() + { + $this->__construct(get_object_vars($this)); + } + + private function _readMap(&$var, $spec, $input) { + $xfer = 0; + $ktype = $spec['ktype']; + $vtype = $spec['vtype']; + $kread = $vread = null; + if (isset(TBase::$tmethod[$ktype])) { + $kread = 'read'.TBase::$tmethod[$ktype]; + } else { + $kspec = $spec['key']; + } + if (isset(TBase::$tmethod[$vtype])) { + $vread = 'read'.TBase::$tmethod[$vtype]; + } else { + $vspec = $spec['val']; + } + $var = array(); + $_ktype = $_vtype = $size = 0; + $xfer += $input->readMapBegin($_ktype, $_vtype, $size); + for ($i = 0; $i < $size; ++$i) { + $key = $val = null; + if ($kread !== null) { + $xfer += $input->$kread($key); + } else { + switch ($ktype) { + case TType::STRUCT: + $class = $kspec['class']; + $key = new $class(); + $xfer += $key->read($input); + break; + case TType::MAP: + $xfer += $this->_readMap($key, $kspec, $input); + break; + case TType::LST: + $xfer += $this->_readList($key, $kspec, $input, false); + break; + case TType::SET: + $xfer += $this->_readList($key, $kspec, $input, true); + break; + } + } + if ($vread !== null) { + $xfer += $input->$vread($val); + } else { + switch ($vtype) { + case TType::STRUCT: + $class = $vspec['class']; + $val = new $class(); + $xfer += $val->read($input); + break; + case TType::MAP: + $xfer += $this->_readMap($val, $vspec, $input); + break; + case TType::LST: + $xfer += $this->_readList($val, $vspec, $input, false); + break; + case TType::SET: + $xfer += $this->_readList($val, $vspec, $input, true); + break; + } + } + $var[$key] = $val; + } + $xfer += $input->readMapEnd(); + return $xfer; + } + + private function _readList(&$var, $spec, $input, $set=false) { + $xfer = 0; + $etype = $spec['etype']; + $eread = $vread = null; + if (isset(TBase::$tmethod[$etype])) { + $eread = 'read'.TBase::$tmethod[$etype]; + } else { + $espec = $spec['elem']; + } + $var = array(); + $_etype = $size = 0; + if ($set) { + $xfer += $input->readSetBegin($_etype, $size); + } else { + $xfer += $input->readListBegin($_etype, $size); + } + for ($i = 0; $i < $size; ++$i) { + $elem = null; + if ($eread !== null) { + $xfer += $input->$eread($elem); + } else { + $espec = $spec['elem']; + switch ($etype) { + case TType::STRUCT: + $class = $espec['class']; + $elem = new $class(); + $xfer += $elem->read($input); + break; + case TType::MAP: + $xfer += $this->_readMap($elem, $espec, $input); + break; + case TType::LST: + $xfer += $this->_readList($elem, $espec, $input, false); + break; + case TType::SET: + $xfer += $this->_readList($elem, $espec, $input, true); + break; + } + } + if ($set) { + $var[$elem] = true; + } else { + $var []= $elem; + } + } + if ($set) { + $xfer += $input->readSetEnd(); + } else { + $xfer += $input->readListEnd(); + } + return $xfer; + } + + protected function _read($class, $spec, $input) { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + if (isset($spec[$fid])) { + $fspec = $spec[$fid]; + $var = $fspec['var']; + if ($ftype == $fspec['type']) { + $xfer = 0; + if (isset(TBase::$tmethod[$ftype])) { + $func = 'read'.TBase::$tmethod[$ftype]; + $xfer += $input->$func($this->$var); + } else { + switch ($ftype) { + case TType::STRUCT: + $class = $fspec['class']; + $this->$var = new $class(); + $xfer += $this->$var->read($input); + break; + case TType::MAP: + $xfer += $this->_readMap($this->$var, $fspec, $input); + break; + case TType::LST: + $xfer += $this->_readList($this->$var, $fspec, $input, false); + break; + case TType::SET: + $xfer += $this->_readList($this->$var, $fspec, $input, true); + break; + } + } + } else { + $xfer += $input->skip($ftype); + } + } else { + $xfer += $input->skip($ftype); + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + private function _writeMap($var, $spec, $output) { + $xfer = 0; + $ktype = $spec['ktype']; + $vtype = $spec['vtype']; + $kwrite = $vwrite = null; + if (isset(TBase::$tmethod[$ktype])) { + $kwrite = 'write'.TBase::$tmethod[$ktype]; + } else { + $kspec = $spec['key']; + } + if (isset(TBase::$tmethod[$vtype])) { + $vwrite = 'write'.TBase::$tmethod[$vtype]; + } else { + $vspec = $spec['val']; + } + $xfer += $output->writeMapBegin($ktype, $vtype, count($var)); + foreach ($var as $key => $val) { + if (isset($kwrite)) { + $xfer += $output->$kwrite($key); + } else { + switch ($ktype) { + case TType::STRUCT: + $xfer += $key->write($output); + break; + case TType::MAP: + $xfer += $this->_writeMap($key, $kspec, $output); + break; + case TType::LST: + $xfer += $this->_writeList($key, $kspec, $output, false); + break; + case TType::SET: + $xfer += $this->_writeList($key, $kspec, $output, true); + break; + } + } + if (isset($vwrite)) { + $xfer += $output->$vwrite($val); + } else { + switch ($vtype) { + case TType::STRUCT: + $xfer += $val->write($output); + break; + case TType::MAP: + $xfer += $this->_writeMap($val, $vspec, $output); + break; + case TType::LST: + $xfer += $this->_writeList($val, $vspec, $output, false); + break; + case TType::SET: + $xfer += $this->_writeList($val, $vspec, $output, true); + break; + } + } + } + $xfer += $output->writeMapEnd(); + return $xfer; + } + + private function _writeList($var, $spec, $output, $set=false) { + $xfer = 0; + $etype = $spec['etype']; + $ewrite = null; + if (isset(TBase::$tmethod[$etype])) { + $ewrite = 'write'.TBase::$tmethod[$etype]; + } else { + $espec = $spec['elem']; + } + if ($set) { + $xfer += $output->writeSetBegin($etype, count($var)); + } else { + $xfer += $output->writeListBegin($etype, count($var)); + } + foreach ($var as $key => $val) { + $elem = $set ? $key : $val; + if (isset($ewrite)) { + $xfer += $output->$ewrite($elem); + } else { + switch ($etype) { + case TType::STRUCT: + $xfer += $elem->write($output); + break; + case TType::MAP: + $xfer += $this->_writeMap($elem, $espec, $output); + break; + case TType::LST: + $xfer += $this->_writeList($elem, $espec, $output, false); + break; + case TType::SET: + $xfer += $this->_writeList($elem, $espec, $output, true); + break; + } + } + } + if ($set) { + $xfer += $output->writeSetEnd(); + } else { + $xfer += $output->writeListEnd(); + } + return $xfer; + } + + protected function _write($class, $spec, $output) { + $xfer = 0; + $xfer += $output->writeStructBegin($class); + foreach ($spec as $fid => $fspec) { + $var = $fspec['var']; + if ($this->$var !== null) { + $ftype = $fspec['type']; + $xfer += $output->writeFieldBegin($var, $ftype, $fid); + if (isset(TBase::$tmethod[$ftype])) { + $func = 'write'.TBase::$tmethod[$ftype]; + $xfer += $output->$func($this->$var); + } else { + switch ($ftype) { + case TType::STRUCT: + $xfer += $this->$var->write($output); + break; + case TType::MAP: + $xfer += $this->_writeMap($this->$var, $fspec, $output); + break; + case TType::LST: + $xfer += $this->_writeList($this->$var, $fspec, $output, false); + break; + case TType::SET: + $xfer += $this->_writeList($this->$var, $fspec, $output, true); + break; + } + } + $xfer += $output->writeFieldEnd(); + } + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/ClassLoader/ThriftClassLoader.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/ClassLoader/ThriftClassLoader.php b/app/libraries/Thrift/ClassLoader/ThriftClassLoader.php new file mode 100644 index 0000000..bce93f5 --- /dev/null +++ b/app/libraries/Thrift/ClassLoader/ThriftClassLoader.php @@ -0,0 +1,223 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * ClassLoader to load Thrift library and definitions + * Inspired from UniversalClassLoader from Symfony 2 + * + * @package thrift.classloader + */ + +namespace Thrift\ClassLoader; + +class ThriftClassLoader +{ + /** + * Namespaces path + * @var array + */ + protected $namespaces = array(); + + /** + * Thrift definition paths + * @var type + */ + protected $definitions = array(); + + /** + * Do we use APC cache ? + * @var boolean + */ + protected $apc = false; + + /** + * APC Cache prefix + * @var string + */ + protected $apc_prefix; + + /** + * Set autoloader to use APC cache + * @param boolean $apc + * @param string $apc_prefix + */ + public function __construct($apc = false, $apc_prefix = null) + { + $this->apc = $apc; + $this->apc_prefix = $apc_prefix; + } + + /** + * Registers a namespace. + * + * @param string $namespace The namespace + * @param array|string $paths The location(s) of the namespace + */ + public function registerNamespace($namespace, $paths) + { + $this->namespaces[$namespace] = (array) $paths; + } + + /** + * Registers a Thrift definition namespace. + * + * @param string $namespace The definition namespace + * @param array|string $paths The location(s) of the definition namespace + */ + public function registerDefinition($namespace, $paths) + { + $this->definitions[$namespace] = (array) $paths; + } + + /** + * Registers this instance as an autoloader. + * + * @param Boolean $prepend Whether to prepend the autoloader or not + */ + public function register($prepend = false) + { + spl_autoload_register(array($this, 'loadClass'), true, $prepend); + } + + /** + * Loads the given class, definition or interface. + * + * @param string $class The name of the class + */ + public function loadClass($class) + { + if ( + (true === $this->apc && ($file = $this->findFileInApc($class))) or + ($file = $this->findFile($class)) + ) + { + require_once $file; + } + } + + /** + * Loads the given class or interface in APC. + * @param string $class The name of the class + * @return string + */ + protected function findFileInApc($class) + { + if (false === $file = apc_fetch($this->apc_prefix.$class)) { + apc_store($this->apc_prefix.$class, $file = $this->findFile($class)); + } + + return $file; + } + + /** + * Find class in namespaces or definitions directories + * @param string $class + * @return string + */ + public function findFile($class) + { + // Remove first backslash + if ('\\' == $class[0]) + { + $class = substr($class, 1); + } + + if (false !== $pos = strrpos($class, '\\')) + { + // Namespaced class name + $namespace = substr($class, 0, $pos); + + // Iterate in normal namespaces + foreach ($this->namespaces as $ns => $dirs) + { + //Don't interfere with other autoloaders + if (0 !== strpos($namespace, $ns)) + { + continue; + } + + foreach ($dirs as $dir) + { + $className = substr($class, $pos + 1); + + $file = $dir.DIRECTORY_SEPARATOR. + str_replace('\\', DIRECTORY_SEPARATOR, $namespace). + DIRECTORY_SEPARATOR. + $className.'.php'; + + if (file_exists($file)) + { + return $file; + } + } + } + + // Iterate in Thrift namespaces + + // Remove first part of namespace + $m = explode('\\', $class); + + // Ignore wrong call + if(count($m) <= 1) + { + return; + } + + $class = array_pop($m); + $namespace = implode('\\', $m); + + foreach ($this->definitions as $ns => $dirs) + { + //Don't interfere with other autoloaders + if (0 !== strpos($namespace, $ns)) + { + continue; + } + + foreach ($dirs as $dir) + { + /** + * Available in service: Interface, Client, Processor, Rest + * And every service methods (_.+) + */ + if( + 0 === preg_match('#(.+)(if|client|processor|rest)$#i', $class, $n) and + 0 === preg_match('#(.+)_[a-z0-9]+_(args|result)$#i', $class, $n) + ) + { + $className = 'Types'; + } + else + { + $className = $n[1]; + } + + $file = $dir.DIRECTORY_SEPARATOR . + str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . + DIRECTORY_SEPARATOR . + $className . '.php'; + + if (file_exists($file)) + { + return $file; + } + } + } + } + } +} http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Exception/TApplicationException.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Exception/TApplicationException.php b/app/libraries/Thrift/Exception/TApplicationException.php new file mode 100644 index 0000000..9081973 --- /dev/null +++ b/app/libraries/Thrift/Exception/TApplicationException.php @@ -0,0 +1,72 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift + */ + +namespace Thrift\Exception; + +use Thrift\Exception\TException; +use Thrift\Type\TType; + +class TApplicationException extends TException { + static $_TSPEC = + array(1 => array('var' => 'message', + 'type' => TType::STRING), + 2 => array('var' => 'code', + 'type' => TType::I32)); + + const UNKNOWN = 0; + const UNKNOWN_METHOD = 1; + const INVALID_MESSAGE_TYPE = 2; + const WRONG_METHOD_NAME = 3; + const BAD_SEQUENCE_ID = 4; + const MISSING_RESULT = 5; + const INTERNAL_ERROR = 6; + const PROTOCOL_ERROR = 7; + const INVALID_TRANSFORM = 8; + const INVALID_PROTOCOL = 9; + const UNSUPPORTED_CLIENT_TYPE = 10; + + function __construct($message=null, $code=0) { + parent::__construct($message, $code); + } + + public function read($output) { + return $this->_read('TApplicationException', self::$_TSPEC, $output); + } + + public function write($output) { + $xfer = 0; + $xfer += $output->writeStructBegin('TApplicationException'); + if ($message = $this->getMessage()) { + $xfer += $output->writeFieldBegin('message', TType::STRING, 1); + $xfer += $output->writeString($message); + $xfer += $output->writeFieldEnd(); + } + if ($code = $this->getCode()) { + $xfer += $output->writeFieldBegin('type', TType::I32, 2); + $xfer += $output->writeI32($code); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Exception/TException.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Exception/TException.php b/app/libraries/Thrift/Exception/TException.php new file mode 100644 index 0000000..8e8cd28 --- /dev/null +++ b/app/libraries/Thrift/Exception/TException.php @@ -0,0 +1,369 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift + */ + +namespace Thrift\Exception; + +use Thrift\Type\TType; +use Thrift\Base\TBase; + +/** + * NOTE(mcslee): This currently contains a ton of duplicated code from TBase + * because we need to save CPU cycles and this is not yet in an extension. + * Ideally we'd multiply-inherit TException from both Exception and Base, but + * that's not possible in PHP and there are no modules either, so for now we + * apologetically take a trip to HackTown. + * + * Can be called with standard Exception constructor (message, code) or with + * Thrift Base object constructor (spec, vals). + * + * @param mixed $p1 Message (string) or type-spec (array) + * @param mixed $p2 Code (integer) or values (array) + */ +class TException extends \Exception { + function __construct($p1=null, $p2=0) { + if (is_array($p1) && is_array($p2)) { + $spec = $p1; + $vals = $p2; + foreach ($spec as $fid => $fspec) { + $var = $fspec['var']; + if (isset($vals[$var])) { + $this->$var = $vals[$var]; + } + } + } else { + parent::__construct($p1, $p2); + } + } + + static $tmethod = array(TType::BOOL => 'Bool', + TType::BYTE => 'Byte', + TType::I16 => 'I16', + TType::I32 => 'I32', + TType::I64 => 'I64', + TType::DOUBLE => 'Double', + TType::STRING => 'String'); + + private function _readMap(&$var, $spec, $input) { + $xfer = 0; + $ktype = $spec['ktype']; + $vtype = $spec['vtype']; + $kread = $vread = null; + if (isset(TBase::$tmethod[$ktype])) { + $kread = 'read'.TBase::$tmethod[$ktype]; + } else { + $kspec = $spec['key']; + } + if (isset(TBase::$tmethod[$vtype])) { + $vread = 'read'.TBase::$tmethod[$vtype]; + } else { + $vspec = $spec['val']; + } + $var = array(); + $_ktype = $_vtype = $size = 0; + $xfer += $input->readMapBegin($_ktype, $_vtype, $size); + for ($i = 0; $i < $size; ++$i) { + $key = $val = null; + if ($kread !== null) { + $xfer += $input->$kread($key); + } else { + switch ($ktype) { + case TType::STRUCT: + $class = $kspec['class']; + $key = new $class(); + $xfer += $key->read($input); + break; + case TType::MAP: + $xfer += $this->_readMap($key, $kspec, $input); + break; + case TType::LST: + $xfer += $this->_readList($key, $kspec, $input, false); + break; + case TType::SET: + $xfer += $this->_readList($key, $kspec, $input, true); + break; + } + } + if ($vread !== null) { + $xfer += $input->$vread($val); + } else { + switch ($vtype) { + case TType::STRUCT: + $class = $vspec['class']; + $val = new $class(); + $xfer += $val->read($input); + break; + case TType::MAP: + $xfer += $this->_readMap($val, $vspec, $input); + break; + case TType::LST: + $xfer += $this->_readList($val, $vspec, $input, false); + break; + case TType::SET: + $xfer += $this->_readList($val, $vspec, $input, true); + break; + } + } + $var[$key] = $val; + } + $xfer += $input->readMapEnd(); + return $xfer; + } + + private function _readList(&$var, $spec, $input, $set=false) { + $xfer = 0; + $etype = $spec['etype']; + $eread = $vread = null; + if (isset(TBase::$tmethod[$etype])) { + $eread = 'read'.TBase::$tmethod[$etype]; + } else { + $espec = $spec['elem']; + } + $var = array(); + $_etype = $size = 0; + if ($set) { + $xfer += $input->readSetBegin($_etype, $size); + } else { + $xfer += $input->readListBegin($_etype, $size); + } + for ($i = 0; $i < $size; ++$i) { + $elem = null; + if ($eread !== null) { + $xfer += $input->$eread($elem); + } else { + $espec = $spec['elem']; + switch ($etype) { + case TType::STRUCT: + $class = $espec['class']; + $elem = new $class(); + $xfer += $elem->read($input); + break; + case TType::MAP: + $xfer += $this->_readMap($elem, $espec, $input); + break; + case TType::LST: + $xfer += $this->_readList($elem, $espec, $input, false); + break; + case TType::SET: + $xfer += $this->_readList($elem, $espec, $input, true); + break; + } + } + if ($set) { + $var[$elem] = true; + } else { + $var []= $elem; + } + } + if ($set) { + $xfer += $input->readSetEnd(); + } else { + $xfer += $input->readListEnd(); + } + return $xfer; + } + + protected function _read($class, $spec, $input) { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + if (isset($spec[$fid])) { + $fspec = $spec[$fid]; + $var = $fspec['var']; + if ($ftype == $fspec['type']) { + $xfer = 0; + if (isset(TBase::$tmethod[$ftype])) { + $func = 'read'.TBase::$tmethod[$ftype]; + $xfer += $input->$func($this->$var); + } else { + switch ($ftype) { + case TType::STRUCT: + $class = $fspec['class']; + $this->$var = new $class(); + $xfer += $this->$var->read($input); + break; + case TType::MAP: + $xfer += $this->_readMap($this->$var, $fspec, $input); + break; + case TType::LST: + $xfer += $this->_readList($this->$var, $fspec, $input, false); + break; + case TType::SET: + $xfer += $this->_readList($this->$var, $fspec, $input, true); + break; + } + } + } else { + $xfer += $input->skip($ftype); + } + } else { + $xfer += $input->skip($ftype); + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + private function _writeMap($var, $spec, $output) { + $xfer = 0; + $ktype = $spec['ktype']; + $vtype = $spec['vtype']; + $kwrite = $vwrite = null; + if (isset(TBase::$tmethod[$ktype])) { + $kwrite = 'write'.TBase::$tmethod[$ktype]; + } else { + $kspec = $spec['key']; + } + if (isset(TBase::$tmethod[$vtype])) { + $vwrite = 'write'.TBase::$tmethod[$vtype]; + } else { + $vspec = $spec['val']; + } + $xfer += $output->writeMapBegin($ktype, $vtype, count($var)); + foreach ($var as $key => $val) { + if (isset($kwrite)) { + $xfer += $output->$kwrite($key); + } else { + switch ($ktype) { + case TType::STRUCT: + $xfer += $key->write($output); + break; + case TType::MAP: + $xfer += $this->_writeMap($key, $kspec, $output); + break; + case TType::LST: + $xfer += $this->_writeList($key, $kspec, $output, false); + break; + case TType::SET: + $xfer += $this->_writeList($key, $kspec, $output, true); + break; + } + } + if (isset($vwrite)) { + $xfer += $output->$vwrite($val); + } else { + switch ($vtype) { + case TType::STRUCT: + $xfer += $val->write($output); + break; + case TType::MAP: + $xfer += $this->_writeMap($val, $vspec, $output); + break; + case TType::LST: + $xfer += $this->_writeList($val, $vspec, $output, false); + break; + case TType::SET: + $xfer += $this->_writeList($val, $vspec, $output, true); + break; + } + } + } + $xfer += $output->writeMapEnd(); + return $xfer; + } + + private function _writeList($var, $spec, $output, $set=false) { + $xfer = 0; + $etype = $spec['etype']; + $ewrite = null; + if (isset(TBase::$tmethod[$etype])) { + $ewrite = 'write'.TBase::$tmethod[$etype]; + } else { + $espec = $spec['elem']; + } + if ($set) { + $xfer += $output->writeSetBegin($etype, count($var)); + } else { + $xfer += $output->writeListBegin($etype, count($var)); + } + foreach ($var as $key => $val) { + $elem = $set ? $key : $val; + if (isset($ewrite)) { + $xfer += $output->$ewrite($elem); + } else { + switch ($etype) { + case TType::STRUCT: + $xfer += $elem->write($output); + break; + case TType::MAP: + $xfer += $this->_writeMap($elem, $espec, $output); + break; + case TType::LST: + $xfer += $this->_writeList($elem, $espec, $output, false); + break; + case TType::SET: + $xfer += $this->_writeList($elem, $espec, $output, true); + break; + } + } + } + if ($set) { + $xfer += $output->writeSetEnd(); + } else { + $xfer += $output->writeListEnd(); + } + return $xfer; + } + + protected function _write($class, $spec, $output) { + $xfer = 0; + $xfer += $output->writeStructBegin($class); + foreach ($spec as $fid => $fspec) { + $var = $fspec['var']; + if ($this->$var !== null) { + $ftype = $fspec['type']; + $xfer += $output->writeFieldBegin($var, $ftype, $fid); + if (isset(TBase::$tmethod[$ftype])) { + $func = 'write'.TBase::$tmethod[$ftype]; + $xfer += $output->$func($this->$var); + } else { + switch ($ftype) { + case TType::STRUCT: + $xfer += $this->$var->write($output); + break; + case TType::MAP: + $xfer += $this->_writeMap($this->$var, $fspec, $output); + break; + case TType::LST: + $xfer += $this->_writeList($this->$var, $fspec, $output, false); + break; + case TType::SET: + $xfer += $this->_writeList($this->$var, $fspec, $output, true); + break; + } + } + $xfer += $output->writeFieldEnd(); + } + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } + +} http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Exception/TProtocolException.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Exception/TProtocolException.php b/app/libraries/Thrift/Exception/TProtocolException.php new file mode 100644 index 0000000..f2529f5 --- /dev/null +++ b/app/libraries/Thrift/Exception/TProtocolException.php @@ -0,0 +1,50 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + * @author: rmarin ([email protected]) + */ + +namespace Thrift\Exception; + +use Thrift\Exception\TException; + +/** + * Protocol module. Contains all the types and definitions needed to implement + * a protocol encoder/decoder. + * + * @package thrift.protocol + */ + +/** + * Protocol exceptions + */ +class TProtocolException extends TException { + const UNKNOWN = 0; + const INVALID_DATA = 1; + const NEGATIVE_SIZE = 2; + const SIZE_LIMIT = 3; + const BAD_VERSION = 4; + const NOT_IMPLEMENTED = 5; + const DEPTH_LIMIT = 6; + + function __construct($message=null, $code=0) { + parent::__construct($message, $code); + } +} http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Exception/TTransportException.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Exception/TTransportException.php b/app/libraries/Thrift/Exception/TTransportException.php new file mode 100644 index 0000000..f467eb9 --- /dev/null +++ b/app/libraries/Thrift/Exception/TTransportException.php @@ -0,0 +1,41 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.transport + */ + +namespace Thrift\Exception; + +use Thrift\Exception\TException; + +/** + * Transport exceptions + */ +class TTransportException extends TException { + + const UNKNOWN = 0; + const NOT_OPEN = 1; + const ALREADY_OPEN = 2; + const TIMED_OUT = 3; + const END_OF_FILE = 4; + + function __construct($message=null, $code=0) { + parent::__construct($message, $code); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Factory/TBinaryProtocolFactory.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Factory/TBinaryProtocolFactory.php b/app/libraries/Thrift/Factory/TBinaryProtocolFactory.php new file mode 100644 index 0000000..85da567 --- /dev/null +++ b/app/libraries/Thrift/Factory/TBinaryProtocolFactory.php @@ -0,0 +1,43 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + */ + +namespace Thrift\Factory; + +use Thrift\Factory\TProtocolFactory; +use Thrift\Protocol\TBinaryProtocol; + +/** + * Binary Protocol Factory + */ +class TBinaryProtocolFactory implements TProtocolFactory { + private $strictRead_ = false; + private $strictWrite_ = false; + + public function __construct($strictRead=false, $strictWrite=false) { + $this->strictRead_ = $strictRead; + $this->strictWrite_ = $strictWrite; + } + + public function getProtocol($trans) { + return new TBinaryProtocol($trans, $this->strictRead_, $this->strictWrite_); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Factory/TCompactProtocolFactory.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Factory/TCompactProtocolFactory.php b/app/libraries/Thrift/Factory/TCompactProtocolFactory.php new file mode 100644 index 0000000..9f972aa --- /dev/null +++ b/app/libraries/Thrift/Factory/TCompactProtocolFactory.php @@ -0,0 +1,39 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + */ + +namespace Thrift\Factory; + +use Thrift\Factory\TProtocolFactory; +use Thrift\Protocol\TCompactProtocol; + +/** + * Compact Protocol Factory + */ +class TCompactProtocolFactory implements TProtocolFactory { + + public function __construct() { + } + + public function getProtocol($trans) { + return new TCompactProtocol($trans); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Factory/TJSONProtocolFactory.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Factory/TJSONProtocolFactory.php b/app/libraries/Thrift/Factory/TJSONProtocolFactory.php new file mode 100644 index 0000000..27e4391 --- /dev/null +++ b/app/libraries/Thrift/Factory/TJSONProtocolFactory.php @@ -0,0 +1,41 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + */ + +namespace Thrift\Factory; + +use Thrift\Factory\TProtocolFactory; +use Thrift\Protocol\TJSONProtocol; + +/** + * JSON Protocol Factory + */ +class TJSONProtocolFactory implements TProtocolFactory +{ + public function __construct() + { + } + + public function getProtocol($trans) + { + return new TJSONProtocol($trans); + } +} http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Factory/TProtocolFactory.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Factory/TProtocolFactory.php b/app/libraries/Thrift/Factory/TProtocolFactory.php new file mode 100644 index 0000000..6b322eb --- /dev/null +++ b/app/libraries/Thrift/Factory/TProtocolFactory.php @@ -0,0 +1,35 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + */ + +namespace Thrift\Factory; + +/** + * Protocol factory creates protocol objects from transports + */ +interface TProtocolFactory { + /** + * Build a protocol from the base transport + * + * @return Thrift\Protocol\TProtocol protocol + */ + public function getProtocol($trans); +} http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Factory/TStringFuncFactory.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Factory/TStringFuncFactory.php b/app/libraries/Thrift/Factory/TStringFuncFactory.php new file mode 100644 index 0000000..edc3649 --- /dev/null +++ b/app/libraries/Thrift/Factory/TStringFuncFactory.php @@ -0,0 +1,63 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +namespace Thrift\Factory; + +use Thrift\StringFunc\Mbstring; +use Thrift\StringFunc\Core; + +class TStringFuncFactory { + private static $_instance; + + /** + * Get the Singleton instance of TStringFunc implementation that is + * compatible with the current system's mbstring.func_overload settings. + * + * @return TStringFunc + */ + public static function create() { + if(!self::$_instance) { + self::_setInstance(); + } + + return self::$_instance; + } + + private static function _setInstance() { + /** + * Cannot use str* functions for byte counting because multibyte + * characters will be read a single bytes. + * + * See: http://us.php.net/manual/en/mbstring.overload.php + */ + if(ini_get('mbstring.func_overload') & 2) { + self::$_instance = new Mbstring(); + } + /** + * mbstring is not installed or does not have function overloading + * of the str* functions enabled so use PHP core str* functions for + * byte counting. + */ + else { + self::$_instance = new Core(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Factory/TTransportFactory.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Factory/TTransportFactory.php b/app/libraries/Thrift/Factory/TTransportFactory.php new file mode 100644 index 0000000..f3ae123 --- /dev/null +++ b/app/libraries/Thrift/Factory/TTransportFactory.php @@ -0,0 +1,16 @@ +<?php + +namespace Thrift\Factory; + +use Thrift\Transport\TTransport; + +class TTransportFactory { + /** + * @static + * @param TTransport $transport + * @return TTransport + */ + public static function getTransport(TTransport $transport) { + return $transport; + } +} http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Protocol/JSON/BaseContext.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Protocol/JSON/BaseContext.php b/app/libraries/Thrift/Protocol/JSON/BaseContext.php new file mode 100644 index 0000000..e96e504 --- /dev/null +++ b/app/libraries/Thrift/Protocol/JSON/BaseContext.php @@ -0,0 +1,39 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + */ + +namespace Thrift\Protocol\JSON; + +class BaseContext +{ + function escapeNum() + { + return false; + } + + function write() + { + } + + function read() + { + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Protocol/JSON/ListContext.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Protocol/JSON/ListContext.php b/app/libraries/Thrift/Protocol/JSON/ListContext.php new file mode 100644 index 0000000..a2b75b1 --- /dev/null +++ b/app/libraries/Thrift/Protocol/JSON/ListContext.php @@ -0,0 +1,52 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + */ + +namespace Thrift\Protocol\JSON; + +use Thrift\Protocol\JSON\BaseContext; +use Thrift\Protocol\TJSONProtocol; + +class ListContext extends BaseContext +{ + private $first_ = true; + private $p_; + + public function __construct($p) { + $this->p_ = $p; + } + + public function write() { + if ($this->first_) { + $this->first_ = false; + } else { + $this->p_->getTransport()->write(TJSONProtocol::COMMA); + } + } + + public function read() { + if ($this->first_) { + $this->first_ = false; + } else { + $this->p_->readJSONSyntaxChar(TJSONProtocol::COMMA); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Protocol/JSON/LookaheadReader.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Protocol/JSON/LookaheadReader.php b/app/libraries/Thrift/Protocol/JSON/LookaheadReader.php new file mode 100644 index 0000000..128b5fc --- /dev/null +++ b/app/libraries/Thrift/Protocol/JSON/LookaheadReader.php @@ -0,0 +1,54 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + */ + +namespace Thrift\Protocol\JSON; + +class LookaheadReader +{ + private $hasData_ = false; + private $data_ = array(); + private $p_; + + public function __construct($p) + { + $this->p_ = $p; + } + + public function read() { + if ($this->hasData_) { + $this->hasData_ = false; + } else { + $this->data_ = $this->p_->getTransport()->readAll(1); + } + + return substr($this->data_, 0, 1); + } + + public function peek() { + if (!$this->hasData_) { + $this->data_ = $this->p_->getTransport()->readAll(1); + } + + $this->hasData_ = true; + return substr($this->data_, 0, 1); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Protocol/JSON/PairContext.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Protocol/JSON/PairContext.php b/app/libraries/Thrift/Protocol/JSON/PairContext.php new file mode 100644 index 0000000..1c87dd3 --- /dev/null +++ b/app/libraries/Thrift/Protocol/JSON/PairContext.php @@ -0,0 +1,60 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + */ + +namespace Thrift\Protocol\JSON; + +use Thrift\Protocol\JSON\BaseContext; +use Thrift\Protocol\TJSONProtocol; + +class PairContext extends BaseContext { + private $first_ = true; + private $colon_ = true; + private $p_ = null; + + public function __construct($p) { + $this->p_ = $p; + } + + public function write() { + if ($this->first_) { + $this->first_ = false; + $this->colon_ = true; + } else { + $this->p_->getTransport()->write($this->colon_ ? TJSONProtocol::COLON : TJSONProtocol::COMMA); + $this->colon_ = !$this->colon_; + } + } + + public function read() { + if ($this->first_) { + $this->first_ = false; + $this->colon_ = true; + } else { + $this->p_->readJSONSyntaxChar($this->colon_ ? TJSONProtocol::COLON : TJSONProtocol::COMMA); + $this->colon_ = !$this->colon_; + } + } + + public function escapeNum() { + return $this->colon_; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Protocol/TBinaryProtocol.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Protocol/TBinaryProtocol.php b/app/libraries/Thrift/Protocol/TBinaryProtocol.php new file mode 100644 index 0000000..b1fddac --- /dev/null +++ b/app/libraries/Thrift/Protocol/TBinaryProtocol.php @@ -0,0 +1,396 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + */ + +namespace Thrift\Protocol; + +use Thrift\Protocol\TProtocol; +use Thrift\Type\TType; +use Thrift\Exception\TProtocolException; +use Thrift\Factory\TStringFuncFactory; + +/** + * Binary implementation of the Thrift protocol. + * + */ +class TBinaryProtocol extends TProtocol { + + const VERSION_MASK = 0xffff0000; + const VERSION_1 = 0x80010000; + + protected $strictRead_ = false; + protected $strictWrite_ = true; + + public function __construct($trans, $strictRead=false, $strictWrite=true) { + parent::__construct($trans); + $this->strictRead_ = $strictRead; + $this->strictWrite_ = $strictWrite; + } + + public function writeMessageBegin($name, $type, $seqid) { + if ($this->strictWrite_) { + $version = self::VERSION_1 | $type; + return + $this->writeI32($version) + + $this->writeString($name) + + $this->writeI32($seqid); + } else { + return + $this->writeString($name) + + $this->writeByte($type) + + $this->writeI32($seqid); + } + } + + public function writeMessageEnd() { + return 0; + } + + public function writeStructBegin($name) { + return 0; + } + + public function writeStructEnd() { + return 0; + } + + public function writeFieldBegin($fieldName, $fieldType, $fieldId) { + return + $this->writeByte($fieldType) + + $this->writeI16($fieldId); + } + + public function writeFieldEnd() { + return 0; + } + + public function writeFieldStop() { + return + $this->writeByte(TType::STOP); + } + + public function writeMapBegin($keyType, $valType, $size) { + return + $this->writeByte($keyType) + + $this->writeByte($valType) + + $this->writeI32($size); + } + + public function writeMapEnd() { + return 0; + } + + public function writeListBegin($elemType, $size) { + return + $this->writeByte($elemType) + + $this->writeI32($size); + } + + public function writeListEnd() { + return 0; + } + + public function writeSetBegin($elemType, $size) { + return + $this->writeByte($elemType) + + $this->writeI32($size); + } + + public function writeSetEnd() { + return 0; + } + + public function writeBool($value) { + $data = pack('c', $value ? 1 : 0); + $this->trans_->write($data, 1); + return 1; + } + + public function writeByte($value) { + $data = pack('c', $value); + $this->trans_->write($data, 1); + return 1; + } + + public function writeI16($value) { + $data = pack('n', $value); + $this->trans_->write($data, 2); + return 2; + } + + public function writeI32($value) { + $data = pack('N', $value); + $this->trans_->write($data, 4); + return 4; + } + + public function writeI64($value) { + // If we are on a 32bit architecture we have to explicitly deal with + // 64-bit twos-complement arithmetic since PHP wants to treat all ints + // as signed and any int over 2^31 - 1 as a float + if (PHP_INT_SIZE == 4) { + $neg = $value < 0; + + if ($neg) { + $value *= -1; + } + + $hi = (int)($value / 4294967296); + $lo = (int)$value; + + if ($neg) { + $hi = ~$hi; + $lo = ~$lo; + if (($lo & (int)0xffffffff) == (int)0xffffffff) { + $lo = 0; + $hi++; + } else { + $lo++; + } + } + $data = pack('N2', $hi, $lo); + + } else { + $hi = $value >> 32; + $lo = $value & 0xFFFFFFFF; + $data = pack('N2', $hi, $lo); + } + + $this->trans_->write($data, 8); + return 8; + } + + public function writeDouble($value) { + $data = pack('d', $value); + $this->trans_->write(strrev($data), 8); + return 8; + } + + public function writeString($value) { + $len = TStringFuncFactory::create()->strlen($value); + $result = $this->writeI32($len); + if ($len) { + $this->trans_->write($value, $len); + } + return $result + $len; + } + + public function readMessageBegin(&$name, &$type, &$seqid) { + $result = $this->readI32($sz); + if ($sz < 0) { + $version = (int) ($sz & self::VERSION_MASK); + if ($version != (int) self::VERSION_1) { + throw new TProtocolException('Bad version identifier: '.$sz, TProtocolException::BAD_VERSION); + } + $type = $sz & 0x000000ff; + $result += + $this->readString($name) + + $this->readI32($seqid); + } else { + if ($this->strictRead_) { + throw new TProtocolException('No version identifier, old protocol client?', TProtocolException::BAD_VERSION); + } else { + // Handle pre-versioned input + $name = $this->trans_->readAll($sz); + $result += + $sz + + $this->readByte($type) + + $this->readI32($seqid); + } + } + return $result; + } + + public function readMessageEnd() { + return 0; + } + + public function readStructBegin(&$name) { + $name = ''; + return 0; + } + + public function readStructEnd() { + return 0; + } + + public function readFieldBegin(&$name, &$fieldType, &$fieldId) { + $result = $this->readByte($fieldType); + if ($fieldType == TType::STOP) { + $fieldId = 0; + return $result; + } + $result += $this->readI16($fieldId); + return $result; + } + + public function readFieldEnd() { + return 0; + } + + public function readMapBegin(&$keyType, &$valType, &$size) { + return + $this->readByte($keyType) + + $this->readByte($valType) + + $this->readI32($size); + } + + public function readMapEnd() { + return 0; + } + + public function readListBegin(&$elemType, &$size) { + return + $this->readByte($elemType) + + $this->readI32($size); + } + + public function readListEnd() { + return 0; + } + + public function readSetBegin(&$elemType, &$size) { + return + $this->readByte($elemType) + + $this->readI32($size); + } + + public function readSetEnd() { + return 0; + } + + public function readBool(&$value) { + $data = $this->trans_->readAll(1); + $arr = unpack('c', $data); + $value = $arr[1] == 1; + return 1; + } + + public function readByte(&$value) { + $data = $this->trans_->readAll(1); + $arr = unpack('c', $data); + $value = $arr[1]; + return 1; + } + + public function readI16(&$value) { + $data = $this->trans_->readAll(2); + $arr = unpack('n', $data); + $value = $arr[1]; + if ($value > 0x7fff) { + $value = 0 - (($value - 1) ^ 0xffff); + } + return 2; + } + + public function readI32(&$value) { + $data = $this->trans_->readAll(4); + $arr = unpack('N', $data); + $value = $arr[1]; + if ($value > 0x7fffffff) { + $value = 0 - (($value - 1) ^ 0xffffffff); + } + return 4; + } + + public function readI64(&$value) { + $data = $this->trans_->readAll(8); + + $arr = unpack('N2', $data); + + // If we are on a 32bit architecture we have to explicitly deal with + // 64-bit twos-complement arithmetic since PHP wants to treat all ints + // as signed and any int over 2^31 - 1 as a float + if (PHP_INT_SIZE == 4) { + + $hi = $arr[1]; + $lo = $arr[2]; + $isNeg = $hi < 0; + + // Check for a negative + if ($isNeg) { + $hi = ~$hi & (int)0xffffffff; + $lo = ~$lo & (int)0xffffffff; + + if ($lo == (int)0xffffffff) { + $hi++; + $lo = 0; + } else { + $lo++; + } + } + + // Force 32bit words in excess of 2G to pe positive - we deal wigh sign + // explicitly below + + if ($hi & (int)0x80000000) { + $hi &= (int)0x7fffffff; + $hi += 0x80000000; + } + + if ($lo & (int)0x80000000) { + $lo &= (int)0x7fffffff; + $lo += 0x80000000; + } + + $value = $hi * 4294967296 + $lo; + + if ($isNeg) { + $value = 0 - $value; + } + } else { + + // Upcast negatives in LSB bit + if ($arr[2] & 0x80000000) { + $arr[2] = $arr[2] & 0xffffffff; + } + + // Check for a negative + if ($arr[1] & 0x80000000) { + $arr[1] = $arr[1] & 0xffffffff; + $arr[1] = $arr[1] ^ 0xffffffff; + $arr[2] = $arr[2] ^ 0xffffffff; + $value = 0 - $arr[1]*4294967296 - $arr[2] - 1; + } else { + $value = $arr[1]*4294967296 + $arr[2]; + } + } + + return 8; + } + + public function readDouble(&$value) { + $data = strrev($this->trans_->readAll(8)); + $arr = unpack('d', $data); + $value = $arr[1]; + return 8; + } + + public function readString(&$value) { + $result = $this->readI32($len); + if ($len) { + $value = $this->trans_->readAll($len); + } else { + $value = ''; + } + return $result + $len; + } +} http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/89236650/app/libraries/Thrift/Protocol/TBinaryProtocolAccelerated.php ---------------------------------------------------------------------- diff --git a/app/libraries/Thrift/Protocol/TBinaryProtocolAccelerated.php b/app/libraries/Thrift/Protocol/TBinaryProtocolAccelerated.php new file mode 100644 index 0000000..7a40ce9 --- /dev/null +++ b/app/libraries/Thrift/Protocol/TBinaryProtocolAccelerated.php @@ -0,0 +1,62 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.protocol + */ + +namespace Thrift\Protocol; + +use Thrift\Protocol\TBinaryProtocol; +use Thrift\Transport\TBufferedTransport; + +/** + * Accelerated binary protocol: used in conjunction with the thrift_protocol + * extension for faster deserialization + */ +class TBinaryProtocolAccelerated extends TBinaryProtocol { + public function __construct($trans, $strictRead=false, $strictWrite=true) { + // If the transport doesn't implement putBack, wrap it in a + // TBufferedTransport (which does) + + // NOTE (t.heintz): This is very evil to do, because the TBufferedTransport may swallow bytes, which + // are then never written to the underlying transport. This happens precisely when a number of bytes + // less than the max buffer size (512 by default) is written to the transport and then flush() is NOT + // called. In that case the data stays in the writeBuffer of the transport, from where it can never be + // accessed again (for example through read()). + // + // Since the caller of this method does not know about the wrapping transport, this creates bugs which + // are very difficult to find. Hence the wrapping of a transport in a buffer should be left to the + // calling code. An interface could used to mandate the presence of the putBack() method in the transport. + // + // I am leaving this code in nonetheless, because there may be applications depending on this behavior. + // + // @see THRIFT-1579 + + if (!method_exists($trans, 'putBack')) { + $trans = new TBufferedTransport($trans); + } + parent::__construct($trans, $strictRead, $strictWrite); + } + public function isStrictRead() { + return $this->strictRead_; + } + public function isStrictWrite() { + return $this->strictWrite_; + } +}
