pike.git / lib / modules / Standards.pmod / URI.pike

version» Context lines:

pike.git/lib/modules/Standards.pmod/URI.pike:73:    // IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet    // reg-name = *( unreserved / pct-encoded / sub-delims )    sscanf(host_port, "%[^:]%*[:]%d", host, port);    }    DEBUG("parse_authority(): host=%O, port=%O", host, port);   }      // Inherit all properties except raw_uri and base_uri from the URI uri. :-)   protected void inherit_properties(this_program uri)   { +  sprintf_cache = ([]);    authority = uri->authority;    scheme = uri->scheme;    user = uri->user; password = uri->password;    host = uri->host; query = uri->query;    port = uri->port;    path = uri->path; fragment = uri->fragment;   }      //! Compare this URI to something, in a canonical way.   //! @param something   //! Compare the URI to this   int `==(mixed something)   { -  +  if( !objectp( something ) || object_program(something) < this_program ) +  return false; +  Standards.URI other = [object(Standards.URI)]something; +  // For protocols with host/port/user/password we do lower_case on +  // the host when comparing, and use the port according to RFC 2396 +  // section 6.    return -  _sprintf('t') == sprintf("%t", something) && -  _sprintf('x') == sprintf("%x", [object]something); +  ((host +  && other->host +  && lower_case(other->host) == lower_case(host) +  && other->port == port +  && other->user == user +  && other->password == password) +  || (other->authority == authority)) +  && other->path == path +  && other->query == query +  && other->scheme == scheme +  && other->fragment == fragment;   }      string combine_uri_path(string base, string rel)   {    string buf;       // RFC 2396, §5.2.6:    // a) All but the last segment of the base URI's path component is    // copied to the buffer. In other words, any characters after the    // last (right-most) slash character, if any, are excluded.
pike.git/lib/modules/Standards.pmod/URI.pike:179:   //! The resolving is performed according to the guidelines   //! outlined by RFC 2396, Uniform Resource Identifiers (URI): Generic Syntax.   //! @param base_uri   //! Set the new base URI to this.   //! @throws   //! An exception is thrown if the @[uri] is a relative URI or only a   //! fragment, and missing a @[base_uri].   void reparse_uri(this_program|string|void base_uri)   {    string uri = raw_uri; -  +     if(stringp(base_uri))    {    DEBUG("cloning base URI %O", base_uri);    this_program::base_uri = this_program(base_uri); // create a new URI object    }    else    this_program::base_uri = [object(this_program)]base_uri;       // RFC 2396, §5.2:    // 1) The URI reference is parsed into the potential four components and
pike.git/lib/modules/Standards.pmod/URI.pike:290:    // starts with a scheme name, then the reference is interpreted as an    // absolute URI and we are done. Otherwise, the reference URI's    // scheme is inherited from the base URI's scheme component.    if(scheme)    {    if(authority)    parse_authority();       DEBUG("Scheme found! RFC 2396, §5.2, step 3 "    "says we're absolute. Done!"); +  sprintf_cache['s'] = raw_uri;    return;    }    scheme = this_program::base_uri->scheme;    DEBUG("Inherited scheme %O from base URI", scheme);       if(authority)    parse_authority();          // 4) If the authority component is defined, then the reference is a
pike.git/lib/modules/Standards.pmod/URI.pike:356:   //! When uri is another URI object, the created   //! URI will inherit all properties of the supplied uri   //! except, of course, for its base_uri.   //! @throws   //! An exception is thrown if the @[uri] is a relative URI or only a   //! fragment, and missing a @[base_uri].   void create(this_program|string uri,    this_program|string|void base_uri)   {    DEBUG("create(%O, %O) called!", uri, base_uri); +  sprintf_cache = ([]);    if(stringp(uri))    raw_uri = [string]uri; // Keep for future runs of reparse_uri after a base_uri change    else if(objectp(uri)) // If uri is 0, we want to inherit from the base_uri. -  { +     raw_uri = uri->raw_uri; -  inherit_properties([object(this_program)]uri); -  } +        reparse_uri(base_uri);   }      //! Assign a new value to a property of URI   //! @param property   //! When any of the following properties are used, properties that   //! depend on them are recalculated: user, password, host, port, authority, base_uri.   //! @param value   //! The value to assign to @[property]   mixed `->=(string property, mixed value) { return `[]=(property, value); }   mixed `[]=(string property, mixed value)   {    DEBUG("`[]=(%O, %O)", property, value); -  +  sprintf_cache = ([]);    switch(property)    {    case "user":    case "password":    case "host":    if(!stringp(value) && value!=0)    error("%s value not string.\n", property);    case "port":    ::`[]=(property, value);    authority = (user ? user + (password ? ":" + password : "") + "@" : "") +
pike.git/lib/modules/Standards.pmod/URI.pike:472:    variables[var] = val;    else    variables[pair] = 0;    }       return variables;   }      //! Sets the query variables from the provided mapping.   void set_query_variables(mapping(string:string) vars) { +  sprintf_cache = ([]);    variables = vars;    if(!sizeof(vars))    query = 0;    else    {    query = "";    foreach( vars; string var; string val )    {    if( sizeof(query) )    query += "&";
pike.git/lib/modules/Standards.pmod/URI.pike:537:    return query;   }      //! Return the path and query part of the URI, coded according to RFC   //! 1738.   string get_http_path_query() {    string q = get_http_query();    return http_encode(((path||"")/"/")[*])*"/" + (q?"?"+q:"");   }    + int __hash() { return hash_value(_sprintf('s')); }    -  + private mapping(int:string) sprintf_cache = ([]);   string _sprintf(int how, mapping|void args)   { -  +  if( how == 't' ) return "Standards.URI"; +  if( string res = sprintf_cache[how] ) +  return res;    string look, _host = host, getstring; -  switch(how) -  { -  case 't': -  return "Standards.URI"; -  -  case 'x': // A case-mangling version, especially suited for readable hash values -  if(_host) _host = lower_case(_host); -  default: -  case 's': -  case 'O': -  getstring = (path||"") + -  (query ? "?" + query : ""); +  if(how == 'x' && _host) +  _host = lower_case(_host); +  getstring = (path||"") + (query ? "?" + query : ""); +  if(args && args->flag_left) +  return getstring;    look =    (scheme?(scheme + ":"):"") +    (authority ?    "//" +    (user ? user + (password ? ":" + password : "") + "@" : "") +    (_host?(has_value(_host, ":")?("["+_host+"]"):_host):"") +    (port != Protocols.Ports.tcp[scheme] ? ":" + port : "") : ("")) +    getstring +    (fragment ? "#" + fragment : ""); -  break; -  } +        if(how == 'O') -  return "URI(\"" + look + "\")"; -  else -  if(args && args->flag_left) -  return getstring; -  else -  return look; +  look=sprintf("URI(%q)", look); +  return sprintf_cache[how]=look;   }      // Master codec API function. Allows for serialization with   // encode_value.   mapping(string:string|int|this_program) _encode()   {   #define P(X) #X:X    return ([    P(scheme),    P(authority),