Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RTCHTTPRequestHandler] http request sent twice when server returns 'www-authenticate' #2266

Closed
aprock opened this issue Aug 7, 2015 · 2 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@aprock
Copy link
Contributor

aprock commented Aug 7, 2015

Results
When using the fetch api against an http server that returns 'www-authenticate' in the response headers, requests are immediately repeated.

Expected
When running the same fetch in a node.js program, using node-fetch, only one request is sent.

Details
I sent a simple fetch request, something like...

fetch(url)
  .then((response) => response.text())
  .then((responseText) => {
    console.log(responseText);
  })
  .catch((error) => {
    console.warn(error);
  });

To a test server that returns 'www-authenticate'
https://gist.github.com/aprock/e21ed771ff285f5992fc

The server logs two identical requests for that single fetch, within milliseconds of each other.
These are the log snippets from a simple web server I built to show this.

401 refused Fri Aug 07 2015 10:30:23 GMT-0700 (PDT) (1438968623728)
...
401 refused Fri Aug 07 2015 10:30:23 GMT-0700 (PDT) (1438968623732)

Investigation
I did some digging into what would cause this, and have hit a wall. It seems that the Foundation Framework is handling the challenge itself. (NSURLSessionAuthChallengePerformDefaultHandling), however when I try to add - URLSession:task:didReceiveChallenge:completionHandler: to RTCHTTPRequestHandler.m, and override the NSURLSessionAuthChallengeDisposition value...ANY values sent to the completionHandler will still trigger this replay.

@alexeylang
Copy link
Contributor

I've investigated the issue. Looks like it's not a bug of React Native. The reason is in the behaviour of NSURLSession. I've created new empty project with NSURLSession code only and the issue is reproduced. The response of first request is handled in didReceiveChallenge: delegate method by receiving challenge.failureResponse. And response for the second request is handled in didReceiveResponse: delegate method. I think it may be handled by developer depends on specific implementation of delegate methods. I see the same behaviour when I send request by AFNetworking.

@aprock
Copy link
Contributor Author

aprock commented Nov 2, 2015

Thanks for investigating, I wanted to put something up here if anyone else has a similar issue with this flow. The 3rd party webserver I'm connecting to runs on an embedded device, and the second request causes it problems.

My current workaround is to add a category to RCTHTTPRequestHandler that cancel's the task.

- (void)URLSession:(NSURLSession *)session
              task:(NSURLSessionTask *)task
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition,
                             NSURLCredential *credential))completionHandler
{
    // just drop the session, since calling completionHandler causes another request to be sent
    [task cancel];
}

The fetch api pollyfill doesn't seem to have a good path for overriding credentials, however a more permanent fix might be to enable some customization around this.

@aprock aprock closed this as completed Dec 8, 2015
@facebook facebook locked as resolved and limited conversation to collaborators Jul 22, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants