public function readAll($bufsize = null) { if ($bufsize === null) { $bufsize = 8192; } $buf = haxe_io_Bytes::alloc($bufsize); $total = new haxe_io_BytesBuffer(); try { while (true) { $len = $this->readBytes($buf, 0, $bufsize); if ($len === 0) { throw new HException(haxe_io_Error::$Blocked); } if ($len < 0 || $len > $buf->length) { throw new HException(haxe_io_Error::$OutsideBounds); } $total->b .= _hx_string_or_null(substr($buf->b, 0, $len)); unset($len); } } catch (Exception $__hx__e) { $_ex_ = $__hx__e instanceof HException ? $__hx__e->e : $__hx__e; if (($e = $_ex_) instanceof haxe_io_Eof) { } else { throw $__hx__e; } } return $total->getBytes(); }
public function readHttpResponse($api, $sock) { $b = new haxe_io_BytesBuffer(); $k = 4; $s = haxe_io_Bytes::alloc(4); $sock->setTimeout($this->cnxTimeout); while (true) { $p = $sock->input->readBytes($s, 0, $k); while ($p !== $k) { $p += $sock->input->readBytes($s, $p, $k - $p); } if ($k < 0 || $k > $s->length) { throw new HException(haxe_io_Error::$OutsideBounds); } $b->b .= substr($s->b, 0, $k); switch ($k) { case 1: $c = ord($s->b[0]); if ($c === 10) { break 2; } if ($c === 13) { $k = 3; } else { $k = 4; } break; case 2: $c = ord($s->b[1]); if ($c === 10) { if (ord($s->b[0]) === 13) { break 2; } $k = 4; } else { if ($c === 13) { $k = 3; } else { $k = 4; } } break; case 3: $c = ord($s->b[2]); if ($c === 10) { if (ord($s->b[1]) !== 13) { $k = 4; } else { if (ord($s->b[0]) !== 10) { $k = 2; } else { break 2; } } } else { if ($c === 13) { if (ord($s->b[1]) !== 10 || ord($s->b[0]) !== 13) { $k = 1; } else { $k = 3; } } else { $k = 4; } } break; case 4: $c = ord($s->b[3]); if ($c === 10) { if (ord($s->b[2]) !== 13) { continue 2; } else { if (ord($s->b[1]) !== 10 || ord($s->b[0]) !== 13) { $k = 2; } else { break 2; } } } else { if ($c === 13) { if (ord($s->b[2]) !== 10 || ord($s->b[1]) !== 13) { $k = 3; } else { $k = 1; } } } break; } unset($p); } $headers = _hx_explode("\r\n", $b->getBytes()->toString()); $response = $headers->shift(); $rp = _hx_explode(" ", $response); $status = Std::parseInt($rp[1]); if ($status === 0 || $status === null) { throw new HException("Response status error"); } $headers->pop(); $headers->pop(); $this->responseHeaders = new Hash(); $size = null; $chunked = false; $_g = 0; while ($_g < $headers->length) { $hline = $headers[$_g]; ++$_g; $a = _hx_explode(": ", $hline); $hname = $a->shift(); $hval = haxe_Http_3($this, $_g, $a, $api, $b, $chunked, $headers, $hline, $hname, $k, $response, $rp, $s, $size, $sock, $status); $this->responseHeaders->set($hname, $hval); switch (strtolower($hname)) { case "content-length": $size = Std::parseInt($hval); break; case "transfer-encoding": $chunked = strtolower($hval) === "chunked"; break; } unset($hval, $hname, $hline, $a); } $this->onStatus($status); $chunk_re = new EReg("^([0-9A-Fa-f]+)[ ]*\r\n", "m"); $this->chunk_size = null; $this->chunk_buf = null; $bufsize = 1024; $buf = haxe_io_Bytes::alloc($bufsize); if ($size === null) { if (!$this->noShutdown) { $sock->shutdown(false, true); } try { while (true) { $len = $sock->input->readBytes($buf, 0, $bufsize); if ($chunked) { if (!$this->readChunk($chunk_re, $api, $buf, $len)) { break; } } else { $api->writeBytes($buf, 0, $len); } unset($len); } } catch (Exception $»e) { $_ex_ = $»e instanceof HException ? $»e->e : $»e; if (($e = $_ex_) instanceof haxe_io_Eof) { } else { throw $»e; } } } else { $api->prepare($size); try { while ($size > 0) { $len = $sock->input->readBytes($buf, 0, $size > $bufsize ? $bufsize : $size); if ($chunked) { if (!$this->readChunk($chunk_re, $api, $buf, $len)) { break; } } else { $api->writeBytes($buf, 0, $len); } $size -= $len; unset($len); } } catch (Exception $»e) { $_ex_ = $»e instanceof HException ? $»e->e : $»e; if (($e = $_ex_) instanceof haxe_io_Eof) { throw new HException("Transfert aborted"); } else { throw $»e; } } } if ($chunked && ($this->chunk_size !== null || $this->chunk_buf !== null)) { throw new HException("Invalid chunk"); } if ($status < 200 || $status >= 400) { throw new HException("Http Error #" . _hx_string_rec($status, "")); } $api->close(); }
public function readChunk($chunk_re, $api, $buf, $len) { if ($this->chunk_size === null) { if ($this->chunk_buf !== null) { $b = new haxe_io_BytesBuffer(); $b->b .= _hx_string_or_null($this->chunk_buf->b); if ($len < 0 || $len > $buf->length) { throw new HException(haxe_io_Error::$OutsideBounds); } $b->b .= _hx_string_or_null(substr($buf->b, 0, $len)); $buf = $b->getBytes(); $len += $this->chunk_buf->length; $this->chunk_buf = null; } if ($chunk_re->match($buf->toString())) { $p = $chunk_re->matchedPos(); if ($p->len <= $len) { $cstr = $chunk_re->matched(1); $this->chunk_size = Std::parseInt("0x" . _hx_string_or_null($cstr)); if ($cstr === "0") { $this->chunk_size = null; $this->chunk_buf = null; return false; } $len -= $p->len; return $this->readChunk($chunk_re, $api, $buf->sub($p->len, $len), $len); } } if ($len > 10) { $this->onError("Invalid chunk"); return false; } $this->chunk_buf = $buf->sub(0, $len); return true; } if ($this->chunk_size > $len) { $this->chunk_size -= $len; $api->writeBytes($buf, 0, $len); return true; } $end = $this->chunk_size + 2; if ($len >= $end) { if ($this->chunk_size > 0) { $api->writeBytes($buf, 0, $this->chunk_size); } $len -= $end; $this->chunk_size = null; if ($len === 0) { return true; } return $this->readChunk($chunk_re, $api, $buf->sub($end, $len), $len); } if ($this->chunk_size > 0) { $api->writeBytes($buf, 0, $this->chunk_size); } $this->chunk_size -= $len; return true; }