Exemplo n.º 1
0
 private function create_named_host_from_string(h\string $literal)
 {
     $end = $literal->search(':');
     if (-1 === $end) {
         $end = $literal->search('/');
     }
     if (-1 === $end) {
         $end = $literal->length();
     }
     $hostname = $literal->behead($end);
     $impl = new h\inet\host();
     $impl->segments = $hostname->explode('.');
     return $impl;
 }
Exemplo n.º 2
0
 public function get_by_email(h\string $email)
 {
     $sql = h\string::format('select * from accounts where email = %s', $this->source->escape($email));
     $rows = $this->source->query($sql);
     $accounts = $this->accounts_from_select($rows);
     return isset($accounts[0]) ? $accounts[0] : null;
 }
Exemplo n.º 3
0
 private function do_routing(context $ctx)
 {
     $path = $ctx->in->uri->path->_to_string();
     $routes = $this->configuration['routes'];
     $ctrl = null;
     $matches = array();
     $segments = h\collection();
     foreach ($routes as $route_pattern => $route_ctrl) {
         $results = h\regex_execute("^{$route_pattern}\$", $path);
         if ($results->is_match()) {
             $ctrl = $route_ctrl;
             $route = $route_pattern;
             $match = $results->iterate_records()[0];
             foreach ($match as $key => $pair) {
                 if (is_string($key)) {
                     $segments[$key] = \rawurldecode($path->slice($pair->begin, $pair->end));
                 }
             }
             break;
         }
     }
     $ctx->segments = $segments;
     $http_method = $ctx->in->method;
     if (is_null($ctrl)) {
         $ctx->out->status = 'HTTP/1.1 405 Method Not Allowed';
         $tpl = 'Non-supported method \'%s\' on \'%s\'';
         $msg = h\string::format($tpl, $http_method, $path);
         $ctx->error_handling['status'] = false;
         $ctx->error_handling['messages'][] = $msg;
         return false;
     }
     $ctx->route = $route;
     return true;
 }
Exemplo n.º 4
0
 public function do_feed(h\string $meat)
 {
     if ($meat->length() < 1) {
         return null;
     }
     if (h\string(':')->is_equal($meat[0])) {
         $meat->behead(1);
     } else {
         return null;
     }
     for ($end_port = 0; \is_numeric((string) $meat[$end_port]); ++$end_port) {
         /* */
     }
     $port = $meat->behead($end_port);
     $port = $port->as_integer();
     if (1 > $port || $port > 65535) {
         throw $this->_exception_format('Specified port \'%d\' is incorrect', $port);
     }
     return $port;
 }
Exemplo n.º 5
0
 public function _to_string()
 {
     $authority = $this->_host->_to_string();
     is_null($this->port) or $authority->append(h\string::format(':%d', $this->port));
     if (0 < $this->password->length()) {
         if (0 < $this->user->length()) {
             $authority->prepend(h\string::format('%s:%s@', $this->user, $this->password));
         } else {
             throw $this->_exception('Malformed authority: password but no username');
         }
     } elseif (0 < $this->user->length()) {
         $authority->prepend(h\string::format('%s@', $this->user));
     }
     return $authority;
 }
Exemplo n.º 6
0
 public function uri_of($story)
 {
     $uri = h\string::format('/stories/%s', rawurlencode($story->title));
     return $uri;
 }
Exemplo n.º 7
0
function validate_http_method($candidate)
{
    static $methods = array('POST' => POST, 'GET' => GET, 'PUT' => PUT, 'DELETE' => DELETE, 'OPTIONS' => OPTIONS);
    if (isset($methods[strtoupper($candidate)])) {
        return $methods[strtoupper($candidate)];
    }
    throw new h\exception(h\string::format('Method verb \'%s\' is not supported', $candidate));
}
Exemplo n.º 8
0
 public function uri_of($account)
 {
     $uri = h\string::format('/accounts/%s', rawurlencode($account->name));
     return $uri;
 }
Exemplo n.º 9
0
 public function create_relative_part(h\string $literal, h\uri\absolute $base = null)
 {
     $scheme_sep_pos = $literal->search(':');
     if (-1 === $scheme_sep_pos) {
         return $this->create_relative($literal, $base);
     }
     $scheme = $literal->head($scheme_sep_pos);
     $scheme_specific_part = $literal->tail($scheme_sep_pos + 1);
     if (h\regex_match(static::scheme, $scheme)) {
         return $this->create_absolute_path($literal);
     }
     if ($this->factories->has_key($scheme)) {
         return $this->factories[$scheme]->do_feed($scheme_specific_part);
     }
     throw $this->_exception_format('Scheme \'%s\' not supported', $scheme);
 }
Exemplo n.º 10
0
 public function escape_json(h\string $sql, $is_nullable = false)
 {
     if (h\string('null')->is_equal($sql)) {
         if (false === $is_nullable) {
             throw $this->_exception('Value is not nullable');
         } else {
             $escaped = h\string('null');
         }
     } else {
         $escaped = h\string::format('\'%s\'', $this->_con->real_escape_string(json_encode($sql->scalar)));
     }
     return $escaped;
 }
