RequestHeadersView.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
  3. * Copyright (C) IBM Corp. 2009 All rights reserved.
  4. * Copyright (C) 2010 Google Inc. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  16. * its contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.View}
  33. * @param {WebInspector.NetworkRequest} request
  34. */
  35. WebInspector.RequestHeadersView = function(request)
  36. {
  37. WebInspector.View.call(this);
  38. this.registerRequiredCSS("resourceView.css");
  39. this.element.addStyleClass("resource-headers-view");
  40. this._request = request;
  41. this._headersListElement = document.createElement("ol");
  42. this._headersListElement.className = "outline-disclosure";
  43. this.element.appendChild(this._headersListElement);
  44. this._headersTreeOutline = new TreeOutline(this._headersListElement);
  45. this._headersTreeOutline.expandTreeElementsWhenArrowing = true;
  46. this._urlTreeElement = new TreeElement("", null, false);
  47. this._urlTreeElement.selectable = false;
  48. this._headersTreeOutline.appendChild(this._urlTreeElement);
  49. this._requestMethodTreeElement = new TreeElement("", null, false);
  50. this._requestMethodTreeElement.selectable = false;
  51. this._headersTreeOutline.appendChild(this._requestMethodTreeElement);
  52. this._statusCodeTreeElement = new TreeElement("", null, false);
  53. this._statusCodeTreeElement.selectable = false;
  54. this._headersTreeOutline.appendChild(this._statusCodeTreeElement);
  55. this._requestHeadersTreeElement = new TreeElement("", null, true);
  56. this._requestHeadersTreeElement.expanded = true;
  57. this._requestHeadersTreeElement.selectable = false;
  58. this._headersTreeOutline.appendChild(this._requestHeadersTreeElement);
  59. this._decodeRequestParameters = true;
  60. this._showRequestHeadersText = false;
  61. this._showResponseHeadersText = false;
  62. this._queryStringTreeElement = new TreeElement("", null, true);
  63. this._queryStringTreeElement.expanded = true;
  64. this._queryStringTreeElement.selectable = false;
  65. this._queryStringTreeElement.hidden = true;
  66. this._headersTreeOutline.appendChild(this._queryStringTreeElement);
  67. this._urlFragmentTreeElement = new TreeElement("", null, true);
  68. this._urlFragmentTreeElement.expanded = true;
  69. this._urlFragmentTreeElement.selectable = false;
  70. this._urlFragmentTreeElement.hidden = true;
  71. this._headersTreeOutline.appendChild(this._urlFragmentTreeElement);
  72. this._formDataTreeElement = new TreeElement("", null, true);
  73. this._formDataTreeElement.expanded = true;
  74. this._formDataTreeElement.selectable = false;
  75. this._formDataTreeElement.hidden = true;
  76. this._headersTreeOutline.appendChild(this._formDataTreeElement);
  77. this._requestPayloadTreeElement = new TreeElement(WebInspector.UIString("Request Payload"), null, true);
  78. this._requestPayloadTreeElement.expanded = true;
  79. this._requestPayloadTreeElement.selectable = false;
  80. this._requestPayloadTreeElement.hidden = true;
  81. this._headersTreeOutline.appendChild(this._requestPayloadTreeElement);
  82. this._responseHeadersTreeElement = new TreeElement("", null, true);
  83. this._responseHeadersTreeElement.expanded = true;
  84. this._responseHeadersTreeElement.selectable = false;
  85. this._headersTreeOutline.appendChild(this._responseHeadersTreeElement);
  86. }
  87. WebInspector.RequestHeadersView.prototype = {
  88. wasShown: function()
  89. {
  90. this._request.addEventListener(WebInspector.NetworkRequest.Events.RequestHeadersChanged, this._refreshRequestHeaders, this);
  91. this._request.addEventListener(WebInspector.NetworkRequest.Events.ResponseHeadersChanged, this._refreshResponseHeaders, this);
  92. this._request.addEventListener(WebInspector.NetworkRequest.Events.FinishedLoading, this._refreshHTTPInformation, this);
  93. this._refreshURL();
  94. this._refreshQueryString();
  95. this._refreshUrlFragment();
  96. this._refreshRequestHeaders();
  97. this._refreshResponseHeaders();
  98. this._refreshHTTPInformation();
  99. },
  100. willHide: function()
  101. {
  102. this._request.removeEventListener(WebInspector.NetworkRequest.Events.RequestHeadersChanged, this._refreshRequestHeaders, this);
  103. this._request.removeEventListener(WebInspector.NetworkRequest.Events.ResponseHeadersChanged, this._refreshResponseHeaders, this);
  104. this._request.removeEventListener(WebInspector.NetworkRequest.Events.FinishedLoading, this._refreshHTTPInformation, this);
  105. },
  106. /**
  107. * @param {string} name
  108. * @param {string} value
  109. */
  110. _formatHeader: function(name, value)
  111. {
  112. var fragment = document.createDocumentFragment();
  113. fragment.createChild("div", "header-name").textContent = name + ":";
  114. fragment.createChild("div", "header-value source-code").textContent = value;
  115. return fragment;
  116. },
  117. /**
  118. * @param {string} value
  119. * @param {string} className
  120. * @param {boolean} decodeParameters
  121. */
  122. _formatParameter: function(value, className, decodeParameters)
  123. {
  124. var errorDecoding = false;
  125. if (decodeParameters) {
  126. value = value.replace(/\+/g, " ");
  127. if (value.indexOf("%") >= 0) {
  128. try {
  129. value = decodeURIComponent(value);
  130. } catch (e) {
  131. errorDecoding = true;
  132. }
  133. }
  134. }
  135. var div = document.createElement("div");
  136. div.className = className;
  137. if (errorDecoding)
  138. div.createChild("span", "error-message").textContent = WebInspector.UIString("(unable to decode value)");
  139. else
  140. div.textContent = value;
  141. return div;
  142. },
  143. _refreshURL: function()
  144. {
  145. this._urlTreeElement.title = this._formatHeader(WebInspector.UIString("Request URL"), this._request.url);
  146. },
  147. _refreshQueryString: function()
  148. {
  149. var queryString = this._request.queryString();
  150. var queryParameters = this._request.queryParameters;
  151. this._queryStringTreeElement.hidden = !queryParameters;
  152. if (queryParameters)
  153. this._refreshParams(WebInspector.UIString("Query String Parameters"), queryParameters, queryString, this._queryStringTreeElement);
  154. },
  155. _refreshUrlFragment: function()
  156. {
  157. var urlFragment = this._request.parsedURL.fragment;
  158. this._urlFragmentTreeElement.hidden = !urlFragment;
  159. if (!urlFragment)
  160. return;
  161. var sectionTitle = WebInspector.UIString("URL fragment");
  162. this._urlFragmentTreeElement.removeChildren();
  163. this._urlFragmentTreeElement.listItemElement.removeChildren();
  164. this._urlFragmentTreeElement.listItemElement.appendChild(document.createTextNode(sectionTitle));
  165. var fragmentTreeElement = new TreeElement(null, null, false);
  166. fragmentTreeElement.title = this._formatHeader("#", urlFragment);
  167. fragmentTreeElement.selectable = false;
  168. this._urlFragmentTreeElement.appendChild(fragmentTreeElement);
  169. },
  170. _refreshFormData: function()
  171. {
  172. this._formDataTreeElement.hidden = true;
  173. this._requestPayloadTreeElement.hidden = true;
  174. var formData = this._request.requestFormData;
  175. if (!formData)
  176. return;
  177. var formParameters = this._request.formParameters;
  178. if (formParameters) {
  179. this._formDataTreeElement.hidden = false;
  180. this._refreshParams(WebInspector.UIString("Form Data"), formParameters, formData, this._formDataTreeElement);
  181. } else {
  182. this._requestPayloadTreeElement.hidden = false;
  183. try {
  184. var json = JSON.parse(formData);
  185. this._refreshRequestJSONPayload(json, formData, false);
  186. } catch (e) {
  187. this._populateTreeElementWithSourceText(this._requestPayloadTreeElement, formData);
  188. }
  189. }
  190. },
  191. _populateTreeElementWithSourceText: function(treeElement, sourceText)
  192. {
  193. treeElement.removeChildren();
  194. var sourceTreeElement = new TreeElement(null, null, false);
  195. sourceTreeElement.selectable = false;
  196. treeElement.appendChild(sourceTreeElement);
  197. var sourceTextElement = document.createElement("span");
  198. sourceTextElement.addStyleClass("header-value");
  199. sourceTextElement.addStyleClass("source-code");
  200. sourceTextElement.textContent = String(sourceText).trim();
  201. sourceTreeElement.listItemElement.appendChild(sourceTextElement);
  202. },
  203. _refreshParams: function(title, params, sourceText, paramsTreeElement)
  204. {
  205. paramsTreeElement.removeChildren();
  206. paramsTreeElement.listItemElement.removeChildren();
  207. paramsTreeElement.listItemElement.appendChild(document.createTextNode(title));
  208. var headerCount = document.createElement("span");
  209. headerCount.addStyleClass("header-count");
  210. headerCount.textContent = WebInspector.UIString(" (%d)", params.length);
  211. paramsTreeElement.listItemElement.appendChild(headerCount);
  212. function toggleViewSource()
  213. {
  214. paramsTreeElement._viewSource = !paramsTreeElement._viewSource;
  215. this._refreshParams(title, params, sourceText, paramsTreeElement);
  216. }
  217. paramsTreeElement.listItemElement.appendChild(this._createViewSourceToggle(paramsTreeElement._viewSource, toggleViewSource.bind(this)));
  218. if (paramsTreeElement._viewSource) {
  219. this._populateTreeElementWithSourceText(paramsTreeElement, sourceText);
  220. return;
  221. }
  222. var toggleTitle = this._decodeRequestParameters ? WebInspector.UIString("view URL encoded") : WebInspector.UIString("view decoded");
  223. var toggleButton = this._createToggleButton(toggleTitle);
  224. toggleButton.addEventListener("click", this._toggleURLDecoding.bind(this));
  225. paramsTreeElement.listItemElement.appendChild(toggleButton);
  226. for (var i = 0; i < params.length; ++i) {
  227. var paramNameValue = document.createDocumentFragment();
  228. var name = this._formatParameter(params[i].name + ":", "header-name", this._decodeRequestParameters);
  229. var value = this._formatParameter(params[i].value, "header-value source-code", this._decodeRequestParameters);
  230. paramNameValue.appendChild(name);
  231. paramNameValue.appendChild(value);
  232. var parmTreeElement = new TreeElement(paramNameValue, null, false);
  233. parmTreeElement.selectable = false;
  234. paramsTreeElement.appendChild(parmTreeElement);
  235. }
  236. },
  237. /**
  238. * @param {Object} parsedObject
  239. * @param {string} sourceText
  240. * @param {boolean} viewSource
  241. */
  242. _refreshRequestJSONPayload: function(parsedObject, sourceText, viewSource)
  243. {
  244. this._requestPayloadTreeElement.removeChildren();
  245. var listItem = this._requestPayloadTreeElement.listItemElement;
  246. listItem.removeChildren();
  247. listItem.appendChild(document.createTextNode(this._requestPayloadTreeElement.title));
  248. var setViewSource = this._refreshRequestJSONPayload.bind(this, parsedObject, sourceText);
  249. if (viewSource) {
  250. listItem.appendChild(this._createViewSourceToggle(true, setViewSource.bind(this, false)));
  251. this._populateTreeElementWithSourceText(this._requestPayloadTreeElement, sourceText);
  252. } else {
  253. listItem.appendChild(this._createViewSourceToggle(false, setViewSource.bind(this, true)));
  254. var object = WebInspector.RemoteObject.fromLocalObject(parsedObject);
  255. var section = new WebInspector.ObjectPropertiesSection(object, object.description);
  256. section.expand();
  257. section.editable = false;
  258. listItem.appendChild(section.element);
  259. }
  260. },
  261. /**
  262. * @param {boolean} viewSource
  263. * @param {Function} handler
  264. */
  265. _createViewSourceToggle: function(viewSource, handler)
  266. {
  267. var viewSourceToggleTitle = viewSource ? WebInspector.UIString("view parsed") : WebInspector.UIString("view source");
  268. var viewSourceToggleButton = this._createToggleButton(viewSourceToggleTitle);
  269. viewSourceToggleButton.addEventListener("click", handler);
  270. return viewSourceToggleButton;
  271. },
  272. _toggleURLDecoding: function(event)
  273. {
  274. this._decodeRequestParameters = !this._decodeRequestParameters;
  275. this._refreshQueryString();
  276. this._refreshFormData();
  277. },
  278. _getHeaderValue: function(headers, key)
  279. {
  280. var lowerKey = key.toLowerCase();
  281. for (var testKey in headers) {
  282. if (testKey.toLowerCase() === lowerKey)
  283. return headers[testKey];
  284. }
  285. },
  286. _refreshRequestHeaders: function()
  287. {
  288. if (this._showRequestHeadersText)
  289. this._refreshHeadersText(WebInspector.UIString("Request Headers"), this._request.sortedRequestHeaders, this._request.requestHeadersText, this._requestHeadersTreeElement);
  290. else
  291. this._refreshHeaders(WebInspector.UIString("Request Headers"), this._request.sortedRequestHeaders, this._requestHeadersTreeElement);
  292. if (this._request.requestHeadersText) {
  293. var toggleButton = this._createHeadersToggleButton(this._showRequestHeadersText);
  294. toggleButton.addEventListener("click", this._toggleRequestHeadersText.bind(this));
  295. this._requestHeadersTreeElement.listItemElement.appendChild(toggleButton);
  296. }
  297. this._refreshFormData();
  298. },
  299. _refreshResponseHeaders: function()
  300. {
  301. if (this._showResponseHeadersText)
  302. this._refreshHeadersText(WebInspector.UIString("Response Headers"), this._request.sortedResponseHeaders, this._request.responseHeadersText, this._responseHeadersTreeElement);
  303. else
  304. this._refreshHeaders(WebInspector.UIString("Response Headers"), this._request.sortedResponseHeaders, this._responseHeadersTreeElement);
  305. if (this._request.responseHeadersText) {
  306. var toggleButton = this._createHeadersToggleButton(this._showResponseHeadersText);
  307. toggleButton.addEventListener("click", this._toggleResponseHeadersText.bind(this));
  308. this._responseHeadersTreeElement.listItemElement.appendChild(toggleButton);
  309. }
  310. },
  311. _refreshHTTPInformation: function()
  312. {
  313. var requestMethodElement = this._requestMethodTreeElement;
  314. requestMethodElement.hidden = !this._request.statusCode;
  315. var statusCodeElement = this._statusCodeTreeElement;
  316. statusCodeElement.hidden = !this._request.statusCode;
  317. if (this._request.statusCode) {
  318. var statusImageSource = "";
  319. if (this._request.statusCode < 300 || this._request.statusCode === 304)
  320. statusImageSource = "Images/successGreenDot.png";
  321. else if (this._request.statusCode < 400)
  322. statusImageSource = "Images/warningOrangeDot.png";
  323. else
  324. statusImageSource = "Images/errorRedDot.png";
  325. requestMethodElement.title = this._formatHeader(WebInspector.UIString("Request Method"), this._request.requestMethod);
  326. var statusCodeFragment = document.createDocumentFragment();
  327. statusCodeFragment.createChild("div", "header-name").textContent = WebInspector.UIString("Status Code") + ":";
  328. var statusCodeImage = statusCodeFragment.createChild("img", "resource-status-image");
  329. statusCodeImage.src = statusImageSource;
  330. statusCodeImage.title = this._request.statusCode + " " + this._request.statusText;
  331. var value = statusCodeFragment.createChild("div", "header-value source-code");
  332. value.textContent = this._request.statusCode + " " + this._request.statusText;
  333. if (this._request.cached)
  334. value.createChild("span", "status-from-cache").textContent = " " + WebInspector.UIString("(from cache)");
  335. statusCodeElement.title = statusCodeFragment;
  336. }
  337. },
  338. _refreshHeadersTitle: function(title, headersTreeElement, headersLength)
  339. {
  340. headersTreeElement.listItemElement.removeChildren();
  341. headersTreeElement.listItemElement.appendChild(document.createTextNode(title));
  342. var headerCount = document.createElement("span");
  343. headerCount.addStyleClass("header-count");
  344. headerCount.textContent = WebInspector.UIString(" (%d)", headersLength);
  345. headersTreeElement.listItemElement.appendChild(headerCount);
  346. },
  347. _refreshHeaders: function(title, headers, headersTreeElement)
  348. {
  349. headersTreeElement.removeChildren();
  350. var length = headers.length;
  351. this._refreshHeadersTitle(title, headersTreeElement, length);
  352. headersTreeElement.hidden = !length;
  353. for (var i = 0; i < length; ++i) {
  354. var headerTreeElement = new TreeElement(null, null, false);
  355. headerTreeElement.title = this._formatHeader(headers[i].name, headers[i].value);
  356. headerTreeElement.selectable = false;
  357. headersTreeElement.appendChild(headerTreeElement);
  358. }
  359. },
  360. _refreshHeadersText: function(title, headers, headersText, headersTreeElement)
  361. {
  362. this._populateTreeElementWithSourceText(headersTreeElement, headersText);
  363. this._refreshHeadersTitle(title, headersTreeElement, headers.length);
  364. },
  365. _toggleRequestHeadersText: function(event)
  366. {
  367. this._showRequestHeadersText = !this._showRequestHeadersText;
  368. this._refreshRequestHeaders();
  369. },
  370. _toggleResponseHeadersText: function(event)
  371. {
  372. this._showResponseHeadersText = !this._showResponseHeadersText;
  373. this._refreshResponseHeaders();
  374. },
  375. _createToggleButton: function(title)
  376. {
  377. var button = document.createElement("span");
  378. button.addStyleClass("header-toggle");
  379. button.textContent = title;
  380. return button;
  381. },
  382. _createHeadersToggleButton: function(isHeadersTextShown)
  383. {
  384. var toggleTitle = isHeadersTextShown ? WebInspector.UIString("view parsed") : WebInspector.UIString("view source");
  385. return this._createToggleButton(toggleTitle);
  386. },
  387. __proto__: WebInspector.View.prototype
  388. }