示例#1
0
文件: c4.go 项目: juju2013/gsnova
func (c4 *C4RemoteSession) writeHttpRequest(preq *http.Request) error {
	ev := new(event.HTTPRequestEvent)
	ev.FromRequest(preq)
	ev.SetHash(c4.sess.SessionID)
	if strings.Contains(ev.Url, "http://") {
		ev.Url = ev.Url[7+len(preq.Host):]
	}
	//log.Printf("Session[%d]Range Request %s\n", c4.sess.SessionID, ev.Url)
	c4.offerRequestEvent(ev)
	return nil
}
示例#2
0
文件: gae.go 项目: juju2013/gsnova
func (gae *GAEHttpConnection) doRangeFetch(req *http.Request, firstChunkRes *http.Response) {
	task := new(rangeFetchTask)
	task.FetchLimit = int(gae_cfg.FetchLimitSize)
	task.FetchWorkerNum = int(gae_cfg.ConcurrentRangeFetcher)
	task.SessionID = gae.sess.SessionID
	//	task.TaskValidation = func() bool {
	//		return !util.IsDeadConnection(gae.sess.LocalRawConn)
	//	}
	gae.rangeWorker = task
	fetch := func(preq *http.Request) (*http.Response, error) {
		ev := new(event.HTTPRequestEvent)
		ev.FromRequest(preq)
		ev.SetHash(gae.sess.SessionID)
		err, xres := gae.requestEvent(gaeHttpClient, gae.sess, ev)
		if nil != err {
			//try again
			err, xres = gae.requestEvent(gaeHttpClient, gae.sess, ev)
		}
		if nil == err {
			httpresev := xres.(*event.HTTPResponseEvent)
			httpres := httpresev.ToResponse()
			return httpres, err
		}
		return nil, err
	}
	pres, err := task.SyncGet(req, firstChunkRes, fetch)
	if nil == err {
		err = pres.Write(gae.sess.LocalRawConn)
		if nil != err {
			task.Close()
			gae.rangeWorker = nil
		}
		if nil != pres.Body {
			pres.Body.Close()
		}
	}
	if nil != err {
		log.Printf("Session[%d]Range task failed for reason:%v\n", gae.sess.SessionID, err)
	}
	if nil != err || !util.IsResponseKeepAlive(pres) || !util.IsRequestKeepAlive(req) {
		gae.sess.LocalRawConn.Close()
		gae.sess.State = STATE_SESSION_CLOSE
		gae.Close()
	}
}
示例#3
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
}