I am writing a webserver for a music sharing app ... when I have a large file (i.e an mp3) this does not work. It crashes on SIGPIPE error code. The header I am sending has "Connection: close" -- but I assumed this would wait until after the download finishes to close the connection. I know this should probably be forked into a thread but for testing i want to get it working synchronously.
NSData *fileData =[NSData dataWithContentsOfFile:filePath];
CFHTTPMessageRef response =
CFHTTPMessageCreateResponse(
kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1);
CFHTTPMessageSetHeaderFieldValue(
response, (CFStringRef)@"Content-Type", (CFStringRef)@"audio/mpeg");
CFHTTPMessageSetHeaderFieldValue(
response, (CFStringRef)@"Connection", (CFStringRef)@"close");
CFHTTPMessageSetHeaderFieldValue(
response,
(CFStringRef)@"Content-Length",
(CFStringRef)[NSString stringWithFormat:@"%ld", [fileData length]]);
CFDataRef headerData = CFHTTPMessageCopySerializedMessage(response);
@try
{
[fileHandle writeData:(NSData *)headerData];
[fileHandle writeData:fileData];
}@catch (NSException *exception)
{
// Ignore the exception, it normally just means the client
// closed the connection from the other end.
}