summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/actions/http-custom-request.js
blob: e045107410733a1f156ce7222673bfae1367eb37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  OPEN_ACTION_BAR,
  SELECT_ACTION_BAR_TAB,
  PANELS,
  RIGHT_CLICK_REQUEST,
  PRESELECT_REQUEST,
} = require("resource://devtools/client/netmonitor/src/constants.js");

const {
  selectRequest,
} = require("resource://devtools/client/netmonitor/src/actions/selection.js");

const {
  openNetworkDetails,
} = require("resource://devtools/client/netmonitor/src/actions/ui.js");

const {
  getRequestById,
  getRequestByChannelId,
} = require("resource://devtools/client/netmonitor/src/selectors/index.js");

const {
  fetchNetworkUpdatePacket,
} = require("resource://devtools/client/netmonitor/src/utils/request-utils.js");

/**
 * Open the entire HTTP Custom Request panel
 * @returns {Function}
 */
function openHTTPCustomRequest(isOpen) {
  return ({ dispatch, getState }) => {
    dispatch({ type: OPEN_ACTION_BAR, open: isOpen });

    dispatch({
      type: SELECT_ACTION_BAR_TAB,
      id: PANELS.HTTP_CUSTOM_REQUEST,
    });
  };
}

/**
 * Toggle visibility of New Custom Request panel in network panel
 */
function toggleHTTPCustomRequestPanel() {
  return ({ dispatch, getState }) => {
    const state = getState();

    const shouldClose =
      state.ui.networkActionOpen &&
      state.ui.selectedActionBarTabId === PANELS.HTTP_CUSTOM_REQUEST;
    dispatch({ type: OPEN_ACTION_BAR, open: !shouldClose });

    // reset the right clicked request
    dispatch({ type: RIGHT_CLICK_REQUEST, id: null });

    dispatch({
      type: SELECT_ACTION_BAR_TAB,
      id: PANELS.HTTP_CUSTOM_REQUEST,
    });
  };
}

/**
 * Send a new HTTP request using the data in the custom request form.
 */
function sendHTTPCustomRequest(request) {
  return async ({ dispatch, getState, connector, commands }) => {
    if (!request) {
      return;
    }

    // Fetch request headers and post data from the backend, if needed.
    // This is only needed if we are resending a request without editing.

    if (request.requestHeadersAvailable || request.requestPostDataAvailable) {
      await fetchNetworkUpdatePacket(connector.requestData, request, [
        "requestHeaders",
        "requestPostData",
      ]);

      // Get the request again, to get all the updated data
      request = getRequestById(getState(), request.id);
    }

    // Send a new HTTP request using the data in the custom request form
    const data = {
      cause: request.cause || {},
      url: request.url,
      method: request.method,
      httpVersion: request.httpVersion,
    };

    if (request.requestHeaders) {
      data.headers = request.requestHeaders.headers;
    }

    if (request.requestPostData) {
      data.body = request.requestPostData.postData?.text;
    }

    const { channelId } = await commands.networkCommand.sendHTTPRequest(data);

    const newRequest = getRequestByChannelId(getState(), channelId);
    // If the new custom request is available already select the request, else
    // preselect the request.
    if (newRequest) {
      await dispatch(selectRequest(newRequest.id));
    } else {
      await dispatch({
        type: PRESELECT_REQUEST,
        id: channelId,
      });
    }
    dispatch(openNetworkDetails(true));
  };
}

module.exports = {
  openHTTPCustomRequest,
  toggleHTTPCustomRequestPanel,
  sendHTTPCustomRequest,
};