// Handler creates a handler function for this router, that can be used with the
// http-package to handle WebSocket-Connections.
972679
func (router *Router) Handler() func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		// Check if method used was GET.
		if r.Method != "GET" {
			http.Error(w, "Method not allowed", 405)
			return
		}

		// Disallow cross-origin connections.
		if len(router.Origins) > 0 {
			originFound := false
			for _, origin := range router.Origins {
				if r.Header.Get("Origin") == origin {
					originFound = true
					break
				}
			}
			if !originFound {
				http.Error(w, "Origin not allowed", 403)
				return
			}
		} else {
			if len(r.Header.Get("Access-Control-Allow-Origin")) > 0 {
				allowedOrigin := r.Header.Get("Access-Control-Allow-Origin")
				if allowedOrigin != "*" {
					if r.URL.Scheme+"://"+r.Host != allowedOrigin {
						http.Error(w, "Origin not allwed", 403)
						return
					}
				}
			}
		}

		// Check if handshake callback verifies upgrade.
		if !router.handshakeFunc(w, r) {
			http.Error(w, "Authorization failed", 403)
			return
		}

		// Upgrade websocket connection.
		protocols := websocket.Subprotocols(r)
		var responseHeader http.Header = nil
		if len(protocols) > 0 {
			responseHeader = http.Header{"Sec-Websocket-Protocol": {protocols[0]}}
		}
		socket, err := websocket.Upgrade(w, r, responseHeader, 1024, 1024)
		// Check if handshake was successful
		if _, ok := err.(websocket.HandshakeError); ok {
			http.Error(w, "Not a websocket handshake", 400)
			return
		} else if err != nil {
			log.Println(err)
			return
		}

		// Create the connection.
		conn := newConnection(socket, router)
		//
		if router.connExtensionConstructor.IsValid() {
			conn.extend(router.connExtensionConstructor.Call([]reflect.Value{reflect.ValueOf(conn)})[0].Interface())
		}

		// Connection established with possible extension, so callback
		router.connectionFunc(conn, r)

		// And start reading and writing routines.
		conn.run()
	}
}
978261
func (mux *TypeMux) Handler(r *Request) Handler {
	return mux.mux.Handler(r)
}
1214405
func NewHandler() *Handler {
	h := &Handler{
		router: httprouter.New(),
		Logger: log15.New(),
	}
	h.router.Handler("GET", status.Path, status.Handler(h.healthStatus))
	h.router.GET("/status", h.handleGetStatus)
	h.router.GET("/backup", h.handleGetBackup)
	h.router.POST("/stop", h.handlePostStop)
	return h
}
113579
func (mux *Mux) handler() *handler {
	return &handler{
		mux: mux,
		c:   make(chan net.Conn),
	}
}
1120545
func (conn *Conn) Handle(name string, h Handler) Remover {
	return conn.fgHandlers.add(name, h)
}
7271
func (om *ClientIDMiddleware) Handler(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var clientID string

		accessToken := om.GetAccessToken(r)

		token, err := oauth2.GetValidJWT(r, security.JWTPublicKey)
		if err != nil {
			log.Error("Failed to get valid JWT: ", err)
			http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
			return
		}
		if token != nil {
			clientID = token.Claims["azp"].(string)

		} else if accessToken != "" {
			oauthMgr := oauthservice.NewManager(r)
			at, err := oauthMgr.GetAccessToken(accessToken)
			if err != nil {
				log.Error("Failed to get access token: ", err)
				http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
				return
			}
			if at == nil {
				http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
				return
			}
			clientID = at.ClientID
		} else {
			if webuser, ok := context.GetOk(r, "webuser"); ok {
				if parsedusername, ok := webuser.(string); ok && parsedusername != "" {
					clientID = "itsyouonline"
				}
			}
		}
		if clientID == "" {
			http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
			return
		}

		context.Set(r, "client_id", clientID)

		next.ServeHTTP(w, r)
	})
}
75340
func Handler(h StatusHandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		status, response := h(w, r)
		w.WriteHeader(status)
		if response == nil {
			return
		}

		Responder(w, r, response)
	}
}
637674
func (r *Router) Handle(method, path string, handle Handle) {
	r.Router.Handle(method, path, r.middlewareSelect(handle))
}
695118
func (xff *XFF) Handler(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		r.RemoteAddr = GetRemoteAddrIfAllowed(r, xff.allowed)
		h.ServeHTTP(w, r)
	})
}
544728
func (r *Route) Handler(h Handler) *Route {
	r.route.Handler(r.handler(h))
	return r
}
540232
func (h *Hitch) Handle(method, path string, handler http.Handler, middleware ...func(http.Handler) http.Handler) {
	for i := len(middleware) - 1; i >= 0; i-- {
		handler = middleware[i](handler)
	}
	h.Router.Handle(method, path, wrap(handler))
}
1191413
func (h HandlerFunc) Handle(ctx *Context, res *http.Response, err error) {
	h(ctx, res, err)
}
699848
func (r *ResponseMatcher) Handler(h Handler) *ResponseMatcher {
	r.mux.mu.Lock()
	defer r.mux.mu.Unlock()
	r.h = h
	if !r.mux.res[r] {
		r.mux.res[r] = true
	}
	return r
}
244777
func (s *Server) handle(method, path string, h handlerFunc, authStrategy ...string) *route {
	r := s.route(h)
	s.mux.Handle(path, r).Methods(method)
	return r
}
1105936
func (d *SUIData3) Handler(notFound http.Handler, defaultDef string) http.Handler {
	return suihttp.Handler(d.as, notFound, d.index, d.def, defaultDef)
}
1041393
func (t *TcpListener) handler(conn *net.TCPConn, id string) {
	t.CurrentConnections.Incr(1)
	t.TotalConnections.Incr(1)
	// connection cleanup function
	defer func() {
		t.wg.Done()
		conn.Close()
		// Add one connection potential back to channel when this one closes
		t.accept <- true
		t.forget(id)
		t.CurrentConnections.Incr(-1)
	}()

	var n int
	scanner := bufio.NewScanner(conn)
	for {
		select {
		case <-t.done:
			return
		default:
			if !scanner.Scan() {
				return
			}
			n = len(scanner.Bytes())
			if n == 0 {
				continue
			}
			t.BytesRecv.Incr(int64(n))
			t.PacketsRecv.Incr(1)
			bufCopy := make([]byte, n+1)
			copy(bufCopy, scanner.Bytes())
			bufCopy[n] = '\n'

			select {
			case t.in <- bufCopy:
			default:
				t.drops++
				if t.drops == 1 || t.drops%t.AllowedPendingMessages == 0 {
					log.Printf(dropwarn, t.drops)
				}
			}
		}
	}
}
893113
func (s *Server) Handle(method string, handlerFunc interface{}) {
	addHandler(s.handlers, method, handlerFunc)
}