robbiehanson/CocoaHTTPServer — Issue #11: request timeout on SSL connection
Author: oleksiivinogradov
Created: 2012-02-25 14:51:29+00:00
State: open

Hi. I'm using a server in simple json exchange project. If i'm using isSecureServer, client start to produce timeouts:

2012-02-25 16:40:21.011 snowClientEnterprise[38925:7003] CLIENT CONTROLLER:>>>>>>> carrier start update:TWT S.p.A.
2012-02-25 16:41:27.951 snowClientEnterprise[38925:7003] CLIENT CONTROLLER: getJSON answer error download:The request timed out.
2012-02-25 16:41:27.952 snowClientEnterprise[38925:7003] CLIENT CONTROLLER: getJSON answer failed to decode answer with error:Reached the end of the buffer.

here is a client code to download:

NSString *jsonStringForReturn = [request JSONStringWithOptions:JKSerializeOptionNone serializeUnsupportedClassesUsingBlock:nil error:&error];
if (error) NSLog(@"CLIENT CONTROLLER: json decoding error:%@ in function:%@",[error localizedDescription],function);
NSData *bodyData = [jsonStringForReturn dataUsingEncoding:NSUTF8StringEncoding];
NSData *dataForBody = [[[NSData alloc] initWithData:bodyData] autorelease];
//NSLog(@"CLIENT CONTROLLER: string lenght is:%@ bytes",[NSNumber numberWithUnsignedInteger:[dataForBody length]]);
NSString *functionString = [NSString stringWithFormat:@"/%@",function];
NSURL *urlForRequest = [NSURL URLWithString:functionString relativeToURL:mainServer];
NSMutableURLRequest *requestToServer = [NSMutableURLRequest requestWithURL:urlForRequest];
[requestToServer setHTTPMethod:@"POST"];
[requestToServer setHTTPBody:dataForBody];
NSData *receivedResult = [NSURLConnection sendSynchronousRequest:requestToServer returningResponse:nil error:&error];

if (error) NSLog(@"CLIENT CONTROLLER: getJSON answer error download:%@",[error localizedDescription]);
NSString *answer = [[NSString alloc] initWithData:receivedResult encoding:NSUTF8StringEncoding];
JSONDecoder *jkitDecoder = [JSONDecoder decoder];
NSDictionary *finalResult = [jkitDecoder objectWithUTF8String:(const unsigned char *)[answer UTF8String] length:[answer length] error:&error];
[answer release];

if (error) NSLog(@"CLIENT CONTROLLER: getJSON answer failed to decode answer with error:%@",[error localizedDescription]);

when ssl disabled, all working fine for same code.
here is myHTTPclass config:

- (BOOL)isPasswordProtected:(NSString *)path
{
// We're only going to password protect the "secret" directory.

BOOL result = [path hasPrefix:@"/"];

HTTPLogTrace2(@"%@[%p]: isPasswordProtected(%@) - %@", THIS_FILE, self, path, (result ? @"YES" : @"NO"));

return NO;
}
//
//- (BOOL)useDigestAccessAuthentication
//{
//	HTTPLogTrace();
//
//	// Digest access authentication is the default setting.
//	// Notice in Safari that when you're prompted for your password,
//	// Safari tells you "Your login information will be sent securely."
//	//
//	// If you return NO in this method, the HTTP server will use
//	// basic authentication. Try it and you'll see that Safari
//	// will tell you "Your password will be sent unencrypted",
//	// which is strongly discouraged.
//
//	return YES;
//}
//
- (NSString *)passwordForUser:(NSString *)username
{
HTTPLogTrace();

// You can do all kinds of cool stuff here.
// For simplicity, we're not going to check the username, only the password.
if ([username isEqualToString:@"xxx"]) return @"xxx
else return [NSString stringWithFormat:@"%@",[NSNumber numberWithInt:arc4random() % 1000000000000000000]];

}
//
- (BOOL)isSecureServer
{
HTTPLogTrace();

// Create an HTTPS server (all connections will be secured via SSL/TLS)
return NO;
}
//
- (NSArray *)sslIdentityAndCertificates
{
HTTPLogTrace();

NSArray *result = [DDKeychain SSLIdentityAndCertificates];
if([result count] == 0)
{
[DDKeychain createNewIdentity];
return [DDKeychain SSLIdentityAndCertificates];
}
return result;
}