Exemplo n.º 1
0
// print http response
func (h *HttpTrafficHandler) printResponse(resp *httpport.Response) {
	defer tcpreader.DiscardBytesToEOF(resp.Body)
	if h.config.level == "url" {
		return
	}

	h.writeLine(resp.StatusLine)
	for _, header := range resp.RawHeaders {
		h.writeLine(header)
	}

	var hasBody = true
	if resp.ContentLength == 0 || resp.StatusCode == 304 || resp.StatusCode == 204 {
		hasBody = false
	}

	if h.config.level == "header" {
		if hasBody {
			h.writeLine("\n{body size:", tcpreader.DiscardBytesToEOF(resp.Body),
				", set [level = all] to display body content}")
		}
		return
	}

	h.writeLine()
	h.printBody(hasBody, resp.Header, resp.Body)
}
Exemplo n.º 2
0
// print http request
func (h *HttpTrafficHandler) printRequest(req *httpport.Request) {
	defer tcpreader.DiscardBytesToEOF(req.Body)
	//TODO: expect-100 continue handle
	if h.config.level == "url" {
		h.writeLine(req.Method, req.Host+req.RequestURI)
		return
	}

	h.writeLine()
	h.writeLine(strings.Repeat("*", 10), h.key.srcString(), " -----> ", h.key.dstString(), strings.Repeat("*", 10))
	h.writeLine(req.RequestLine)
	for _, header := range req.RawHeaders {
		h.writeLine(header)
	}

	var hasBody = true
	if req.ContentLength == 0 || req.Method == "GET" || req.Method == "HEAD" || req.Method == "TRACE" ||
		req.Method == "OPTIONS" {
		hasBody = false
	}

	if h.config.level == "header" {
		if hasBody {
			h.writeLine("\n{body size:", tcpreader.DiscardBytesToEOF(req.Body),
				", set [level = all] to display http body}")
		}
		return
	}

	h.writeLine()
	h.printBody(hasBody, req.Header, req.Body)
}
Exemplo n.º 3
0
func (stream HttpRequestStream) start() {
	defer errorHandler()

	//
	logHRS.Debug("开始获取请求数据")

	//
	buf := bufio.NewReader(&stream.reader)
	for {
		logHRS.Debug("1")
		req, err := http.ReadRequest(buf)
		logHRS.Debug("2")
		if err == io.EOF {
			logHRS.Debug("请求数据全部获取完, %v", logHRSCount)
			stream.end()
			break
		} else if err != nil {
			logHRS.Debug("something error %v", err)
		} else {
			logHRS.Debug("请求数据获取完成")
			stream.request = req
			bodyBytes := tcpreader.DiscardBytesToEOF(req.Body)
			req.Body.Close()
			log.Println("Received request from stream:", req, "with", bodyBytes, "bytes in request body")
		}
	}
}
Exemplo n.º 4
0
func (s *rtmpStream) parseStream(wg *sync.WaitGroup, r io.Reader) {
	defer func() {
		// Clean up all the chunkstream processors.
		for _, v := range s.track {
			if v != nil && v.writer != nil {
				v.writer.CloseWithError(fmt.Errorf("stream exiting"))
			}
		}
		s.Wait()                       // chunk processors
		s.finalizer.exit()             // show any results we found
		tcpreader.DiscardBytesToEOF(r) // read rest of tcp stream
		wg.Done()                      // this tcp stream is done
	}()

	buf := bufio.NewReader(r)

	// C0
	if d, err := buf.ReadByte(); err == io.EOF {
		return
	} else if err != nil {
		glog.V(2).Infof("read: %v", err)
		return
	} else if d != 0x03 {
		glog.V(1).Info("not RTMP")
		return
	}

	glog.V(2).Info("MAYBE RTMP")
	glog.V(2).Info("...should read more")

	data1 := make([]byte, 0x600)
	// C1
	if _, err := io.ReadFull(buf, data1); err != nil {
		glog.V(2).Infof("read: %v", err)
		return
	}

	// C2
	if _, err := io.ReadFull(buf, data1); err != nil {
		glog.V(2).Infof("read: %v", err)
		return
	}

	glog.V(1).Info("RTMP?")

	for {
		if err := s.processChunk(buf); errIsEOF(err) {
			glog.V(1).Info(err)
			break
		} else if err != nil {
			glog.Error(err)
			break
		}
	}
}
Exemplo n.º 5
0
func (h *httpStream) run() {
	buf := bufio.NewReader(&h.r)
	for {
		req, err := http.ReadRequest(buf)
		if err == io.EOF {
			// We must read until we see an EOF... very important!
			return
		} else if err != nil {
			log.Println("Error reading stream", h.net, h.transport, ":", err)
		} else {
			bodyBytes := tcpreader.DiscardBytesToEOF(req.Body)
			req.Body.Close()
			log.Println("Received request from stream", h.net, h.transport, ":", req, "with", bodyBytes, "bytes in request body")
		}
	}
}
Exemplo n.º 6
0
func (h *HttpTrafficHandler) printNonTextTypeBody(reader io.Reader, contentType string, isBinary bool) error {
	if h.config.force && !isBinary {
		data, err := ioutil.ReadAll(reader)
		if err != nil {
			return err
		}
		// TODO: try to guess charset
		str := string(data)
		if err != nil {
			return err
		}
		h.writeLine(str)
		h.writeLine()
	} else {
		h.writeLine("{Non-text body, content-type:", contentType, ", len:", tcpreader.DiscardBytesToEOF(reader), "}")
	}
	return nil
}
Exemplo n.º 7
0
func (h *httpStream) run() {
	buf := bufio.NewReader(&h.r)
	for {
		req, err := http.ReadRequest(buf)
		if err == io.EOF {
			// We must read until we see an EOF... very important!
			return
		} else if err != nil {
			log.Println("Error reading stream", h.net, h.transport, ":", err)
		} else {
			bodyBytes := tcpreader.DiscardBytesToEOF(req.Body)
			// if bodyBytes > 0 {
			log.Printf("Received request from stream:\n")
			log.Printf("h.r=>%T:\n%#v\n", h.r, h.r)
			log.Printf("net: %T\t%v\tsrcIP=%v\tdstIP=%v\n", h.net, h.net, h.net.Src(), h.net.Dst())
			log.Printf("transport=%v\tsport=%v\tdport=%v\n", h.transport, h.transport.Src(), h.transport.Dst())
			log.Printf("req: %T\n%v\n", req, req)
			log.Printf("request bodyBytes=%#v\n", bodyBytes)
			// }
			req.Body.Close()
		}
	}
}
Exemplo n.º 8
0
// read http request/response stream, and do output
func (h *HttpTrafficHandler) handle(connection *TcpConnection) {
	defer waitGroup.Done()
	defer connection.upStream.Close()
	defer connection.downStream.Close()
	// filter by args setting

	requestReader := bufio.NewReader(connection.upStream)
	defer tcpreader.DiscardBytesToEOF(requestReader)
	responseReader := bufio.NewReader(connection.downStream)
	defer tcpreader.DiscardBytesToEOF(responseReader)

	for {
		h.buffer = new(bytes.Buffer)
		filtered := false
		req, err := httpport.ReadRequest(requestReader)

		if err == io.EOF {
			break
		}
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing HTTP requests:", err)
			break
		}
		if h.config.domain != "" && !strings.HasSuffix(req.Host, h.config.domain) {
			filtered = true
		}
		if h.config.urlPath != "" && !strings.Contains(req.RequestURI, h.config.urlPath) {
			filtered = true
		}

		if !filtered {
			h.printRequest(req)
			h.writeLine("")
		}

		// if is websocket request,  by header: Upgrade: websocket
		websocket := req.Header.Get("Upgrade") == "websocket"
		expectContinue := req.Header.Get("Expect") == "100-continue"

		resp, err := httpport.ReadResponse(responseReader, nil)
		if err == io.EOF {
			fmt.Fprintln(os.Stderr, "Error parsing HTTP requests: unexpected end, ", err)
			break
		}
		if err == io.ErrUnexpectedEOF {
			fmt.Fprintln(os.Stderr, "Error parsing HTTP requests: unexpected end, ", err)
			// here return directly too, to avoid error when long polling connection is used
			break
		}
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing HTTP response:", err, connection.clientId)
			break
		}
		if !filtered {
			h.printResponse(resp)
			h.printer.send(h.buffer.String())
		}

		if websocket {
			if resp.StatusCode == 101 && resp.Header.Get("Upgrade") == "websocket" {
				// change to handle websocket
				h.handleWebsocket(requestReader, responseReader)
				break
			}
		}

		if expectContinue {
			if resp.StatusCode == 100 {
				// read next response, the real response
				resp, err := httpport.ReadResponse(responseReader, nil)
				if err == io.EOF {
					fmt.Fprintln(os.Stderr, "Error parsing HTTP requests: unexpected end, ", err)
					break
				}
				if err == io.ErrUnexpectedEOF {
					fmt.Fprintln(os.Stderr, "Error parsing HTTP requests: unexpected end, ", err)
					// here return directly too, to avoid error when long polling connection is used
					break
				}
				if err != nil {
					fmt.Fprintln(os.Stderr, "Error parsing HTTP response:", err, connection.clientId)
					break
				}
				if !filtered {
					h.printResponse(resp)
					h.printer.send(h.buffer.String())
				}
			} else if resp.StatusCode == 417 {

			}
		}
	}

	h.printer.send(h.buffer.String())
}
Exemplo n.º 9
0
// print http request/response body
func (h *HttpTrafficHandler) printBody(hasBody bool, header httpport.Header, reader io.ReadCloser) {

	if !hasBody {
		return
	}

	// deal with content encoding such as gzip, deflate
	contentEncoding := header.Get("Content-Encoding")
	var nr io.ReadCloser
	var err error
	if contentEncoding == "" {
		// do nothing
		nr = reader
	} else if strings.Contains(contentEncoding, "gzip") {
		nr, err = gzip.NewReader(reader)
		if err != nil {
			h.writeLine("{Decompress gzip err:", err, ", len:", tcpreader.DiscardBytesToEOF(reader), "}")
			return
		}
		defer nr.Close()
	} else if strings.Contains(contentEncoding, "deflate") {
		nr, err = zlib.NewReader(reader)
		if err != nil {
			h.writeLine("{Decompress deflate err:", err, ", len:", tcpreader.DiscardBytesToEOF(reader), "}")
			return
		}
		defer nr.Close()
	} else {
		h.writeLine("{Unsupport Content-Encoding:", contentEncoding, ", len:", tcpreader.DiscardBytesToEOF(reader), "}")
		return
	}

	// check mime type and charset
	contentType := header.Get("Content-Type")
	mimeTypeStr, charset := parseContentType(contentType)
	var mimeType = parseMimeType(mimeTypeStr)
	isText := mimeType.isTextContent()
	isBinary := mimeType.isBinaryContent()

	if !isText {
		err = h.printNonTextTypeBody(nr, contentType, isBinary)
		if err != nil {
			h.writeLine("{Read content error", err, "}")
		}
		return
	}

	var body string
	if charset == "" {
		// response do not set charset, try to detect
		var data []byte
		data, err = ioutil.ReadAll(nr)
		if err == nil {
			// TODO: try to guess charset
			body = string(data)
		}
	} else {
		body, err = readToStringWithCharset(nr, charset)
	}
	if err != nil {
		h.writeLine("{Read body failed", err, "}")
		return
	}

	// prettify json
	if mimeType.subType == "json" || likeJSON(body) {
		var jsonValue interface{}
		json.Unmarshal([]byte(body), &jsonValue)
		prettyJSON, err := json.MarshalIndent(jsonValue, "", "    ")
		if err == nil {
			body = string(prettyJSON)
		}
	}
	h.writeLine(body)
	h.writeLine()
}