-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (99 loc) · 2.13 KB
/
main.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
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 main
import (
"bufio"
"bytes"
"fmt"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
"image"
"image/color"
"image/png"
"log"
"net/http"
"sort"
)
func addLabel(img *image.RGBA, x, y int, label string) {
col := color.RGBA{R: 0, G: 0, B: 0, A: 255}
point := fixed.Point26_6{X: fixed.I(x), Y: fixed.I(y)}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(col),
Face: basicfont.Face7x13,
Dot: point,
}
d.DrawString(label)
}
type handler struct{}
func (h *handler) ServeHTTP(writer http.ResponseWriter, r *http.Request) {
lines := make([]string, 0)
lines = append(lines, fmt.Sprintf("%s %s", r.Method, r.URL.String()))
lines = append(lines, "")
// header
headerLines := make([]string, 0)
headerLines = append(headerLines, fmt.Sprintf("%s: %s", "Host", r.Host))
for k, v := range r.Header {
for _, item := range v {
headerLines = append(headerLines, fmt.Sprintf("%s: %s", k, item))
}
}
sort.Strings(headerLines)
for _, item := range headerLines {
lines = append(lines, item)
}
log.Print(lines)
lines = append(lines, "")
width := 0
for _, str := range lines {
if len(str) > width {
width = len(str)
}
}
if width < 100 {
width = 100
}
// body
lineWidth := width * 4 / 5
body := bufio.NewReader(r.Body)
for {
line, _, err := body.ReadLine()
if err != nil {
break
}
if len(line) > lineWidth {
lineReader := bytes.NewReader(append(line, 10))
for {
buffer := make([]byte, lineWidth)
read, err := lineReader.Read(buffer)
if err != nil {
break
}
if read > 0 {
lines = append(lines, string(buffer[:read]))
}
}
} else {
lines = append(lines, string(append(line, 10)))
}
}
// print
width = width*7 + 100
height := len(lines)*15 + 100
img := image.NewRGBA(image.Rect(0, 0, width, height))
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
img.Set(x, y, image.White)
}
}
for line, str := range lines {
addLabel(img, 50, 50+line*15, str)
}
err := png.Encode(writer, img)
if err != nil {
return
}
}
func main() {
handler := &handler{}
http.ListenAndServe(":8090", handler)
}