Skip to content

Commit

Permalink
fix(ios): re-implement user agent overwrite (#268)
Browse files Browse the repository at this point in the history
This closes #263 #258
  • Loading branch information
timbru31 authored Aug 25, 2020
1 parent 2d5d351 commit 64bfd15
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions src/ios/CDVFileTransfer.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,36 +103,34 @@ - (NSString*)escapePathComponentForUrlString:(NSString*)urlString
- (void)applyRequestHeaders:(NSDictionary*)headers toRequest:(NSMutableURLRequest*)req
{
[req setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];

NSString* userAgent = [self.commandDelegate userAgent];
if (userAgent) {
[self.webViewEngine evaluateJavaScript:@"navigator.userAgent" completionHandler:^(NSString* userAgent, NSError* error) {
[req setValue:userAgent forHTTPHeaderField:@"User-Agent"];
}

for (NSString* headerName in headers) {
id value = [headers objectForKey:headerName];
if (!value || (value == [NSNull null])) {
value = @"null";
}

// First, remove an existing header if one exists.
[req setValue:nil forHTTPHeaderField:headerName];

if (![value isKindOfClass:[NSArray class]]) {
value = [NSArray arrayWithObject:value];
}

// Then, append all header values.
for (id __strong subValue in value) {
// Convert from an NSNumber -> NSString.
if ([subValue respondsToSelector:@selector(stringValue)]) {
subValue = [subValue stringValue];
for (NSString* headerName in headers) {
id value = [headers objectForKey:headerName];
if (!value || (value == [NSNull null])) {
value = @"null";
}

// First, remove an existing header if one exists.
[req setValue:nil forHTTPHeaderField:headerName];

if (![value isKindOfClass:[NSArray class]]) {
value = [NSArray arrayWithObject:value];
}
if ([subValue isKindOfClass:[NSString class]]) {
[req addValue:subValue forHTTPHeaderField:headerName];

// Then, append all header values.
for (id __strong subValue in value) {
// Convert from an NSNumber -> NSString.
if ([subValue respondsToSelector:@selector(stringValue)]) {
subValue = [subValue stringValue];
}
if ([subValue isKindOfClass:[NSString class]]) {
[req addValue:subValue forHTTPHeaderField:headerName];
}
}
}
}
}];
}

- (NSURLRequest*)requestForUploadCommand:(CDVInvokedUrlCommand*)command fileData:(NSData*)fileData
Expand Down

0 comments on commit 64bfd15

Please sign in to comment.