requests.js (4251B)
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 ADD_REQUEST, 9 CLEAR_REQUESTS, 10 CLONE_REQUEST, 11 CLONE_SELECTED_REQUEST, 12 REMOVE_SELECTED_CUSTOM_REQUEST, 13 RIGHT_CLICK_REQUEST, 14 SEND_CUSTOM_REQUEST, 15 SET_EVENT_STREAM_FLAG, 16 SET_RECORDING_STATE, 17 UPDATE_REQUEST, 18 } = require("resource://devtools/client/netmonitor/src/constants.js"); 19 const { 20 getSelectedRequest, 21 getRequestById, 22 getRecordingState, 23 } = require("resource://devtools/client/netmonitor/src/selectors/index.js"); 24 const { 25 fetchNetworkUpdatePacket, 26 } = require("resource://devtools/client/netmonitor/src/utils/request-utils.js"); 27 28 function addRequest(id, data, batch) { 29 return { 30 type: ADD_REQUEST, 31 id, 32 data, 33 meta: { batch }, 34 }; 35 } 36 37 function updateRequest(id, data, batch) { 38 return { 39 type: UPDATE_REQUEST, 40 id, 41 data, 42 meta: { batch }, 43 }; 44 } 45 46 function setEventStreamFlag(id, batch) { 47 return { 48 type: SET_EVENT_STREAM_FLAG, 49 id, 50 meta: { batch }, 51 }; 52 } 53 54 /** 55 * Clone request by id. Used when cloning a request 56 * through the "Edit and Resend" option present in the context menu. 57 */ 58 function cloneRequest(id) { 59 return { 60 id, 61 type: CLONE_REQUEST, 62 }; 63 } 64 65 /** 66 * Right click a request without selecting it. 67 */ 68 function rightClickRequest(id) { 69 return { 70 id, 71 type: RIGHT_CLICK_REQUEST, 72 }; 73 } 74 75 /** 76 * Clone the currently selected request, set the "isCustom" attribute. 77 * Used by the "Edit and Resend" feature. 78 */ 79 function cloneSelectedRequest() { 80 return { 81 type: CLONE_SELECTED_REQUEST, 82 }; 83 } 84 85 /** 86 * Send a new HTTP request using the data in the custom request form. 87 */ 88 function sendCustomRequest(requestId = null) { 89 return async ({ dispatch, getState, connector, commands }) => { 90 let request; 91 if (requestId) { 92 request = getRequestById(getState(), requestId); 93 } else { 94 request = getSelectedRequest(getState()); 95 } 96 97 if (!request) { 98 return; 99 } 100 101 // Fetch request headers and post data from the backend. 102 await fetchNetworkUpdatePacket(connector.requestData, request, [ 103 "requestHeaders", 104 "requestPostData", 105 ]); 106 107 // Reload the request from the store to get the headers. 108 request = getRequestById(getState(), request.id); 109 110 // Send a new HTTP request using the data in the custom request form 111 const data = { 112 cause: request.cause, 113 url: request.url, 114 method: request.method, 115 httpVersion: request.httpVersion, 116 }; 117 118 if (request.requestHeaders) { 119 data.headers = request.requestHeaders.headers; 120 } 121 122 if (request.requestPostData) { 123 data.body = request.requestPostData.postData.text; 124 } 125 126 const { channelId } = await commands.networkCommand.sendHTTPRequest(data); 127 128 dispatch({ 129 type: SEND_CUSTOM_REQUEST, 130 id: channelId, 131 }); 132 }; 133 } 134 135 /** 136 * Remove a request from the list. Supports removing only cloned requests with a 137 * "isCustom" attribute. Other requests never need to be removed. 138 */ 139 function removeSelectedCustomRequest() { 140 return { 141 type: REMOVE_SELECTED_CUSTOM_REQUEST, 142 }; 143 } 144 /** 145 * Clear all requests 146 * 147 * @param {object} options 148 * @param {boolean} options.isExplicitClear 149 * Set to true if the call to clear requests is explicitly requested by the 150 * user, to false if this is an automated clear, eg on navigation. 151 */ 152 function clearRequests({ isExplicitClear }) { 153 return ({ dispatch, connector }) => { 154 dispatch({ type: CLEAR_REQUESTS }); 155 connector.clear({ isExplicitClear }); 156 }; 157 } 158 159 /** 160 * Toggle monitoring 161 */ 162 function toggleRecording() { 163 return async ({ dispatch, getState, connector }) => { 164 const recording = !getRecordingState(getState()); 165 if (recording) { 166 await connector.resume(); 167 } else { 168 connector.pause(); 169 } 170 dispatch({ 171 type: SET_RECORDING_STATE, 172 recording, 173 }); 174 }; 175 } 176 177 module.exports = { 178 addRequest, 179 clearRequests, 180 cloneRequest, 181 cloneSelectedRequest, 182 rightClickRequest, 183 removeSelectedCustomRequest, 184 sendCustomRequest, 185 setEventStreamFlag, 186 toggleRecording, 187 updateRequest, 188 };