我正在尝试将用户地址簿中的联系人流式传输到我们的服务器。将所有联系人一次拉入内存可能会崩溃或使设备无响应。我不想承担将所有联系人写入文件和上传文件的开销。我可以看到通过网络发送的数据,但看起来格式无效。服务器无法识别请求正文。
我正在从地址簿中读取联系人并将它们写入 NSOutputStream。此 NSOutputStream 通过此代码与 NSInputStream 共享一个缓冲区
Buffering NSOutputStream used as NSInputStream?
//
// NSStream+BoundPairAdditions.m
// WAControls
//
//
#import "NSStream+BoundPairAdditions.h"
#include <sys/socket.h>
static void CFStreamCreateBoundPairCompat(
CFAllocatorRef alloc,
CFReadStreamRef * readStreamPtr,
CFWriteStreamRef * writeStreamPtr,
CFIndex transferBufferSize
)
// This is a drop-in replacement for CFStreamCreateBoundPair that is necessary because that
// code is broken on iOS versions prior to iOS 5.0 <rdar://problem/7027394> <rdar://problem/7027406>.
// This emulates a bound pair by creating a pair of UNIX domain sockets and wrapper each end in a
// CFSocketStream. This won't give great performance, but it doesn't crash!
{
#pragma unused(transferBufferSize)
int err;
Boolean success;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
int fds[2];
assert(readStreamPtr != NULL);
assert(writeStreamPtr != NULL);
readStream = NULL;
writeStream = NULL;
// Create the UNIX domain socket pair.
err = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
if (err == 0) {
CFStreamCreatePairWithSocket(alloc, fds[0], &readStream, NULL);
CFStreamCreatePairWithSocket(alloc, fds[1], NULL, &writeStream);
// If we failed to create one of the streams, ignore them both.
if ( (readStream == NULL) || (writeStream == NULL) ) {
if (readStream != NULL) {
CFRelease(readStream);
readStream = NULL;
}
if (writeStream != NULL) {
CFRelease(writeStream);
writeStream = NULL;
}
}
assert( (readStream == NULL) == (writeStream == NULL) );
// Make sure that the sockets get closed (by us in the case of an error,
// or by the stream if we managed to create them successfull).
if (readStream == NULL) {
err = close(fds[0]);
assert(err == 0);
err = close(fds[1]);
assert(err == 0);
} else {
success = CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
assert(success);
success = CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
assert(success);
}
}
*readStreamPtr = readStream;
*writeStreamPtr = writeStream;
}
// A category on NSStream that provides a nice, Objective-C friendly way to create
// bound pairs of streams.
@implementation NSStream (BoundPairAdditions)
+ (void)createBoundInputStream:(NSInputStream **)inputStreamPtr outputStream:(NSOutputStream **)outputStreamPtr bufferSize:(NSUInteger)bufferSize
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
assert( (inputStreamPtr != NULL) || (outputStreamPtr != NULL) );
readStream = NULL;
writeStream = NULL;
#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
#error If you support Mac OS X prior to 10.7, you must re-enable CFStreamCreateBoundPairCompat.
#endif
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && (__IPHONE_OS_VERSION_MIN_REQUIRED < 50000)
#error If you support iOS prior to 5.0, you must re-enable CFStreamCreateBoundPairCompat.
#endif
if (NO) {
CFStreamCreateBoundPairCompat(
NULL,
((inputStreamPtr != nil) ? &readStream : NULL),
((outputStreamPtr != nil) ? &writeStream : NULL),
(CFIndex) bufferSize
);
} else {
CFStreamCreateBoundPair(
NULL,
((inputStreamPtr != nil) ? &readStream : NULL),
((outputStreamPtr != nil) ? &writeStream : NULL),
(CFIndex) bufferSize
);
}
if (inputStreamPtr != NULL) {
*inputStreamPtr = CFBridgingRelease(readStream);
}
if (outputStreamPtr != NULL) {
*outputStreamPtr = CFBridgingRelease(writeStream);
}
}
@end
在这里,我通过处理 NSOutputStream 委托(delegate)来构建请求主体。
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
switch(eventCode) {
case NSStreamEventHasSpaceAvailable: {
if(self.contactIndex == 0 && [self.producerStream hasSpaceAvailable]) {
NSMutableData *data = [[NSMutableData alloc] init];
[data appendData:[@"\r\n\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[@"{\"contacts\": [" dataUsingEncoding:NSUTF8StringEncoding]];
[self.producerStream write:[data bytes] maxLength:[data length]];
}
while([self.producerStream hasSpaceAvailable] && self.contactIndex < [self.dataContactIDs count]) {
NSMutableData *contactData = [[[self getNextContact] dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
if(self.contactIndex < [self.dataContactIDs count]) {
[contactData appendData:[@"," dataUsingEncoding:NSUTF8StringEncoding]];
}
[self.producerStream write:[contactData bytes] maxLength:[contactData length]];
}
if(self.contactIndex == self.dataContactIDs.count) {
NSMutableData *data = [[NSMutableData alloc] init];
[data appendData:[@"]}" dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:[@"\r\n\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[self.producerStream write:[data bytes] maxLength:[data length]];
[stream close];
[stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
stream = nil;
}
} break;
case NSStreamEventHasBytesAvailable: {
} break;
case NSStreamEventErrorOccurred: {
} break;
case NSStreamEventEndEncountered: {
[stream close];
[stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
stream = nil;
} break;
default: {
} break;
}
}
我正在使用 AFNetworking 进行联网。我将请求主体流设置为 NSInputStream。
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBodyStream:inputStream];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFHTTPResponseSerializer serializer];
[op setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"PROGRESS %d %lld %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
}];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[self processResponse:responseObject success:success error:error log:log];
} failure:^(AFHTTPRequestOperation *operation, NSError *e) {
[self processError:e op:operation error:error log:log];
}];
[[NSOperationQueue mainQueue] addOperation:op];
然后网络请求是这样的:(使用 Wireshark 捕获)
POST /upload?token=dd224bceb02929b36d35&agent=iPhone%20Simulator&v=1.0 HTTP/1.1
Host: localhost:6547
Transfer-Encoding: Chunked
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
Accept-Language: en-us
Connection: keep-alive
Accept: */*
User-Agent: MyApp/2.0 CFNetwork/672.0.8 Darwin/13.0.0
9BD
{"contacts": [(valid json array)]}
0
我不确定为什么请求正文中包含 9BD 和 0。我认为缓冲区的设置方式存在错误,我相信这会导致服务器忽略 http 正文,因为它无效。看起来我正在正确构建请求吗?有一个更好的方法吗?我正在使用 pyramid/python 来处理请求。服务器收到请求没问题,但是请求体是空的。
编辑
如果我不发送任何联系人,“9BD”就会消失。如果我更改联系人数据,“9BD”会更改为不同的字符。 “0”总是在底部。
编辑2
Jim 指出请求的格式有效。这意味着服务器没有正确处理流。请求可以正常访问服务器,服务器也可以正常回复。但是,我没有看到任何请求正文。服务器正在运行 Pyramid/python。在服务器上,request.body 是空的。
最佳答案
这个要求很好。您的请求已分 block :
Transfer-Encoding: Chunked
9BD
表示下一个 block 的长度。末尾的零表示没有更多的 block 。
参见 section 3.6.1 of RFC 2616了解详情。
您的问题可能是您的服务器不理解分 block 请求。
关于python - iOS分块上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20107529/