-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
125 lines (112 loc) · 2.5 KB
/
http.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
113
114
115
116
117
118
119
120
121
122
123
124
125
package wireray
import (
"bufio"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"sync"
"time"
)
type HTTPReader struct {
ident string
isClient bool
bytes chan []byte
data []byte
parent *TCPStream
time time.Time
}
func (h *HTTPReader) Read(p []byte) (int, error) {
ok := true
for ok && len(h.data) == 0 {
h.data, ok = <-h.bytes
}
if !ok || len(h.data) == 0 {
return 0, io.EOF
}
l := copy(p, h.data)
h.data = h.data[l:]
return l, nil
}
func (h *HTTPReader) run(wg *sync.WaitGroup) {
defer wg.Done()
b := bufio.NewReader(h)
for {
if h.isClient {
h.time = time.Now()
req, err := http.ReadRequest(b)
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
} else if err != nil {
log.Println(fmt.Sprintf("HTTP-request", "HTTP/%s Request error: %s (%v,%+v)\n", h.ident, err, err, err))
continue
}
body, err := ioutil.ReadAll(req.Body)
s := len(body)
if err != nil {
log.Println(fmt.Sprintf("HTTP-request-body", "Got body err: %s\n", err))
continue
}
defer req.Body.Close()
h.parent.method = req.Method
h.parent.url = req.URL.EscapedPath()
hl := HTTPLog{
id: h.parent.id,
time: h.time,
bodyBytes: s,
method: h.parent.method,
url: h.parent.url,
header: req.Header,
isRequest: true,
}
if h.parent.dumpBody {
hl.body = string(body)
}
h.parent.logch <- hl
} else {
res, err := http.ReadResponse(b, nil)
h.time = time.Now()
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
} else if err != nil {
log.Println(fmt.Sprintf("HTTP-response", "HTTP/%s Response error: %s (%v,%+v)\n", h.ident, err, err, err))
continue
}
var reader io.ReadCloser
switch res.Header.Get("Content-Encoding") {
case "gzip":
if h.parent.gunzip {
reader, err = gzip.NewReader(res.Body)
defer reader.Close()
} else {
reader = res.Body
}
default:
reader = res.Body
}
body, err := ioutil.ReadAll(reader)
s := len(body)
if err != nil {
log.Println(fmt.Sprintf("HTTP-response-body", "HTTP/%s: failed to get body(parsed len:%d): %s\n", h.ident, s, err))
continue
}
defer res.Body.Close()
hl := HTTPLog{
id: h.parent.id,
time: h.time,
bodyBytes: s,
method: h.parent.method,
url: h.parent.url,
status: res.StatusCode,
header: res.Header,
isRequest: false,
}
if h.parent.dumpBody {
hl.body = string(body)
}
h.parent.logch <- hl
}
}
}