MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / validateAndWriteHeaders

Method validateAndWriteHeaders

rest/handler.go:309–704  ·  view source on GitHub ↗

validateAndWriteHeaders sets up handler.db and validates the permission of the user and returns an error if there is not permission.

(method handlerMethod, accessPermissions []Permission, responsePermissions []Permission)

Source from the content-addressed store, hash-verified

307
308// validateAndWriteHeaders sets up handler.db and validates the permission of the user and returns an error if there is not permission.
309func (h *handler) validateAndWriteHeaders(method handlerMethod, accessPermissions []Permission, responsePermissions []Permission) (err error) {
310 var isRequestLogged bool
311 defer func() {
312 if !isRequestLogged {
313 h.logRequestLine()
314 }
315 h.logRESTCount()
316
317 // Perform request logging unless it's a silent endpoint
318 if h.httpLogLevel == nil {
319 auditFields := base.AuditFields{
320 base.AuditFieldHTTPMethod: h.rq.Method,
321 base.AuditFieldHTTPPath: h.rq.URL.Path,
322 base.AuditFieldHTTPStatus: h.status,
323 }
324 switch h.serverType {
325 case publicServer:
326 base.Audit(h.ctx(), base.AuditIDPublicHTTPAPIRequest, auditFields)
327 case adminServer:
328 base.Audit(h.ctx(), base.AuditIDAdminHTTPAPIRequest, auditFields)
329 case metricsServer:
330 base.Audit(h.ctx(), base.AuditIDMetricsHTTPAPIRequest, auditFields)
331 case diagnosticServer:
332 base.Audit(h.ctx(), base.AuditIDDiagnosticsHTTPAPIRequest, auditFields)
333 }
334 }
335 }()
336
337 defer func() {
338 // Now that we know the DB, add CORS headers to the response:
339 if h.privs != adminPrivs && h.privs != metricsPrivs {
340 cors := h.getCORSConfig()
341 if !cors.IsEmpty() {
342 cors.AddResponseHeaders(h.rq, h.response)
343 }
344 }
345 }()
346
347 if h.server.Config.Unsupported.AuditInfoProvider != nil && h.server.Config.Unsupported.AuditInfoProvider.RequestInfoHeaderName != nil {
348 auditLoggingFields := h.rq.Header.Get(*h.server.Config.Unsupported.AuditInfoProvider.RequestInfoHeaderName)
349 if auditLoggingFields != "" {
350 var fields base.AuditFields
351 err := base.JSONUnmarshal([]byte(auditLoggingFields), &fields)
352 if err != nil {
353 base.WarnfCtx(h.ctx(), "Error unmarshalling audit logging fields: %v", err)
354 } else {
355 h.rqCtx = base.AuditLogCtx(h.ctx(), fields)
356 }
357 }
358 }
359
360 if h.server.Config.Unsupported.EffectiveUserHeaderName != nil {
361 effectiveUserFields := h.rq.Header.Get(*h.server.Config.Unsupported.EffectiveUserHeaderName)
362 if effectiveUserFields != "" {
363 var fields base.EffectiveUserPair
364 err := base.JSONUnmarshal([]byte(effectiveUserFields), &fields)
365 if err != nil {
366 base.WarnfCtx(h.ctx(), "Error unmarshalling effective user header fields: %v", err)

Callers 1

invokeMethod · 0.95

Calls 15

logRequestLineMethod · 0.95
logRESTCountMethod · 0.95
ctxMethod · 0.95
getCORSConfigMethod · 0.95
setHeaderMethod · 0.95
shouldCheckAdminRBACMethod · 0.95
PathVarMethod · 0.95
addDatabaseLogContextMethod · 0.95

Tested by

no test coverage detected