Skip to content

Commit

Permalink
Merge pull request #307 from xcflytosky/dev
Browse files Browse the repository at this point in the history
eTag字段支持service更改设置
  • Loading branch information
vasdeveloper authored Apr 15, 2019
2 parents 99a4ce9 + 06613b4 commit 59936be
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
16 changes: 15 additions & 1 deletion sonic-iOS/Sonic/Cache/SonicCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,22 @@ - (SonicCacheItem *)saveHtmlString:(NSString *)htmlString
return cacheItem;
}

- (NSString *)getSonicHeaderETagWithHeaders:(NSDictionary *)headers
{
NSString *keyETag = [headers objectForKey:[SonicHeaderKeyCustomeETag lowercaseString]];
if (keyETag && [keyETag isKindOfClass:[NSString class]] && keyETag.length > 0) {
// do nothing
} else {
keyETag = [SonicHeaderKeyETag lowercaseString];
}

return [headers objectForKey:keyETag];
}

- (NSDictionary *)createConfigFromResponseHeaders:(NSDictionary *)headers
{
//Etag,template-tag
NSString *eTag = headers[SonicHeaderKeyETag];
NSString *eTag = [self getSonicHeaderETagWithHeaders:headers];
NSString *templateTag = headers[SonicHeaderKeyTemplate];
NSTimeInterval timeNow = (long)[[NSDate date ]timeIntervalSince1970]*1000;
NSString *localRefresh = [@(timeNow) stringValue];
Expand All @@ -479,6 +491,8 @@ - (NSDictionary *)createConfigFromResponseHeaders:(NSDictionary *)headers
eTag = eTag.length > 0? eTag:@"";

NSDictionary *cfgDict = @{
// SonicHeaderKeyCustomeETag:[self getCustomSonicHeaderKeyETagWithHeaders:headers]
// [self getCustomSonicHeaderKeyETagWithHeaders:headers]:eTag
SonicHeaderKeyETag:eTag,
SonicHeaderKeyTemplate:templateTag,
kSonicLocalRefreshTime:localRefresh,
Expand Down
20 changes: 16 additions & 4 deletions sonic-iOS/Sonic/Network/SonicServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ - (NSString *)responseHeaderForKey:(NSString *)aKey
return header;
}

- (NSString *)getSonicHeaderETagWithHeaders:(NSDictionary *)headers
{
NSString *keyETag = [headers objectForKey:[SonicHeaderKeyCustomeETag lowercaseString]];
if (keyETag && [keyETag isKindOfClass:[NSString class]] && keyETag.length > 0) {
// do nothing
} else {
keyETag = [SonicHeaderKeyETag lowercaseString];
}

return [headers objectForKey:keyETag];
}

- (NSDictionary *)sonicItemForCache
{
if (self.isCompletion) {
Expand All @@ -294,7 +306,7 @@ - (NSDictionary *)sonicItemForCache
self.data = splitResult[kSonicDataFieldName];

NSMutableDictionary *headers = [[_response.allHeaderFields mutableCopy]autorelease];
NSString *responseEtag = [headers objectForKey:[SonicHeaderKeyETag lowercaseString]];
NSString *responseEtag = [self getSonicHeaderETagWithHeaders:headers];
if (!responseEtag) {
responseEtag = getDataSha1([self.htmlString dataUsingEncoding:NSUTF8StringEncoding]);
[headers setObject:responseEtag forKey:[SonicHeaderKeyETag lowercaseString]];
Expand Down Expand Up @@ -336,7 +348,7 @@ - (BOOL)isValidateSonicResponse:(NSHTTPURLResponse *)response
return NO;
}

if ([response.allHeaderFields[SonicHeaderKeyETag] length] == 0) {
if ([[self getSonicHeaderETagWithHeaders:response.allHeaderFields] length] == 0) {
return NO;
}

Expand Down Expand Up @@ -381,7 +393,7 @@ - (void)connection:(SonicConnection *)connection didReceiveResponse:(NSHTTPURLRe
}

// fix Weak-Etag case like -> etag: W/"66f0-m2UmCBEh78dNYPv+boO5ETXk4FU".[https://github.com/Tencent/VasSonic/issues/128]
NSString *eTag = [headers objectForKey:[SonicHeaderKeyETag lowercaseString]];
NSString *eTag = [self getSonicHeaderETagWithHeaders:headers];
if ([eTag hasPrefix:@"W/"] && eTag.length > 3) {
// fix Weak-Etag get value length error
NSString *eTagValue = [eTag substringFromIndex:2];
Expand Down Expand Up @@ -463,7 +475,7 @@ - (void)connectionDidCompleteWithoutError:(SonicConnection *)connection
[headers setValue:@"true" forKey:[SonicHeaderKeyCacheOffline lowercaseString]];
}
NSString *htmlSha1 = nil;
NSString *responseEtag = [headers objectForKey:[SonicHeaderKeyETag lowercaseString]];
NSString *responseEtag = [self getSonicHeaderETagWithHeaders:headers];
if (!responseEtag) {
responseEtag = htmlSha1 = getDataSha1([self.htmlString dataUsingEncoding:NSUTF8StringEncoding]);
[headers setObject:responseEtag forKey:[SonicHeaderKeyETag lowercaseString]];
Expand Down
14 changes: 13 additions & 1 deletion sonic-iOS/Sonic/Session/SonicSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,25 @@ - (void)syncCookies
}
}

- (NSString *)getSonicHeaderETagWithHeaders:(NSDictionary *)headers
{
NSString *keyETag = [headers objectForKey:[SonicHeaderKeyCustomeETag lowercaseString]];
if (keyETag && [keyETag isKindOfClass:[NSString class]] && keyETag.length > 0) {
// do nothing
} else {
keyETag = [SonicHeaderKeyETag lowercaseString];
}

return [headers objectForKey:keyETag];
}

- (NSDictionary *)buildRequestHeaderFields
{
NSDictionary *cacheHeaders = self.cacheConfigHeaders;
NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionary];

if (cacheHeaders) {
NSString *eTag = cacheHeaders[SonicHeaderKeyETag];
NSString *eTag = [self getSonicHeaderETagWithHeaders:cacheHeaders];
if (eTag.length > 0) {
[requestHeaders setObject:eTag forKey:HTTPHeaderKeyIfNoneMatch];
}
Expand Down
6 changes: 6 additions & 0 deletions sonic-iOS/Sonic/SonicConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ typedef NS_ENUM(NSInteger, SonicErrorType) {
*/
#define SonicHeaderKeyETag @"etag"

/**
* Pass Etag through this field.
* This header represents that the "eTag" key can be modified by service.
*/
#define SonicHeaderKeyCustomeETag @"sonic-etag-key"

/**
* Content-Security-Policy key for header.
*/
Expand Down

0 comments on commit 59936be

Please sign in to comment.