// setHandler sets the specified function as the request handler. Whenever
// a mock broker reads a request from the wire it passes the request to the
// function and sends back whatever the handler function returns.
961817
func (b *MockBroker) setHandler(handler requestHandlerFunc) {
	b.lock.Lock()
	b.handler = handler
	b.lock.Unlock()
}
1218350
func RequestHandler(fieldKey string) func(next http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			log := zerolog.Ctx(r.Context())
			log.UpdateContext(func(c zerolog.Context) zerolog.Context {
				return c.Str(fieldKey, r.Method+" "+r.URL.String())
			})
			next.ServeHTTP(w, r)
		})
	}
}
415317
func RequestHandler(name string) func(next xhandler.HandlerC) xhandler.HandlerC {
	return func(next xhandler.HandlerC) xhandler.HandlerC {
		return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
			FromContext(ctx).SetField(name, r.Method+" "+r.URL.String())
			next.ServeHTTPC(ctx, w, r)
		})
	}
}
1219976
func NewBucketHandler(mappingService platform.UserResourceMappingService, labelService platform.LabelService, userService platform.UserService) *BucketHandler {
	h := &BucketHandler{
		Router:                     NewRouter(),
		Logger:                     zap.NewNop(),
		UserResourceMappingService: mappingService,
		LabelService:               labelService,
		UserService:                userService,
	}

	h.HandlerFunc("POST", bucketsPath, h.handlePostBucket)
	h.HandlerFunc("GET", bucketsPath, h.handleGetBuckets)
	h.HandlerFunc("GET", bucketsIDPath, h.handleGetBucket)
	h.HandlerFunc("GET", bucketsIDLogPath, h.handleGetBucketLog)
	h.HandlerFunc("PATCH", bucketsIDPath, h.handlePatchBucket)
	h.HandlerFunc("DELETE", bucketsIDPath, h.handleDeleteBucket)

	h.HandlerFunc("POST", bucketsIDMembersPath, newPostMemberHandler(h.UserResourceMappingService, h.UserService, platform.BucketsResource, platform.Member))
	h.HandlerFunc("GET", bucketsIDMembersPath, newGetMembersHandler(h.UserResourceMappingService, h.UserService, platform.BucketsResource, platform.Member))
	h.HandlerFunc("DELETE", bucketsIDMembersIDPath, newDeleteMemberHandler(h.UserResourceMappingService, platform.Member))

	h.HandlerFunc("POST", bucketsIDOwnersPath, newPostMemberHandler(h.UserResourceMappingService, h.UserService, platform.BucketsResource, platform.Owner))
	h.HandlerFunc("GET", bucketsIDOwnersPath, newGetMembersHandler(h.UserResourceMappingService, h.UserService, platform.BucketsResource, platform.Owner))
	h.HandlerFunc("DELETE", bucketsIDOwnersIDPath, newDeleteMemberHandler(h.UserResourceMappingService, platform.Owner))

	h.HandlerFunc("GET", bucketsIDLabelsPath, newGetLabelsHandler(h.LabelService))
	h.HandlerFunc("POST", bucketsIDLabelsPath, newPostLabelHandler(h.LabelService))
	h.HandlerFunc("DELETE", bucketsIDLabelsNamePath, newDeleteLabelHandler(h.LabelService))
	h.HandlerFunc("PATCH", bucketsIDLabelsNamePath, newPatchLabelHandler(h.LabelService))

	return h
}
507790
func (p *THttpClient) SetHeader(key string, value string) {
	p.header.Add(key, value)
}
1206429
func (p *Flow) SetHeader(k, v string) *Flow {
	if p.err != nil {
		return p
	}
	p.message.SetHeader(k, v)
	return p
}
620676
func (s *CancelHandshakeOutput) SetHandshake(v *Handshake) *CancelHandshakeOutput {
	s.Handshake = v
	return s
}
322112
func (rc *RequestConfig) SetHeader(headers H) *RequestConfig {
	if len(headers) > 0 {
		rc.Headers = headers
	}

	return rc
}
1102844
func NewAPIHandler(b *APIBackend) *APIHandler {
	h := &APIHandler{}

	internalURM := b.UserResourceMappingService
	b.UserResourceMappingService = authorizer.NewURMService(b.OrgLookupService, b.UserResourceMappingService)

	documentBackend := NewDocumentBackend(b)
	h.DocumentHandler = NewDocumentHandler(documentBackend)

	sessionBackend := NewSessionBackend(b)
	h.SessionHandler = NewSessionHandler(sessionBackend)

	bucketBackend := NewBucketBackend(b)
	bucketBackend.BucketService = authorizer.NewBucketService(b.BucketService)
	h.BucketHandler = NewBucketHandler(bucketBackend)

	orgBackend := NewOrgBackend(b)
	orgBackend.OrganizationService = authorizer.NewOrgService(b.OrganizationService)
	h.OrgHandler = NewOrgHandler(orgBackend)

	userBackend := NewUserBackend(b)
	userBackend.UserService = authorizer.NewUserService(b.UserService)
	h.UserHandler = NewUserHandler(userBackend)

	dashboardBackend := NewDashboardBackend(b)
	dashboardBackend.DashboardService = authorizer.NewDashboardService(b.DashboardService)
	h.DashboardHandler = NewDashboardHandler(dashboardBackend)

	variableBackend := NewVariableBackend(b)
	variableBackend.VariableService = authorizer.NewVariableService(b.VariableService)
	h.VariableHandler = NewVariableHandler(variableBackend)

	authorizationBackend := NewAuthorizationBackend(b)
	authorizationBackend.AuthorizationService = authorizer.NewAuthorizationService(b.AuthorizationService)
	h.AuthorizationHandler = NewAuthorizationHandler(authorizationBackend)

	scraperBackend := NewScraperBackend(b)
	scraperBackend.ScraperStorageService = authorizer.NewScraperTargetStoreService(b.ScraperTargetStoreService,
		b.UserResourceMappingService,
		b.OrganizationService)
	h.ScraperHandler = NewScraperHandler(scraperBackend)

	sourceBackend := NewSourceBackend(b)
	sourceBackend.SourceService = authorizer.NewSourceService(b.SourceService)
	sourceBackend.BucketService = authorizer.NewBucketService(b.BucketService)
	h.SourceHandler = NewSourceHandler(sourceBackend)

	setupBackend := NewSetupBackend(b)
	h.SetupHandler = NewSetupHandler(setupBackend)

	taskBackend := NewTaskBackend(b)
	h.TaskHandler = NewTaskHandler(taskBackend)
	h.TaskHandler.UserResourceMappingService = internalURM

	telegrafBackend := NewTelegrafBackend(b)
	telegrafBackend.TelegrafService = authorizer.NewTelegrafConfigService(b.TelegrafService, b.UserResourceMappingService)
	h.TelegrafHandler = NewTelegrafHandler(telegrafBackend)

	writeBackend := NewWriteBackend(b)
	h.WriteHandler = NewWriteHandler(writeBackend)

	fluxBackend := NewFluxBackend(b)
	h.QueryHandler = NewFluxHandler(fluxBackend)

	h.ChronografHandler = NewChronografHandler(b.ChronografService)
	h.SwaggerHandler = newSwaggerLoader(b.Logger.With(zap.String("service", "swagger-loader")))
	h.LabelHandler = NewLabelHandler(authorizer.NewLabelService(b.LabelService))

	return h
}
770250
func (r *Response) SetHeader(key, value string) *Response {
	r.Header.Set(key, value)
	return r
}
680152
func (a *authenticator) logoutHandler(w http.ResponseWriter, req *http.Request) {
	decoder := json.NewDecoder(req.Body)
	var credentials credentials
	err := decoder.Decode(&credentials)
	if err != nil {
		errStr := fmt.Sprintf("500 internal server error: failed to decode json: %v", err)
		a.formatter.Text(w, http.StatusInternalServerError, errStr)
		return
	}

	a.userDb.SetLogoutTime(credentials.Username)
	a.log.Debugf("user %s was logged out", credentials.Username)
}
861240
func SetHandler(r *chi.Mux, d Definition) {
	r.Route("/"+d.URL, func(r chi.Router) {

		if d.ReaderPipe != nil {
			if ops, err := NewReadOperationsFromJSON(d.ReaderPipe); err != nil {
				d.Error(err)
			} else {
				SetReadHandler(r, ops)
			}
		}

		if d.WriterPipe != nil {
			if ops, err := NewWriteOperationsFromJSON(d.WriterPipe); err != nil {
				d.Error(err)
			} else {
				SetWriteHandler(r, ops)
			}
		}

		if d.Deleter != nil {
			if del, err := DeleterFromJSON(d.Deleter); err != nil {
				d.Error(err)
			} else {
				SetDeleteHandler(r, del)
			}
		}

	})

}
720043
func (a *EnableArgs) SetHandleAuthRequests(handleAuthRequests bool) *EnableArgs {
	a.HandleAuthRequests = &handleAuthRequests
	return a
}
146079
func (c *NetworkClient) SetHeader(header *http.Header) {
	c.Client.RequestHeader = header
}
426344
func (c *BitcoindClient) ntfnHandler() {
	defer c.wg.Done()

	for {
		select {
		case tx := <-c.zmqTxNtfns:
			if _, _, err := c.filterTx(tx, nil, true); err != nil {
				log.Errorf("Unable to filter transaction %v: %v",
					tx.TxHash(), err)
			}
		case newBlock := <-c.zmqBlockNtfns:
			// If the new block's previous hash matches the best
			// hash known to us, then the new block is the next
			// successor, so we'll update our best block to reflect
			// this and determine if this new block matches any of
			// our existing filters.
			c.bestBlockMtx.Lock()
			bestBlock := c.bestBlock
			c.bestBlockMtx.Unlock()
			if newBlock.Header.PrevBlock == bestBlock.Hash {
				newBlockHeight := bestBlock.Height + 1
				_, err := c.filterBlock(
					newBlock, newBlockHeight, true,
				)
				if err != nil {
					log.Errorf("Unable to filter block %v: %v",
						newBlock.BlockHash(), err)
					continue
				}

				// With the block succesfully filtered, we'll
				// make it our new best block.
				bestBlock.Hash = newBlock.BlockHash()
				bestBlock.Height = newBlockHeight
				bestBlock.Timestamp = newBlock.Header.Timestamp

				c.bestBlockMtx.Lock()
				c.bestBlock = bestBlock
				c.bestBlockMtx.Unlock()

				continue
			}

			// Otherwise, we've encountered a reorg.
			if err := c.reorg(bestBlock, newBlock); err != nil {
				log.Errorf("Unable to process chain reorg: %v",
					err)
			}
		case <-c.quit:
			return
		}
	}
}
89857
func UserAgentHandler(name string) func(next http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			if ua := r.Header.Get("User-Agent"); ua != "" {
				l := FromContext(r.Context())
				l.SetField(name, ua)
			}
			next.ServeHTTP(w, r)
		})
	}
}
592806
func NewHTTPHandler(c libnetwork.NetworkController) func(w http.ResponseWriter, req *http.Request) {
	h := &httpHandler{c: c}
	h.initRouter()
	return h.handleRequest
}