public function handleRequest($httpContext) { $doneTrigger = new tink_core_FutureTrigger(); if (ufront_remoting_RemotingHandler_0($this, $doneTrigger, $httpContext)) { $r = $httpContext->response; $remotingResponse = null; $r->setOk(); $path = null; $args = null; try { $this->initializeContext($httpContext->injector); $params = $httpContext->request->get_params(); if (!$params->exists("__x")) { throw new HException("Remoting call did not have parameter `__x` which describes which API call to make. Aborting"); } $remotingCall = null; $s = ufront_core__MultiValueMap_MultiValueMap_Impl_::get($params, "__x"); $remotingCall = urldecode($s); $u = new haxe_Unserializer($remotingCall); $path = $u->unserialize(); $args = $u->unserialize(); $apiCallFinished = $this->executeApiCall($path, $args, $this->context, $httpContext->actionContext); $remotingResponse = tink_core__Future_Future_Impl_::map($apiCallFinished, array(new _hx_lambda(array(&$apiCallFinished, &$args, &$doneTrigger, &$httpContext, &$params, &$path, &$r, &$remotingCall, &$remotingResponse, &$u), "ufront_remoting_RemotingHandler_1"), 'execute'), null); } catch (Exception $__hx__e) { $_ex_ = $__hx__e instanceof HException ? $__hx__e->e : $__hx__e; $e = $_ex_; $error = $e; $apiNotFoundMessages = new _hx_array(array("Invalid path", "No such object", "Can't access", "No such method")); if ($path !== null && $args !== null && Std::is($e, _hx_qtype("String")) && Lambda::exists($apiNotFoundMessages, array(new _hx_lambda(array(&$apiNotFoundMessages, &$args, &$doneTrigger, &$e, &$error, &$httpContext, &$path, &$r, &$remotingResponse), "ufront_remoting_RemotingHandler_2"), 'execute'))) { $remotingResponse = tink_core__Future_Future_Impl_::sync("Unable to access " . _hx_string_or_null($path->join(".")) . " - API Not Found (" . _hx_string_or_null($error) . "). See " . Std::string($this->context->objects)); $r->setNotFound(); } else { $r->setInternalError(); $remotingResponse = tink_core__Future_Future_Impl_::sync($this->remotingError($e, $httpContext)); } } $remotingResponse(array(new _hx_lambda(array(&$args, &$doneTrigger, &$e, &$httpContext, &$path, &$r, &$remotingResponse), "ufront_remoting_RemotingHandler_3"), 'execute')); } else { $result1 = tink_core_Outcome::Success(tink_core_Noise::$Noise); if ($doneTrigger->{"list"} === null) { false; } else { $list1 = $doneTrigger->{"list"}; $doneTrigger->{"list"} = null; $doneTrigger->result = $result1; tink_core__Callback_CallbackList_Impl_::invoke($list1, $result1); tink_core__Callback_CallbackList_Impl_::clear($list1); true; } } return $doneTrigger->future; }
public function customRequest($post, $api, $sock = null, $method = null) { $this->responseData = 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 = null; if ($portString === null || $portString === "") { if ($secure) { $port = 443; } else { $port = 80; } } else { $port = 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 = "-" . _hx_string_or_null($boundary); } $b = new StringBuf(); if (null == $this->params) { throw new HException('null iterable'); } $__hx__it = $this->params->iterator(); while ($__hx__it->hasNext()) { unset($p); $p = $__hx__it->next(); $b->add("--"); $b->add($boundary); $b->add("\r\n"); $b->add("Content-Disposition: form-data; name=\""); $b->add($p->param); $b->add("\""); $b->add("\r\n"); $b->add("\r\n"); $b->add($p->value); $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: " . _hx_string_or_null($this->file->mimeType) . "\r\n" . "\r\n"); $uri = $b->b; } else { if (null == $this->params) { throw new HException('null iterable'); } $__hx__it = $this->params->iterator(); while ($__hx__it->hasNext()) { unset($p1); $p1 = $__hx__it->next(); if ($uri === null) { $uri = ""; } else { $uri .= "&"; } $uri .= _hx_string_or_null(rawurlencode($p1->param)) . "=" . _hx_string_or_null(rawurlencode($p1->value)); } } $b1 = new StringBuf(); if ($method !== null) { $b1->add($method); $b1->add(" "); } else { if ($post) { $b1->add("POST "); } else { $b1->add("GET "); } } if (_hx_field(_hx_qtype("haxe.Http"), "PROXY") !== null) { $b1->add("http://"); $b1->add($host); if ($port !== 80) { $b1->add(":"); $b1->add($port); } } $b1->add($request); if (!$post && $uri !== null) { if (_hx_index_of($request, "?", 0) >= 0) { $b1->add("&"); } else { $b1->add("?"); } $b1->add($uri); } $b1->add(" HTTP/1.1\r\nHost: " . _hx_string_or_null($host) . "\r\n"); if ($this->postData !== null) { $b1->add("Content-Length: " . _hx_string_rec(strlen($this->postData), "") . "\r\n"); } else { if ($post && $uri !== null) { if ($multipart || !Lambda::exists($this->headers, array(new _hx_lambda(array(&$api, &$b1, &$boundary, &$data, &$host, &$method, &$multipart, &$port, &$portString, &$post, &$request, &$secure, &$sock, &$uri, &$url_regexp), "haxe_Http_5"), 'execute'))) { $b1->add("Content-Type: "); if ($multipart) { $b1->add("multipart/form-data"); $b1->add("; boundary="); $b1->add($boundary); } else { $b1->add("application/x-www-form-urlencoded"); } $b1->add("\r\n"); } if ($multipart) { $b1->add("Content-Length: " . _hx_string_rec(strlen($uri) + $this->file->size + strlen($boundary) + 6, "") . "\r\n"); } else { $b1->add("Content-Length: " . _hx_string_rec(strlen($uri), "") . "\r\n"); } } } if (null == $this->headers) { throw new HException('null iterable'); } $__hx__it = $this->headers->iterator(); while ($__hx__it->hasNext()) { unset($h1); $h1 = $__hx__it->next(); $b1->add($h1->header); $b1->add(": "); $b1->add($h1->value); $b1->add("\r\n"); } $b1->add("\r\n"); if ($this->postData !== null) { $b1->add($this->postData); } else { if ($post && $uri !== null) { $b1->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($b1->b); if ($multipart) { $bufsize = 4096; $buf = haxe_io_Bytes::alloc($bufsize); while ($this->file->size > 0) { $size = null; if ($this->file->size > $bufsize) { $size = $bufsize; } else { $size = $this->file->size; } $len = 0; try { $len = $this->file->io->readBytes($buf, 0, $size); } catch (Exception $__hx__e) { $_ex_ = $__hx__e instanceof HException ? $__hx__e->e : $__hx__e; if (($e = $_ex_) instanceof haxe_io_Eof) { break; } else { throw $__hx__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 $__hx__e) { $_ex_ = $__hx__e instanceof HException ? $__hx__e->e : $__hx__e; $e1 = $_ex_; try { $sock->close(); } catch (Exception $__hx__e) { $_ex_ = $__hx__e instanceof HException ? $__hx__e->e : $__hx__e; $e2 = $_ex_; } $this->onError(Std::string($e1)); } }