| 143 | } |
| 144 | |
| 145 | func (h *httpReader) run(wg *sync.WaitGroup) { |
| 146 | defer wg.Done() |
| 147 | b := bufio.NewReader(h) |
| 148 | for true { |
| 149 | if h.isClient { |
| 150 | req, err := http.ReadRequest(b) |
| 151 | if err == io.EOF || err == io.ErrUnexpectedEOF { |
| 152 | break |
| 153 | } else if err != nil { |
| 154 | Error("HTTP-request", "HTTP/%s Request error: %s (%v,%+v)\n", h.ident, err, err, err) |
| 155 | continue |
| 156 | } |
| 157 | body, err := ioutil.ReadAll(req.Body) |
| 158 | s := len(body) |
| 159 | if err != nil { |
| 160 | Error("HTTP-request-body", "Got body err: %s\n", err) |
| 161 | } else if h.hexdump { |
| 162 | Info("Body(%d/0x%x)\n%s\n", len(body), len(body), hex.Dump(body)) |
| 163 | } |
| 164 | req.Body.Close() |
| 165 | Info("HTTP/%s Request: %s %s (body:%d)\n", h.ident, req.Method, req.URL, s) |
| 166 | h.parent.Lock() |
| 167 | h.parent.urls = append(h.parent.urls, req.URL.String()) |
| 168 | h.parent.Unlock() |
| 169 | } else { |
| 170 | res, err := http.ReadResponse(b, nil) |
| 171 | var req string |
| 172 | h.parent.Lock() |
| 173 | if len(h.parent.urls) == 0 { |
| 174 | req = fmt.Sprintf("<no-request-seen>") |
| 175 | } else { |
| 176 | req, h.parent.urls = h.parent.urls[0], h.parent.urls[1:] |
| 177 | } |
| 178 | h.parent.Unlock() |
| 179 | if err == io.EOF || err == io.ErrUnexpectedEOF { |
| 180 | break |
| 181 | } else if err != nil { |
| 182 | Error("HTTP-response", "HTTP/%s Response error: %s (%v,%+v)\n", h.ident, err, err, err) |
| 183 | continue |
| 184 | } |
| 185 | body, err := ioutil.ReadAll(res.Body) |
| 186 | s := len(body) |
| 187 | if err != nil { |
| 188 | Error("HTTP-response-body", "HTTP/%s: failed to get body(parsed len:%d): %s\n", h.ident, s, err) |
| 189 | } |
| 190 | if h.hexdump { |
| 191 | Info("Body(%d/0x%x)\n%s\n", len(body), len(body), hex.Dump(body)) |
| 192 | } |
| 193 | res.Body.Close() |
| 194 | sym := "," |
| 195 | if res.ContentLength > 0 && res.ContentLength != int64(s) { |
| 196 | sym = "!=" |
| 197 | } |
| 198 | contentType, ok := res.Header["Content-Type"] |
| 199 | if !ok { |
| 200 | contentType = []string{http.DetectContentType(body)} |
| 201 | } |
| 202 | encoding := res.Header["Content-Encoding"] |