SecureTransport を使用して、HTTPS 経由で Google Places API と通信しています。ただし、応答を読み取ると、途中で SSLRead ルーチンが最初に数分間ハングし、その後ゼロバイトの読み取りで返されるため、不完全な応答が残ります。どうなり得るか?
do {
if (httpResponseBodyContentLength > -1 && offset_recv >= httpResponseBodyContentLength) {
break;
}
size_t bytes_available;
for (int try = 0; try < 30; try++) {
if ((ssl_status = SSLGetBufferedReadSize(ssl_context, &bytes_available)) != 0) {
NetDebugLog(@"%@", [NSError errorWithDomain:NSOSStatusErrorDomain code:ssl_status userInfo:nil]);
bytes_available = 0;
@try {
usleep(30000);
}
@catch (NSException *exception) {
NetDebugLog(@"%@", exception);
}
}
else if (bytes_available == 0) {
@try {
usleep(30000);
}
@catch (NSException *exception) {
NetDebugLog(@"%@", exception);
}
}
else {
break;
}
}
ssl_status = SSLRead(ssl_context, responseBodyBuffer + offset_recv, MIN(responseBodyBufferLength - offset_recv, bytes_available), &bytes_recv_size_t);
bytes_recv_int = bytes_recv_size_t;
NetDebugLog(@"SSLRead() body: %zd bytes of %zd length (%ld offset)", bytes_recv_size_t, MIN(responseBodyBufferLength - offset_recv, bytes_available), offset_recv);
if (ssl_status != 0) {
NSError *ssl_error = [NSError errorWithDomain:NSOSStatusErrorDomain code:ssl_status userInfo:nil];
NetDebugLog(@"%@", ssl_error);
}
[responseBufferNSMutableData appendBytes:responseBodyBuffer + offset_recv length:bytes_recv_int];
responseBodyTotalLength += bytes_recv_int;
offset_recv += bytes_recv_int;
if (ssl_status == -36) {
@throw [NSException exceptionWithName:@"NAExtensions"
reason:[[NSString alloc]initWithFormat:@"SSLRead() failed: %ld", bytes_recv_size_t]
userInfo:nil];
}
else if (offset_recv == responseBodyBufferLength) {
offset_recv = 0;
}
}
while (bytes_recv_int > 0);