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.4 2003/02/22 10:21:18 mirar Exp $ + // $Id: Session.pike,v 1.5 2003/03/04 17:01:57 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.
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:56:      //! Prepares the HTTP Query object for the connection,   //! 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, -  string|Standards.URI url, +  URL url,    void|mapping query_variables,    void|mapping request_headers,    void|string data)    {    if(stringp(url))    url=Standards.URI(url);    url_requested=url;       if(!request_headers)    request_headers = ([]);
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:91:       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",    ]); +  if (url->referer) +  request_headers->referer=(string)url->referer;    -  +     if(url->user || url->passwd)    default_headers->authorization = "Basic "    + MIME.encode_base64(url->user + ":" +    (url->password || ""));    request_headers = default_headers | request_headers;       array v=get_cookies(url_requested);    if (v)    if (request_headers->cookie)    if (!arrayp(request_headers->cookie))
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:527:    map(cookies,set_cookie,0);   }      //! Get the cookies that we should send to this server,   //! for this url. They are presented in the form suitable   //! for HTTP headers (as an array).   //! This will also take in count expiration of cookies,   //! and delete expired cookies from the @[Session] unless   //! @[no_delete] is true.    - array(string) get_cookies(Standards.URI for_url, + array(string) get_cookies(Standards.URI|SessionURL for_url,    void|int(0..1) no_delete)   {    mapping(string:Cookie) sc=    cookie_lookup[for_url->host+":"+for_url->port]||([]);       array(string) res=({});    int now=time();       foreach (sc;string key;Cookie c)    {
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:754:       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, - // string|Standards.URI url, + // 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))
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:795:   // 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(string|Standards.URI url, @ + //! @decl Request get_url(URL url, @   //! void|mapping query_variables) - //! @decl Request post_url(string|Standards.URI url, @ + //! @decl Request post_url(URL url, @   //! mapping query_variables) - //! @decl Request put_url(string|Standards.URI url,string file, @ + //! @decl Request put_url(URL url,string file, @   //! void|mapping query_variables) - //! @decl Request delete_url(string|Standards.URI url, @ + //! @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(string|Standards.URI url, + Request get_url(URL url,    void|mapping query_variables)   {    return do_method("GET", url, query_variables);   }    - Request put_url(string|Standards.URI url, + Request put_url(URL url,    void|string file,    void|mapping query_variables)   {    return do_method("PUT", url, query_variables, file);   }    - Request delete_url(string|Standards.URI url, + Request delete_url(URL url,    void|mapping query_variables)   {    return do_method("DELETE", url, query_variables);   }    - Request post_url(string|Standards.URI url, + Request post_url(URL url,    mapping query_variables)   {    return do_method("POST", url, 0,    http_encode_query(query_variables));   }       - //! @decl array(string) get_url_nice(string|Standards.URI url, @ + //! @decl array(string) get_url_nice(URL url, @   //! mapping query_variables) - //! @decl string get_url_data(string|Standards.URI url, @ + //! @decl string get_url_data(URL url, @   //! mapping query_variables) - //! @decl array(string) post_url_nice(string|Standards.URI url, @ + //! @decl array(string) post_url_nice(URL url, @   //! mapping query_variables) - //! @decl string post_url_data(string|Standards.URI url, @ + //! @decl string post_url_data(URL url, @   //! mapping query_variables)   //! Returns an array of @tt{({content_type,data})@} and just the data   //! string respective,   //! after calling the requested server for the information.   //! 0 is returned upon failure.   //!   //! post* is similar to the @[get_url()] class of functions,   //! except that the query variables is sent as a POST request instead   //! of as a GET.   //!    - array(string) get_url_nice(string|Standards.URI url, + array(string) get_url_nice(URL url,    void|mapping query_variables)   {    Request c = get_url(url, query_variables);    return c && ({ c->headers()["content-type"], c->data() });   }    - string get_url_data(string|Standards.URI url, + string get_url_data(URL url,    void|mapping query_variables)   {    Request z = get_url(url, query_variables);    return z && z->data();   }       - array(string) post_url_nice(string|Standards.URI url, + array(string) post_url_nice(URL url,    mapping query_variables)   {    object c = post_url(url, query_variables);    return c && ({ c->headers["content-type"], c->data() });   }    - string post_url_data(string|Standards.URI url, + 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, -  Standards.URI|string url, +  URL url,    void|mapping query_variables,    void|string data,    function callback_headers_ok,    function callback_data_ok,    function callback_fail,    array callback_arguments)   {    if(stringp(url)) url=Standards.URI(url);       Request p=Request();
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:912:    mapping extra_headers=0;    if (method=="POST")    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(string|Standards.URI url, + //! @decl Request async_get_url(URL url,   //! void|mapping query_variables,   //! function callback_headers_ok,   //! function callback_data_ok,   //! function callback_fail,   //! mixed... callback_arguments); - //! @decl Request async_put_url(string|Standards.URI url, + //! @decl 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); - //! @decl Request async_delete_url(string|Standards.URI url, + //! @decl Request async_delete_url(URL url,   //! void|mapping query_variables,   //! function callback_headers_ok,   //! function callback_data_ok,   //! function callback_fail,   //! mixed... callback_arguments); - //! @decl Request async_post_url(string|Standards.URI url, + //! @decl Request async_post_url(URL url,   //! mapping query_variables,   //! function callback_headers_ok,   //! function callback_data_ok,   //! function callback_fail,   //! mixed... callback_arguments);   //!   //! Sends a HTTP GET, POST, PUT or DELETE request to the server in   //! the URL asynchroneously, and call the corresponding callbacks   //! when result arrives (or not). The callbacks will receive   //! the created Request object as first argument, then
pike.git/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike:955:   //! headers.   //!   //! @[callback_data_ok] is called when the HTTP request has been   //! received completely, data and all.   //!   //! @[callback_fail] is called when the HTTP request has failed,   //! on a TCP/IP or DNS level, or has recieved a forced timeout.   //!   //! The created Request object is returned.    - Request async_get_url(string|Standards.URI url, + 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,    callback_headers_ok,callback_data_ok,    callback_fail,callback_arguments);   }    - Request async_put_url(string|Standards.URI url, + 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,    callback_headers_ok,callback_data_ok,    callback_fail,callback_arguments);   }    - Request async_delete_url(string|Standards.URI url, + 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,    callback_headers_ok,callback_data_ok,    callback_fail,callback_arguments);   }    - Request async_post_url(string|Standards.URI url, + 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),    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; +  } + }