/** * Extract a ".ar" file into a given target directory * * @param string base * @param string ar * @param io.Folder target * @throws lang.IllegalStateException in case the target is not found * @throws lang.FormatException in case the .ar-file is not parseable */ protected function extract($base, $ar, Folder $target) { // Open a HTTP connection $url = new \peer\URL($base . $ar . '.ar'); $r = create(new HttpConnection($url))->get(); if (\peer\http\HttpConstants::STATUS_OK != $r->getStatusCode()) { throw new \lang\IllegalStateException(sprintf('Unexpected response %d:%s for %s', $r->getStatusCode(), $r->getMessage(), $url->getURL())); } $in = new BufferedInputStream($r->getInputStream()); do { // Seach for first section header, --[LENGTH]:[FILENAME]-- and parse it do { $line = $this->readLine($in); if (!$in->available()) { throw new \lang\FormatException('Cannot locate section header'); } } while (2 !== sscanf($line, '--%d:%[^:]--', $length, $filename)); // Calculate target file $file = new File($target, $filename); $folder = new Folder($file->getPath()); $folder->exists() || $folder->create(); \util\cmd\Console::writef(' >> [%-10s] %s (%.2f kB) [%s]%s', $ar, $filename, $length / 1024, str_repeat('.', self::PROGRESS_INDICATOR_WIDTH), str_repeat("", self::PROGRESS_INDICATOR_WIDTH + 1)); // Transfer length bytes into file $c = 0; $out = $file->getOutputStream(); $size = 0; while ($size < $length) { $chunk = $in->read(min(0x1000, $length - $size)); $size += strlen($chunk); $out->write($chunk); // Update progress $d = ceil($size / $length * self::PROGRESS_INDICATOR_WIDTH); if ($d == $c) { continue; } \util\cmd\Console::write(str_repeat('#', $d - $c)); $c = $d; } $out->close(); \util\cmd\Console::writeLine(); } while ($in->available() > 0); $in->close(); }
/** * Constructor * * @param util.cmd.ParamString args */ public function __construct(\util\cmd\ParamString $args) { $url = new \peer\URL($args->value(0)); // If protocol string does not contain port number, set default. if (self::ESDL_PORT === $url->getPort(self::ESDL_PORT)) { $url->setPort(self::ESDL_PORT); } // Check given URL to inform user if invalid port used. if (self::ESDL_PORT !== $url->getPort()) { \util\cmd\Console::$err->writeLine('Notice: using non-standard port ' . $url->getPort() . ', ESDL services are usually available at port 6449.'); } $this->remote = Remote::forName($url->getURL()); $this->jndi = $args->value(1); $this->processor = new DomXSLProcessor(); $this->processor->setXSLBuf($this->getClass()->getPackage()->getResource($args->value('lang', 'l', 'xp5') . '.xsl')); }
/** * Navigate to a given URL * * @param string target * @param string params * @param string method * @throws unittest.AssertionFailedError */ public function navigateTo($target, $params = null, $method = HttpConstants::GET) { if (strstr($target, '://')) { $url = new \peer\URL($target); $this->conn = $this->getConnection(sprintf('%s://%s%s/', $url->getScheme(), $url->getHost(), -1 === $url->getPort(-1) ? '' : ':' . $url->getPort())); $params ? $url->setParams($params) : ''; $this->beginAt($url->getPath(), $url->getParams(), $method); } else { if ('' !== $target && '/' === $target[0]) { $this->beginAt($target, $params, $method); } else { $base = $this->getBase(); $this->beginAt(substr($base, 0, strrpos($base, '/')) . '/' . $target, $params, $method); } } }
public function doCreate() { $req = $this->newRequest('GET', new \peer\URL('http://localhost/')); $res = new \scriptlet\HttpScriptletResponse(); $s = newinstance('scriptlet.xml.XMLScriptlet', array(), '{ public function needsSession($request) { return TRUE; } }'); $s->service($req, $res); $this->assertEquals(\peer\http\HttpConstants::STATUS_FOUND, $res->statusCode); // Check URL from Location: header contains the session ID with($redirect = new \peer\URL(substr($res->headers[0], strlen('Location: ')))); $this->assertEquals('http', $redirect->getScheme()); $this->assertEquals('localhost', $redirect->getHost()); $this->assertEquals(sprintf('/xml/psessionid=%s/static', session_id()), $redirect->getPath()); $this->assertEquals(array(), $redirect->getParams(), $redirect->getURL()); }
public function handleSessionInitializationError() { $req = $this->newRequest('GET', new \peer\URL('http://localhost/?psessionid=MALFORMED')); $res = new \scriptlet\HttpScriptletResponse(); $s = newinstance('scriptlet.HttpScriptlet', array(), '{ public function needsSession($request) { return TRUE; } public function handleSessionInitialization($request) { if (!preg_match("/^a-f0-9$/", $request->getSessionId())) { throw new IllegalArgumentException("Invalid characters in session id"); } parent::handleSessionInitialization($request); } public function handleSessionInitializationError($request, $response) { $request->getURL()->addParam("relogin", 1); return $request->session->initialize(NULL); } }'); $s->service($req, $res); $this->assertEquals(\peer\http\HttpConstants::STATUS_FOUND, $res->statusCode); // Check URL from Location: header contains the session ID with($redirect = new \peer\URL(substr($res->headers[0], strlen('Location: ')))); $this->assertEquals('http', $redirect->getScheme()); $this->assertEquals('localhost', $redirect->getHost()); $this->assertEquals('/', $redirect->getPath()); $this->assertEquals(session_id(), $redirect->getParam('psessionid')); $this->assertEquals('1', $redirect->getParam('relogin')); }