aboutsummaryrefslogtreecommitdiff
path: root/parsers/http_request_parser.go
blob: 98ba8e31c41db82b89a21ef2e4d565ae892b399a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * 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 (
	"bufio"
	"bytes"
	"encoding/json"
	log "github.com/sirupsen/logrus"
	"io/ioutil"
	"moul.io/http2curl"
	"net/http"
	"strings"
)

type HTTPRequestMetadata struct {
	BasicMetadata
	Method        string                         `json:"method"`
	URL           string                         `json:"url"`
	Protocol      string                         `json:"protocol"`
	Host          string                         `json:"host"`
	Headers       map[string]string              `json:"headers"`
	Cookies       map[string]string              `json:"cookies" binding:"omitempty"`
	ContentLength int64                          `json:"content_length"`
	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"`
}

type HTTPRequestMetadataReproducers struct {
	CurlCommand  string `json:"curl_command"`
	RequestsCode string `json:"requests_code"`
	FetchRequest string `json:"fetch_request"`
}

type HTTPRequestParser struct {
}

func (p HTTPRequestParser) TryParse(content []byte) Metadata {
	reader := bufio.NewReader(bytes.NewReader(content))
	request, err := http.ReadRequest(reader)
	if err != nil {
		return nil
	}
	var body string
	if buffer, err := ioutil.ReadAll(request.Body); err == nil {
		body = string(buffer)
	} else {
		log.WithError(err).Error("failed to read body in http_request_parser")
		return nil
	}
	_ = request.Body.Close()
	_ = request.ParseForm()

	return HTTPRequestMetadata{
		BasicMetadata: BasicMetadata{"http-request"},
		Method:        request.Method,
		URL:           request.URL.String(),
		Protocol:      request.Proto,
		Host:          request.Host,
		Headers:       JoinArrayMap(request.Header),
		Cookies:       CookiesMap(request.Cookies()),
		ContentLength: request.ContentLength,
		FormData:      JoinArrayMap(request.Form),
		Body:          body,
		Trailer:       JoinArrayMap(request.Trailer),
		Reproducers: HTTPRequestMetadataReproducers{
			CurlCommand:  curlCommand(content),
			RequestsCode: requestsCode(request, body),
			FetchRequest: fetchRequest(request, body),
		},
	}
}

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)
	command, err := http2curl.GetCurlCommand(request)
	if err == nil {
		return command.String()
	}
	return err.Error()
}

func requestsCode(request *http.Request, body string) string {
	var b strings.Builder
	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 body != "" {
		b.WriteString(", data = \"" + strings.Replace(body, "\"", "\\\"", -1) + "\"")
	}
	if headers != "" {
		b.WriteString(", headers = " + headers)
	}
	if cookies != "" {
		b.WriteString(", cookies = " + cookies)
	}
	b.WriteString(")\n")
	b.WriteString(`
# print(response.url)
# print(response.text)
# print(response.content)
# print(response.json())
# print(response.raw)
# print(response.status_code)
# print(response.cookies)
# print(response.history)
`)

	return b.String()
}

func fetchRequest(request *http.Request, body string) string {
	headers := JoinArrayMap(request.Header)
	data := make(map[string]interface{})
	data["headers"] = headers
	if referrer := request.Header.Get("referrer"); referrer != "" {
		data["Referrer"] = referrer
	}
	// TODO: referrerPolicy
	if body == "" {
		data["body"] = nil
	} else {
		data["body"] = body
	}
	data["method"] = request.Method
	// TODO: mode

	if jsonData := toJSON(data); jsonData != "" {
		return "fetch(\"" + request.URL.String() + "\", " + jsonData + ");"
	}
	return "invalid-request"
}

func toJSON(obj interface{}) string {
	if buffer, err := json.MarshalIndent(obj, "", "\t"); err == nil {
		return string(buffer)
	} else {
		return ""
	}
}