http-custom-request.js (3509B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 OPEN_ACTION_BAR, 9 SELECT_ACTION_BAR_TAB, 10 PANELS, 11 RIGHT_CLICK_REQUEST, 12 PRESELECT_REQUEST, 13 } = require("resource://devtools/client/netmonitor/src/constants.js"); 14 15 const { 16 selectRequest, 17 } = require("resource://devtools/client/netmonitor/src/actions/selection.js"); 18 19 const { 20 openNetworkDetails, 21 } = require("resource://devtools/client/netmonitor/src/actions/ui.js"); 22 23 const { 24 getRequestById, 25 getRequestByChannelId, 26 } = require("resource://devtools/client/netmonitor/src/selectors/index.js"); 27 28 const { 29 fetchNetworkUpdatePacket, 30 } = require("resource://devtools/client/netmonitor/src/utils/request-utils.js"); 31 32 /** 33 * Open the entire HTTP Custom Request panel 34 * 35 * @returns {Function} 36 */ 37 function openHTTPCustomRequest(isOpen) { 38 return ({ dispatch }) => { 39 dispatch({ type: OPEN_ACTION_BAR, open: isOpen }); 40 41 dispatch({ 42 type: SELECT_ACTION_BAR_TAB, 43 id: PANELS.HTTP_CUSTOM_REQUEST, 44 }); 45 }; 46 } 47 48 /** 49 * Toggle visibility of New Custom Request panel in network panel 50 */ 51 function toggleHTTPCustomRequestPanel() { 52 return ({ dispatch, getState }) => { 53 const state = getState(); 54 55 const shouldClose = 56 state.ui.networkActionOpen && 57 state.ui.selectedActionBarTabId === PANELS.HTTP_CUSTOM_REQUEST; 58 dispatch({ type: OPEN_ACTION_BAR, open: !shouldClose }); 59 60 // reset the right clicked request 61 dispatch({ type: RIGHT_CLICK_REQUEST, id: null }); 62 63 dispatch({ 64 type: SELECT_ACTION_BAR_TAB, 65 id: PANELS.HTTP_CUSTOM_REQUEST, 66 }); 67 }; 68 } 69 70 /** 71 * Send a new HTTP request using the data in the custom request form. 72 */ 73 function sendHTTPCustomRequest(request) { 74 return async ({ dispatch, getState, connector, commands }) => { 75 if (!request) { 76 return; 77 } 78 79 // Fetch request headers and post data from the backend, if needed. 80 // This is only needed if we are resending a request without editing. 81 82 if (request.requestHeadersAvailable || request.requestPostDataAvailable) { 83 await fetchNetworkUpdatePacket(connector.requestData, request, [ 84 "requestHeaders", 85 "requestPostData", 86 ]); 87 88 // Get the request again, to get all the updated data 89 request = getRequestById(getState(), request.id); 90 } 91 92 // Send a new HTTP request using the data in the custom request form 93 const data = { 94 cause: request.cause || {}, 95 url: request.url, 96 method: request.method, 97 httpVersion: request.httpVersion, 98 securityFlags: request.securityFlags, 99 }; 100 101 if (request.requestHeaders) { 102 data.headers = request.requestHeaders.headers; 103 } 104 105 if (request.requestPostData) { 106 data.body = request.requestPostData.postData?.text; 107 } 108 109 const { channelId } = await commands.networkCommand.sendHTTPRequest(data); 110 111 const newRequest = getRequestByChannelId(getState(), channelId); 112 // If the new custom request is available already select the request, else 113 // preselect the request. 114 if (newRequest) { 115 await dispatch(selectRequest(newRequest.id)); 116 } else { 117 await dispatch({ 118 type: PRESELECT_REQUEST, 119 id: channelId, 120 }); 121 } 122 dispatch(openNetworkDetails(true)); 123 }; 124 } 125 126 module.exports = { 127 openHTTPCustomRequest, 128 toggleHTTPCustomRequestPanel, 129 sendHTTPCustomRequest, 130 };