MCPcopy Create free account
hub / github.com/OpenNHP/opennhp / corsMiddleware

Function corsMiddleware

endpoints/server/httpserver.go:298–322  ·  view source on GitHub ↗

corsMiddleware is a middleware function that adds CORS headers to the HTTP response. It allows cross-origin resource sharing, specifies allowed methods, exposes headers, and sets maximum age. If the request method is OPTIONS, PUT, or DELETE, it aborts the request with a 204 status code.

()

Source from the content-addressed store, hash-verified

296// It allows cross-origin resource sharing, specifies allowed methods, exposes headers, and sets maximum age.
297// If the request method is OPTIONS, PUT, or DELETE, it aborts the request with a 204 status code.
298func corsMiddleware() gin.HandlerFunc {
299 return func(c *gin.Context) {
300 // HTTP headers for CORS
301 c.Writer.Header().Set("Access-Control-Allow-Origin", "*") // allow cross-origin resource sharing
302 c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST") // methods
303 c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Type, Content-Length, Set-Cookie")
304 c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, X-NHP-Ver, Cookie")
305 c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
306 c.Writer.Header().Set("Access-Control-Max-Age", "300")
307 // NHP headers
308 c.Writer.Header().Set("Access-Control-NHP-Ver", version.Version+"/"+version.CommitId)
309
310 if c.Request.Method == "OPTIONS" {
311 c.Status(http.StatusOK)
312 return
313 }
314
315 if c.Request.Method == "DELETE" || c.Request.Method == "PUT" {
316 c.AbortWithStatus(http.StatusNoContent)
317 return
318 }
319
320 c.Next()
321 }
322}
323
324func (hs *HttpServer) handleHttpOpenResource(req *common.HttpKnockRequest, res *common.ResourceData) (ack *common.ServerKnockAckMsg, err error) {
325 defer hs.wg.Done()

Callers 1

StartMethod · 0.70

Calls 2

SetMethod · 0.80
HeaderMethod · 0.80

Tested by

no test coverage detected