Roxen.git / server / etc / modules / HTTPClient.pmod

version» Context lines:

Roxen.git/server/etc/modules/HTTPClient.pmod:30:   //! });   //!   //! args->on_failure(lambda (HTTPClient.Result resp) {   //! werror("Failed request for %O (%d %s)\n", resp->url,   //! resp->status, resp->status_description);   //! });   //!   //! HTTPClient.async_get("http://domain.com", args);   //! @endcode    - //#define HTTP_CLIENT_DEBUG + #define HTTP_CLIENT_DEBUG      #ifdef HTTP_CLIENT_DEBUG   # define TRACE(X...)werror("%s:%d: %s",basename(__FILE__),__LINE__,sprintf(X))   #else   # define TRACE(X...)0   #endif      protected constant DEFAULT_MAXTIME = 60;      
Roxen.git/server/etc/modules/HTTPClient.pmod:120:    Thread.Queue q;    mixed co_maxtime;       if (args->maxtime) s->maxtime = args->maxtime;    if (args->timeout) s->timeout = args->timeout;       if (!async) {    q = Thread.Queue();    }    -  qr = s->async_do_method_url(http_method, uri, -  args->variables, -  args->data, -  args->headers, -  0, /* headers received callback */ -  lambda (Result ok) { -  res = ok; +  function(Result:void) cb = lambda (Result r) { +  TRACE("Got callback: %O\n", r); +  res = r;    q && q->write("@");    if (async) { -  if (args->on_success) { +  if (r->ok && args->on_success) {    args->on_success(res);    } -  qr = 0; -  s = 0; +  else if (!r->ok && args->on_failure) { +  args->on_failure(r);    } -  }, -  lambda (Result fail) { -  res = fail; -  q && q->write("@"); -  if (async) { -  if (args->on_failure) { -  args->on_failure(res); -  } +     qr = 0;    s = 0;    } -  }, +  }; +  +  qr = s->async_do_method_url(http_method, uri, +  args->variables, +  args->data, +  args->headers, +  0, // headers received callback +  cb, // ok callback +  cb, // fail callback    ({}));       if (!query_has_maxtime()) { -  TRACE("External timeout\n"); +  TRACE("No maxtime in Protocols.HTTP.Query. Set external max timeout\n");    co_maxtime = call_out(lambda () {    TRACE("Timeout callback: %O\n", qr);       res = Failure(([    "status" : 504,    "status_desc" : "Gateway timeout",    "host" : qr->con->host,    "headers" : qr->con->headers,    "url" : qr->url_requested    ]));
Roxen.git/server/etc/modules/HTTPClient.pmod:211:   protected int(-1..1) _query_has_maxtime = -1;   protected bool query_has_maxtime()   {    if (_query_has_maxtime != -1) {    return _query_has_maxtime == 1;    }       Protocols.HTTP.Query q = Protocols.HTTP.Query();    _query_has_maxtime = (int) has_index(q, "maxtime");    destruct(q); -  return _query_has_maxtime; +  return _query_has_maxtime == 1;   }         class Arguments   {    //! Data fetch timeout    int timeout;       //! Request timeout    int maxtime;    -  //! The URL to fetch -  Protocols.HTTP.Session.URL url; -  +     //! Additional request headers    mapping(string:string) headers;       //! Query variables    mapping(string:mixed) variables;       //! POST data    void|string|mapping data;       //! Callback to call on successful async request    function(Result:void) on_success;       //! Callback to call on failed async request    function(Result:void) on_failure;       //! If @[args] is given the indices that match any of this object's    //! members will set those object members to the value of the -  //! corresponding mapping index. +  //! corresponding mapping member.    protected void create(void|mapping(string:mixed) args)    {    if (args) {    foreach (args; string key; mixed val) {    if (has_index(this, key)) {    this[key] = val;    }    }    }    }
Roxen.git/server/etc/modules/HTTPClient.pmod:385:    Request async_do_method_url(string method,    URL url,    void|mapping query_variables,    void|string|mapping 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); +  } +  +  // Due to a bug in Protocols.HTTP.Session which is fixed in Pike 8.1 +  // but not yet in 8.0. (2016-05-20) +  if (!extra_headers || !extra_headers->host || !extra_headers->Host) { +  extra_headers = extra_headers || ([]); +  +  if (url->scheme == "http" && url->port != 80) { +  extra_headers->host = url->host + ":" + url->port; +  } +  else if (url->scheme == "https" && url->port != 443) { +  extra_headers->host = url->host + ":" + url->port; +  } +  +  if (!sizeof(extra_headers)) { +  extra_headers = 0; +  } +  +  TRACE("Host header set?: %O\n", extra_headers); +  } +     return ::async_do_method_url(method, url, query_variables, data,    extra_headers, callback_headers_ok,    callback_data_ok, callback_fail,    callback_arguments);    }          class Request    {    inherit parent::Request;    -  protected void async_fail(object q) +  protected void async_fail(SessionQuery q)    { -  TRACE("fail q: %O\n", q); +  TRACE("fail q: %O -> %O\n", q, ::url_requested);       mapping ret = ([    "status" : q->status,    "status_desc" : q->status_desc,    "host" : q->host,    "headers" : copy_value(q->headers),    "url" : ::url_requested    ]);    -  +  TRACE("Ret: %O\n", ret); +     // clear callbacks for possible garbation of this Request object    con->set_callbacks(0, 0);       function fc = fail_callback;    set_callbacks(0, 0, 0); // drop all references    extra_callback_arguments = 0;       if (fc) {    fc(Failure(ret));    }    }       -  protected void async_ok(object q) +  protected void async_ok(SessionQuery q)    {    TRACE("async_ok: %O -> %s!\n", q->host, ::url_requested);       ::check_for_cookies();       if (con->status >= 300 && con->status < 400 &&    con->headers->location && follow_redirects)    {    Standards.URI loc = Standards.URI(con->headers->location,url_requested); -  TRACE("New location: %O\n", loc); +  TRACE("New location: %O -> %O (%O)\n", url_requested, loc, con->headers);       if (loc->scheme == "http" || loc->scheme == "https") { -  destroy(); // clear +  con->set_callbacks(0, 0); +  ::destroy(); // clear    follow_redirects--;    do_async(prepare_method("GET", loc));    return;    }    }       // clear callbacks for possible garbation of this Request object    con->set_callbacks(0, 0);       if (data_callback) {
Roxen.git/server/etc/modules/HTTPClient.pmod:477:    if (data_callback) {    data_callback(Success(ret));    }       extra_callback_arguments = 0;    }       void destroy()    {    TRACE("Destructor called in Request: %O\n", ::url_requested); -  ::set_callbacks(0, 0, 0, 0); +  ::set_callbacks(0, 0, 0);    ::destroy();    }    }          class SessionQuery    {    inherit parent::SessionQuery;       protected void create()