mirror of
https://github.com/curlconverter/curlconverter.git
synced 2022-05-22 02:35:29 +03:00
escape strings in Go generator (#381)
This commit is contained in:
committed by
GitHub
parent
ac70a67e55
commit
1640ca881a
@@ -3,43 +3,58 @@ import type { Request } from "../util.js";
|
||||
|
||||
import jsesc from "jsesc";
|
||||
|
||||
const reprMaybeBacktick = (s: string): string => {
|
||||
return s.includes('"') && !s.includes("`") ? reprBacktick(s) : repr(s);
|
||||
};
|
||||
const reprBacktick = (s: string): string => {
|
||||
return "`" + s + "`";
|
||||
};
|
||||
const repr = (s: string): string => {
|
||||
return '"' + jsesc(s, { quotes: "double" }) + '"';
|
||||
};
|
||||
|
||||
export const _toGo = (request: Request): string => {
|
||||
let goCode = "package main\n\n";
|
||||
goCode += 'import (\n\t"fmt"\n\t"io/ioutil"\n\t"log"\n\t"net/http"\n)\n\n';
|
||||
goCode += "import (\n";
|
||||
goCode += '\t"fmt"\n';
|
||||
goCode += '\t"io/ioutil"\n';
|
||||
goCode += '\t"log"\n';
|
||||
goCode += '\t"net/http"\n';
|
||||
if (request.data) {
|
||||
goCode += '\t"strings"\n';
|
||||
}
|
||||
goCode += ")\n\n";
|
||||
goCode += "func main() {\n";
|
||||
goCode += "\tclient := &http.Client{}\n";
|
||||
if (request.data) {
|
||||
if (request.data.indexOf("'") > -1) {
|
||||
request.data = jsesc(request.data);
|
||||
}
|
||||
// import strings
|
||||
goCode = goCode.replace("\n)", '\n\t"strings"\n)');
|
||||
goCode += "\tvar data = strings.NewReader(`" + request.data + "`)\n";
|
||||
goCode +=
|
||||
'\treq, err := http.NewRequest("' +
|
||||
request.method +
|
||||
'", "' +
|
||||
request.url +
|
||||
'", data)\n';
|
||||
} else {
|
||||
goCode +=
|
||||
'\treq, err := http.NewRequest("' +
|
||||
request.method +
|
||||
'", "' +
|
||||
request.url +
|
||||
'", nil)\n';
|
||||
"\tvar data = strings.NewReader(" + reprBacktick(request.data) + ")\n";
|
||||
}
|
||||
goCode += "\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n";
|
||||
goCode +=
|
||||
"\treq, err := http.NewRequest(" +
|
||||
repr(request.method) +
|
||||
", " +
|
||||
repr(request.url);
|
||||
goCode += ", " + (request.data ? "data" : "nil") + ")\n";
|
||||
|
||||
goCode += "\tif err != nil {\n";
|
||||
goCode += "\t\tlog.Fatal(err)\n";
|
||||
goCode += "\t}\n";
|
||||
if (request.headers) {
|
||||
for (const [headerName, headerValue] of request.headers || []) {
|
||||
goCode +=
|
||||
'\treq.Header.Set("' + headerName + '", "' + headerValue + '")\n';
|
||||
"\treq.Header.Set(" +
|
||||
repr(headerName) +
|
||||
", " +
|
||||
reprMaybeBacktick(headerValue ?? "") +
|
||||
")\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (request.auth) {
|
||||
const [user, password] = request.auth;
|
||||
goCode += '\treq.SetBasicAuth("' + user + '", "' + password + '")\n';
|
||||
goCode +=
|
||||
"\treq.SetBasicAuth(" + repr(user) + ", " + repr(password) + ")\n";
|
||||
}
|
||||
goCode += "\tresp, err := client.Do(req)\n";
|
||||
goCode += "\tif err != nil {\n";
|
||||
|
||||
4
test/fixtures/curl_commands/get_header_with_quotes.sh
vendored
Normal file
4
test/fixtures/curl_commands/get_header_with_quotes.sh
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# https://github.com/curlconverter/curlconverter/issues/272
|
||||
curl 'http://localhost:28139' \
|
||||
-H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="92"' \
|
||||
--compressed
|
||||
27
test/fixtures/go/get_header_with_quotes.go
vendored
Normal file
27
test/fixtures/go/get_header_with_quotes.go
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", "http://localhost:28139", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="92"`)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
bodyText, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("%s\n", bodyText)
|
||||
}
|
||||
Reference in New Issue
Block a user