示例#1
0
 public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal, $simple = NULL)
 {
     if ($simple === NULL) {
         $simple = $this->simple;
     }
     $stream = new HproseStringStream(HproseTags::TagCall);
     $hproseWriter = $simple ? new HproseSimpleWriter($stream) : new HproseWriter($stream);
     $hproseWriter->writeString($functionName);
     if (count($arguments) > 0 || $byRef) {
         $hproseWriter->reset();
         $hproseWriter->writeList($arguments);
         if ($byRef) {
             $hproseWriter->writeBoolean(true);
         }
     }
     $stream->write(HproseTags::TagEnd);
     $request = $stream->toString();
     if ($this->filter) {
         $request = $this->filter->outputFilter($request);
     }
     $stream->close();
     $response = $this->send($request);
     if ($this->filter) {
         $response = $this->filter->inputFilter($response);
     }
     if ($resultMode == HproseResultMode::RawWithEndTag) {
         return $response;
     }
     if ($resultMode == HproseResultMode::Raw) {
         return substr($response, 0, -1);
     }
     $stream = new HproseStringStream($response);
     $hproseReader = new HproseReader($stream);
     $result = NULL;
     while (($tag = $hproseReader->checkTags(array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) {
         switch ($tag) {
             case HproseTags::TagResult:
                 if ($resultMode == HproseResultMode::Serialized) {
                     $result = $hproseReader->readRaw()->toString();
                 } else {
                     $hproseReader->reset();
                     $result =& $hproseReader->unserialize();
                 }
                 break;
             case HproseTags::TagArgument:
                 $hproseReader->reset();
                 $args =& $hproseReader->readList(true);
                 for ($i = 0; $i < count($arguments); $i++) {
                     $arguments[$i] =& $args[$i];
                 }
                 break;
             case HproseTags::TagError:
                 $hproseReader->reset();
                 throw new HproseException($hproseReader->readString(true));
                 break;
         }
     }
     return $result;
 }
示例#2
0
 public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal)
 {
     $stream = new HproseStringStream(HproseTags::TagCall);
     $hproseWriter = new HproseWriter($stream);
     $hproseWriter->writeString($functionName, false);
     if (0 < count($arguments) || $byRef) {
         $hproseWriter->reset();
         $hproseWriter->writeList($arguments, false);
     }
     if ($byRef) {
         $hproseWriter->writeBoolean(true);
     }
     $stream->write(HproseTags::TagEnd);
     $request = $stream->toString();
     if ($this->filter) {
         $request = $this->filter->outputFilter($request);
     }
     $stream->close();
     $f = new SaeFetchurl();
     $cookie = $this->getCookie();
     if ($cookie != "") {
         $f->setHeader("Cookie", $cookie);
     }
     if ($this->keepAlive) {
         $f->setHeader("Connection", "keep-alive");
         $f->setHeader("Keep-Alive", $this->keepAliveTimeout);
     } else {
         $f->setHeader("Connection", "close");
     }
     foreach ($this->header as $name => $value) {
         $f->setHeader($name, $value);
     }
     $f->setMethod("post");
     $f->setPostData($request);
     $f->setConnectTimeout($this->timeout);
     $f->setSendTimeout($this->timeout);
     $f->setReadTimeout($this->timeout);
     $response = $f->fetch($this->url);
     if ($f->errno()) {
         throw new HproseException($f->errno() . ": " . $f->errmsg());
     }
     $http_response_header = $f->responseHeaders(false);
     $this->setCookie($http_response_header);
     if ($this->filter) {
         $response = $this->filter->inputFilter($response);
     }
     if ($resultMode == HproseResultMode::RawWithEndTag) {
         return $response;
     }
     if ($resultMode == HproseResultMode::Raw) {
         return substr($response, 0, -1);
     }
     $stream = new HproseStringStream($response);
     $hproseReader = new HproseReader($stream);
     $result = NULL;
     $error = NULL;
     while (($tag = $hproseReader->checkTags(array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) {
         switch ($tag) {
             case HproseTags:
                 if ($resultMode == HproseResultMode::Serialized) {
                     $result = $hproseReader->readRaw()->toString();
                 } else {
                     $hproseReader->reset();
                     $result =& $hproseReader->unserialize();
                 }
                 break;
             case HproseTags:
                 $hproseReader->reset();
                 $args =& $hproseReader->readList();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $arguments[$i] =& $args[$i];
                 }
                 break;
             case HproseTags:
                 $hproseReader->reset();
                 $error = new HproseException($hproseReader->readString());
                 break;
         }
     }
     if (!is_null($error)) {
         throw $error;
     }
     return $result;
 }
示例#3
0
 public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal)
 {
     $stream = new HproseStringStream(HproseTags::TagCall);
     $hproseWriter = new HproseWriter($stream);
     $hproseWriter->writeString($functionName, false);
     if (count($arguments) > 0 || $byRef) {
         $hproseWriter->reset();
         $hproseWriter->writeList($arguments, false);
         if ($byRef) {
             $hproseWriter->writeBoolean(true);
         }
     }
     $stream->write(HproseTags::TagEnd);
     $request = $stream->toString();
     $stream->close();
     $opts = array('http' => array('method' => 'POST', 'header' => $this->getCookie() . "Content-Length: " . strlen($request) . "\r\n" . ($this->keepAlive ? "Connection: keep-alive\r\n" . "Keep-Alive: " . $this->keepAliveTimeout . "\r\n" : "Connection: close\r\n"), 'content' => $request, 'timeout' => $this->timeout / 1000.0));
     foreach ($this->header as $name => $value) {
         $opts['http']['header'] .= "{$name}: {$value}\r\n";
     }
     if ($this->proxy) {
         $opts['http']['proxy'] = $this->proxy;
         $opts['http']['request_fulluri'] = true;
     }
     $context = stream_context_create($opts);
     set_error_handler(array(&$this, '__errorHandler'));
     $response = file_get_contents($this->url, false, $context);
     restore_error_handler();
     $this->setCookie($http_response_header);
     if ($resultMode == HproseResultMode::RawWithEndTag) {
         return $response;
     }
     if ($resultMode == HproseResultMode::Raw) {
         return substr($response, 0, -1);
     }
     $stream = new HproseStringStream($response);
     $hproseReader = new HproseReader($stream);
     $result = NULL;
     $error = NULL;
     while (($tag = $hproseReader->checkTags(array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) {
         switch ($tag) {
             case HproseTags::TagResult:
                 if ($resultMode == HproseResultMode::Serialized) {
                     $result = $hproseReader->readRaw()->toString();
                 } else {
                     $hproseReader->reset();
                     $result =& $hproseReader->unserialize();
                 }
                 break;
             case HproseTags::TagArgument:
                 $hproseReader->reset();
                 $args =& $hproseReader->readList();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $arguments[$i] =& $args[$i];
                 }
                 break;
             case HproseTags::TagError:
                 $hproseReader->reset();
                 $error = new HproseException($hproseReader->readString());
                 break;
         }
     }
     if (!is_null($error)) {
         throw $error;
     }
     return $result;
 }
示例#4
0
 private function doInvoke()
 {
     $simpleReader = new HproseSimpleReader($this->input);
     do {
         $functionName = $simpleReader->readString(true);
         $aliasName = strtolower($functionName);
         $resultMode = HproseResultMode::Normal;
         if (array_key_exists($aliasName, $this->functions)) {
             $function = $this->functions[$aliasName];
             $resultMode = $this->resultModes[$aliasName];
             $simple = $this->simpleModes[$aliasName];
         } elseif (array_key_exists('*', $this->functions)) {
             $function = $this->functions['*'];
             $resultMode = $this->resultModes['*'];
             $simple = $this->resultModes['*'];
         } else {
             throw new HproseException("Can't find this function " . $functionName . "().");
         }
         if ($simple === NULL) {
             $simple = $this->simple;
         }
         $writer = $simple ? new HproseSimpleWriter($this->output) : new HproseWriter($this->output);
         $args = array();
         $byref = false;
         $tag = $simpleReader->checkTags(array(HproseTags::TagList, HproseTags::TagEnd, HproseTags::TagCall));
         if ($tag == HproseTags::TagList) {
             $reader = new HproseReader($this->input);
             $args =& $reader->readList();
             $tag = $reader->checkTags(array(HproseTags::TagTrue, HproseTags::TagEnd, HproseTags::TagCall));
             if ($tag == HproseTags::TagTrue) {
                 $byref = true;
                 $tag = $reader->checkTags(array(HproseTags::TagEnd, HproseTags::TagCall));
             }
         }
         if ($this->onBeforeInvoke) {
             call_user_func($this->onBeforeInvoke, $functionName, $args, $byref);
         }
         if (array_key_exists('*', $this->functions) && $function === $this->functions['*']) {
             $arguments = array($functionName, &$args);
         } elseif ($byref) {
             $arguments = array();
             for ($i = 0; $i < count($args); $i++) {
                 $arguments[$i] =& $args[$i];
             }
         } else {
             $arguments = $args;
         }
         $result = call_user_func_array($function, $arguments);
         if ($this->onAfterInvoke) {
             call_user_func($this->onAfterInvoke, $functionName, $args, $byref, $result);
         }
         // some service functions/methods may echo content, we need clean it
         ob_clean();
         if ($resultMode == HproseResultMode::RawWithEndTag) {
             $this->output->write($result);
             return;
         } elseif ($resultMode == HproseResultMode::Raw) {
             $this->output->write($result);
         } else {
             $this->output->write(HproseTags::TagResult);
             if ($resultMode == HproseResultMode::Serialized) {
                 $this->output->write($result);
             } else {
                 $writer->reset();
                 $writer->serialize($result);
             }
             if ($byref) {
                 $this->output->write(HproseTags::TagArgument);
                 $writer->reset();
                 $writer->writeList($args);
             }
         }
     } while ($tag == HproseTags::TagCall);
     $this->output->write(HproseTags::TagEnd);
     ob_end_flush();
 }
示例#5
0
 public static function unserialize($variable_representation)
 {
     $stream = new HproseStringStream($variable_representation);
     $hproseReader = new HproseReader($stream);
     return $hproseReader->unserialize();
 }
示例#6
0
 public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal)
 {
     $stream = new HproseStringStream(HproseTags::TagCall);
     $hproseWriter = new HproseWriter($stream);
     $hproseWriter->writeString($functionName, false);
     if (count($arguments) > 0 || $byRef) {
         $hproseWriter->reset();
         $hproseWriter->writeList($arguments, false);
     }
     if ($byRef) {
         $hproseWriter->writeBoolean(true);
     }
     $stream->write(HproseTags::TagEnd);
     $request = $stream->toString();
     $stream->close();
     curl_setopt($this->curl, CURLOPT_URL, $this->url);
     curl_setopt($this->curl, CURLOPT_HEADER, TRUE);
     curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($this->curl, CURLOPT_POST, TRUE);
     curl_setopt($this->curl, CURLOPT_POSTFIELDS, $request);
     $headers_array = array($this->getCookie(), "Content-Length: " . strlen($request));
     if ($this->keepAlive) {
         $headers_array[] = "Connection: keep-alive";
         $headers_array[] = "Keep-Alive: " . $this->keepAliveTimeout;
     } else {
         $headers_array[] = "Connection: close";
     }
     foreach ($this->header as $name => $value) {
         $headers_array[] = $name . ": " . $value;
     }
     curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers_array);
     if ($this->proxy) {
         curl_setopt($this->curl, CURLOPT_PROXY, $this->proxy);
     }
     if (defined(CURLOPT_TIMEOUT_MS)) {
         curl_setopt($this->curl, CURLOPT_TIMEOUT_MS, $this->timeout);
     } else {
         curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout / 1000);
     }
     $response = curl_exec($this->curl);
     $errno = curl_errno($this->curl);
     if ($errno) {
         throw new HproseException($errno . ": " . curl_error($this->curl));
     }
     do {
         list($response_headers, $response) = explode("\r\n\r\n", $response, 2);
         $http_response_header = explode("\r\n", $response_headers);
         $http_response_firstline = array_shift($http_response_header);
         if (preg_match('@^HTTP/[0-9]\\.[0-9]\\s([0-9]{3})\\s(.*?)@', $http_response_firstline, $matches)) {
             $response_code = $matches[1];
             $response_status = trim($matches[2]);
         } else {
             $response_code = "500";
             $response_status = "Unknown Error.";
         }
     } while (substr($response_code, 0, 1) == "1");
     if ($response_code != '200') {
         throw new HproseException($response_code . ": " . $response_status);
     }
     $this->setCookie($http_response_header);
     if ($resultMode == HproseResultMode::RawWithEndTag) {
         return $response;
     }
     if ($resultMode == HproseResultMode::Raw) {
         return substr($response, 0, -1);
     }
     $stream = new HproseStringStream($response);
     $hproseReader = new HproseReader($stream);
     $result = NULL;
     $error = NULL;
     while (($tag = $hproseReader->checkTags(array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) {
         switch ($tag) {
             case HproseTags::TagResult:
                 if ($resultMode == HproseResultMode::Serialized) {
                     $result = $hproseReader->readRaw()->toString();
                 } else {
                     $hproseReader->reset();
                     $result =& $hproseReader->unserialize();
                 }
                 break;
             case HproseTags::TagArgument:
                 $hproseReader->reset();
                 $args =& $hproseReader->readList();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $arguments[$i] =& $args[$i];
                 }
                 break;
             case HproseTags::TagError:
                 $hproseReader->reset();
                 $error = new HproseException($hproseReader->readString());
                 break;
         }
     }
     if (!is_null($error)) {
         throw $error;
     }
     return $result;
 }