Exemplo n.º 11
0
 public function get_by_legacy_path(h\string $legacy_path)
 {
     $db = $this->model->services->get('db');
     $sql = h\string::format('select * from stories s right join legacy_stories ls' . '	on s.id = ls.story_id where path = %s', $db->escape($legacy_path));
     $rows = $db->query($sql);
     $stories = $this->create_stories_from_select($rows);
     return isset($stories[0]) ? $stories[0] : null;
 }
Exemplo n.º 12
0
 public function do_parse(h\string $template)
 {
     $begin = 0;
     $end = 0;
     $opening_delimiter = self::OPENING_DELIMITER;
     $closing_delimiter = self::CLOSING_DELIMITER;
     $parser_stack = h\collection();
     $parser_stack[] = new tag\begin();
     do {
         // Open tag ////////////////////////////////////////////////////////////////////
         $end = $template->search($opening_delimiter, $begin);
         if (-1 === $end) {
             $end = $template->length();
         }
         $element = new tag\raw();
         $element->content = $template->slice($begin, $end);
         $parser_stack[] = $element;
         $begin = $end;
         if ($template->length() === $begin) {
             break;
         }
         // Close tag ///////////////////////////////////////////////////////////////////
         $begin += strlen($opening_delimiter);
         $end = $template->search($closing_delimiter, $begin);
         $first = $template[$begin];
         ++$begin;
         if ($first->is_equal(h\string('#'))) {
             $element = new tag\section();
             $element->name = $template->slice($begin, $end);
             $parser_stack[] = $element;
         } elseif ($first->is_equal(h\string('^'))) {
             $element = new tag\inverted();
             $element->name = $template->slice($begin, $end);
             $parser_stack[] = $element;
         } elseif ($first->is_equal(h\string('/'))) {
             $element = new tag\close();
             $element->name = $template->slice($begin, $end);
             $parser_stack[] = $element;
         } elseif ($first->is_equal(h\string('!'))) {
             $element = new tag\comment();
             $element->content = $template->slice($begin, $end);
             $parser_stack[] = $element;
         } elseif ($first->is_equal(h\string('{'))) {
             if ('}' !== $template[++$end]) {
                 throw 'Ill-formed';
             }
             $element = new tag\unescaped();
             $element->name = $template->slice($begin, $end - 1)->trimmed();
             $parser_stack[] = $element;
         } elseif ($first->is_equal(h\string('&'))) {
             $element = new tag\unescaped();
             $element->name = $template->slice($begin, $end - 1)->trimmed();
             $parser_stack[] = $element;
         } elseif ($first->is_equal(h\string('>'))) {
             throw 'TODO';
         } elseif ($first->is_equal(h\string('='))) {
             throw 'TODO';
         } else {
             $element = new tag\variable();
             $element->name = $template->slice($begin - 1, $end)->trimmed();
             $parser_stack[] = $element;
         }
         $begin = $end = $end + strlen($closing_delimiter);
     } while (true);
     $parser_stack[] = new tag\end();
     return $parser_stack;
 }
Exemplo n.º 13
0
 private function do_deduce_content_type(context $ctx)
 {
     $ctx->out->head['Content-type'] = h\string::format('%s; charset=%s', $this->configuration['content_type']['mime_type'], $this->configuration['content_type']['encoding']);
 }
Exemplo n.º 14
0
 private function do_populate_head(context $ctx)
 {
     $cors_headers = h\c(array('Access-Control-Allow-Credentials' => 'true', 'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE', 'Access-Control-Allow-Headers' => 'Content-Type', 'Access-Control-Allow-Origin' => h\string::format('https://%s', $this->configuration['front_base_url'])));
     $ctx->out->head->join($cors_headers);
 }
Exemplo n.º 15
0
 public function is_scheme_supported(h\string $scheme)
 {
     return in_array($scheme->to_lower(), array('http', 'https'));
 }
Exemplo n.º 16
0
 public function _to_string()
 {
     $literal = h\string::format('%s:%s', $this->scheme, $this->scheme_specific_part);
     return $literal;
 }
Exemplo n.º 17
0
 public function do_render(h\string $name, $c)
 {
     $e = $this->escaper;
     include h\string::format('%s/%s.php', $this->path, $name);
 }
Exemplo n.º 18
0
 private function copy_and_convert(h\string $text)
 {
     return $text->to_converted($this->charset);
 }
Exemplo n.º 19
0
 protected function &_get_pattern()
 {
     $pattern = h\string::format("%1\$s%2\$s%1\$s", $this->delimiter, $this->_pattern);
     return $pattern;
 }
Exemplo n.º 20
0
 protected function copy_and_convert(h\string $from)
 {
     return $from->charset != $this->charset ? $from->to_converted($this->charset) : clone $from;
 }
Exemplo n.º 21
0
 public function do_create_net_path(h\string $meat)
 {
     $impl = new net_path();
     $slashes = $meat->behead(2);
     if (!h\string('//')->is_equal($slashes)) {
         throw $this->_exception('Not a net path');
     }
     $impl->authority->assign($this->master->factories['authority']->do_feed($meat));
     if (0 === $meat->search(h\string('/'))) {
         $impl->path = $this->do_feed($meat);
     } else {
         $impl->path->set_impl(new empty_path());
     }
     return $impl;
 }
Exemplo n.º 22
0
 private function location($uri)
 {
     $to = new h\http\url(h\string::format('%s://%s%s', 'http', 'blog.localhost', $uri));
     h\http\response_methods::location($this->context->out, $to);
 }