According to MDN:
The onfetch property of the ServiceWorkerGlobalScope interface is an event handler fired whenever a fetch event occurs (usually when the WindowOrWorkerGlobalScope.fetch() method is called.)
Does this mean that service worker can only intercept requests sent by fetch() from main thread? How about ajax requests? Is there any way to intercept them through service worker?
Fetch events are sent when any resource is being fetched through HTTP, be it scripts, medias, links etc., and XHR.
So yes, a ServiceWorker should intercept XHR requests (though synchronous ones are only intercepted in Firefox...)
Related
I am using WebView2 and am looking to stop cookies from being stored when they are received in responses to third-party resource requests.
WebView2 exposes the CoreWebView2.WebResourceResponseReceived event which initially looked promising. However, the documentation states:
There is no guarantee about the order in which the WebView processes the response and the host app's handler runs. The app's handler will not block the WebView from processing the response.
Hence it is not possible to modify the response or delete the cookie in this event handler. I guess you could record the response and delete it 'later', but this seems like it could be awkward to do reliably.
Is there a way to block or reliably delete cookies received in a response when using WebView2?
There's currently no way to intercept and modify web responses.
I imagine as a workaround you might try like you suggest of running some code asynchronously later like during the corresponding NavigationCompleted event to remove the cookie using the CoreWebView2.CookieManager APIs.
Another work around might be to use the WebResourceRequested event to intercept requests, use the GetDeferral method on the eventargs to get a deferral while you perform the web request yourself in native code, receive the response in native code, modify the response as you like, and then provide that modified response back in the WebResourceRequested eventargs and complete the deferral. However this has the drawback that you would need to convert the WebView2s web resource request and response objects back and forth between the request and response objects of whichever HTTP stack you use.
Otherwise, you can file your feedback as a feature request on the WebView2 Feedback github project.
I have some tasks to do for certain commands I receive and would like to keep the client from (read-)timeouts by sending some data while doing the task.
Does the AResponseInfo object offer anything to accomplish this?
I currently use the ContentText property, but it sends the data (as it seems) at the end of the event handler.
By default, TIdHTTPServer sends a response when the OnCommand... event handler exits. However, it is possible to send a response sooner, by either:
calling the AResponseInfo.WriteHeader() and AResponseInfo.WriteContent() methods after populating the AResponseInfo properties as needed.
writing directly to the TCP socket, and then setting the AResponseInfo.HeaderHasBeenWritten property to true so TIdHTTPServer does not try to send its own response.
Note: once you send a response to the client, the client is free to send new requests using the same TCP connection (if HTTP keep-alives are used), but you will be holding up the connection from processing those requests until you exit the OnCommand... event handler.
If you have long processing to do, it is generally better to send a normal response back to the client as soon as possible, for instance with a 102 Processing response, exit the OnCommand... event handler so the server regains control of the connection, do the actual processing in the background, and let the client query the status of the processing using subsequent requests as needed. You could assign a cookie or other type of server-generated ID to the processing, and then have the client send that cookie/ID back to the server to query the processing's status.
My task is to deliver request content before the request has been processed.
How can I do that?
public ActionResult UpdateRecord()
{
Response.Write("OK");
Response.End();
// I need to complete the request here and
// perform some action in the work thread
Thread.Sleep(2000);
return Content("Something user never sees");
}
You cannot send content back to the client in the same request after the request has ended. It's just not possible.
It sounds to me like what you actually want is an asynchronous processing request. This is where you start a process in one request, then you continue to poll the server (refresh the page) periodically until the request is done.
This is typically accomplished using a background service of some kind, such as a Message Queue, or Web Service that triggers a Windows Service to process something. In a pinch, you can use the a Scheduled Task, or even a Cache removal callback trick as well.
Using a background thread won't generally work because threads started by an ASP.NET worker process are terminated after the request ends.
Another possibility, if you don't want to use polling is to use a push service like SignalR to signal the page that the job has been finished.
EDIT:
Well, as I said, you can't do it like that. You have to push that work into some kind of background process, however you can't just spin off a thread because threads will get terminated once the request ends.
There are many questions (and answers) here on SO on how to do this, and they all boil down to what I mentioned above. Scheduled tasks, windows service to do the processing, the Cache removal callback trick, etc.. you could also use a WCF one-way call with a WCF service or any of a number of other ways.
I have a scenario where my rails controller action has to make a API request to a backend business logic server which does a lot of computations and returns me the result.
I'm thinking to show a loading page to the user and make the call asynchronous using Faye or any other option and redirect the user when the call is complete..
But even if I make the call asynchronous, the HTTP request needs to wait for the server to return the data after process, which would take around 20 seconds.
I would like to know what is the best way to make such calls in rails.?
I had faced a similar situation, below is the route that I took:
When the controller action is triggered
a. I fired off a 'async' request to the API using a worker(I used sidekiq)
b. Loaded a 'AJAX' spinner gif on top of a modal
The worker handling the API request runs on another thread which is synchronous and waits for the result from the API
When the processing is done, the worker fires off notification via 'Faye' which removes the modal and populates the data.
Return an HTTP response with status 202 Accepted(for the request that need to take long to process) and start making AJAX requests(to a URL, e.g /jobs/1) to check the status of the background job. Once your job has finished, update it's status so that your Javascript(AJAX) can handle the result of that background job.
I am implementing an json api using rails. I wish to make requests to another web service using delayed job to prevent it from blocking my rails app. So far so good. So i have a function defined in my model which does a http POST to this other web service.
However, the other web service is is an asynchronous api with callbacks. Hence I want to also receive callbacks from this api within my delayed job.
Is this possible? Can I have a http listener in my delayed job whose port number I can control or know within my code?