pike.git / lib / modules / Protocols.pmod / HTTP.pmod / Session.pike

version» Context lines:

pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:1:   #pike __REAL_VERSION__    - // $Id: Session.pike,v 1.6 2003/03/12 09:58:18 agehall Exp $ + // $Id: Session.pike,v 1.7 2003/03/13 07:52:20 mirar Exp $      import Protocols.HTTP;      typedef string|Standards.URI|SessionURL URL;      //! The number of redirects to follow, if any.   //! This is the default to the created Request objects.   //!   //! A redirect automatically turns into a GET request,   //! and all header, query, post or put information is dropped.   //!   //! Default is 20 redirects. A negative number will mean infinity.   //! @bugs   //! Loops will currently not be detected, only the limit   //! works to stop loops.   //! @seealso   //! @[Request.follow_redirects]      int follow_redirects=20;    -  + //! Default HTTP headers. +  + mapping default_headers = ([ +  "user-agent":"Mozilla/5.0 (compatible; MSIE 6.0; Pike HTTP client)" +  " Pike/"+__REAL_MAJOR__+"."+__REAL_MINOR__+"."+__REAL_BUILD__, + ]); +    class Request   {   //! Raw connection object    Query con;      //! URL requested (set by prepare_method).   //! This will update according to followed redirects.    Standards.URI url_requested;      //! Number of redirects to follow;
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:60:   //! and returns the parameters to use with @[do_sync],   //! @[do_async] or @[do_thread].   //!   //! This method will also use cookie information from the   //! parent @[Session], and may reuse connections (keep-alive).       array(string|int|mapping) prepare_method(    string method,    URL url,    void|mapping query_variables, -  void|mapping request_headers, +  void|mapping extra_headers,    void|string data)    {    if(stringp(url))    url=Standards.URI(url);    url_requested=url;    -  if(!request_headers) -  request_headers = ([]); -  +    #if constant(SSL.sslfile)    if(url->scheme!="http" && url->scheme!="https")    error("Protocols.HTTP can't handle %O or any other "    "protocols than HTTP or HTTPS\n",    url->scheme);       con_https= (url->scheme=="https")? 1 : 0;   #else    if(url->scheme!="http" )    error("Protocols.HTTP can't handle %O or any other "    "protocol than HTTP\n",    url->scheme);   #endif -  -  if(!request_headers) -  request_headers = ([]); -  mapping default_headers = ([ -  "user-agent":"Mozilla/5.0 (compatible; MSIE 6.0; Pike HTTP client)" -  " Pike/"+__REAL_MAJOR__+"."+__REAL_MINOR__+"."+__REAL_BUILD__, -  "host":url->host, -  "connection": -  (time_to_keep_unused_connections<=0)?"Close":"Keep-Alive", -  ]); +  mapping request_headers = default_headers;    if (url->referer)    request_headers->referer=(string)url->referer;       if(url->user || url->passwd) -  default_headers->authorization = "Basic " +  request_headers->authorization = "Basic "    + MIME.encode_base64(url->user + ":" +    (url->password || "")); -  request_headers = default_headers | request_headers; +     -  +  request_headers->connection= +  (time_to_keep_unused_connections<=0)?"Close":"Keep-Alive"; +  +  request_headers->host=url->host; +  +  if (extra_headers) +  request_headers|=extra_headers; +     array v=get_cookies(url_requested); -  if (v) +  if (v && sizeof(v))    if (request_headers->cookie)    if (!arrayp(request_headers->cookie))    request_headers->cookie=    ({request_headers->cookie})+v;    else    request_headers->cookie=    request_headers->cookie+v;    else    request_headers->cookie=v;   
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:430:       if (sizeof(v)<1) return;    v=v[0];    if (sizeof(v)<1) return;    [key,data]=v[0];       foreach (v[1..];;[string what,string value])    switch (lower_case(what))    {    case "expires": -  expires=Calendar.ISO.parse("%e, %D %M %Y %h:%m:%s %z",value) +  werror("%O\n",value); +  expires= +  (Calendar.ISO.parse("%e, %D %M %Y %h:%m:%s %z",value)|| +  Calendar.ISO.parse("%e, %D-%M-%y %h:%m:%s %z",value) )    ->unix_time();    break;       case "path":    path=value;    break;    }    }       string encode()
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:739:    destruct(query->con);    }    destruct(query);    if (!--connections_host_n[lookup])    m_delete(connections_host_n,lookup);    freed_connection(lookup);   }      // ================================================================    - Request do_method(string method, + Request do_method_url(string method,    string url,    void|mapping query_variables, -  void|string data) +  void|string data, +  void|mapping extra_headers)   { -  mapping extra_headers=0; +     if (method=="POST") -  extra_headers=(["content-type":"application/x-www-form-urlencoded"]); +  extra_headers= +  (extra_headers||([]))| +  (["content-type":"application/x-www-form-urlencoded"]);       Request p=Request();    p->do_sync(p->prepare_method(method,url,query_variables,    extra_headers,data));    return p;   }    - // Request|array(string) generic_do_method( - // string method, - // string mode, - // URL url, - // string|mapping|function ...misc) - // { - // array args; - // mapping query_variables=0; - // mapping extra_headers=0; - // string data=0; - // - // misc+=({0,0,0,0}); - // - // switch (lower_case(method)) - // { - // case "get": - // case "delete": - // query_variables=misc[0]; - // misc=misc[1..]; - // break; - // case "put": - // query_variables=misc[0]; - // data=misc[1]; - // if (data && !stringp(data)) - // error("Bad argument 5 to Session.put*, expected string\n"); - // misc=misc[2..]; - // break; - // case "post": - // if (!mappingp(misc[0])) - // error("Bad argument 4 to Session.put*, expected mapping\n"); - // data=http_encode_query(misc[0]); - // extra_headers=(["content-type":"application/x-www-form-urlencoded"]); - // break; - // default: - // error("unknown HTTP method %O (expected get, post, put or delete)\n"); - // } - // if (query_variables && !mappingp(query_variables)) - // error("Bad argument 4 to Session."+method+"*, " - // "expected mapping or void\n"); - // - // args=p->prepare_method(upper_case(method),url,query_variables, - // extra_headers,data); - // } -  +    //! @decl Request get_url(URL url, @   //! void|mapping query_variables)   //! @decl Request post_url(URL url, @   //! mapping query_variables)   //! @decl Request put_url(URL url,string file, @   //! void|mapping query_variables)   //! @decl Request delete_url(URL url, @   //! void|mapping query_variables)   //! Sends a HTTP GET, POST, PUT or DELETE request to the server in the URL   //! and returns the created and initialized @[Request] object.   //! 0 is returned upon failure.   //!   Request get_url(URL url,    void|mapping query_variables)   { -  return do_method("GET", url, query_variables); +  return do_method_url("GET", url, query_variables);   }      Request put_url(URL url,    void|string file,    void|mapping query_variables)   { -  return do_method("PUT", url, query_variables, file); +  return do_method_url("PUT", url, query_variables, file);   }      Request delete_url(URL url,    void|mapping query_variables)   { -  return do_method("DELETE", url, query_variables); +  return do_method_url("DELETE", url, query_variables);   }      Request post_url(URL url,    mapping query_variables)   { -  return do_method("POST", url, 0, +  return do_method_url("POST", url, 0,    http_encode_query(query_variables));   }         //! @decl array(string) get_url_nice(URL url, @   //! mapping query_variables)   //! @decl string get_url_data(URL url, @   //! mapping query_variables)   //! @decl array(string) post_url_nice(URL url, @   //! mapping query_variables)
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:887:   string post_url_data(URL url,    mapping query_variables)   {    Request z = post_url(url, query_variables);    return z && z->data();   }      // ================================================================   // async operation    - Request async_do_method(string method, + Request async_do_method_url(string method,    URL url,    void|mapping query_variables,    void|string data, -  +  void|mapping extra_headers,    function callback_headers_ok,    function callback_data_ok,    function callback_fail,    array callback_arguments)   {    if(stringp(url)) url=Standards.URI(url);       Request p=Request();       p->set_callbacks(callback_headers_ok,    callback_data_ok,    callback_fail,    p,@callback_arguments);    -  mapping extra_headers=0; +     if (method=="POST") -  extra_headers=(["content-type":"application/x-www-form-urlencoded"]); +  extra_headers= +  (extra_headers||([]))| +  (["content-type":"application/x-www-form-urlencoded"]);       p->do_async(p->prepare_method(method,url,query_variables,    extra_headers,data));    return p;   }         //! @decl Request async_get_url(URL url,   //! void|mapping query_variables,   //! function callback_headers_ok,
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:965:   //!   //! The created Request object is returned.      Request async_get_url(URL url,    void|mapping query_variables,    function callback_headers_ok,    function callback_data_ok,    function callback_fail,    mixed ...callback_arguments)   { -  return async_do_method("GET", url, query_variables,0, +  return async_do_method_url("GET", url, query_variables,0,0,    callback_headers_ok,callback_data_ok,    callback_fail,callback_arguments);   }      Request async_put_url(URL url,    void|string file,    void|mapping query_variables,    function callback_headers_ok,    function callback_data_ok,    function callback_fail,    mixed ...callback_arguments)   { -  return async_do_method("PUT", url, query_variables, file, +  return async_do_method_url("PUT", url, query_variables, file,0,    callback_headers_ok,callback_data_ok,    callback_fail,callback_arguments);   }      Request async_delete_url(URL url,    void|mapping query_variables,    function callback_headers_ok,    function callback_data_ok,    function callback_fail,    mixed ...callback_arguments)   { -  return async_do_method("DELETE", url, query_variables, 0, +  return async_do_method_url("DELETE", url, query_variables, 0,0,    callback_headers_ok,callback_data_ok,    callback_fail,callback_arguments);   }      Request async_post_url(URL url,    mapping query_variables,    function callback_headers_ok,    function callback_data_ok,    function callback_fail,    mixed ...callback_arguments)   { -  return async_do_method("POST", url, 0, -  http_encode_query(query_variables), +  return async_do_method_url("POST", url, 0, +  http_encode_query(query_variables),0,    callback_headers_ok,callback_data_ok,    callback_fail,callback_arguments);   }    -  + // ---------------------------------------------------------------- + //   //! Class to store URL+referer      class SessionURL   {    inherit Standards.URI;      //! the referer to this URL    URL referer;      //! instantiate a SessionURL object;   //! when fed to Protocols.HTTP.Session calls, will add   //! referer to the HTTP handshaking variables    void create(URL uri,    URL base_uri,    URL _referer)    {    ::create(uri,base_uri);    referer=_referer;    }   }