IosHttpURLConnection. During redirection, if intermediate cookies have expired, the next request still has this cookie
- Dominant language
- Java
- Stars
- 6k
- Forks
- 999
- Avg merge
- 19h 20m
- Merged PRs (30d)
- 14
Description
If cookie expiries while redirecting(Set-Cookie: cook=1 Expires=1999), it still present on next redirected request.
Android successfully copes with this, but unfortunately the implementation for IOS does not.
I think problem in this code:
**/jre_emul/Classes/com/google/j2objc/net/IosHttpURLConnection.java:794**
It cannot determine that a cookie similar to this "sessionid=22; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly"
shoul be removed.
To test this problem here is a test service with GET method https://damp-dawn-68572.herokuapp.com/myapp
It checks cookies and if it contains "sessionid=22" expires that cookie and redirects to itself
Server Node.js source code:
```
app.get('/myapp', (req, res) => {
cookie = req.headers.cookie || "";
if (cookie.indexOf("sessionid=22") >= 0) {
res.cookie('sessionid', 22, { expires: new Date(0), httpOnly: true });
return res.redirect('/myapp');
} else {
return res.send('Request cookies: ' + cookie.split('; '));
}
});
```
```
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
try {
CookieManager cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
CookieHandler cookieHandler = CookieHandler.getDefault();
cookieHandler.put(new URI("https://damp-dawn-68572.herokuapp.com"), new HashMap>() {{
put("Set-Cookie", Arrays.asList("sessionid=22", "name=1"));
}});
HttpURLConnection connection = (HttpURLConnection) new URL("https://damp-dawn-68572.herokuapp.com/myapp").openConnection();
connection.getResponseCode();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
}
//Expect here 'Request cookies: name=1'
System.out.println("!@# body " + sb.toString());
connection.disconnect();
} catch (Exception e) {
//Error here on iOS: Error Domain=NSURLErrorDomain Code=-1007 "too many HTTP redirects"
System.out.println("!@# error " + e.getMessage());
e.printStackTrace();
}
CookieHandler cookieHandler = (CookieHandler)CookieHandler.getDefault();
Map> s = cookieHandler.get(new URI("https://damp-dawn-68572.herokuapp.com"), new HashMap());
List cc = s.get("Cookie");
if (cc == null) {
System.out.println("!@# Cookie store empty");
} else {
System.out.println("!@# Cookie store contains:");
for (String c : cc) {
//Expect here only 'name=1'
System.out.println("!@# -- " + c);
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.