/** * 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); } }
/** * 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(); }
/** * 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; }
/** * 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; }
public function sqliteUrl() { $u = new URL('sqlite:///path/to/file.db'); $this->assertEquals(null, $u->getHost()); $this->assertEquals('/path/to/file.db', $u->getPath()); }
/** * 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(); } }
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)); }
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')); }
/** * 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; }