diff options
Diffstat (limited to 'parsers')
-rw-r--r-- | parsers/http_request_parser.go | 60 | ||||
-rw-r--r-- | parsers/http_response_parser.go | 29 | ||||
-rw-r--r-- | parsers/parser.go | 21 | ||||
-rw-r--r-- | parsers/parser_utils.go | 17 |
4 files changed, 95 insertions, 32 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 { diff --git a/parsers/http_response_parser.go b/parsers/http_response_parser.go index 1770116..e61fffd 100644 --- a/parsers/http_response_parser.go +++ b/parsers/http_response_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 ( @@ -9,7 +26,7 @@ import ( "net/http" ) -type HttpResponseMetadata struct { +type HTTPResponseMetadata struct { BasicMetadata Status string `json:"status"` StatusCode int `json:"status_code"` @@ -23,10 +40,10 @@ type HttpResponseMetadata struct { Trailer map[string]string `json:"trailer" binding:"omitempty"` } -type HttpResponseParser struct { +type HTTPResponseParser struct { } -func (p HttpResponseParser) TryParse(content []byte) Metadata { +func (p HTTPResponseParser) TryParse(content []byte) Metadata { reader := bufio.NewReader(bytes.NewReader(content)) response, err := http.ReadResponse(reader, nil) if err != nil { @@ -57,11 +74,11 @@ func (p HttpResponseParser) TryParse(content []byte) Metadata { _ = response.Body.Close() var location string - if locationUrl, err := response.Location(); err == nil { - location = locationUrl.String() + if locationURL, err := response.Location(); err == nil { + location = locationURL.String() } - return HttpResponseMetadata{ + return HTTPResponseMetadata{ BasicMetadata: BasicMetadata{"http-response"}, Status: response.Status, StatusCode: response.StatusCode, diff --git a/parsers/parser.go b/parsers/parser.go index 06cc0dc..9aca3b6 100644 --- a/parsers/parser.go +++ b/parsers/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 type Parser interface { @@ -13,8 +30,8 @@ type BasicMetadata struct { } var parsers = []Parser{ // order matter - HttpRequestParser{}, - HttpResponseParser{}, + HTTPRequestParser{}, + HTTPResponseParser{}, } func Parse(content []byte) Metadata { diff --git a/parsers/parser_utils.go b/parsers/parser_utils.go index b688262..575b666 100644 --- a/parsers/parser_utils.go +++ b/parsers/parser_utils.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 ( |