Example #1
0
func handleEvent(ev event.Event, ctx *ConnContext) (event.Event, error) {
	switch ev.(type) {
	case *event.AuthEvent:
		auth := ev.(*event.AuthEvent)
		err := authConnection(auth, ctx)
		var authres event.NotifyEvent
		authres.SetId(ev.GetId())
		if nil == err {
			authres.Code = event.SuccessAuthed
		} else {
			authres.Code = event.ErrAuthFailed
		}
		return &authres, nil
	case *event.HeartBeatEvent:
		//do nothing
	case *event.ChannelCloseReqEvent:
		ctx.Closing = true
		queue := getEventQueue(ctx.ConnId, false)
		if nil != queue {
			queue.Publish(nil, 1*time.Minute)
		}
	case *event.ConnTestEvent:
		session := getProxySessionByEvent(ctx, ev)
		if nil == session {
			log.Printf("Session:%d is NOT exist now.", ev.GetId())
			queue := getEventQueue(ctx.ConnId, false)
			if nil != queue {
				closeEv := &event.ConnCloseEvent{}
				closeEv.SetId(ev.GetId())
				queue.Publish(closeEv, 10*time.Millisecond)
			}
		}
	default:
		session := getProxySessionByEvent(ctx, ev)
		if nil != session {
			session.offer(ev)
		}
		if _, ok := ev.(*event.ConnCloseEvent); !ok {
			if nil == session {
				log.Printf("No session:%d found for event %T", ev.GetId(), ev)
			}
		} else {
			if nil != session {
				session.closeByClient = true
			}
		}
	}
	return nil, nil
}
Example #2
0
func fetch(context appengine.Context, ev *event.HTTPRequestEvent) event.Event {
	errorResponse := new(event.NotifyEvent)
	errorResponse.SetId(ev.GetId())
	req, err := ev.ToRequest("")

	if nil != err {
		errorResponse.Code = event.ErrInvalidHttpRequest
		errorResponse.Reason = fmt.Sprintf("Invalid fetch url:%s with err:%v", ev.URL, err)
		return errorResponse
	}
	var t urlfetch.Transport
	t.Context = context
	t.Deadline, _ = time.ParseDuration("10s")
	t.AllowInvalidServerCertificate = true
	retryCount := 2
	for retryCount > 0 {
		resp, err := t.RoundTrip(req)
		if err == nil {
			res := event.NewHTTPResponseEvent(resp)
			for nil != resp.Body {
				buffer := make([]byte, 8192)
				n, er := resp.Body.Read(buffer)
				if nil != er {
					context.Errorf("Failed to read body for reason:%v", er)
					break
				}
				res.Content = append(res.Content, buffer[0:n]...)
			}
			if resp.ContentLength != int64(len(res.Content)) {
				context.Errorf("Failed to read body %d %d", resp.ContentLength, len(res.Content))
			}
			context.Errorf("%v %d %d", resp.Header.Get("Content-Length"), resp.ContentLength, len(res.Content))
			return res
		}
		context.Errorf("Failed to fetch URL[%s] for reason:%v", ev.URL, err)
		retryCount--
		if strings.EqualFold(req.Method, "GET") && strings.Contains(err.Error(), "RESPONSE_TOO_LARGE") {
			errorResponse.Code = event.ErrTooLargeResponse
			return errorResponse
		}
	}
	errorResponse.Code = event.ErrRemoteProxyTimeout
	errorResponse.Reason = fmt.Sprintf("Fetch timeout for url:%s", ev.URL)
	return errorResponse

}