page.onResourceRequested = function(requestData) {
// request data
}
page.onResourceRequested = function(request) {
request.cancel(); // cancel the request
request.data // access the request data like in the old API
}
Initial thoughts:
1. I'm not familiar enough with QtWebKit to comment on the implementation but I love the functionality.
2. I'd prefer to see `cancel` renamed to `abort`, which is the common term used for this HTTP/XHR functionality.
3. Is `data` the right word? I think its OK but it makes me think of the request/response body instead of what I would generally consider the request/response metadata.
4. More broadly (beyond just this commit), it would be good to get our HTTP classes/models more aligned with Node.js's HTTP classes where possible.
~~James
--
You received this message because you are subscribed to the Google Groups "phantomjs" group.
Visit this group at http://groups.google.com/group/phantomjs?hl=en.
Oh yes, I also really like the network resource error callback idea. :)
I think Ariya's proposal for an alternate method signature is a good idea, too.
I would still advise renaming `cancel` to `abort`, and otherwise working to align our HTTP models with Node.js's where feasible.
~~James
Vivek --
Check the updated code in Vitaliy's branch: the handler for `onResourceRequested` now takes a 2nd argument which exposes the `abort` method. The 1st argument remains in its original form instead of adding a layer with `abort` and `data`.
~~James
--
--
You received this message because you are subscribed to the Google Groups "phantomjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phantomjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Vitaliy can answer more authoritatively but I would say this behavior is [mostly] expected as the `setTimeout` call causes the callback to become asynchronous and skip to the next event loop, which is AFTER the `onResourceRequested` callback handler has finished. This also implies that the request was already issued and cannot be aborted anymore. Think of this `abort` implementation as more of a `prevent`/`disallow` concept: it is only effective/possible BEFORE the HTTP call is allowed to be sent.
Likewise, because of the nature of this API, I would assume the JsNetworkRequest object is disposed of just a few lines after the resolution of the `onResourceRequested` callback, thus why the `req` object becomes `undefined` for you. The mixed C++/JS nature of Qt makes it more complex as a simple closure like yours does not guarantee the survival of the object.
~~James
Sure, your use case makes sense.
Oddly enough, I don't think Qt/WebKit offers a way to specify the max timeout for QNetworkRequest/QNetworkReply objects, which would be the ideal approach for your scenario.
As such, you could actually need access to the QNetworkReply object [instead of a proxy to the QNetworkRequest object as here] to abort the request after it has been sent. We could probably achieve this with a similar proxy object for the `onResourceReceived` callbacks but those callbacks only get triggered on first byte received and last byte received. Therefore, these callbacks are not useful in this scenario if it takes a long time to establish the initial connection; only if the actual data transfer is slow/long. Perhaps we could workaround this scenario with either/both a "onResourceRequestSent" or a polling "onResourceProgress" callback?
I think there is actually a pending PR for the latter but I haven't looked at it yet. I'm unsure if this callback would trigger before or only after the initial `onResourcedReceived` callback.
~~James
I've looked around the web and indeed it seems that there is no built-in way to handle network timeout. There is even an open bug for it here: https://bugreports.qt-project.org/browse/QTBUG-3443. Most people facing this issue handle it by initializing a timer after the request is sent that aborts the request after the given timeout. This is impossible to do from the Javascript code because of the earlier mentioned problem that there is no reference to the request object once running.
Handling timeouts is something normal for networking libraries IMO, compare for example libraries like Python's Requests or Ruby's Net::HTTP. It would be nice I think if PhantomJS implements a timer internally that handles timeouts. Then e.g. one can set a maximum timeout on page.open that on timeout aborts the request and throws an error so the developer can handle it.
--