-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
112 lines (98 loc) · 2.93 KB
/
parser.go
File metadata and controls
112 lines (98 loc) · 2.93 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
package httpmatter
import (
"bufio"
"bytes"
"io"
"net/http"
"strconv"
)
func ParseRequest(content []byte) (*http.Request, error) {
normalizedContent := normalizeLineEndings(content)
req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(normalizedContent)))
if err != nil {
return nil, err
}
// http.ReadRequest parses a server-side request (RequestURI is typically set).
// For http.Client.Do, RequestURI must be empty.
req.RequestURI = ""
// Ensure Host is set for client-side usage.
if req.URL != nil && req.URL.Host != "" {
req.Host = req.URL.Host
}
// Make the request body rewindable so callers can inspect it (BodyBytes/BodyString)
// and still send it using http.Client.
if req.Body != nil {
b, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
_ = req.Body.Close()
req.Body = io.NopCloser(bytes.NewReader(b))
req.ContentLength = int64(len(b))
req.GetBody = func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(b)), nil
}
}
return req, nil
}
func ParseResponse(content []byte) (*http.Response, error) {
// Normalize line endings to CRLF for HTTP parsing
normalizedContent := normalizeLineEndings(content)
response, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(normalizedContent)), nil)
if err != nil {
return nil, err
}
return response, nil
}
// normalizeLineEndings normalizes line endings only in the header part (meta).
// The body (everything after the first blank line) is returned untouched.
func normalizeLineEndings(content []byte) []byte {
// find header/body separator (try CRLFCRLF first, then LF LF, then CR CR)
sepIdx := bytes.Index(content, []byte("\r\n\r\n"))
sepLen := 4
if sepIdx == -1 {
if i := bytes.Index(content, []byte("\n\n")); i != -1 {
sepIdx = i
sepLen = 2
} else if i := bytes.Index(content, []byte("\r\r")); i != -1 {
sepIdx = i
sepLen = 2
}
}
var headerPart []byte
var bodyPart []byte
if sepIdx == -1 {
// no explicit blank line -> everything is header (no body)
headerPart = content
bodyPart = nil
} else {
headerPart = content[:sepIdx]
bodyPart = content[sepIdx+sepLen:]
}
// normalize header line endings:
// 1) collapse CRLF -> LF, CR -> LF so we have a single separator
tmp := bytes.ReplaceAll(headerPart, []byte("\r\n"), []byte("\n"))
tmp = bytes.ReplaceAll(tmp, []byte("\r"), []byte("\n"))
// 2) rebuild header with CRLF for each line
lines := bytes.Split(tmp, []byte("\n"))
var out bytes.Buffer
for _, ln := range lines {
// if line start with content-length,
// skip it we will add it as last header
if bytes.HasPrefix(ln, []byte("Content-Length:")) {
continue
}
out.Write(ln)
out.WriteString("\r\n")
}
// append body unchanged (no normalization in body)
if len(bodyPart) == 0 {
out.WriteString("\r\n")
} else {
out.WriteString("Content-Length: ")
out.WriteString(strconv.Itoa(len(bodyPart)))
out.WriteString("\r\n\r\n")
out.Write(bodyPart)
}
return out.Bytes()
}