示例#1
0
 public function setTimeout($timeout)
 {
     $s = Std::int($timeout);
     $ms = Std::int(($timeout - $s) * 1000000);
     $r = stream_set_timeout($this->__s, $s, $ms);
     sys_net_Socket::checkError($r, 0, "Unable to set timeout");
 }
 public function __construct()
 {
     if (!php_Boot::$skip_constructor) {
         parent::__construct();
         $this->protocol = "ssl";
     }
 }
示例#3
0
 public function connect($host, $port)
 {
     $errs = null;
     $errn = null;
     $r = stream_socket_client($this->protocol . "://" . $host->hostName . ":" . _hx_string_rec($port, ""), $errn, $errs);
     sys_net_Socket::checkError($r, $errn, $errs);
     $this->__s = $r;
     $this->assignHandler();
 }
示例#4
0
 public function customRequest($post, $api, $sock = null, $method = null)
 {
     $url_regexp = new EReg("^(https?://)?([a-zA-Z\\.0-9-]+)(:[0-9]+)?(.*)\$", "");
     if (!$url_regexp->match($this->url)) {
         $this->onError("Invalid URL");
         return;
     }
     $secure = $url_regexp->matched(1) === "https://";
     if ($sock === null) {
         if ($secure) {
             $sock = new php_net_SslSocket();
         } else {
             $sock = new sys_net_Socket();
         }
     }
     $host = $url_regexp->matched(2);
     $portString = $url_regexp->matched(3);
     $request = $url_regexp->matched(4);
     if ($request === "") {
         $request = "/";
     }
     $port = $portString === null || $portString === "" ? $secure ? 443 : 80 : Std::parseInt(_hx_substr($portString, 1, strlen($portString) - 1));
     $data = null;
     $multipart = _hx_field($this, "file") !== null;
     $boundary = null;
     $uri = null;
     if ($multipart) {
         $post = true;
         $boundary = Std::string(Std::random(1000)) . Std::string(Std::random(1000)) . Std::string(Std::random(1000)) . Std::string(Std::random(1000));
         while (strlen($boundary) < 38) {
             $boundary = "-" . $boundary;
         }
         $b = new StringBuf();
         if (null == $this->params) {
             throw new HException('null iterable');
         }
         $»it = $this->params->keys();
         while ($»it->hasNext()) {
             $p = $»it->next();
             $b->add("--");
             $b->add($boundary);
             $b->add("\r\n");
             $b->add("Content-Disposition: form-data; name=\"");
             $b->add($p);
             $b->add("\"");
             $b->add("\r\n");
             $b->add("\r\n");
             $b->add($this->params->get($p));
             $b->add("\r\n");
         }
         $b->add("--");
         $b->add($boundary);
         $b->add("\r\n");
         $b->add("Content-Disposition: form-data; name=\"");
         $b->add($this->file->param);
         $b->add("\"; filename=\"");
         $b->add($this->file->filename);
         $b->add("\"");
         $b->add("\r\n");
         $b->add("Content-Type: " . "application/octet-stream" . "\r\n" . "\r\n");
         $uri = $b->b;
     } else {
         if (null == $this->params) {
             throw new HException('null iterable');
         }
         $»it = $this->params->keys();
         while ($»it->hasNext()) {
             $p = $»it->next();
             if ($uri === null) {
                 $uri = "";
             } else {
                 $uri .= "&";
             }
             $uri .= rawurlencode($p) . "=" . rawurlencode($this->params->get($p));
         }
     }
     $b = new StringBuf();
     if ($method !== null) {
         $b->add($method);
         $b->add(" ");
     } else {
         if ($post) {
             $b->add("POST ");
         } else {
             $b->add("GET ");
         }
     }
     if (_hx_field(_hx_qtype("haxe.Http"), "PROXY") !== null) {
         $b->add("http://");
         $b->add($host);
         if ($port !== 80) {
             $b->add(":");
             $b->add($port);
         }
     }
     $b->add($request);
     if (!$post && $uri !== null) {
         if (_hx_index_of($request, "?", 0) >= 0) {
             $b->add("&");
         } else {
             $b->add("?");
         }
         $b->add($uri);
     }
     $b->add(" HTTP/1.1\r\nHost: " . $host . "\r\n");
     if ($this->postData !== null) {
         $b->add("Content-Length: " . _hx_string_rec(strlen($this->postData), "") . "\r\n");
     } else {
         if ($post && $uri !== null) {
             if ($multipart || $this->headers->get("Content-Type") === null) {
                 $b->add("Content-Type: ");
                 if ($multipart) {
                     $b->add("multipart/form-data");
                     $b->add("; boundary=");
                     $b->add($boundary);
                 } else {
                     $b->add("application/x-www-form-urlencoded");
                 }
                 $b->add("\r\n");
             }
             if ($multipart) {
                 $b->add("Content-Length: " . _hx_string_rec(strlen($uri) + $this->file->size + strlen($boundary) + 6, "") . "\r\n");
             } else {
                 $b->add("Content-Length: " . _hx_string_rec(strlen($uri), "") . "\r\n");
             }
         }
     }
     if (null == $this->headers) {
         throw new HException('null iterable');
     }
     $»it = $this->headers->keys();
     while ($»it->hasNext()) {
         $h = $»it->next();
         $b->add($h);
         $b->add(": ");
         $b->add($this->headers->get($h));
         $b->add("\r\n");
     }
     $b->add("\r\n");
     if ($this->postData !== null) {
         $b->add($this->postData);
     } else {
         if ($post && $uri !== null) {
             $b->add($uri);
         }
     }
     try {
         if (_hx_field(_hx_qtype("haxe.Http"), "PROXY") !== null) {
             $sock->connect(new sys_net_Host(haxe_Http::$PROXY->host), haxe_Http::$PROXY->port);
         } else {
             $sock->connect(new sys_net_Host($host), $port);
         }
         $sock->write($b->b);
         if ($multipart) {
             $bufsize = 4096;
             $buf = haxe_io_Bytes::alloc($bufsize);
             while ($this->file->size > 0) {
                 $size = haxe_Http_4($this, $api, $b, $boundary, $buf, $bufsize, $data, $host, $method, $multipart, $port, $portString, $post, $request, $secure, $sock, $uri, $url_regexp);
                 $len = 0;
                 try {
                     $len = $this->file->io->readBytes($buf, 0, $size);
                 } catch (Exception $»e) {
                     $_ex_ = $»e instanceof HException ? $»e->e : $»e;
                     if (($e = $_ex_) instanceof haxe_io_Eof) {
                         break;
                     } else {
                         throw $»e;
                     }
                 }
                 $sock->output->writeFullBytes($buf, 0, $len);
                 $this->file->size -= $len;
                 unset($size, $len, $e);
             }
             $sock->write("\r\n");
             $sock->write("--");
             $sock->write($boundary);
             $sock->write("--");
         }
         $this->readHttpResponse($api, $sock);
         $sock->close();
     } catch (Exception $»e) {
         $_ex_ = $»e instanceof HException ? $»e->e : $»e;
         $e = $_ex_;
         try {
             $sock->close();
         } catch (Exception $»e) {
             $_ex_ = $»e instanceof HException ? $»e->e : $»e;
             $e1 = $_ex_;
         }
         $this->onError(Std::string($e));
     }
 }