KTVHCDataResponse.m 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // KTVHCDataResponse.m
  3. // KTVHTTPCache
  4. //
  5. // Created by Single on 2017/8/24.
  6. // Copyright © 2017年 Single. All rights reserved.
  7. //
  8. #import "KTVHCDataResponse.h"
  9. #import "KTVHCData+Internal.h"
  10. #import "KTVHCLog.h"
  11. @interface KTVHCDataResponse ()
  12. @property (nonatomic, readonly) KTVHCRange contentRange;
  13. @property (nonatomic, copy, readonly) NSString *contentRangeString;
  14. @end
  15. @implementation KTVHCDataResponse
  16. - (instancetype)initWithURL:(NSURL *)URL headers:(NSDictionary *)headers
  17. {
  18. if (self = [super init]) {
  19. KTVHCLogAlloc(self);
  20. self->_URL = URL;
  21. self->_headers = headers;
  22. self->_contentType = [self headerValueWithKey:@"Content-Type"];
  23. self->_contentLength = [self headerValueWithKey:@"Content-Length"].longLongValue;
  24. self->_contentRangeString = [self headerValueWithKey:@"Content-Range"];
  25. if (self->_contentRangeString == nil && self->_contentLength > 0) {
  26. self->_contentRangeString = KTVHCResponseRangeStringWithContentLength(self->_contentLength);
  27. }
  28. if (self->_contentRangeString == nil) {
  29. self->_contentRange = KTVHCRangeInvaild();
  30. self->_totalLength = self->_contentLength;
  31. } else {
  32. self->_contentRange = KTVHCRangeWithResponseHeaderValue(self->_contentRangeString, &self->_totalLength);
  33. }
  34. KTVHCLogDataResponse(@"%p Create data response\nURL : %@\nHeaders : %@\ncontentType : %@\ntotalLength : %lld\ncurrentLength : %lld", self, self.URL, self.headers, self.contentType, self.totalLength, self.contentLength);
  35. }
  36. return self;
  37. }
  38. - (void)dealloc
  39. {
  40. KTVHCLogDealloc(self);
  41. }
  42. - (NSString *)headerValueWithKey:(NSString *)key
  43. {
  44. NSString *value = [self.headers objectForKey:key];
  45. if (!value) {
  46. value = [self.headers objectForKey:[key lowercaseString]];
  47. }
  48. return value;
  49. }
  50. @end