Implemented response headers when using `XMLHttpRequest`
Summary: I think perhaps these were left out by mistake? Closes https://github.com/facebook/react-native/pull/382 Github Author: Mike Driver <mikedriver@gmail.com> Test Plan: Imported from GitHub, without a `Test Plan:` line.
This commit is contained in:
parent
7f9ee949ee
commit
b1850f8fca
|
@ -10,6 +10,7 @@
|
||||||
#import "RCTDataManager.h"
|
#import "RCTDataManager.h"
|
||||||
|
|
||||||
#import "RCTAssert.h"
|
#import "RCTAssert.h"
|
||||||
|
#import "RCTConvert.h"
|
||||||
#import "RCTLog.h"
|
#import "RCTLog.h"
|
||||||
#import "RCTUtils.h"
|
#import "RCTUtils.h"
|
||||||
|
|
||||||
|
@ -22,27 +23,18 @@ RCT_EXPORT_MODULE()
|
||||||
* The responseSender block won't be called on same thread as called.
|
* The responseSender block won't be called on same thread as called.
|
||||||
*/
|
*/
|
||||||
RCT_EXPORT_METHOD(queryData:(NSString *)queryType
|
RCT_EXPORT_METHOD(queryData:(NSString *)queryType
|
||||||
withQuery:(id)query
|
withQuery:(NSDictionary *)query
|
||||||
queryHash:(__unused NSString *)queryHash
|
queryHash:(__unused NSString *)queryHash
|
||||||
responseSender:(RCTResponseSenderBlock)responseSender)
|
responseSender:(RCTResponseSenderBlock)responseSender)
|
||||||
{
|
{
|
||||||
if ([queryType isEqualToString:@"http"]) {
|
if ([queryType isEqualToString:@"http"]) {
|
||||||
|
|
||||||
// Parse query
|
|
||||||
NSDictionary *queryDict = query;
|
|
||||||
if ([query isKindOfClass:[NSString class]]) {
|
|
||||||
// TODO: it would be more efficient just to send a dictionary
|
|
||||||
queryDict = RCTJSONParse(query, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build request
|
// Build request
|
||||||
NSURL *url = [NSURL URLWithString:queryDict[@"url"]];
|
NSURL *URL = [RCTConvert NSURL:query[@"url"]];
|
||||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
|
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
|
||||||
request.HTTPMethod = queryDict[@"method"] ?: @"GET";
|
request.HTTPMethod = [RCTConvert NSString:query[@"method"]] ?: @"GET";
|
||||||
request.allHTTPHeaderFields = queryDict[@"headers"];
|
request.allHTTPHeaderFields = [RCTConvert NSDictionary:query[@"headers"]];
|
||||||
if ([queryDict[@"data"] isKindOfClass:[NSString class]]) {
|
request.HTTPBody = [RCTConvert NSData:query[@"data"]];
|
||||||
request.HTTPBody = [queryDict[@"data"] dataUsingEncoding:NSUTF8StringEncoding];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build data task
|
// Build data task
|
||||||
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *connectionError) {
|
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *connectionError) {
|
||||||
|
@ -50,18 +42,27 @@ RCT_EXPORT_METHOD(queryData:(NSString *)queryType
|
||||||
// Build response
|
// Build response
|
||||||
NSDictionary *responseJSON;
|
NSDictionary *responseJSON;
|
||||||
if (connectionError == nil) {
|
if (connectionError == nil) {
|
||||||
NSStringEncoding encoding;
|
NSStringEncoding encoding = NSUTF8StringEncoding;
|
||||||
if (response.textEncodingName) {
|
if (response.textEncodingName) {
|
||||||
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
|
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
|
||||||
encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
|
encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
|
||||||
} else {
|
|
||||||
encoding = NSUTF8StringEncoding;
|
|
||||||
}
|
}
|
||||||
int responseCode = (int)[((NSHTTPURLResponse *)response) statusCode];
|
NSHTTPURLResponse *httpResponse = nil;
|
||||||
NSString *returnData = [[NSString alloc] initWithData:data encoding:encoding];
|
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
||||||
responseJSON = @{@"status": @(responseCode), @"responseText": returnData};
|
// Might be a local file request
|
||||||
|
httpResponse = (NSHTTPURLResponse *)response;
|
||||||
|
}
|
||||||
|
responseJSON = @{
|
||||||
|
@"status": @([httpResponse statusCode] ?: 200),
|
||||||
|
@"responseHeaders": [httpResponse allHeaderFields] ?: @{},
|
||||||
|
@"responseText": [[NSString alloc] initWithData:data encoding:encoding] ?: @""
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
responseJSON = @{@"status": @0, @"responseText": [connectionError localizedDescription]};
|
responseJSON = @{
|
||||||
|
@"status": @0,
|
||||||
|
@"responseHeaders": @{},
|
||||||
|
@"responseText": [connectionError localizedDescription]
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send response (won't be sent on same thread as caller)
|
// Send response (won't be sent on same thread as caller)
|
||||||
|
|
|
@ -22,12 +22,13 @@ class XMLHttpRequest extends XMLHttpRequestBase {
|
||||||
sendImpl(method: ?string, url: ?string, headers: Object, data: any): void {
|
sendImpl(method: ?string, url: ?string, headers: Object, data: any): void {
|
||||||
RCTDataManager.queryData(
|
RCTDataManager.queryData(
|
||||||
'http',
|
'http',
|
||||||
JSON.stringify({
|
{
|
||||||
method: method,
|
method: method,
|
||||||
url: url,
|
url: url,
|
||||||
data: data,
|
data: data,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
}),
|
},
|
||||||
|
// TODO: Do we need this? is it used anywhere?
|
||||||
'h' + crc32(method + '|' + url + '|' + data),
|
'h' + crc32(method + '|' + url + '|' + data),
|
||||||
(result) => {
|
(result) => {
|
||||||
result = JSON.parse(result);
|
result = JSON.parse(result);
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
+ (NSDictionary *)NSDictionary:(id)json;
|
+ (NSDictionary *)NSDictionary:(id)json;
|
||||||
+ (NSString *)NSString:(id)json;
|
+ (NSString *)NSString:(id)json;
|
||||||
+ (NSNumber *)NSNumber:(id)json;
|
+ (NSNumber *)NSNumber:(id)json;
|
||||||
|
+ (NSData *)NSData:(id)json;
|
||||||
|
|
||||||
+ (NSURL *)NSURL:(id)json;
|
+ (NSURL *)NSURL:(id)json;
|
||||||
+ (NSURLRequest *)NSURLRequest:(id)json;
|
+ (NSURLRequest *)NSURLRequest:(id)json;
|
||||||
|
|
|
@ -50,6 +50,12 @@ RCT_CONVERTER(NSString *, NSString, description)
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
+ (NSData *)NSData:(id)json
|
||||||
|
{
|
||||||
|
// TODO: should we automatically decode base64 data? Probably not...
|
||||||
|
return [[self NSString:json] dataUsingEncoding:NSUTF8StringEncoding];
|
||||||
|
}
|
||||||
|
|
||||||
+ (NSURL *)NSURL:(id)json
|
+ (NSURL *)NSURL:(id)json
|
||||||
{
|
{
|
||||||
if (![json isKindOfClass:[NSString class]]) {
|
if (![json isKindOfClass:[NSString class]]) {
|
||||||
|
|
Loading…
Reference in New Issue