Skip to content

Commit 086d894

Browse files
authored
fix: request query params in log har object (#20)
* fix: request params in log har object * chore: use query params instead of query string for har query params * chore: multiple query entry in case array has value array has multiple values
1 parent 01cf063 commit 086d894

File tree

3 files changed

+95
-16
lines changed

3 files changed

+95
-16
lines changed

src/components/proxy-middleware/helpers/harObectCreator.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,27 @@ const createHarHeaders = (request_headers_obj) => {
1212
return headers;
1313
};
1414

15-
const createHarQueryStrings = (path) => {
16-
// TODO -> http://www.softwareishard.com/blog/har-12-spec/#queryString
17-
return [];
15+
const createHarQueryStrings = (query_params) => {
16+
let res = []
17+
18+
Object.keys(query_params).forEach(query => {
19+
const query_value = query_params[query]
20+
if(Array.isArray(query_value)) {
21+
query_value.forEach(val => {
22+
res.push({
23+
"name" : query,
24+
"value": val
25+
})
26+
})
27+
} else {
28+
res.push({
29+
"name" : query,
30+
"value": query_value
31+
})
32+
}
33+
})
34+
35+
return res
1836
};
1937

2038
const getContentType = (headers) => {
@@ -90,6 +108,7 @@ export const createRequestHarObject = (
90108
body,
91109
headers,
92110
agent,
111+
query_params
93112
} = proxyToServerRequestOptions;
94113

95114
return {
@@ -99,7 +118,7 @@ export const createRequestHarObject = (
99118
cookies: [], // TODO: add support for Cookies
100119
headers: requestHarObject.headers || createHarHeaders(headers),
101120
method: requestHarObject.method || method,
102-
queryString: requestHarObject.queryString || createHarQueryStrings(path),
121+
queryString: requestHarObject.queryString || createHarQueryStrings(query_params),
103122
url:
104123
requestHarObject.url || (agent?.protocol || "http:") + "//" + host + path,
105124
postData:
@@ -109,25 +128,25 @@ export const createRequestHarObject = (
109128
};
110129

111130

112-
export const createHar = (requestHeaders, method, protocol, host, path, requestBody, responseStatusCode, response, responseHeaders) => {
131+
export const createHar = (requestHeaders, method, protocol, host, path, requestBody, responseStatusCode, response, responseHeaders, requestParams) => {
113132
return {
114133
"log": {
115134
"version" : "1.2",
116135
"creator" : {},
117136
"browser" : {},
118137
"pages": [],
119-
"entries": [createHarEntry(requestHeaders, method, protocol, host, path, requestBody, responseStatusCode, response, responseHeaders)],
138+
"entries": [createHarEntry(requestHeaders, method, protocol, host, path, requestBody, responseStatusCode, response, responseHeaders, requestParams)],
120139
"comment": ""
121140
}
122141
};
123142
}
124143

125-
export const createHarEntry = (requestHeaders, method, protocol, host, path, requestBody, responseStatusCode, response, responseHeaders) => {
144+
export const createHarEntry = (requestHeaders, method, protocol, host, path, requestBody, responseStatusCode, response, responseHeaders, requestParams) => {
126145
return {
127146
// "pageref": "page_0",
128147
"startedDateTime": new Date().toISOString(),
129148
// "time": 50,
130-
"request": createHarRequest(requestHeaders, method, protocol, host, path, requestBody),
149+
"request": createHarRequest(requestHeaders, method, protocol, host, path, requestBody, requestParams),
131150
"response": createHarResponse(responseStatusCode, response, responseHeaders),
132151
"cache": {},
133152
"timings": {},
@@ -137,15 +156,15 @@ export const createHarEntry = (requestHeaders, method, protocol, host, path, req
137156
};
138157
}
139158

140-
export const createHarRequest = (requestHeaders, method, protocol, host, path, requestBody) => {
159+
export const createHarRequest = (requestHeaders, method, protocol, host, path, requestBody, requestParams) => {
141160
return {
142161
bodySize: -1, // TODO: calculate the body size
143162
headersSize: -1, // TODO: calculate the header size
144163
httpVersion: "HTTP/1.1",
145164
cookies: [], // TODO: add support for Cookies
146165
headers: createHarHeaders(requestHeaders),
147166
method: method,
148-
queryString: createHarQueryStrings(path),
167+
queryString: createHarQueryStrings(requestParams),
149168
url:
150169
protocol + "://" + host + path,
151170
postData:

src/components/proxy-middleware/helpers/proxy_ctx_helper.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,65 @@
55
// } from "../../../../../../../common/components/utils/utils";
66
// const CONSTANTS = require("../../../../../../../common/constants");
77

8+
import {
9+
CONSTANTS as GLOBAL_CONSTANTS,
10+
} from "@requestly/requestly-core"
11+
12+
13+
function extractUrlComponent(url, name) { // need this in proxy
14+
const myUrl = new URL(url);
15+
16+
switch (name) {
17+
case GLOBAL_CONSTANTS.URL_COMPONENTS.URL:
18+
return url;
19+
case GLOBAL_CONSTANTS.URL_COMPONENTS.PROTOCOL:
20+
return myUrl.protocol;
21+
case GLOBAL_CONSTANTS.URL_COMPONENTS.HOST:
22+
return myUrl.host;
23+
case GLOBAL_CONSTANTS.URL_COMPONENTS.PATH:
24+
return myUrl.pathname;
25+
case GLOBAL_CONSTANTS.URL_COMPONENTS.QUERY:
26+
return myUrl.search;
27+
case GLOBAL_CONSTANTS.URL_COMPONENTS.HASH:
28+
return myUrl.hash;
29+
}
30+
31+
console.error("Invalid source key", url, name);
32+
}
33+
34+
/**
35+
*
36+
* @param queryString e.g. ?a=1&b=2 or a=1 or ''
37+
* @returns object { paramName -> [value1, value2] }
38+
*/
39+
export function getQueryParamsMap(queryString) {
40+
var map = {},
41+
queryParams;
42+
43+
if (!queryString || queryString === "?") {
44+
return map;
45+
}
46+
47+
if (queryString[0] === "?") {
48+
queryString = queryString.substr(1);
49+
}
50+
51+
queryParams = queryString.split("&");
52+
53+
queryParams.forEach(function (queryParam) {
54+
var paramName = queryParam.split("=")[0],
55+
paramValue = queryParam.split("=")[1];
56+
57+
// We are keeping value of param as array so that in future we can support multiple param values of same name
58+
// And we do not want to lose the params if url already contains multiple params of same name
59+
map[paramName] = map[paramName] || [];
60+
map[paramName].push(paramValue);
61+
});
62+
63+
return map;
64+
}
65+
66+
867
export const get_request_url = (ctx) => {
968
return (
1069
(ctx.isSSL ? "https://" : "http://") +
@@ -52,15 +111,15 @@ export const get_response_options = (ctx) => {
52111
export const get_request_options = (ctx) => {
53112
return {
54113
...ctx.proxyToServerRequestOptions,
55-
// query_params: get_json_query_params(ctx),
114+
query_params: get_json_query_params(ctx),
56115
};
57116
};
58117

59-
// export const get_json_query_params = (ctx) => {
60-
// const url = get_request_url(ctx);
61-
// let queryString = extractUrlComponent(url, CONSTANTS.URL_COMPONENTS.QUERY);
62-
// return getQueryParamsMap(queryString) || null;
63-
// };
118+
export const get_json_query_params = (ctx) => {
119+
const url = get_request_url(ctx);
120+
let queryString = extractUrlComponent(url, GLOBAL_CONSTANTS.URL_COMPONENTS.QUERY);
121+
return getQueryParamsMap(queryString) || null;
122+
};
64123

65124
export const getRequestHeaders = (ctx) => {
66125
if (ctx && ctx.proxyToServerRequestOptions) {

src/components/proxy-middleware/middlewares/logger_middleware.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class LoggerMiddleware {
8686
ctx.rq.final_response.status_code,
8787
ctx.rq.final_response.body,
8888
ctx.rq.final_response.headers || {},
89+
ctx.rq.final_request.query_params
8990
),
9091
requestShellCurl: this.generate_curl_from_har(ctx?.rq?.final_request?.requestHarObject), // TODO: Move this to client side
9192
actions: get_success_actions_from_action_results(action_result_objs),

0 commit comments

Comments
 (0)