Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-16557] HTTPClient send numbers in post form #5433

Merged
merged 8 commits into from
Mar 10, 2014
39 changes: 22 additions & 17 deletions iphone/Classes/TiHTTPClient/HTTPClientProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,42 +121,47 @@ -(void)send:(id)args
NSDictionary *dict = (NSDictionary*)arg;
for(NSString *key in dict) {
id value = [dict objectForKey:key];
if([value isKindOfClass:[NSString class]]) {
[form addFormKey:key andValue: (NSString*)value];
}
else if([value isKindOfClass:[TiBlob class]]|| [value isKindOfClass:[TiFile class]]) {
if([value isKindOfClass:[TiBlob class]]|| [value isKindOfClass:[TiFile class]]) {
TiBlob *blob;
NSString *name;
NSString *mime;
if([value isKindOfClass:[TiBlob class]]) {
blob = (TiBlob*)value;
name = [NSString stringWithFormat:@"file%i", dataIndex++];
if([blob path] != nil) {
name = [[blob path] lastPathComponent];
} else {
name = [NSString stringWithFormat:@"file%i", dataIndex++];
}
}else{
blob = [(TiFile*)value blob];
name = [[(TiFile*)value path] lastPathComponent];
}
[form addFormData:[(TiBlob*)blob data]
fileName:name
fieldName:key];
mime = [blob mimeType];
if(mime != nil) {
[form addFormData:[blob data] fileName:name fieldName:key contentType:mime];
} else {
[form addFormData:[blob data] fileName:name fieldName:key];
}
}
else if([value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSArray class]]) {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:value options:kNilOptions error:nil];
else {
[form addFormKey:key
andValue:[NSString stringWithUTF8String:[jsonData bytes]]];
andValue:[TiUtils stringValue:value]];
}
}
} else if ([arg isKindOfClass:[TiBlob class]] || [arg isKindOfClass:[TiFile class]]) {
TiBlob *blob;
NSString *name;
if([arg isKindOfClass:[TiBlob class]]) {
blob = (TiBlob*)arg;
name = [NSString stringWithFormat:@"file%i", dataIndex++];
} else {
blob = [(TiFile*)arg blob];
name = [[(TiFile*)arg path] lastPathComponent];
}
[form addFormData:[blob data] fileName:name];
} else if([arg isKindOfClass:[NSString class]]) {
[form setStringData:(NSString*)arg];
NSString *mime = [blob mimeType];
if(mime == nil) {
mime = @"application/octet-stream";
}
[form appendData:[blob data] withContentType:mime];
} else {
[form setStringData:[TiUtils stringValue:arg]];
}
}

Expand Down
2 changes: 2 additions & 0 deletions iphone/Classes/TiHTTPClient/TiHTTPPostForm.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
}
@property(nonatomic, readonly) NSData *requestData;
@property(nonatomic, readonly) NSDictionary *requestHeaders;
@property(nonatomic, readonly) NSString *contentType;

-(void)setJSONData:(id)json;
-(void)setStringData:(NSString*)str;
-(void)appendData:(NSData*)data withContentType:(NSString*)contentType;

-(void)addDictionay:(NSDictionary*)dict;
-(void)addFormKey:(NSString*)key andValue:(NSString*)value;
Expand Down
41 changes: 24 additions & 17 deletions iphone/Classes/TiHTTPClient/TiHTTPPostForm.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ - (void)dealloc
RELEASE_TO_NIL(_requestFilesArray);
RELEASE_TO_NIL(_jsonData);
RELEASE_TO_NIL(_stringData);
RELEASE_TO_NIL(_contentType);
[super dealloc];
}

Expand All @@ -32,6 +33,12 @@ -(void)appendData:(NSData*)data
{
[[self postFormData] appendData: data];
}
-(void)appendData:(NSData *)data withContentType:(NSString *)contentType
{
RELEASE_TO_NIL(_contentType);
_contentType = [contentType retain];
[[self postFormData] appendData: data];
}

-(void)buildStringPostData
{
Expand Down Expand Up @@ -84,7 +91,7 @@ -(void)buildFilePostData

// Content-Disposition: form-data; name="username"
//
// pec1095
// pec1985
// --0xTibOuNdArY
}

Expand All @@ -100,8 +107,8 @@ -(void)buildFilePostData
[self appendData:[dict valueForKey:@"fileData"]];
[self appendStringData:[NSString stringWithFormat:@"\r\n--%@\r\n", last ? [boundry stringByAppendingString:@"--"] : boundry]];

// Content-Disposition: form-data; name="file0"; filename="image.jpg"
// Content-Type: application/octet-stream
// Content-Disposition: form-data; name="file[0]"; filename="image.jpg"
// Content-Type: imgae/jpeg
//
// [binary data]
// --0xTibOuNdArY
Expand All @@ -112,22 +119,22 @@ -(void)buildFilePostData

-(NSData*)requestData
{
NSInteger fileCount = [[self requestFilesArray] count];
RELEASE_TO_NIL(_postFormData);
if(_stringData != nil) {
if(_postFormData != nil && _contentType != nil) {
[self addHeaderKey:@"Content-Type" andHeaderValue: _contentType];
} else if(_stringData != nil) {
[self appendData:_stringData];
}
if(_jsonData != nil) {
NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
} else if(_jsonData != nil) {
[self appendData:_jsonData];
[self addHeaderKey:@"Content-Type" andHeaderValue:[NSString stringWithFormat: @"application/json;charset=%@", charset]];
} else if(fileCount == 0) {
[self buildStringPostData];
[self addHeaderKey:@"Content-Type" andHeaderValue:@"application/json;charset=utf-8"];
} else {
[self buildFilePostData];
NSInteger fileCount = [[self requestFilesArray] count];
if(fileCount == 0) {
[self buildStringPostData];
} else {
[self buildFilePostData];
}
}
[self addHeaderKey:@"Connection" andHeaderValue:@"close"];
[self addHeaderKey:@"Content-Length" andHeaderValue:[NSString stringWithFormat:@"%i", [[self postFormData] length]]];
[self addHeaderKey:@"Content-Length" andHeaderValue:[NSString stringWithFormat:@"%i", [_postFormData length]]];
return [self postFormData];
}
-(NSMutableData*)postFormData
Expand Down Expand Up @@ -217,14 +224,14 @@ -(void)addFormFile:(NSString*)path fieldName:(NSString*)name contentType:(NSStri
-(void)addFormData:(NSData*)data
{
[self addFormData:data
fileName:[NSString stringWithFormat:@"file%i", (unsigned int)[[self requestFilesArray] count]]
fileName:[NSString stringWithFormat:@"file[%i]", (unsigned int)[[self requestFilesArray] count]]
];
}
-(void)addFormData:(NSData*)data fileName:(NSString*)fileName
{
[self addFormData: data
fileName: fileName
fieldName: [NSString stringWithFormat:@"file_%i", (unsigned int)[[self requestFilesArray] count]]
fieldName: [NSString stringWithFormat:@"file[%i]", (unsigned int)[[self requestFilesArray] count]]
];

}
Expand Down
2 changes: 1 addition & 1 deletion iphone/Classes/TiUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@ +(id)jsonParse:(NSString*)value error:(NSError**)error;
}
+(NSString*)jsonStringify:(id)value
{
NSError *error;
NSError *error = nil;
NSString *r = [self jsonStringify:value error:&error];
if(error != nil) {
NSLog(@"Could not stringify JSON. Error: %@", error);
Expand Down