Trim whitespace and quotes from urls, prevent download data urls
Showing
1 changed file
with
19 additions
and
8 deletions
| @@ -20,6 +20,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -20,6 +20,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
| 20 | - (void)updateResourceURLs; | 20 | - (void)updateResourceURLs; |
| 21 | - (void)parseAsHTML; | 21 | - (void)parseAsHTML; |
| 22 | - (void)parseAsCSS; | 22 | - (void)parseAsCSS; |
| 23 | +- (void)addURLToFetch:(NSString *)newURL; | ||
| 23 | + (NSArray *)CSSURLsFromString:(NSString *)string; | 24 | + (NSArray *)CSSURLsFromString:(NSString *)string; |
| 24 | @property (retain, nonatomic) ASINetworkQueue *externalResourceQueue; | 25 | @property (retain, nonatomic) ASINetworkQueue *externalResourceQueue; |
| 25 | @property (retain, nonatomic) NSMutableDictionary *resourceList; | 26 | @property (retain, nonatomic) NSMutableDictionary *resourceList; |
| @@ -68,15 +69,11 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -68,15 +69,11 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
| 68 | [self setResourceList:[NSMutableDictionary dictionary]]; | 69 | [self setResourceList:[NSMutableDictionary dictionary]]; |
| 69 | 70 | ||
| 70 | for (NSString *theURL in urls) { | 71 | for (NSString *theURL in urls) { |
| 71 | - if (([[theURL substringToIndex:1] isEqualToString:@"'"] && [[theURL substringFromIndex:[theURL length]-1] isEqualToString:@"'"]) ||([[theURL substringToIndex:1] isEqualToString:@"\""] && [[theURL substringFromIndex:[theURL length]-1] isEqualToString:@"\""])) { | ||
| 72 | - theURL = [theURL substringWithRange:NSMakeRange(1,[theURL length]-2)]; | ||
| 73 | - } | ||
| 74 | NSURL *newURL = [NSURL URLWithString:theURL relativeToURL:[self url]]; | 72 | NSURL *newURL = [NSURL URLWithString:theURL relativeToURL:[self url]]; |
| 75 | if (newURL) { | 73 | if (newURL) { |
| 76 | - [[self resourceList] setObject:[NSMutableDictionary dictionary] forKey:theURL]; | 74 | + [self addURLToFetch:theURL]; |
| 77 | } | 75 | } |
| 78 | } | 76 | } |
| 79 | - | ||
| 80 | if (![[self resourceList] count]) { | 77 | if (![[self resourceList] count]) { |
| 81 | [super requestFinished]; | 78 | [super requestFinished]; |
| 82 | [super markAsFinished]; | 79 | [super markAsFinished]; |
| @@ -95,6 +92,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -95,6 +92,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
| 95 | ASIWebPageRequest *externalResourceRequest = [ASIWebPageRequest requestWithURL:[NSURL URLWithString:theURL relativeToURL:[self url]]]; | 92 | ASIWebPageRequest *externalResourceRequest = [ASIWebPageRequest requestWithURL:[NSURL URLWithString:theURL relativeToURL:[self url]]]; |
| 96 | [externalResourceRequest setRequestHeaders:[self requestHeaders]]; | 93 | [externalResourceRequest setRequestHeaders:[self requestHeaders]]; |
| 97 | [externalResourceRequest setDownloadCache:[self downloadCache]]; | 94 | [externalResourceRequest setDownloadCache:[self downloadCache]]; |
| 95 | + [externalResourceRequest setCachePolicy:[self cachePolicy]]; | ||
| 98 | [externalResourceRequest setUserInfo:[NSDictionary dictionaryWithObject:theURL forKey:@"Path"]]; | 96 | [externalResourceRequest setUserInfo:[NSDictionary dictionaryWithObject:theURL forKey:@"Path"]]; |
| 99 | [[self externalResourceQueue] addOperation:externalResourceRequest]; | 97 | [[self externalResourceQueue] addOperation:externalResourceRequest]; |
| 100 | } | 98 | } |
| @@ -168,6 +166,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -168,6 +166,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
| 168 | ASIWebPageRequest *externalResourceRequest = [ASIWebPageRequest requestWithURL:[NSURL URLWithString:theURL relativeToURL:[self url]]]; | 166 | ASIWebPageRequest *externalResourceRequest = [ASIWebPageRequest requestWithURL:[NSURL URLWithString:theURL relativeToURL:[self url]]]; |
| 169 | [externalResourceRequest setRequestHeaders:[self requestHeaders]]; | 167 | [externalResourceRequest setRequestHeaders:[self requestHeaders]]; |
| 170 | [externalResourceRequest setDownloadCache:[self downloadCache]]; | 168 | [externalResourceRequest setDownloadCache:[self downloadCache]]; |
| 169 | + [externalResourceRequest setCachePolicy:[self cachePolicy]]; | ||
| 171 | [externalResourceRequest setUserInfo:[NSDictionary dictionaryWithObject:theURL forKey:@"Path"]]; | 170 | [externalResourceRequest setUserInfo:[NSDictionary dictionaryWithObject:theURL forKey:@"Path"]]; |
| 172 | [[self externalResourceQueue] addOperation:externalResourceRequest]; | 171 | [[self externalResourceQueue] addOperation:externalResourceRequest]; |
| 173 | } | 172 | } |
| @@ -238,6 +237,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -238,6 +237,7 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
| 238 | [self setResponseHeaders:newHeaders]; | 237 | [self setResponseHeaders:newHeaders]; |
| 239 | 238 | ||
| 240 | [super requestFinished]; | 239 | [super requestFinished]; |
| 240 | + [[self downloadCache] storeResponseForRequest:self maxAge:[self secondsToCache]]; | ||
| 241 | [super markAsFinished]; | 241 | [super markAsFinished]; |
| 242 | } | 242 | } |
| 243 | 243 | ||
| @@ -271,10 +271,10 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -271,10 +271,10 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
| 271 | if ([[nodeName lowercaseString] isEqualToString:@"style"]) { | 271 | if ([[nodeName lowercaseString] isEqualToString:@"style"]) { |
| 272 | NSArray *externalResources = [[self class] CSSURLsFromString:value]; | 272 | NSArray *externalResources = [[self class] CSSURLsFromString:value]; |
| 273 | for (NSString *theURL in externalResources) { | 273 | for (NSString *theURL in externalResources) { |
| 274 | - [resourceList setObject:[NSMutableDictionary dictionary] forKey:theURL]; | 274 | + [self addURLToFetch:theURL]; |
| 275 | } | 275 | } |
| 276 | } else { | 276 | } else { |
| 277 | - [resourceList setObject:[NSMutableDictionary dictionary] forKey:value]; | 277 | + [self addURLToFetch:value]; |
| 278 | } | 278 | } |
| 279 | if (nodes->nodeTab[i]->type != XML_NAMESPACE_DECL) { | 279 | if (nodes->nodeTab[i]->type != XML_NAMESPACE_DECL) { |
| 280 | nodes->nodeTab[i] = NULL; | 280 | nodes->nodeTab[i] = NULL; |
| @@ -285,6 +285,16 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -285,6 +285,16 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
| 285 | xmlXPathFreeContext(xpathCtx); | 285 | xmlXPathFreeContext(xpathCtx); |
| 286 | } | 286 | } |
| 287 | 287 | ||
| 288 | +- (void)addURLToFetch:(NSString *)newURL | ||
| 289 | +{ | ||
| 290 | + // Get rid of any surrounding whitespace | ||
| 291 | + newURL = [newURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | ||
| 292 | + // Don't attempt to fetch data URIs | ||
| 293 | + if (![[[newURL substringToIndex:5] lowercaseString] isEqualToString:@"data:"]) { | ||
| 294 | + [[self resourceList] setObject:[NSMutableDictionary dictionary] forKey:newURL]; | ||
| 295 | + } | ||
| 296 | +} | ||
| 297 | + | ||
| 288 | 298 | ||
| 289 | - (void)updateResourceURLs | 299 | - (void)updateResourceURLs |
| 290 | { | 300 | { |
| @@ -409,7 +419,8 @@ static NSMutableArray *requestsUsingXMLParser = nil; | @@ -409,7 +419,8 @@ static NSMutableArray *requestsUsingXMLParser = nil; | ||
| 409 | if (!theURL) { | 419 | if (!theURL) { |
| 410 | break; | 420 | break; |
| 411 | } | 421 | } |
| 412 | - [urls addObject:theURL]; | 422 | + // Remove any quotes around the url |
| 423 | + [urls addObject:[theURL stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\"'"]]]; | ||
| 413 | } | 424 | } |
| 414 | return urls; | 425 | return urls; |
| 415 | } | 426 | } |
-
Please register or login to post a comment