/** * Connect to store using a DSN * * @param string dsn * @return bool success * @see php://imap_open * @throws lang.IllegalArgumentException in case scheme is not recognized * @throws peer.mail.MessagingException */ public function connect($dsn) { $flags = OP_HALFOPEN; // Parse DSN $u = new URL($dsn); $attr = $u->getParams(); // DSN supported? if (false === $this->_supports($u, $attr)) { return false; } // Read-only? if ($u->getParam('open')) { $flags ^= OP_HALFOPEN; } if ($u->getParam('read-only')) { $flags |= OP_READONLY; } $mbx = isset($attr['mbx']) ? $attr['mbx'] : sprintf('{%s:%d/%s}', $u->getHost(), $u->getPort($attr['port']), $u->getParam('proto', $attr['proto'])); // Connect if (false === ($conn = $this->_connect($mbx, @$u->getUser(), @$u->getPassword(), $flags))) { throw new \peer\mail\MessagingException('Connect to "' . $u->getUser() . '@' . $mbx . '" failed', $this->_errors()); } $this->_hdl = [$conn, $mbx]; return true; }
/** * Creates a socket - overridden from parent class * * @param peer.URL $url * @param string $arg * @return peer.Socket */ protected function newSocket(\peer\URL $url, $arg) { if ('tls' === $arg) { return new TLSSocket($url->getHost(), $url->getPort(443), null); } else { sscanf($arg, 'v%d', $version); return new SSLSocket($url->getHost(), $url->getPort(443), null, $version); } }
/** * Create client by inspecting the URL * * @param string url The API url * @return com.atlassian.jira.api.JiraClientProtocol */ public static function forURL($url) { $u = new URL($url); if (strstr($u->getPath(), '/rest/api/2')) { return XPClass::forName('com.atlassian.jira.api.protocol.JiraClientRest2Protocol')->newInstance($u); } else { throw new IllegalArgumentException('No suitable client found for ' . $url); } }
/** * Set URL * * @param peer.URL url object */ public function setUrl(URL $url) { $this->url = $url; if ($url->getUser() && $url->getPassword()) { $this->setHeader('Authorization', new BasicAuthorization($url->getUser(), $url->getPassword())); } $port = $this->url->getPort(-1); $this->headers['Host'] = [$this->url->getHost() . (-1 == $port ? '' : ':' . $port)]; $this->target = $this->url->getPath('/'); }
/** * Handle a single request * * @param string method request method * @param string query query string * @param [:string] headers request headers * @param string data post data * @param peer.Socket socket * @return int */ public function handleRequest($method, $query, array $headers, $data, Socket $socket) { $url = new URL('http://' . (isset($headers['Host']) ? $headers['Host'] : 'localhost') . $query); $port = $url->getPort(-1); $request = $this->scriptlet->request(); $response = $this->scriptlet->response(); // Fill request $request->method = $method; $request->env = $this->env; $request->env['SERVER_PROTOCOL'] = 'HTTP/1.1'; $request->env['REQUEST_URI'] = $query; $request->env['QUERY_STRING'] = substr($query, strpos($query, '?') + 1); $request->env['HTTP_HOST'] = $url->getHost() . (-1 === $port ? '' : ':' . $port); if (isset($headers['Authorization'])) { if (0 === strncmp('Basic', $headers['Authorization'], 5)) { $credentials = explode(':', base64_decode(substr($headers['Authorization'], 6))); $request->env['PHP_AUTH_USER'] = $credentials[0]; $request->env['PHP_AUTH_PW'] = $credentials[1]; } } $request->setHeaders($headers); // Merge POST and GET parameters if (isset($headers['Content-Type']) && 'application/x-www-form-urlencoded' === $headers['Content-Type']) { parse_str($data, $params); $request->setParams(array_merge($url->getParams(), $params)); } else { $request->setParams($url->getParams()); } // Rewire request and response I/O $request->readData = function () use($data) { return new \io\streams\MemoryInputStream($data); }; $response->sendHeaders = function ($version, $statusCode, $headers) use($socket) { $this->sendHeader($socket, $statusCode, '', $headers); }; $response->sendContent = function ($content) use($socket) { $socket->write($content); }; try { $this->scriptlet->service($request, $response); } catch (ScriptletException $e) { $e->printStackTrace(); $this->sendErrorMessage($socket, $e->getStatus(), nameof($e), $e->getMessage()); return $e->getStatus(); } if (!$response->isCommitted()) { $response->flush(); } $response->sendContent(); return $response->statusCode; }
/** * Handle a single request * * @param string method request method * @param string query query string * @param [:string] headers request headers * @param string data post data * @param peer.Socket socket * @return int */ public function handleRequest($method, $query, array $headers, $data, Socket $socket) { $url = new URL('http://' . (isset($headers['Host']) ? $headers['Host'] : $this->serverName) . $query); $request = $this->request->invoke($this->scriptlet, array()); $response = $this->response->invoke($this->scriptlet, array()); // Fill request $request->method = $method; $request->env = $this->env; $request->env['SERVER_PROTOCOL'] = 'HTTP/1.1'; $request->env['REQUEST_URI'] = $query; $request->env['QUERY_STRING'] = substr($query, strpos($query, '?') + 1); $request->env['HTTP_HOST'] = $url->getHost(); if ('https' === $url->getScheme()) { $request->env['HTTPS'] = 'on'; } if (isset($headers['Authorization'])) { if (0 === strncmp('Basic', $headers['Authorization'], 5)) { $credentials = explode(':', base64_decode(substr($headers['Authorization'], 6))); $request->env['PHP_AUTH_USER'] = $credentials[0]; $request->env['PHP_AUTH_PW'] = $credentials[1]; } } $_COOKIE = array(); if (isset($headers['Cookie'])) { foreach (explode(';', $headers['Cookie']) as $cookie) { sscanf(trim($cookie), "%[^=]=%[^\r]", $name, $value); $_COOKIE[$name] = $value; } } $request->setHeaders($headers); try { $this->scriptlet->service($request, $response); } catch (ScriptletException $e) { $e->printStackTrace(); $this->sendErrorMessage($socket, $e->getStatus(), $e->getClassName(), $e->getMessage()); return; } $h = array(); foreach ($response->headers as $header) { list($name, $value) = explode(': ', $header, 2); if (isset($h[$name])) { $h[$name] = array($h[$name], $value); } else { $h[$name] = $value; } } $this->sendHeader($socket, $response->statusCode, '', $h); $socket->write($response->getContent()); return $response->statusCode; }
/** * Setup the class instance. A dsn string must be given with the relevant information * about the server and script: * * Eg: nagios://nagios.xp_framework.net:5667/service_to_monitor * * @param string dsn */ public function setup($dsn) { $url = new URL($dsn); $this->server = $url->getHost(); $this->port = $url->getPort(5667); $this->version = $url->getParam('version', NscaProtocol::VERSION_2); $this->service = trim($url->getPath(), '/'); $this->host = $url->getParam('hostname', System::getProperty('host.name')); if (false !== $url->getParam('domain', false)) { $this->host .= '.' . ltrim($url->getParam('domain'), '.'); } }
/** * Creates a new request object. Uses the system environment and global * variables to put necessary parameters into place. * * @param string method * @param peer.URL url */ protected function newRequest($method, \peer\URL $url) { $q = $url->getQuery(); $_SERVER['REQUEST_METHOD'] = $method; $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; $_SERVER['HTTP_HOST'] = $url->getHost(); $_SERVER['REQUEST_URI'] = $url->getPath('/') . ($q ? '?' . $q : ''); $_SERVER['QUERY_STRING'] = $q; if ('https' === $url->getScheme()) { $_SERVER['HTTPS'] = 'on'; } $_REQUEST = $url->getParams(); }
/** * Creates a new request object * * @param string method * @param peer.URL url * @return scriptlet.HttpScriptletRequest */ protected function newRequest($method, URL $url) { $q = $url->getQuery(''); $req = new \scriptlet\HttpScriptletRequest(); $req->method = $method; $req->env['SERVER_PROTOCOL'] = 'HTTP/1.1'; $req->env['REQUEST_URI'] = $url->getPath('/') . ($q ? '?' . $q : ''); $req->env['QUERY_STRING'] = $q; $req->env['HTTP_HOST'] = $url->getHost(); if ('https' === $url->getScheme()) { $req->env['HTTPS'] = 'on'; } $req->setHeaders(array()); $req->setParams($url->getParams()); return $req; }
private function _sendAuthenticateFrame(URL $url) { $frame = $this->sendFrame(new LoginFrame($url->getUser(), $url->getPassword(), $url->getParam('vhost', $url->getHost()), $url->hasParam('versions') ? explode(',', $url->getParam('versions')) : ['1.0', '1.1'])); if (!$frame instanceof Frame) { throw new ProtocolException('Did not receive frame, got: ' . \xp::stringOf($frame)); } if ($frame instanceof ErrorFrame) { throw new AuthenticationException('Could not establish connection to broker "' . $url->toString() . '": ' . $frame->getBody(), $url->getUser(), strlen($url->getPassword() > 0) ? 'with password' : 'no password'); } if (!$frame instanceof ConnectedFrame) { throw new AuthenticationException('Could not log in to stomp broker "' . $url->toString() . '": Got "' . $frame->command() . '" frame', $url->getUser(), strlen($url->getPassword() > 0) ? 'with password' : 'no password'); } $this->debug('~ Connected to server; server ' . ($frame->getProtocolVersion() ? 'chose protocol version ' . $frame->getProtocolVersion() : 'did not indicate protocol version')); }
public function scalar_parameter_overwritten_by_hash() { $u = new URL('http://unittest.localhost/includes/orderSuccess.inc.php?&glob=1&cart_order_id=1&glob[rootDir]=http://cirt.net/rfiinc.txt?'); $this->assertEquals(['rootDir' => 'http://cirt.net/rfiinc.txt?'], $u->getParam('glob')); }
/** * Parse DSN * * @param string dsn * @return bool success */ protected function _parsedsn($dsn) { if (null === $dsn) { return true; } $u = new URL($dsn); if (!$u->getHost()) { throw new \lang\IllegalArgumentException('DSN parsing failed ["' . $dsn . '"]'); } // Scheme switch (strtoupper($u->getScheme())) { case 'ESMTP': $this->ext = true; break; case 'SMTP': $this->ext = false; break; default: throw new \lang\IllegalArgumentException('Scheme "' . $u->getScheme() . '" not supported'); } // Copy host and port $this->host = $u->getHost(); $this->port = $u->getPort() ? $u->getPort() : 25; // User & password if ($u->getUser()) { $this->auth = $u->getParam('auth', SMTP_AUTH_PLAIN); $this->user = $u->getUser(); $this->pass = $u->getPassword(); } }
/** * Returns string representation for the URL * * The URL is build by using sprintf() and the following * parameters: * <pre> * Ord Fill Example * --- --------------- -------------------- * 1 scheme http * 2 host host.foo.bar * 3 path /foo/bar/index.html * 4 dirname(path) /foo/bar/ * 5 basename(path) index.html * 6 query a=b&b=c * 7 session id cb7978876218bb7 * 8 fraction #test * </pre> * * @return string */ public function getURL() { $sessionId = $this->getSessionId(); if ($sessionId) { $cloned = clone $this; $cloned->setParam('psessionid', $sessionId); $cloned->setSessionId(null); return $cloned->getURL(); } return parent::getURL(); }
public function doCreate() { $req = $this->newRequest('GET', new URL('http://localhost/')); $res = new \scriptlet\HttpScriptletResponse(); $s = newinstance('scriptlet.xml.XMLScriptlet', [], '{ public function needsSession($request) { return true; } }'); $s->service($req, $res); $this->assertEquals(HttpConstants::STATUS_FOUND, $res->statusCode); // Check URL from Location: header contains the session ID with($redirect = new 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([], $redirect->getParams(), $redirect->getURL()); }
public function handleSessionInitializationError() { $req = $this->newRequest('GET', new URL('http://localhost/?psessionid=MALFORMED')); $res = new HttpScriptletResponse(); $s = newinstance('scriptlet.HttpScriptlet', [], ['needsSession' => function ($request) { return true; }, 'handleSessionInitialization' => function ($request) { if (!preg_match('/^a-f0-9$/', $request->getSessionId())) { throw new IllegalArgumentException('Invalid characters in session id'); } parent::handleSessionInitialization($request); }, 'handleSessionInitializationError' => function ($request, $response) { $request->getURL()->addParam('relogin', 1); return $request->session->initialize(null); }]); $s->service($req, $res); $this->assertEquals(HttpConstants::STATUS_FOUND, $res->statusCode); // Check URL from Location: header contains the session ID with($redirect = new 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')); }
/** * Send proxy request * * @param peer.Socket $s Connection to proxy * @param peer.http.HttpRequest $request * @param peer.URL $url */ protected function proxy($s, $request, $url) { $request->setTarget(sprintf('%s://%s%s%s', $url->getScheme(), $url->getHost(), $url->getPort() ? ':' . $url->getPort() : '', $url->getPath('/'))); }
/** * Creates a socket * * @param peer.URL $url * @param string $arg * @return peer.Socket */ protected function newSocket(URL $url, $arg) { return new Socket($url->getHost(), $url->getPort(80)); }
public function string_representation_does_not_include_password() { $u = new URL('http://*****:*****@localhost/path?query#fragment'); $this->assertEquals('http://*****:*****@localhost/path?query#fragment', $u->toString()); }
/** * Get transport implementation for a specific URL * * @param peer.URL url * @return peer.http.HttpTransport * @throws lang.IllegalArgumentException in case the scheme is not supported */ public static function transportFor(URL $url) { sscanf($url->getScheme(), '%[^+]+%s', $scheme, $arg); if (!isset(self::$transports[$scheme])) { throw new \lang\IllegalArgumentException('Scheme "' . $scheme . '" unsupported'); } $transport = self::$transports[$scheme]->newInstance($url, $arg); $transport->setProxy(self::$settings->proxy($scheme)); return $transport; }
public function arrayParamOverwritesParamWithoutFatalError() { $u = new URL('http://unittest.localhost/includes/orderSuccess.inc.php?&glob=1&cart_order_id=1&glob[rootDir]=http://cirt.net/rfiinc.txt?'); $this->assertEquals(array('rootDir' => 'http://cirt.net/rfiinc.txt?'), $u->getParam('glob')); }
/** * Copy authentication if on same host * * @param peer.URL $base * @param peer.URL $url * @return peer.URL The given URL */ private function authenticate($base, $url) { if ($base && $url->getHost() === $base->getHost()) { $url->setUser($base->getUser()); $url->setPassword($base->getPassword()); } return $url; }