From 18f97b6ec1093f7c8554df1bdb2ca79d0e4211c7 Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Thu, 15 Oct 2015 03:01:34 -0700 Subject: [PATCH] Always set Access-Control-Allow-Origin on response Error responses were not having this set, which obscured the true error from the client. --- ReactNative/RealmReact.m | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ReactNative/RealmReact.m b/ReactNative/RealmReact.m index 3a9a2b01..c05526a6 100644 --- a/ReactNative/RealmReact.m +++ b/ReactNative/RealmReact.m @@ -79,12 +79,16 @@ static id s_executor; NSError *error; NSData *data = [(GCDWebServerDataRequest *)request data]; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; - if (error) { - NSLog(@"%@", error); - return [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_UnprocessableEntity underlyingError:error message:@"Invalid RPC request"]; + GCDWebServerResponse *response; + + if (error || ![json isKindOfClass:[NSDictionary class]]) { + NSLog(@"Invalid RPC request - %@", error ?: json); + response = [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_UnprocessableEntity underlyingError:error message:@"Invalid RPC request"]; } - - GCDWebServerDataResponse *response = [GCDWebServerDataResponse responseWithJSONObject:[rpcServer performRequest:request.path args:json]]; + else { + response = [GCDWebServerDataResponse responseWithJSONObject:[rpcServer performRequest:request.path args:json]]; + } + [response setValue:@"http://localhost:8081" forAdditionalHeader:@"Access-Control-Allow-Origin"]; return response; }];