Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eTag字段支持service更改设置 #307

Merged
merged 1 commit into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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