Skip to content

Commit f1473b0

Browse files
authored
[RQ-637] fix: allow static response modification for all content-type (#22)
1 parent 086d894 commit f1473b0

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/components/proxy-middleware/index.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,31 +219,21 @@ class ProxyMiddlewareManager {
219219
const contentTypeHeader = getResponseContentTypeHeader(ctx);
220220
const contentType = getContentType(contentTypeHeader);
221221
const parsedBody = bodyParser(contentTypeHeader, body);
222+
ctx.rq.set_original_response({ body: parsedBody });
222223

223224
ctx.rq_response_body = body;
225+
ctx.rq_parsed_response_body = parsedBody;
224226
ctx.rq_response_status_code = getResponseStatusCode(ctx);
227+
const { action_result_objs, continue_request } = await rules_middleware.on_response_end(ctx);
225228

226-
if (RQ_INTERCEPTED_CONTENT_TYPES.includes(contentType) && parsedBody) {
227-
ctx.rq.set_original_response({ body: parsedBody });
228229

229-
// Body and status code before any modifications
230-
ctx.rq_response_body = parsedBody;
231-
232-
const { action_result_objs, continue_request } =
233-
await rules_middleware.on_response_end(ctx);
234-
235-
// ctx.rq_response_body, ctx.rq_response_status_code after modifications
236-
237-
// TODO: @sahil to investigate why this is need
238-
// Remove some conflicting headers like content-length, if any
239-
delete getResponseHeaders(ctx)["content-length"];
240-
}
241230
const statusCode = ctx.rq_response_status_code || getResponseStatusCode(ctx);
242231
ctx.proxyToClientResponse.writeHead(
243232
statusCode,
244233
http.STATUS_CODES[statusCode],
245234
getResponseHeaders(ctx)
246235
);
236+
247237
ctx.proxyToClientResponse.write(ctx.rq_response_body);
248238

249239
ctx.rq.set_final_response({

src/components/proxy-middleware/rule_action_processor/processors/modify_response_processor.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { PROXY_HANDLER_TYPE } from "../../../../lib/proxy";
33
import {
44
CONSTANTS as GLOBAL_CONSTANTS,
55
} from "@requestly/requestly-core";
6-
import { get_request_url } from "../../helpers/proxy_ctx_helper";
6+
import { getResponseContentTypeHeader, getResponseHeaders, get_request_url } from "../../helpers/proxy_ctx_helper";
77
import { build_action_processor_response, build_post_process_data } from "../utils";
88
import fs from "fs";
9-
import { parseJsonBody } from "../../helpers/http_helpers";
9+
import { getContentType, parseJsonBody } from "../../helpers/http_helpers";
1010
import ConsoleCapture from "capture-console-logs";
1111
import { getFunctionFromString } from "../../../../utils";
12+
import { RQ_INTERCEPTED_CONTENT_TYPES } from "../../constants";
1213

1314
const { types } = require("util");
1415

@@ -54,19 +55,35 @@ const process_modify_response_action = async (action, ctx) => {
5455
action.responseType &&
5556
action.responseType === GLOBAL_CONSTANTS.RESPONSE_BODY_TYPES.CODE
5657
) {
57-
await modify_response_using_code(action, ctx);
58-
return build_action_processor_response(action, true);
58+
const contentTypeHeader = getResponseContentTypeHeader(ctx);
59+
const contentType = getContentType(contentTypeHeader);
60+
if (RQ_INTERCEPTED_CONTENT_TYPES.includes(contentType) || contentType == null) {
61+
await modify_response_using_code(action, ctx);
62+
delete_breaking_headers(ctx);
63+
return build_action_processor_response(action, true);
64+
}
65+
66+
// Sentry not working
67+
// Sentry.captureException(new Error(`Content Type ${contentType} not supported for modification in programmatic mode`));
68+
console.log(`Content Type ${contentType} not supported for modification in programmatic mode`);
69+
return build_action_processor_response(action, false);
5970
} else if (
6071
action.responseType === GLOBAL_CONSTANTS.RESPONSE_BODY_TYPES.LOCAL_FILE
6172
) {
6273
modify_response_using_local(action, ctx);
74+
delete_breaking_headers(ctx);
6375
return build_action_processor_response(action, true);
6476
} else {
6577
modify_response(ctx, action.response, action.statusCode);
78+
delete_breaking_headers(ctx);
6679
return build_action_processor_response(action, true);
6780
}
6881
};
6982

83+
const delete_breaking_headers = (ctx) => {
84+
delete getResponseHeaders(ctx)['content-length'];
85+
}
86+
7087
const modify_response = (ctx, new_resp, status_code) => {
7188
ctx.rq_response_body = new_resp;
7289
ctx.rq_response_status_code = status_code;
@@ -113,7 +130,7 @@ const modify_response_using_code = async (action, ctx) => {
113130
? ctx.clientToProxyRequest.method
114131
: null
115132
: null,
116-
response: ctx?.rq_response_body,
133+
response: ctx?.rq_parsed_response_body,
117134
url: get_request_url(ctx),
118135
responseType: ctx?.serverToProxyResponse?.headers?.["content-type"],
119136
requestHeaders: ctx.clientToProxyRequest.headers,

0 commit comments

Comments
 (0)