-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.go
57 lines (48 loc) · 1.46 KB
/
demo.go
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
package demo
import (
"fmt"
"io"
"log"
"regexp"
"strings"
)
const fixedBoundary = "3a494cd3b73de6555202"
const commonBoundary = "1879bcd06ac39a4d8fa5"
var contentTypeBoundary = regexp.MustCompile("boundary=.+")
var requestBodyBoundary = regexp.MustCompile("--[0-9a-z]+")
func PrintRequestWithLength(contentLength int64, contentType string, reqBody io.ReadCloser) {
PrintContentLength(contentLength)
PrintRequest(contentType, reqBody)
}
func PrintRequest(contentType string, reqBody io.ReadCloser) {
PrintContentType(contentType)
fmt.Println()
PrintRequestBody(reqBody)
}
func PrintContentType(contentType string) {
if contentType[len(contentType)-20:] != fixedBoundary {
contentType = contentTypeBoundary.ReplaceAllLiteralString(contentType, "boundary="+commonBoundary)
}
fmt.Printf("Content-Type: %s\n", contentType)
}
func PrintContentLength(contentLength int64) {
fmt.Printf("Content-Length: %d\n", contentLength)
}
func PrintRequestBody(reqBody io.ReadCloser) {
defer reqBody.Close()
reqBuf := stringifyReader(reqBody)
if err := reqBody.Close(); err != nil {
log.Fatal(err)
}
if reqBuf[len(reqBuf)-24:len(reqBuf)-4] != fixedBoundary {
reqBuf = requestBodyBoundary.ReplaceAllLiteralString(reqBuf, "--"+commonBoundary)
}
fmt.Println(reqBuf)
}
func stringifyReader(reqBody io.Reader) string {
builder := new(strings.Builder)
if _, err := io.Copy(builder, reqBody); err != nil {
log.Fatal(err)
}
return strings.ReplaceAll(builder.String(), "\r\n", "\n")
}