2016-10-04 00:58:19 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTMultipartStreamReader.h"
|
|
|
|
|
2017-08-14 17:54:33 +00:00
|
|
|
#import <QuartzCore/CAAnimation.h>
|
|
|
|
|
2016-10-04 00:58:19 +00:00
|
|
|
#define CRLF @"\r\n"
|
|
|
|
|
|
|
|
@implementation RCTMultipartStreamReader {
|
|
|
|
__strong NSInputStream *_stream;
|
|
|
|
__strong NSString *_boundary;
|
2017-08-14 17:54:33 +00:00
|
|
|
CFTimeInterval _lastDownloadProgress;
|
2016-10-04 00:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithInputStream:(NSInputStream *)stream boundary:(NSString *)boundary
|
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
_stream = stream;
|
|
|
|
_boundary = boundary;
|
2017-08-14 17:54:33 +00:00
|
|
|
_lastDownloadProgress = CACurrentMediaTime();
|
2016-10-04 00:58:19 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)parseHeaders:(NSData *)data
|
|
|
|
{
|
|
|
|
NSMutableDictionary *headers = [NSMutableDictionary new];
|
|
|
|
NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
|
|
NSArray<NSString *> *lines = [text componentsSeparatedByString:CRLF];
|
|
|
|
for (NSString *line in lines) {
|
|
|
|
NSUInteger location = [line rangeOfString:@":"].location;
|
|
|
|
if (location == NSNotFound) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
NSString *key = [line substringToIndex:location];
|
|
|
|
NSString *value = [[line substringFromIndex:location + 1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
|
|
|
[headers setValue:value forKey:key];
|
|
|
|
}
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:54:33 +00:00
|
|
|
- (void)emitChunk:(NSData *)data headers:(NSDictionary *)headers callback:(RCTMultipartCallback)callback done:(BOOL)done
|
2016-10-04 00:58:19 +00:00
|
|
|
{
|
|
|
|
NSData *marker = [CRLF CRLF dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSRange range = [data rangeOfData:marker options:0 range:NSMakeRange(0, data.length)];
|
|
|
|
if (range.location == NSNotFound) {
|
|
|
|
callback(nil, data, done);
|
2017-08-14 17:54:33 +00:00
|
|
|
} else if (headers != nil) {
|
|
|
|
// If headers were parsed already just use that to avoid doing it twice.
|
|
|
|
NSInteger bodyStart = range.location + marker.length;
|
|
|
|
NSData *bodyData = [data subdataWithRange:NSMakeRange(bodyStart, data.length - bodyStart)];
|
|
|
|
callback(headers, bodyData, done);
|
2016-10-04 00:58:19 +00:00
|
|
|
} else {
|
|
|
|
NSData *headersData = [data subdataWithRange:NSMakeRange(0, range.location)];
|
|
|
|
NSInteger bodyStart = range.location + marker.length;
|
|
|
|
NSData *bodyData = [data subdataWithRange:NSMakeRange(bodyStart, data.length - bodyStart)];
|
|
|
|
callback([self parseHeaders:headersData], bodyData, done);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:54:33 +00:00
|
|
|
- (void)emitProgress:(NSDictionary *)headers
|
|
|
|
contentLength:(NSUInteger)contentLength
|
|
|
|
final:(BOOL)final
|
|
|
|
callback:(RCTMultipartProgressCallback)callback
|
|
|
|
{
|
|
|
|
if (headers == nil) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Throttle progress events so we don't send more that around 60 per second.
|
|
|
|
CFTimeInterval currentTime = CACurrentMediaTime();
|
|
|
|
|
|
|
|
NSUInteger headersContentLength = headers[@"Content-Length"] != nil ? [headers[@"Content-Length"] unsignedIntValue] : 0;
|
|
|
|
if (callback && (currentTime - _lastDownloadProgress > 0.016 || final)) {
|
|
|
|
_lastDownloadProgress = currentTime;
|
|
|
|
callback(headers, @(headersContentLength), @(contentLength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)readAllPartsWithCompletionCallback:(RCTMultipartCallback)callback
|
|
|
|
progressCallback:(RCTMultipartProgressCallback)progressCallback
|
2016-10-04 00:58:19 +00:00
|
|
|
{
|
2016-10-11 19:14:37 +00:00
|
|
|
NSInteger chunkStart = 0;
|
|
|
|
NSInteger bytesSeen = 0;
|
|
|
|
|
2016-10-04 00:58:19 +00:00
|
|
|
NSData *delimiter = [[NSString stringWithFormat:@"%@--%@%@", CRLF, _boundary, CRLF] dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSData *closeDelimiter = [[NSString stringWithFormat:@"%@--%@--%@", CRLF, _boundary, CRLF] dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSMutableData *content = [[NSMutableData alloc] initWithCapacity:1];
|
2017-08-14 17:54:33 +00:00
|
|
|
NSDictionary *currentHeaders = nil;
|
|
|
|
NSUInteger currentHeadersLength = 0;
|
2016-10-04 00:58:19 +00:00
|
|
|
|
|
|
|
const NSUInteger bufferLen = 4 * 1024;
|
|
|
|
uint8_t buffer[bufferLen];
|
|
|
|
|
|
|
|
[_stream open];
|
|
|
|
while (true) {
|
|
|
|
BOOL isCloseDelimiter = NO;
|
2016-10-11 19:14:37 +00:00
|
|
|
// Search only a subset of chunk that we haven't seen before + few bytes
|
|
|
|
// to allow for the edge case when the delimiter is cut by read call
|
|
|
|
NSInteger searchStart = MAX(bytesSeen - (NSInteger)closeDelimiter.length, chunkStart);
|
|
|
|
NSRange remainingBufferRange = NSMakeRange(searchStart, content.length - searchStart);
|
2017-08-14 17:54:33 +00:00
|
|
|
|
|
|
|
// Check for delimiters.
|
2016-10-04 00:58:19 +00:00
|
|
|
NSRange range = [content rangeOfData:delimiter options:0 range:remainingBufferRange];
|
|
|
|
if (range.location == NSNotFound) {
|
|
|
|
isCloseDelimiter = YES;
|
|
|
|
range = [content rangeOfData:closeDelimiter options:0 range:remainingBufferRange];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range.location == NSNotFound) {
|
2017-08-14 17:54:33 +00:00
|
|
|
if (currentHeaders == nil) {
|
|
|
|
// Check for the headers delimiter.
|
|
|
|
NSData *headersMarker = [CRLF CRLF dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSRange headersRange = [content rangeOfData:headersMarker options:0 range:remainingBufferRange];
|
|
|
|
if (headersRange.location != NSNotFound) {
|
|
|
|
NSData *headersData = [content subdataWithRange:NSMakeRange(chunkStart, headersRange.location - chunkStart)];
|
|
|
|
currentHeadersLength = headersData.length;
|
|
|
|
currentHeaders = [self parseHeaders:headersData];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// When headers are loaded start sending progress callbacks.
|
|
|
|
[self emitProgress:currentHeaders
|
|
|
|
contentLength:content.length - currentHeadersLength
|
|
|
|
final:NO
|
|
|
|
callback:progressCallback];
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:14:37 +00:00
|
|
|
bytesSeen = content.length;
|
2016-10-04 00:58:19 +00:00
|
|
|
NSInteger bytesRead = [_stream read:buffer maxLength:bufferLen];
|
|
|
|
if (bytesRead <= 0 || _stream.streamError) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
[content appendBytes:buffer length:bytesRead];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:14:37 +00:00
|
|
|
NSInteger chunkEnd = range.location;
|
|
|
|
NSInteger length = chunkEnd - chunkStart;
|
|
|
|
bytesSeen = chunkEnd;
|
2016-10-04 00:58:19 +00:00
|
|
|
|
|
|
|
// Ignore preamble
|
2016-10-11 19:14:37 +00:00
|
|
|
if (chunkStart > 0) {
|
|
|
|
NSData *chunk = [content subdataWithRange:NSMakeRange(chunkStart, length)];
|
2017-08-14 17:54:33 +00:00
|
|
|
[self emitProgress:currentHeaders
|
|
|
|
contentLength:chunk.length - currentHeadersLength
|
|
|
|
final:YES
|
|
|
|
callback:progressCallback];
|
|
|
|
[self emitChunk:chunk headers:currentHeaders callback:callback done:isCloseDelimiter];
|
|
|
|
currentHeaders = nil;
|
|
|
|
currentHeadersLength = 0;
|
2016-10-04 00:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isCloseDelimiter) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:14:37 +00:00
|
|
|
chunkStart = chunkEnd + delimiter.length;
|
2016-10-04 00:58:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|