Added proposed binary proxy response code

This commit is contained in:
Andrew Bastin
2020-06-28 02:50:14 -04:00
parent 8ea64e2224
commit 7778202439
3 changed files with 52 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
import axios from "axios" import axios from "axios"
// import { isJSONContentType } from "../utils/contenttypes" import { decodeB64StringToArrayBuffer } from "../utils/b64"
let cancelSource = axios.CancelToken.source() let cancelSource = axios.CancelToken.source()
@@ -14,11 +14,19 @@ const axiosWithProxy = async (req, { state }) => {
try { try {
const { data } = await axios.post( const { data } = await axios.post(
state.postwoman.settings.PROXY_URL || "https://postwoman.apollosoftware.xyz/", state.postwoman.settings.PROXY_URL || "https://postwoman.apollosoftware.xyz/",
req, {
...req,
wantsBinary: true,
},
{ {
cancelToken: cancelSource.token, cancelToken: cancelSource.token,
} }
) )
if (data.isBinary) {
data.data = decodeB64StringToArrayBuffer(data.data)
}
return data return data
} catch (e) { } catch (e) {
// Check if the throw is due to a cancellation // Check if the throw is due to a cancellation

View File

@@ -1,3 +1,5 @@
import { decodeB64StringToArrayBuffer } from "../utils/b64"
export const hasExtensionInstalled = () => export const hasExtensionInstalled = () =>
typeof window.__POSTWOMAN_EXTENSION_HOOK__ !== "undefined" typeof window.__POSTWOMAN_EXTENSION_HOOK__ !== "undefined"
@@ -17,8 +19,16 @@ const extensionWithProxy = async (req, { state }) => {
const { data } = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({ const { data } = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({
method: "post", method: "post",
url: state.postwoman.settings.PROXY_URL || "https://postwoman.apollosoftware.xyz/", url: state.postwoman.settings.PROXY_URL || "https://postwoman.apollosoftware.xyz/",
data: req, data: {
...req,
wantsBinary: true,
},
}) })
if (data.isBinary) {
data.data = decodeB64StringToArrayBuffer(data.data)
}
return data return data
} }

31
helpers/utils/b64.js Normal file
View File

@@ -0,0 +1,31 @@
export const decodeB64StringToArrayBuffer = (input) => {
const bytes = Math.floor((input / 4) * 3)
const ab = new ArrayBuffer(bytes)
const uarray = new Uint8Array(ab)
const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
let chr1, chr2, chr3
let enc1, enc2, enc3, enc4
let j = 0
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "")
for (let i = 0; i < bytes; i += 3) {
//get the 3 octets in 4 ASCII chars
enc1 = keyStr.indexOf(input.charAt(j++))
enc2 = keyStr.indexOf(input.charAt(j++))
enc3 = keyStr.indexOf(input.charAt(j++))
enc4 = keyStr.indexOf(input.charAt(j++))
chr1 = (enc1 << 2) | (enc2 >> 4)
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
chr3 = ((enc3 & 3) << 6) | enc4
uarray[i] = chr1
if (enc3 != 64) uarray[i + 1] = chr2
if (enc4 != 64) uarray[i + 2] = chr3
}
return ab
}