Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 * @param {!WebInspector.NetworkRequest} request | 34 * @param {!WebInspector.NetworkRequest} request |
| 35 * @param {!WebInspector.ParsedJSON} parsedJSON | 35 * @param {!WebInspector.ParsedJSON} parsedJSON |
| 36 */ | 36 */ |
| 37 WebInspector.RequestJSONView = function(request, parsedJSON) | 37 WebInspector.RequestJSONView = function(request, parsedJSON) |
| 38 { | 38 { |
| 39 WebInspector.RequestView.call(this, request); | 39 WebInspector.RequestView.call(this, request); |
| 40 this._parsedJSON = parsedJSON; | 40 this._parsedJSON = parsedJSON; |
| 41 this.element.classList.add("json"); | 41 this.element.classList.add("json"); |
| 42 } | 42 } |
| 43 | 43 |
| 44 // "false", "true", "null", ",", "{", "}", "[", "]", number, double-quoted strin g. | |
| 45 WebInspector.RequestJSONView._jsonToken = new RegExp('(?:false|true|null|[/*&\\| ;=\\(\\),\\{\\}\\[\\]]|(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+ )?\\b)|(?:\"(?:[^\\0-\\x08\\x0a-\\x1f\"\\\\]|\\\\(?:[\"/\\\\bfnrt]|u[0-9A-Fa-f]{ 4}))*\"))', 'g'); | |
| 46 | |
| 47 // Escaped unicode char. | |
| 48 WebInspector.RequestJSONView._escapedUnicode = new RegExp('\\\\(?:([^u])|u(.{4}) )', 'g'); | |
| 49 | |
| 50 // Map from escaped char to its literal value. | |
| 51 WebInspector.RequestJSONView._standardEscapes = {'"': '"', '/': '/', '\\': '\\', 'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t'}; | |
| 52 | |
| 53 /** | |
| 54 * @param {string} full | |
| 55 * @param {string} standard | |
| 56 * @param {string} unicode | |
| 57 * @return {string} | |
| 58 */ | |
| 59 WebInspector.RequestJSONView._unescape = function(full, standard, unicode) | |
| 60 { | |
| 61 return standard ? WebInspector.RequestJSONView._standardEscapes[standard] : String.fromCharCode(parseInt(unicode, 16)); | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * @param {string} text | |
| 66 * @return {string} | |
| 67 */ | |
| 68 WebInspector.RequestJSONView._unescapeString = function(text) | |
| 69 { | |
| 70 return text.indexOf("\\") === -1 ? text : text.replace(WebInspector.RequestJ SONView._escapedUnicode, WebInspector.RequestJSONView._unescape); | |
| 71 } | |
| 72 | |
| 73 /** | |
| 74 * @return {*} | |
| 75 */ | |
| 76 WebInspector.RequestJSONView._buildObjectFromJSON = function(text) | |
| 77 { | |
| 78 var regExp = WebInspector.RequestJSONView._jsonToken; | |
| 79 regExp.lastIndex = 0; | |
| 80 var result = []; | |
| 81 var tip = result; | |
| 82 var stack = []; | |
| 83 var key = undefined; | |
| 84 var token = undefined; | |
| 85 var lastToken = undefined; | |
| 86 while (true) { | |
| 87 var match = regExp.exec(text); | |
| 88 if (match === null) | |
| 89 break; | |
| 90 lastToken = token; | |
| 91 token = match[0]; | |
| 92 var code = token.charCodeAt(0); | |
| 93 if ((code === 0x5b) || (code === 0x7b)) { // [ or { | |
| 94 var newTip = (code === 0x5b) ? [] : {}; | |
| 95 tip[key || tip.length] = newTip; | |
| 96 stack.push(tip); | |
| 97 tip = newTip; | |
| 98 } else if ((code === 0x5d) || (code === 0x7d)) { // ] or } | |
| 99 tip = stack.pop(); | |
| 100 if (!tip) | |
| 101 break; | |
| 102 } else if (code === 0x2C) { // , | |
| 103 if (Array.isArray(tip) && (lastToken === undefined || lastToken === "[" || lastToken === ",")) | |
| 104 tip[tip.length] = undefined; | |
| 105 } else if (code === 0x22) { // " | |
| 106 token = WebInspector.RequestJSONView._unescapeString(token.substring (1, token.length - 1)); | |
| 107 if (!key) { | |
| 108 if (Array.isArray(tip)) { | |
| 109 key = tip.length; | |
| 110 } else { | |
| 111 key = token || ""; | |
| 112 continue; | |
| 113 } | |
| 114 } | |
| 115 tip[key] = token; | |
| 116 } else if (code === 0x66) { // f | |
| 117 tip[key || tip.length] = false; | |
| 118 } else if (code === 0x6e) { // n | |
| 119 tip[key || tip.length] = null; | |
| 120 } else if (code === 0x74) { // t | |
| 121 tip[key || tip.length] = true; | |
| 122 } else if (code === 0x2f || code === 0x2a || code === 0x26 || code === 0 x7c || code === 0x3b || code === 0x3d || code === 0x28 || code === 0x29) { // /* &|;=() | |
| 123 // Looks like JavaScript | |
| 124 throw "Invalid JSON"; | |
| 125 } else { // sign or digit | |
| 126 tip[key || tip.length] = +(token); | |
| 127 } | |
| 128 key = undefined; | |
| 129 } | |
| 130 return (result.length > 1) ? result : result[0]; | |
| 131 } | |
| 132 | |
| 133 /** | 44 /** |
| 134 * @param {string} text | 45 * @param {string} text |
| 135 * @return {?WebInspector.ParsedJSON} | 46 * @return {?WebInspector.ParsedJSON} |
| 136 */ | 47 */ |
| 137 WebInspector.RequestJSONView.parseJSON = function(text) | 48 WebInspector.RequestJSONView.parseJSON = function(text) |
| 138 { | 49 { |
| 139 // Trim stubs like "while(1)", "for(;;)", weird numbers, etc. We need JSON s tart. | 50 // Do not treat HTML as JSON. |
| 51 if (text.startsWith("<")) | |
| 52 return; | |
| 140 var inner = WebInspector.RequestJSONView._findBrackets(text, "{", "}"); | 53 var inner = WebInspector.RequestJSONView._findBrackets(text, "{", "}"); |
| 141 var inner2 = WebInspector.RequestJSONView._findBrackets(text, "[", "]"); | 54 var inner2 = WebInspector.RequestJSONView._findBrackets(text, "[", "]"); |
| 142 inner = inner2.length > inner.length ? inner2 : inner; | 55 inner = inner2.length > inner.length ? inner2 : inner; |
| 143 var inner3 = WebInspector.RequestJSONView._findBrackets(text, "(", ")"); | 56 |
| 144 if (inner3.length - 2 > inner.length) { | 57 // Return on blank payloads or on payloads significantly smaller than origin al text. |
| 145 inner = inner3; | 58 if (inner.length === -1 || text.length - inner.length > 80) |
| 146 ++inner.start; | |
| 147 --inner.end; | |
| 148 } | |
| 149 if (inner.length === -1) | |
| 150 return null; | 59 return null; |
| 151 | 60 |
| 152 | |
| 153 var prefix = text.substring(0, inner.start); | 61 var prefix = text.substring(0, inner.start); |
| 154 var suffix = text.substring(inner.end + 1); | 62 var suffix = text.substring(inner.end + 1); |
| 155 text = text.substring(inner.start, inner.end + 1); | 63 text = text.substring(inner.start, inner.end + 1); |
| 156 | 64 |
| 65 // Only process valid JSONP | |
|
dgozman
2015/05/28 11:12:43
nit: full stop please.
| |
| 66 if (suffix.length && !suffix.trim().startsWith(")")) | |
|
dgozman
2015/05/28 11:12:43
&& prefix.trim().endsWith("(")
pfeldman
2015/05/28 11:27:01
"while (1) [1, 2]" is a valid json request.
dgozman
2015/05/28 11:41:31
But if we have suffix starting with ")", there mus
| |
| 67 return null; | |
| 68 | |
| 157 try { | 69 try { |
| 158 return new WebInspector.ParsedJSON(WebInspector.RequestJSONView._buildOb jectFromJSON(text), prefix, suffix); | 70 return new WebInspector.ParsedJSON(JSON.parse(text), prefix, suffix); |
| 159 } catch (e) { | 71 } catch (e) { |
| 160 return null; | 72 return null; |
| 161 } | 73 } |
| 162 } | 74 } |
| 163 | 75 |
| 164 /** | 76 /** |
| 165 * @param {string} text | 77 * @param {string} text |
| 166 * @param {string} open | 78 * @param {string} open |
| 167 * @param {string} close | 79 * @param {string} close |
| 168 * @return {{start: number, end: number, length: number}} | 80 * @return {{start: number, end: number, length: number}} |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 | 114 |
| 203 /** | 115 /** |
| 204 * @constructor | 116 * @constructor |
| 205 */ | 117 */ |
| 206 WebInspector.ParsedJSON = function(data, prefix, suffix) | 118 WebInspector.ParsedJSON = function(data, prefix, suffix) |
| 207 { | 119 { |
| 208 this.data = data; | 120 this.data = data; |
| 209 this.prefix = prefix; | 121 this.prefix = prefix; |
| 210 this.suffix = suffix; | 122 this.suffix = suffix; |
| 211 } | 123 } |
| OLD | NEW |