aboutsummaryrefslogtreecommitdiff
path: root/parsers/http_request_parser.go
diff options
context:
space:
mode:
Diffstat (limited to 'parsers/http_request_parser.go')
-rw-r--r--parsers/http_request_parser.go60
1 files changed, 36 insertions, 24 deletions
diff --git a/parsers/http_request_parser.go b/parsers/http_request_parser.go
index e2224b8..98ba8e3 100644
--- a/parsers/http_request_parser.go
+++ b/parsers/http_request_parser.go
@@ -1,3 +1,20 @@
+/*
+ * This file is part of caronte (https://github.com/eciavatta/caronte).
+ * Copyright (c) 2020 Emiliano Ciavatta.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
package parsers
import (
@@ -11,7 +28,7 @@ import (
"strings"
)
-type HttpRequestMetadata struct {
+type HTTPRequestMetadata struct {
BasicMetadata
Method string `json:"method"`
URL string `json:"url"`
@@ -23,19 +40,19 @@ type HttpRequestMetadata struct {
FormData map[string]string `json:"form_data" binding:"omitempty"`
Body string `json:"body" binding:"omitempty"`
Trailer map[string]string `json:"trailer" binding:"omitempty"`
- Reproducers HttpRequestMetadataReproducers `json:"reproducers"`
+ Reproducers HTTPRequestMetadataReproducers `json:"reproducers"`
}
-type HttpRequestMetadataReproducers struct {
+type HTTPRequestMetadataReproducers struct {
CurlCommand string `json:"curl_command"`
RequestsCode string `json:"requests_code"`
FetchRequest string `json:"fetch_request"`
}
-type HttpRequestParser struct {
+type HTTPRequestParser struct {
}
-func (p HttpRequestParser) TryParse(content []byte) Metadata {
+func (p HTTPRequestParser) TryParse(content []byte) Metadata {
reader := bufio.NewReader(bytes.NewReader(content))
request, err := http.ReadRequest(reader)
if err != nil {
@@ -51,7 +68,7 @@ func (p HttpRequestParser) TryParse(content []byte) Metadata {
_ = request.Body.Close()
_ = request.ParseForm()
- return HttpRequestMetadata{
+ return HTTPRequestMetadata{
BasicMetadata: BasicMetadata{"http-request"},
Method: request.Method,
URL: request.URL.String(),
@@ -63,9 +80,9 @@ func (p HttpRequestParser) TryParse(content []byte) Metadata {
FormData: JoinArrayMap(request.Form),
Body: body,
Trailer: JoinArrayMap(request.Trailer),
- Reproducers: HttpRequestMetadataReproducers{
+ Reproducers: HTTPRequestMetadataReproducers{
CurlCommand: curlCommand(content),
- RequestsCode: requestsCode(request),
+ RequestsCode: requestsCode(request, body),
FetchRequest: fetchRequest(request, body),
},
}
@@ -75,26 +92,22 @@ func curlCommand(content []byte) string {
// a new reader is required because all the body is read before and GetBody() doesn't works
reader := bufio.NewReader(bytes.NewReader(content))
request, _ := http.ReadRequest(reader)
- if command, err := http2curl.GetCurlCommand(request); err == nil {
+ command, err := http2curl.GetCurlCommand(request)
+ if err == nil {
return command.String()
- } else {
- return err.Error()
}
+ return err.Error()
}
-func requestsCode(request *http.Request) string {
+func requestsCode(request *http.Request, body string) string {
var b strings.Builder
- var params string
- if request.Form != nil {
- params = toJson(JoinArrayMap(request.PostForm))
- }
- headers := toJson(JoinArrayMap(request.Header))
- cookies := toJson(CookiesMap(request.Cookies()))
+ headers := toJSON(JoinArrayMap(request.Header))
+ cookies := toJSON(CookiesMap(request.Cookies()))
b.WriteString("import requests\n\nresponse = requests." + strings.ToLower(request.Method) + "(")
b.WriteString("\"" + request.URL.String() + "\"")
- if params != "" {
- b.WriteString(", data = " + params)
+ if body != "" {
+ b.WriteString(", data = \"" + strings.Replace(body, "\"", "\\\"", -1) + "\"")
}
if headers != "" {
b.WriteString(", headers = " + headers)
@@ -133,14 +146,13 @@ func fetchRequest(request *http.Request, body string) string {
data["method"] = request.Method
// TODO: mode
- if jsonData := toJson(data); jsonData != "" {
+ if jsonData := toJSON(data); jsonData != "" {
return "fetch(\"" + request.URL.String() + "\", " + jsonData + ");"
- } else {
- return "invalid-request"
}
+ return "invalid-request"
}
-func toJson(obj interface{}) string {
+func toJSON(obj interface{}) string {
if buffer, err := json.MarshalIndent(obj, "", "\t"); err == nil {
return string(buffer)
} else {