-
Notifications
You must be signed in to change notification settings - Fork 12
fix(redirect): RQ-5543 — apply response header rules to redirect destinations #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
11bcaf6
3414fe0
75848b6
edf90cf
a414079
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,36 @@ | ||
| const axios = require("axios"); | ||
| const parser = require("ua-parser-js"); | ||
| import fs from "fs"; | ||
| import * as Sentry from "@sentry/browser"; | ||
| const mime = require('mime-types'); | ||
|
|
||
| const handleMixedResponse = async (ctx, destinationUrl) => { | ||
| // Handling mixed response from safari | ||
| let user_agent_str = null; | ||
| user_agent_str = ctx?.clientToProxyRequest?.headers["user-agent"]; | ||
| const user_agent = parser(user_agent_str)?.browser?.name; | ||
| const LOCAL_DOMAINS = ["localhost", "127.0.0.1"]; | ||
|
|
||
| if (ctx.isSSL && destinationUrl.includes("http:")) { | ||
| if ( | ||
| user_agent === "Safari" || | ||
| !LOCAL_DOMAINS.some((domain) => destinationUrl.includes(domain)) | ||
| ) { | ||
| try { | ||
| const resp = await axios.get(destinationUrl, { | ||
| headers: { | ||
| "Cache-Control": "no-cache", | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| status: true, | ||
| response_data: { | ||
| headers: { "Cache-Control": "no-cache" }, | ||
| status_code: 200, | ||
| body: resp.data, | ||
| }, | ||
| }; | ||
| } catch (e) { | ||
| Sentry.captureException(e); | ||
| return { | ||
| status: true, | ||
| response_data: { | ||
| headers: { "Cache-Control": "no-cache" }, | ||
| status_code: 502, | ||
| body: e.response ? e.response.data : null, | ||
| }, | ||
| }; | ||
| } | ||
| try { | ||
| const resp = await axios.get(destinationUrl, { | ||
| responseType: "arraybuffer", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why Arraybugger and not keeping it the same type as before? |
||
| decompress: false, // 2. CRITICAL: Prevent Axios from unwrapping gzip | ||
| validateStatus: () => true, | ||
| headers: { "Cache-Control": "no-cache" }, | ||
| }); | ||
| return { | ||
| status: true, | ||
| response_data: { | ||
| status_code: resp.status, | ||
| headers: resp.headers, // real upstream headers (content-encoding already stripped by axios) | ||
| body: resp.data, // Buffer | ||
| }, | ||
| }; | ||
| } catch (e) { | ||
| // Only transport failures reach here now (DNS, refused, timeout, redirect loop). | ||
| Sentry.captureException(e); | ||
| return { | ||
| status: true, | ||
| response_data: { | ||
| headers: e.response ? e.response.headers : { "Cache-Control": "no-cache" }, | ||
| status_code: e.response ? e.response.status : 502, | ||
| body: e.response ? e.response.data : null, | ||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import handleMixedResponse from "../handle_mixed_response"; | |
| import { | ||
| build_action_processor_response, | ||
| build_post_process_data, | ||
| stripHopByHopHeaders, | ||
| } from "../utils"; | ||
|
|
||
| // adding util to get origin header for handling cors | ||
|
|
@@ -46,14 +47,37 @@ const process_redirect_action = async (action, ctx) => { | |
| ); | ||
|
|
||
| if (isMixedResponse) { | ||
| // Feed the fetched response into the response context, then run the existing | ||
| // response Modify Headers processor against the destination URL. | ||
| ctx.serverToProxyResponse = { | ||
| statusCode: response_data.status_code, | ||
| headers: { ...(response_data.headers || {}) }, | ||
| }; | ||
| if (typeof ctx.rq.applyResponseHeaderRulesForRedirect === "function") { | ||
| ctx.rq.applyResponseHeaderRulesForRedirect(new_url); | ||
| } | ||
| const headers = stripHopByHopHeaders(ctx.serverToProxyResponse.headers); | ||
|
Comment on lines
+56
to
+59
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the right fix as other type of modifications might be required too in future. This should via the same action -> processing pipeline somehow so that rule gets triggered automatically |
||
|
|
||
| // Recalculate content-length for the flattened buffer | ||
| let body = response_data.body; | ||
| if (body !== null && body !== undefined) { | ||
| if (Buffer.isBuffer(body)) { | ||
| headers['content-length'] = body.length; | ||
| } else if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) { | ||
| headers['content-length'] = body.byteLength; | ||
| } else if (typeof body === 'string') { | ||
| headers['content-length'] = Buffer.byteLength(body); | ||
| } else { | ||
| headers['content-length'] = Buffer.byteLength(JSON.stringify(body)); | ||
| } | ||
|
dinex-dev marked this conversation as resolved.
|
||
| } else { | ||
| headers["content-length"] = 0; | ||
| } | ||
|
Comment on lines
+62
to
+75
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should do all the preparation in the upstream method handleMixedResponse which should prepare and return the final response |
||
|
|
||
| return build_action_processor_response( | ||
| action, | ||
| true, | ||
| build_post_process_data( | ||
| response_data.status_code, | ||
| response_data.headers, | ||
| response_data.body | ||
| ) | ||
| build_post_process_data(response_data.status_code, headers, body) | ||
| ); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did we remove this check below?