Example #1
0
 public static function serialize(&$var, $simple = false)
 {
     $stream = new HproseStringStream();
     $hproseWriter = $simple ? new HproseSimpleWriter($stream) : new HproseWriter($stream);
     $hproseWriter->serialize($var);
     return $stream->toString();
 }
Example #2
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;
 }
Example #3
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 = new HproseWriter($stream, $simple);
     // $hproseWriter->writeString($functionName);
     $stream->write(hprose_serialize_string($functionName));
     if (count($arguments) > 0 || $byRef) {
         // $hproseWriter->reset();
         // $hproseWriter->writeList($arguments);
         $stream->write(hprose_serialize_list($arguments, $simple));
         if ($byRef) {
             // $hproseWriter->writeBoolean(true);
             $stream->write(hprose_serialize_bool(true));
         }
     }
     $stream->write(HproseTags::TagEnd);
     $request = $stream->toString();
     $count = count($this->filters);
     for ($i = 0; $i < $count; $i++) {
         $request = $this->filters[$i]->outputFilter($request, $this);
     }
     $stream->close();
     $response = $this->sendAndReceive($request);
     for ($i = $count - 1; $i >= 0; $i--) {
         $response = $this->filters[$i]->inputFilter($response, $this);
     }
     if ($resultMode == HproseResultMode::RawWithEndTag) {
         return $response;
     }
     if ($resultMode == HproseResultMode::Raw) {
         return substr($response, 0, -1);
     }
     $stream = new HproseStringStream($response);
     $hproseReader = new HproseRawReader($stream);
     $result = NULL;
     while (($tag = $stream->getc()) !== HproseTags::TagEnd) {
         switch ($tag) {
             case HproseTags::TagResult:
                 if ($resultMode == HproseResultMode::Serialized) {
                     $result = $hproseReader->readRaw()->toString();
                 } else {
                     // $hproseReader->reset();
                     // $result = &$hproseReader->unserialize();
                     $result =& hprose_unserialize_with_stream($stream);
                 }
                 break;
             case HproseTags::TagArgument:
                 // $hproseReader->reset();
                 // $args = &$hproseReader->readList();
                 $args =& hprose_unserialize_with_stream($stream);
                 for ($i = 0; $i < count($arguments); $i++) {
                     $arguments[$i] =& $args[$i];
                 }
                 break;
             case HproseTags::TagError:
                 // $hproseReader->reset();
                 throw new Exception(hprose_unserialize_with_stream($stream));
                 break;
             default:
                 throw new Exception("Wrong Response: \r\n" . $response);
                 break;
         }
     }
     return $result;
 }
Example #4
0
 public function readRaw($ostream = NULL, $tag = NULL)
 {
     if (is_null($ostream)) {
         $ostream = new HproseStringStream();
     }
     if (is_null($tag)) {
         $tag = $this->stream->getc();
     }
     $ostream->write($tag);
     switch ($tag) {
         case '0':
         case '1':
         case '2':
         case '3':
         case '4':
         case '5':
         case '6':
         case '7':
         case '8':
         case '9':
         case HproseTags::TagNull:
         case HproseTags::TagEmpty:
         case HproseTags::TagTrue:
         case HproseTags::TagFalse:
         case HproseTags::TagNaN:
             break;
         case HproseTags::TagInfinity:
             $ostream->write($this->stream->getc());
             break;
         case HproseTags::TagInteger:
         case HproseTags::TagLong:
         case HproseTags::TagDouble:
         case HproseTags::TagRef:
             $this->readNumberRaw($ostream);
             break;
         case HproseTags::TagDate:
         case HproseTags::TagTime:
             $this->readDateTimeRaw($ostream);
             break;
         case HproseTags::TagUTF8Char:
             $this->readUTF8CharRaw($ostream);
             break;
         case HproseTags::TagBytes:
             $this->readBytesRaw($ostream);
             break;
         case HproseTags::TagString:
             $this->readStringRaw($ostream);
             break;
         case HproseTags::TagGuid:
             $this->readGuidRaw($ostream);
             break;
         case HproseTags::TagList:
         case HproseTags::TagMap:
         case HproseTags::TagObject:
             $this->readComplexRaw($ostream);
             break;
         case HproseTags::TagClass:
             $this->readComplexRaw($ostream);
             $this->readRaw($ostream);
             break;
         case HproseTags::TagError:
             $this->readRaw($ostream);
             break;
         default:
             $this->unexpectedTag($tag);
     }
     return $ostream;
 }
Example #5
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;
 }
Example #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();
     $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;
 }
Example #7
0
 public static function serialize($variable)
 {
     $stream = new HproseStringStream();
     $hproseWriter = new HproseWriter($stream);
     $hproseWriter->serialize($variable);
     return $stream->toString();
 }
Example #8
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;
 }