-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
196 lines (172 loc) · 5.49 KB
/
Copy pathparser.go
File metadata and controls
196 lines (172 loc) · 5.49 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
type ParsedRequest struct {
Method string
Scheme string
Host string
Path string
Proto string // "HTTP/1.1" or "HTTP/2"
Headers map[string][]string
Body string
BodyType string
Fields map[string]any
}
func ParseRequestFile(path string) (ParsedRequest, error) {
rawBytes, err := os.ReadFile(path)
if err != nil {
return ParsedRequest{}, fmt.Errorf("reading file: %w", err)
}
raw := string(rawBytes)
// Detect the original protocol version from the request line,
// then normalize to HTTP/1.1 so http.ReadRequest can parse it.
proto := "HTTP/1.1"
raw = normalizeProto(raw, &proto)
// Handle HTTP/2 pseudo-headers exported by Burp Suite.
// These look like regular headers but start with ":".
// Extract them and convert to standard HTTP/1.1 format.
raw = convertPseudoHeaders(raw, &proto)
// http.ReadRequest requires Content-Length for body parsing.
// If missing, inject it so the body is read properly.
parts := strings.SplitN(raw, "\n\n", 2)
if len(parts) == 2 {
bodyPart := strings.TrimSpace(parts[1])
if bodyPart != "" && !strings.Contains(parts[0], "Content-Length:") && !strings.Contains(parts[0], "content-length:") {
raw = parts[0] + "\nContent-Length: " + strconv.Itoa(len(bodyPart)) + "\n\n" + bodyPart
}
}
req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(raw)))
if err != nil {
return ParsedRequest{}, fmt.Errorf("parsing request: %w", err)
}
parsedRequest := new(ParsedRequest)
parsedRequest.Method = req.Method
parsedRequest.Scheme = detectScheme(req.Host)
parsedRequest.Host = req.Host
parsedRequest.Path = req.URL.RequestURI()
parsedRequest.Proto = proto
parsedRequest.Headers = req.Header
bodyBytes, err := io.ReadAll(req.Body)
if err != nil {
return ParsedRequest{}, fmt.Errorf("reading body: %w", err)
}
parsedRequest.Body = string(bodyBytes)
contentType := req.Header.Get("Content-Type")
if strings.Contains(contentType, "application/json") {
parsedRequest.BodyType = "json"
var bodyMap map[string]any
err = json.Unmarshal([]byte(parsedRequest.Body), &bodyMap)
if err != nil {
return ParsedRequest{}, err
}
parsedRequest.Fields = make(map[string]any)
for k, v := range bodyMap {
parsedRequest.Fields[k] = fmt.Sprintf("%v", v)
}
} else if strings.Contains(contentType, "application/x-www-form-urlencoded") {
parsedRequest.BodyType = "form"
formValues, err := url.ParseQuery(parsedRequest.Body)
if err != nil {
return ParsedRequest{}, err
}
parsedRequest.Fields = make(map[string]any)
for k, v := range formValues {
parsedRequest.Fields[k] = v[0]
}
} else {
parsedRequest.BodyType = "none"
}
return *parsedRequest, nil
}
// normalizeProto detects HTTP/2 in the request line and rewrites it
// to HTTP/1.1 so http.ReadRequest can parse the raw text.
// The original proto is stored via the pointer for later use.
func normalizeProto(raw string, proto *string) string {
// Find end of first line (request line).
idx := strings.IndexAny(raw, "\r\n")
if idx == -1 {
return raw
}
requestLine := raw[:idx]
// Match HTTP/2 variants: "HTTP/2", "HTTP/2.0"
if strings.HasSuffix(requestLine, " HTTP/2") ||
strings.HasSuffix(requestLine, " HTTP/2.0") {
*proto = "HTTP/2"
// Replace the version token on the request line only.
normalized := strings.TrimSuffix(requestLine, " HTTP/2")
normalized = strings.TrimSuffix(normalized, " HTTP/2.0")
return normalized + " HTTP/1.1" + raw[idx:]
}
return raw
}
// convertPseudoHeaders handles HTTP/2 pseudo-header style requests
// that Burp Suite exports. These have no traditional request line;
// instead they use pseudo-headers like :method, :path, :authority, :scheme.
//
// Example Burp HTTP/2 export:
//
// :method: POST
// :path: /forgot-password
// :authority: example.com
// :scheme: https
// Content-Type: application/x-www-form-urlencoded
//
// username=carlos
//
// This function detects that format and synthesizes a standard HTTP/1.1
// request line + Host header so http.ReadRequest can parse it.
func convertPseudoHeaders(raw string, proto *string) string {
lines := strings.Split(raw, "\n")
if len(lines) == 0 {
return raw
}
// Quick check: if the first non-empty line starts with ":" it's a
// pseudo-header format.
firstLine := strings.TrimSpace(lines[0])
if !strings.HasPrefix(firstLine, ":") {
return raw
}
*proto = "HTTP/2"
var (
method = "GET"
path = "/"
authority = ""
scheme = "https"
remaining []string
)
for _, line := range lines {
trimmed := strings.TrimRight(line, "\r")
if strings.HasPrefix(trimmed, ":method:") {
method = strings.TrimSpace(strings.TrimPrefix(trimmed, ":method:"))
} else if strings.HasPrefix(trimmed, ":path:") {
path = strings.TrimSpace(strings.TrimPrefix(trimmed, ":path:"))
} else if strings.HasPrefix(trimmed, ":authority:") {
authority = strings.TrimSpace(strings.TrimPrefix(trimmed, ":authority:"))
} else if strings.HasPrefix(trimmed, ":scheme:") {
scheme = strings.TrimSpace(strings.TrimPrefix(trimmed, ":scheme:"))
_ = scheme // stored but we already default to https
} else {
remaining = append(remaining, line)
}
}
// Build a standard request with synthesized request line.
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s %s HTTP/1.1\n", method, path))
if authority != "" {
sb.WriteString(fmt.Sprintf("Host: %s\n", authority))
}
for _, line := range remaining {
sb.WriteString(line)
sb.WriteString("\n")
}
return sb.String()
}