Always set Access-Control-Allow-Origin on response

Error responses were not having this set, which obscured the true error from the client.
This commit is contained in:
Scott Kyle 2015-10-15 03:01:34 -07:00
parent 94521b73bb
commit 18f97b6ec1

View File

@ -79,12 +79,16 @@ static id s_executor;
NSError *error; NSError *error;
NSData *data = [(GCDWebServerDataRequest *)request data]; NSData *data = [(GCDWebServerDataRequest *)request data];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error) { GCDWebServerResponse *response;
NSLog(@"%@", error);
return [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_UnprocessableEntity underlyingError:error message:@"Invalid RPC request"]; if (error || ![json isKindOfClass:[NSDictionary class]]) {
NSLog(@"Invalid RPC request - %@", error ?: json);
response = [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_UnprocessableEntity underlyingError:error message:@"Invalid RPC request"];
} }
else {
GCDWebServerDataResponse *response = [GCDWebServerDataResponse responseWithJSONObject:[rpcServer performRequest:request.path args:json]]; response = [GCDWebServerDataResponse responseWithJSONObject:[rpcServer performRequest:request.path args:json]];
}
[response setValue:@"http://localhost:8081" forAdditionalHeader:@"Access-Control-Allow-Origin"]; [response setValue:@"http://localhost:8081" forAdditionalHeader:@"Access-Control-Allow-Origin"];
return response; return response;
}]; }];