Skip to content

Commit f64393e

Browse files
Fix a URIError encountered in Chrome when interpreting an X-JSON header with extended characters.
1 parent 4cace0b commit f64393e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/prototype/ajax/response.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,23 @@ Ajax.Response = Class.create({
151151
_getHeaderJSON: function() {
152152
var json = this.getHeader('X-JSON');
153153
if (!json) return null;
154-
json = decodeURIComponent(escape(json));
154+
155+
try {
156+
// Browsers expect HTTP headers to be ASCII and nothing else. Running
157+
// them through `decodeURIComponent` processes them with the page's
158+
// specified encoding.
159+
json = decodeURIComponent(escape(json));
160+
} catch(e) {
161+
// Except Chrome doesn't seem to need this, and calling
162+
// `decodeURIComponent` on text that's already in the proper encoding
163+
// will throw a `URIError`. The ugly solution is to assume that a
164+
// `URIError` raised here signifies that the text is, in fact, already
165+
// in the correct encoding, and treat the failure as a good sign.
166+
//
167+
// This is ugly, but so too is sending extended characters in an HTTP
168+
// header with no spec to back you up.
169+
}
170+
155171
try {
156172
return json.evalJSON(this.request.options.sanitizeJSON ||
157173
!this.request.isSameOrigin());

0 commit comments

Comments
 (0)