Permalink
Browse files

Lowercased the keys in component.req_headers and component.headers. C…

…loses #2.
  • Loading branch information...
1 parent 9b25267 commit c2d7d78559b3e54d7d99bdf8c852b937c59777fd @papandreou papandreou committed Feb 17, 2012
@@ -215,19 +215,26 @@ YUI().use(function (iY) {
// find and get comp from comps hash
var comp,
redir = v.redirect,
- headers = '';
+ rawHeaders = '';
// check for redirect
if (redir) {
redir = [].concat(redir);
comp = hash[redir[0].url];
arrayEach(redir, function (red) {
+ var headerName,
+ headers = {};
+ for (headerName in red.headers) {
+ if (red.headers.hasOwnProperty(headerName)) {
+ headers[headerName.toLowerCase()] = red.headers[headerName];
+ }
+ }
comps.push({
url: red.url,
href: red.url,
- rawHeaders: 'Location: ' + red.headers.Location + '\n',
+ rawHeaders: 'Location: ' + headers.location + '\n',
status: red.status,
- headers: red.headers,
+ headers: headers,
type: 'redirect'
});
});
@@ -238,9 +245,9 @@ YUI().use(function (iY) {
// build raw headers
Y.Object.each(v.headers, function (v, k) {
- headers += k + ': ' + v + '\n';
+ rawHeaders += k + ': ' + v + '\n';
});
- comp.rawHeaders = headers;
+ comp.rawHeaders = rawHeaders;
comp.status = v.status;
comp.headers = v.headers;
View
@@ -82,12 +82,23 @@ YSLOW.Component.prototype.setComponentDetails = function (o) {
// firefox
parseResponse = function (response) {
+ var headerName;
+
// copy from the response object
comp.status = response.status;
- comp.headers = response.headers;
+ for (headerName in response.headers) {
+ if (response.headers.hasOwnProperty(headerName)) {
+ comp.headers[headerName.toLowerCase()] = response.headers[headerName];
+ }
+ }
comp.raw_headers = response.raw_headers;
if (response.req_headers) {
- comp.req_headers = response.req_headers;
+ comp.req_headers = {};
+ for (headerName in response.req_headers) {
+ if (response.req_headers.hasOwnProperty(headerName)) {
+ comp.req_headers[headerName.toLowerCase()] = response.req_headers[headerName];
+ }
+ }
}
comp.body = (response.body !== null) ? response.body : '';
if (typeof response.method === 'string') {
@@ -136,13 +147,13 @@ YSLOW.Component.prototype.setComponentDetails = function (o) {
comp.raw_headers = '';
for (i = 0, len = response.headers.length; i < len; i += 1) {
header = response.headers[i];
- comp.headers[header.name] = header.value;
+ comp.headers[header.name.toLowerCase()] = header.value;
comp.raw_headers += header.name + ': ' + header.value + '\n';
}
comp.req_headers = {};
for (i = 0, len = request.headers.length; i < len; i += 1) {
header = request.headers[i];
- comp.req_headers[header.name] = header.value;
+ comp.req_headers[header.name.toLowerCase()] = header.value;
}
comp.method = request.method;
if (response.content && response.content.text) {
@@ -156,8 +167,8 @@ YSLOW.Component.prototype.setComponentDetails = function (o) {
}
// for security checking
comp.response_type = comp.type;
- comp.cookie = (comp.headers['Set-Cookie'] || '') + (comp.req_headers['Cookie'] || '');
- comp.nsize = parseInt(comp.headers['Content-Length'], 10) ||
+ comp.cookie = (comp.headers['set-cookie'] || '') + (comp.req_headers['cookie'] || '');
+ comp.nsize = parseInt(comp.headers['content-length'], 10) ||
response.bodySize || response.content.size;
comp.respTime = entry.time;
comp.after_onload = (new Date(entry.startedDateTime)
@@ -178,21 +189,25 @@ YSLOW.Component.prototype.setComponentDetails = function (o) {
// parse component (chrome and bookmarklet)
parseComponent = function (component) {
- var h, i, len, m,
+ var headerName, h, i, len, m,
reHeader = /^([^:]+):\s*([\s\S]+)$/,
headers = component.rawHeaders;
// copy from the response object
comp.status = component.status;
comp.raw_headers = headers;
if (component.headers) {
- comp.headers = component.headers;
+ for (headerName in component.headers) {
+ if (component.headers.hasOwnProperty(headerName)) {
+ comp.headers[headerName.toLowerCase()] = component.headers[headerName];
+ }
+ }
} else if (typeof headers === 'string') {
h = headers.split('\n');
for (i = 0, len = h.length; i < len; i += 1) {
m = reHeader.exec(h[i]);
if (m) {
- comp.headers[m[1]] = m[2];
+ comp.headers[m[1].toLowerCase()] = m[2];
}
}
}
@@ -202,8 +217,8 @@ YSLOW.Component.prototype.setComponentDetails = function (o) {
comp.type = component.type;
// for security checking
comp.response_type = comp.type;
- comp.cookie = comp.headers['Set-Cookie'] || '';
- comp.nsize = parseInt(comp.headers['Content-Length'], 10) ||
+ comp.cookie = comp.headers['set-cookie'] || '';
+ comp.nsize = parseInt(comp.headers['content-length'], 10) ||
comp.body.length;
comp.respTime = 0;
if (component.after_onload) {
@@ -259,20 +274,20 @@ YSLOW.Component.prototype.populateProperties = function (resolveRedirect, ignore
// check location
// bookmarklet and har already handle redirects
- if (that.headers.Location && resolveRedirect) {
+ if (that.headers.location && resolveRedirect) {
// Add a new component.
- comp = that.parent.addComponentNoDuplicate(that.headers.Location,
+ comp = that.parent.addComponentNoDuplicate(that.headers.location,
(that.type !== 'redirect' ? that.type : 'unknown'), that.url);
if (comp && that.after_onload) {
comp.after_onload = true;
}
that.type = 'redirect';
}
- content_length = that.headers['Content-Length'];
+ content_length = that.headers['content-length'];
// gzip, deflate
- encoding = YSLOW.util.trim(that.headers['Content-Encoding']);
+ encoding = YSLOW.util.trim(that.headers['content-encoding']);
if (encoding === 'gzip' || encoding === 'deflate') {
that.compressed = encoding;
that.size = (that.body.length) ? that.body.length : NULL;
@@ -307,8 +322,8 @@ YSLOW.Component.prototype.populateProperties = function (resolveRedirect, ignore
}
that.uncompressed_size = that.body.length;
- // expiration based on either Expires or Cache-control headers
- expires = that.headers.Expires;
+ // expiration based on either Expires or Cache-Control headers
+ expires = that.headers.expires;
if (expires && expires.length > 0) {
// set expires as a JS object
that.expires = new Date(expires);
@@ -329,7 +344,7 @@ YSLOW.Component.prototype.populateProperties = function (resolveRedirect, ignore
obj = document.createElement('img');
}
if (that.body.length) {
- img_src = 'data:' + that.headers['Content-Type'] + ';base64,' +
+ img_src = 'data:' + that.headers['content-type'] + ';base64,' +
YSLOW.util.base64Encode(that.body);
dataUri = 1;
} else {
@@ -369,7 +384,7 @@ YSLOW.Component.prototype.populateProperties = function (resolveRedirect, ignore
*/
YSLOW.Component.prototype.hasOldModifiedDate = function () {
var now = Number(new Date()),
- modified_date = this.headers['Last-Modified'];
+ modified_date = this.headers['last-modified'];
if (typeof modified_date !== 'undefined') {
// at least 1 day in the past
@@ -402,15 +417,12 @@ YSLOW.Component.prototype.hasFarFutureExpiresOrMaxAge = function () {
};
YSLOW.Component.prototype.getEtag = function () {
- var headers = this.headers,
- etag = headers.Etag || headers.ETag;
-
- return etag || '';
+ return this.headers.etag || '';
};
YSLOW.Component.prototype.getMaxAge = function () {
var index, maxage, expires,
- cache_control = this.headers['Cache-Control'];
+ cache_control = this.headers['cache-control'];
if (cache_control) {
index = cache_control.indexOf('max-age');
@@ -435,8 +447,8 @@ YSLOW.Component.prototype.getSetCookieSize = function () {
var aCookies, k,
size = 0;
- if (this.headers && this.headers['Set-Cookie']) {
- aCookies = this.headers['Set-Cookie'].split('\n');
+ if (this.headers && this.headers['set-cookie']) {
+ aCookies = this.headers['set-cookie'].split('\n');
if (aCookies.length > 0) {
for (k = 0; k < aCookies.length; k += 1) {
size += aCookies[k].length;
View
@@ -156,7 +156,7 @@ YSLOW.registerRule({
}
// experimental custom header, waiting for specification
- match = headers['X-CDN'] || headers['X-Cdn'] || headers['X-cdn'];
+ match = headers['x-cdn'];
if (match) {
continue;
}
@@ -186,7 +186,7 @@ YSLOW.registerRule({
patterns = config.servers;
for (j = 0, lenJ = patterns.length; j < lenJ; j += 1) {
re = new RegExp(patterns[j]);
- if (re.test(headers.Server)) {
+ if (re.test(headers.server)) {
match = 1;
break;
}
@@ -660,7 +660,7 @@ YSLOW.registerRule({
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
offenders.push(briefUrl(comp.url, 80) + ' redirects to ' +
- briefUrl(comp.headers.Location, 60));
+ briefUrl(comp.headers.location, 60));
}
score = 100 - comps.length * parseInt(config.points, 10);
@@ -740,14 +740,13 @@ YSLOW.registerRule({
lint: function (doc, cset, config) {
- var i, len, score, comp, etag, headers,
+ var i, len, score, comp, etag,
offenders = [],
comps = cset.getComponentsByType(config.types);
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
- headers = comp.headers;
- etag = headers && (headers.ETag || headers.Etag);
+ etag = comp.headers && comp.headers.etag;
if (etag && !YSLOW.util.isETagGood(etag)) {
offenders.push(comp);
}
@@ -788,7 +787,7 @@ YSLOW.registerRule({
for (i = 0; i < comps.length; i += 1) {
// check for cache-control: no-cache and cache-control: no-store
- cache_control = comps[i].headers['Cache-Control'];
+ cache_control = comps[i].headers['cache-control'];
if (cache_control) {
if (cache_control.indexOf('no-cache') !== -1 ||
cache_control.indexOf('no-store') !== -1) {
@@ -309,7 +309,7 @@ YSLOW.registerRule({
comps = cset.getComponentsByType('redirect', true, true);
for (i = 0; i < comps.length; i += 1) {
- offenders.push(YSLOW.util.briefUrl(comps[i].url, 80) + "<br> redirects to <br>" + YSLOW.util.briefUrl(comps[i].headers.Location, 60));
+ offenders.push(YSLOW.util.briefUrl(comps[i].url, 80) + "<br> redirects to <br>" + YSLOW.util.briefUrl(comps[i].headers.location, 60));
}
score = 100 - comps.length * parseInt(config.points, 10);
@@ -400,7 +400,7 @@ YSLOW.registerRule({
comps = cset.getComponentsByType(config.types, true, false);
for (i = 0; i < comps.length; i += 1) {
- if (comps[i].headers && comps[i].headers.Etag && comps[i].headers.Server && !YSLOW.util.isETagGood(comps[i].headers.Etag, comps[i].headers.Server)) {
+ if (comps[i].headers && comps[i].headers.etag && comps[i].headers.server && !YSLOW.util.isETagGood(comps[i].headers.etag, comps[i].headers.server)) {
offenders.push(comps[i]);
}
}
@@ -132,7 +132,7 @@ YSLOW.net = {
* @ignore
*/
req.onreadystatechange = function (e) {
- var response, endTimestamp,
+ var response, headerName, endTimestamp,
xhr = (e ? e.target : req);
if (xhr.readyState === 4) { // DONE
@@ -147,8 +147,8 @@ YSLOW.net = {
response.headers = YSLOW.net.getXHRResponseHeaders(xhr);
response.body = xhr.responseText;
- if (response.headers && response.headers['Content-Type']) {
- response.type = YSLOW.util.getComponentType(response.headers['Content-Type']);
+ if (response.headers['content-type']) {
+ response.type = YSLOW.util.getComponentType(response.headers['content-type']);
}
endTimestamp = (new Date()).getTime();
@@ -180,11 +180,11 @@ YSLOW.net = {
* Accounts for set-cookie and potentially other duplicate headers.
*
* @param {XMLHttpRequest} req The XHR object
- * @return {Object} A hash of headers, like {'Content-Type': 'image/png', 'Content-encoding': 'gzip', etc... }
+ * @return {Object} A hash of headers with the keys in lower case, like {'content-type': 'image/png', 'content-encoding': 'gzip', etc... }
*/
getXHRResponseHeaders: function (req) {
var res = {},
- name, value, i, hdr;
+ name, nameLowerCased, value, i, hdr;
try {
hdr = req.getAllResponseHeaders();
@@ -197,12 +197,13 @@ YSLOW.net = {
for (i = 0; i < hdr.length; i += 1) {
name = hdr[i].split(':')[0];
if (name) {
+ nameLowerCased = name.toLowerCase();
value = req.getResponseHeader(name);
if (value) {
- if (name.toLowerCase() === "set-cookie") {
- res[name] = value;
+ if (nameLowerCased === "set-cookie") {
+ res[nameLowerCased] = value;
} else {
- res[name] = value.replace(/\n/g, ' ');
+ res[nameLowerCased] = value.replace(/\n/g, ' ');
}
}
}
View
@@ -53,7 +53,7 @@
comps = cset.getComponentsByType('image');
for (i = 0, len = comps.length; i < len; i += 1) {
comp = comps[i];
- if (urls[comp.req_headers.Referer]) {
+ if (urls[comp.req_headers.referer]) {
comp.type = 'cssimage';
}
}

0 comments on commit c2d7d78

Please sign in to comment.