Пример #1
0
func (session *SessionConnection) processHttpChunkEvent(ev *event.HTTPChunkEvent) error {
	ev.SetHash(session.SessionID)
	if nil != session.RemoteConn {
		session.RemoteConn.Request(session, ev)
	}
	return nil
}
Пример #2
0
func (session *SessionConnection) process() error {
	close_session := func() {
		session.LocalRawConn.Close()
		if nil != session.RemoteConn {
			session.RemoteConn.Close()
		}
		session.State = STATE_SESSION_CLOSE
	}

	readRequest := func() (*http.Request, error) {
		for {
			needCheckRemote := false
			if nil != session.RemoteConn {
				forward, ok := session.RemoteConn.(*ForwardConnection)
				if ok {
					if forward.IsClosed() {
						return nil, fmt.Errorf("Remote conn closed.")
					}
					needCheckRemote = true
				}
			}
			if needCheckRemote {
				session.LocalRawConn.SetReadDeadline(time.Now().Add(1 * time.Second))
			}
			req, e := http.ReadRequest(session.LocalBufferConn)
			if nil != req {
				req.Header.Del("Proxy-Connection")
			}
			if needCheckRemote {
				var zero time.Time
				session.LocalRawConn.SetReadDeadline(zero)
			}
			if nil != e && util.IsTimeoutError(e) {
				continue
			}
			return req, e
		}
		return nil, nil
	}

	switch session.State {
	case STATE_RECV_HTTP:
		req, rerr := readRequest()
		if nil == rerr {
			var rev event.HTTPRequestEvent
			rev.FromRequest(req)
			rev.SetHash(session.SessionID)
			err := session.processHttpEvent(&rev)
			if err != nil {
				log.Printf("Session[%d]Failed to read http request:%v\n", session.SessionID, err)
				close_session()
				return io.EOF
			}
		}
		if nil != rerr {
			log.Printf("Session[%d]Browser close connection:%v\n", session.SessionID, rerr)
			close_session()
			return io.EOF
		}
	case STATE_RECV_HTTP_CHUNK:
		buf := make([]byte, 8192)
		n, err := session.LocalBufferConn.Read(buf)
		if nil == err {
			rev := new(event.HTTPChunkEvent)
			rev.Content = buf[0:n]
			err = session.processHttpChunkEvent(rev)
		}
		if nil != err {
			close_session()

			return io.EOF
		}
	case STATE_RECV_TCP:

	}
	return nil
}