I'm implementing proxy support for an osx app.
I've created a custom NSURLProtocol
and it is working perfectly for a proxy without authentication. Tried with both CCProxy and FreeProxy on a Windows computer in the local network.
However when proxy authentication is on, any request in the first few seconds works perfectly then the connection goes from ESTABLISHED to CLOSE_WAIT in 5 seconds. The proxy shows 0 connections again and, after that in the app any HTTP request will get a 407, even though the proxy-auth header is pre-set.
My code looks like this :
// set the auth fields
CFStringRef usernameRef = (__bridge CFStringRef)appProxyPrefs.proxyUserName;
CFStringRef passwordRef = (__bridge CFStringRef)appProxyPrefs.proxyPassword;
CFHTTPMessageAddAuthentication(copyOfOriginal, nil, usernameRef, passwordRef, kCFHTTPAuthenticationSchemeBasic, YES);
...
// useless
CFHTTPMessageSetHeaderFieldValue(copyOfOriginal, (__bridge CFStringRef)@"Connection", (CFStringRef)(@"Keep-Alive"));
...
// create stream, callback, schedule
// apply proxy settings to the stream
if (isNoProxyOverride)
CFReadStreamSetProperty(myReadStream, kCFStreamPropertyHTTPProxy, (__bridge CFTypeRef)(noProxyDict));
else
CFReadStreamSetProperty(myReadStream, kCFStreamPropertyHTTPProxy, (__bridge CFTypeRef)(manualProxyDict));
...
CFReadStreamSetProperty(myReadStream, kCFStreamPropertyHTTPAttemptPersistentConnection, kCFBooleanTrue);
if (!CFReadStreamOpen(myReadStream)) { // error }
else
{
// check if there is a prev stream
if (currentStream != nil)
{
[currentStream close];
currentStream = nil;
}
currentStream = (__bridge NSInputStream *)(myReadStream);
}
As you see I tried to store the previous conenction in a static inputstream and releasing it only after I open a new one, but seems useless.
Also tried setting the underlying socket to keep-alive in kCFStreamEventOpenCompleted
as suggested in NSStream TCP Keep-alive iOS , still without success.
Why does the connection close ? How could I debug it or make it work ? Is the connection's fault the proxy goes craxy ?
Thanks.
Edit 1:
Edit 2: It seems it has to do with HTTPS... If I change the the server to be plain http instead of https it will work perfectly.