private void FetchHeaderValues(bool forCache) {
WebHeaderCollection cc = forCache? CacheHeaders: Response.Headers;
FetchCacheControl(cc.CacheControl, forCache);
// Parse Date Header
string s = cc.Date;
DateTime date = DateTime.MinValue;
if (s != null && HttpDateParse.ParseHttpDate(s, out date)) {
date = date.ToUniversalTime();
}
if (forCache) {
CacheDate = date;
}
else {
ResponseDate = date;
}
// Parse Expires Header
s = cc.Expires;
date = DateTime.MinValue;
if (s != null && HttpDateParse.ParseHttpDate(s, out date)) {
date = date.ToUniversalTime();
}
if (forCache) {
CacheExpires = date;
}
else {
ResponseExpires = date;
}
// Parse LastModified Header
s = cc.LastModified;
date = DateTime.MinValue;
if (s != null && HttpDateParse.ParseHttpDate(s, out date)) {
date = date.ToUniversalTime();
}
if (forCache) {
CacheLastModified = date;
}
else {
ResponseLastModified = date;
}
long totalLength = -1;
long startRange = -1;
long end = -1;
HttpWebResponse resp = Response as HttpWebResponse;
if ((forCache? CacheStatusCode: resp.StatusCode) != HttpStatusCode.PartialContent) {
// Parse Content-Length Header
s = cc.ContentLength;
if (s != null && s.Length != 0) {
int i = 0;
char ch = s[0];
while (i < s.Length && ch == ' ') {
ch = s[++i];
}
if (i != s.Length && ch >= '0' && ch <= '9') {
totalLength = ch-'0';
while(++i < s.Length && (ch = s[i]) >= '0' && ch <= '9') {
totalLength = totalLength*10+(ch-'0');
}
}
}
}
else {
//Parse Content-Range
s = cc[HttpKnownHeaderNames.ContentRange];
if(s == null || !Rfc2616.Common.GetBytesRange(s, ref startRange, ref end, ref totalLength, false)) {
if(Logging.On)Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_content_range_error, (s==null ? "<null>" : s)));
startRange=end=totalLength = -1;
}
else if (forCache && totalLength == CacheEntry.StreamSize)
{
// This is a whole response, step back to 200
startRange = -1;
end = -1;
CacheStatusCode = HttpStatusCode.OK;
CacheStatusDescription = Rfc2616.Common.OkDescription;
}
}
if (forCache) {
CacheEntityLength = totalLength;
ResponseRangeStart = startRange;
ResponseRangeEnd = end;
}
else {
ResponseEntityLength = totalLength;
ResponseRangeStart = startRange;
ResponseRangeEnd = end;
}
//Parse Age Header
TimeSpan span = TimeSpan.MinValue;
s = cc[HttpKnownHeaderNames.Age];
if (s != null) {
int i = 0;
int sec = 0;
while(i < s.Length && s[i++] == ' ') {
;
}
while(i < s.Length && s[i] >= '0' && s[i] <= '9') {
sec = sec*10 + (s[i++] - '0');
}
span = TimeSpan.FromSeconds(sec);
}
if (forCache) {
CacheAge = span;
}
else {
ResponseAge = span;
}
}