Skip to content

Commit 2f60dbd

Browse files
committed
update: node fetch ssl ignore, no auto follow request, and copy as curl
1 parent e2b1b08 commit 2f60dbd

File tree

8 files changed

+199
-14
lines changed

8 files changed

+199
-14
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@
271271
"@types/http-string-parser": "^0.0.33",
272272
"@types/mocha": "^10.0.10",
273273
"@types/node": "~20.19.1",
274+
"@types/node-fetch": "^2.6.13",
274275
"@types/vscode": "^1.101.0",
275276
"@typescript-eslint/eslint-plugin": "^8.35.0",
276277
"@typescript-eslint/parser": "^8.35.0",
@@ -285,7 +286,9 @@
285286
"webpack-cli": "^6.0.1"
286287
},
287288
"dependencies": {
289+
"fetch-to-curl": "^0.6.0",
288290
"http-string-parser": "^0.0.6",
291+
"node-fetch": "^2.7.0",
289292
"table": "^6.9.0",
290293
"vscode-variables": "^1.0.1",
291294
"yaml": "^2.8.0"

pnpm-lock.yaml

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/codelens/httpreapter/curl.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { MarkdownHTTPCodeLensGenerator } from "./base";
2+
import * as vscode from "vscode";
3+
4+
export const httpToCurlCodeLens: MarkdownHTTPCodeLensGenerator = (post: string[], startLine: number, document: vscode.TextDocument): vscode.CodeLens[] => {
5+
const codeLenses: vscode.CodeLens[] = [];
6+
const cmd: vscode.Command = {
7+
title: "Copy in curl (HTTP)",
8+
command: "weapon.http_raw_request_to_curl",
9+
arguments: [
10+
{
11+
request: post.join("\r\n"),
12+
isHTTPS: false,
13+
},
14+
],
15+
};
16+
let cmd2: vscode.Command = {
17+
title: "Copy in curl (HTTPS)",
18+
command: "weapon.http_raw_request_to_curl",
19+
arguments: [
20+
{
21+
request: post.join("\r\n"),
22+
isHTTPS: true,
23+
},
24+
],
25+
};
26+
codeLenses.push(
27+
new vscode.CodeLens(
28+
new vscode.Range(
29+
new vscode.Position(startLine, 0),
30+
new vscode.Position(startLine + 1, 0)
31+
),
32+
cmd
33+
),
34+
new vscode.CodeLens(
35+
new vscode.Range(
36+
new vscode.Position(startLine, 0),
37+
new vscode.Position(startLine + 1, 0)
38+
),
39+
cmd2
40+
)
41+
);
42+
43+
return codeLenses;
44+
};

src/codelens/httpreapter/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { MarkdownHTTPCodeLensProvider } from "./base";
22
import { httpRepeaterCodeLens } from "./send";
3+
import { httpToCurlCodeLens } from "./curl";
34

4-
export const httpRepeater = new MarkdownHTTPCodeLensProvider(httpRepeaterCodeLens);
5+
export const httpRepeater = new MarkdownHTTPCodeLensProvider(httpRepeaterCodeLens);
6+
export const httpToCurl = new MarkdownHTTPCodeLensProvider(httpToCurlCodeLens);

src/codelens/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { targetFilePattern } from "../global/const";
33
import { markdownCodelens } from "./yamlconfig";
44
import { CommandCodeLensProvider } from "./command/commandProvider";
55
import { NoteCreationProvider } from "./newnote/noteProvider";
6-
import { httpRepeater } from "./httpreapter";
6+
import { httpRepeater, httpToCurl } from "./httpreapter";
77

88
export function registerCodeLensProviders(context: vscode.ExtensionContext) {
99
context.subscriptions.push(
@@ -19,6 +19,10 @@ export function registerCodeLensProviders(context: vscode.ExtensionContext) {
1919
{ language: "markdown", scheme: "file", pattern: targetFilePattern },
2020
new NoteCreationProvider()
2121
),
22+
vscode.languages.registerCodeLensProvider(
23+
{ language: "markdown", scheme: "file", pattern: targetFilePattern },
24+
httpToCurl
25+
),
2226
vscode.languages.registerCodeLensProvider(
2327
{ language: "markdown", scheme: "file", pattern: targetFilePattern },
2428
httpRepeater

src/commands/http/request.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { callback } from "../utils";
22
import { parseRequest, ParseRequestResult } from "http-string-parser";
33
import * as vscode from "vscode";
44
import { logger } from "../../global/log";
5+
import fetch, { type RequestInit, type Response } from "node-fetch";
6+
import { Agent as httpsAgent } from "https";
7+
import { Agent as HttpAgent } from "http";
58

69
export const rawHTTPRequest: callback = async (args) => {
710
let request: string | undefined = args.request ? args.request : undefined;
@@ -34,20 +37,26 @@ export const rawHTTPRequest: callback = async (args) => {
3437
headers
3538
)}, body: ${body ? body : "none"}`
3639
);
37-
let response: Response | undefined = undefined;
40+
let requestInit: RequestInit = {
41+
method,
42+
headers,
43+
redirect: 'manual',
44+
follow: 0, // Disable automatic following of redirects
45+
};
3846
if (method === "GET" || method === "HEAD") {
3947
// For GET and HEAD requests, we should not send a body
40-
response = await fetch(url, {
41-
method,
42-
headers,
43-
});
48+
requestInit.body = undefined;
49+
// requestInit.body = body; // Explicitly set body to null for clarity
4450
} else {
45-
response = await fetch(url, {
46-
method,
47-
headers,
48-
body,
51+
requestInit.body = body;
52+
}
53+
if (isHTTPS) {
54+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
55+
requestInit.agent = new httpsAgent({
56+
rejectUnauthorized: false, // Disable SSL verification for testing purposes
4957
});
5058
}
59+
let response: Response = await fetch(url, requestInit);
5160
let responseText: string = `HTTP/1.1 ${response.status} ${response.statusText}\n`;
5261
for (const [key, value] of response.headers.entries()) {
5362
responseText += `${key}: ${value}\n`;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { callback } from "../utils";
2+
import { parseRequest, ParseRequestResult } from "http-string-parser";
3+
import * as vscode from "vscode";
4+
import { logger } from "../../global/log";
5+
import fetchToCurl from "fetch-to-curl";
6+
7+
export const rawHTTPRequestToCurl: callback = async (args) => {
8+
let request: string | undefined = args.request ? args.request : undefined;
9+
if (!request) {
10+
vscode.window.showErrorMessage("No request provided");
11+
return;
12+
}
13+
let isHTTPS = args.isHTTPS ? args.isHTTPS : false;
14+
15+
let res: ParseRequestResult;
16+
try {
17+
res = parseRequest(request);
18+
if (!res) {
19+
logger.error("Failed to parse request", request);
20+
vscode.window.showErrorMessage("Invalid request format");
21+
return;
22+
}
23+
} catch (e: any) {
24+
logger.error("raise Error parsing request", e);
25+
vscode.window.showErrorMessage(`Error parsing request: ${e.message}`);
26+
return;
27+
}
28+
try {
29+
const { method, uri, headers, body } = res;
30+
let url = headers["Host"]
31+
? `${isHTTPS ? "https" : "http"}://${headers["Host"]}${uri}`
32+
: uri;
33+
let command = fetchToCurl({
34+
method,
35+
url,
36+
headers,
37+
body
38+
});
39+
await vscode.window.showTextDocument(
40+
vscode.Uri.parse(
41+
"weaponized-editor:response.http?" + encodeURIComponent(command)
42+
),
43+
{
44+
preview: false,
45+
viewColumn: vscode.ViewColumn.Beside,
46+
}
47+
);
48+
vscode.env.clipboard.writeText(command);
49+
vscode.window.showInformationMessage("cURL command copied to clipboard");
50+
} catch (e: any) {
51+
logger.error("raise Error fetching request", e);
52+
vscode.window.showErrorMessage(`Error fetching request: ${e.message}`);
53+
return;
54+
}
55+
};

src/commands/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { switchActiveUser } from "./switch/user";
1212
import { cyberChefMagicDecoder } from "./decoder/cyberchef";
1313
import { copyCommand } from "./copy/copy";
1414
import { rawHTTPRequest } from "./http/request";
15+
import { rawHTTPRequestToCurl } from "./http/request_to_curl";
1516

1617
export function registerCommandsPackage(context: vscode.ExtensionContext) {
1718
context.subscriptions.push(
@@ -44,8 +45,10 @@ export function registerCommandsPackage(context: vscode.ExtensionContext) {
4445
"weaponized-editor",
4546
new ReadOnlyProvider()
4647
),
48+
vscode.commands.registerCommand("weapon.http_raw_request", rawHTTPRequest),
4749
vscode.commands.registerCommand(
48-
"weapon.http_raw_request",
49-
rawHTTPRequest
50-
));
50+
"weapon.http_raw_request_to_curl",
51+
rawHTTPRequestToCurl
52+
)
53+
);
5154
}

0 commit comments

Comments
 (0)