(opts WebFnOpts, fn WebFnType)
| 381 | } |
| 382 | |
| 383 | func WebFnWrap(opts WebFnOpts, fn WebFnType) WebFnType { |
| 384 | return func(w http.ResponseWriter, r *http.Request) { |
| 385 | defer func() { |
| 386 | recErr := panichandler.PanicHandler("WebFnWrap", recover()) |
| 387 | if recErr == nil { |
| 388 | return |
| 389 | } |
| 390 | if opts.JsonErrors { |
| 391 | jsonRtn := marshalReturnValue(nil, recErr) |
| 392 | w.Header().Set(ContentTypeHeaderKey, ContentTypeJson) |
| 393 | w.Header().Set(ContentLengthHeaderKey, fmt.Sprintf("%d", len(jsonRtn))) |
| 394 | w.WriteHeader(http.StatusOK) |
| 395 | w.Write(jsonRtn) |
| 396 | } else { |
| 397 | http.Error(w, recErr.Error(), http.StatusInternalServerError) |
| 398 | } |
| 399 | }() |
| 400 | if !opts.AllowCaching { |
| 401 | w.Header().Set(CacheControlHeaderKey, CacheControlHeaderNoCache) |
| 402 | } |
| 403 | w.Header().Set("Access-Control-Expose-Headers", "X-ZoneFileInfo") |
| 404 | |
| 405 | // Handle CORS preflight OPTIONS requests without auth validation |
| 406 | if r.Method == http.MethodOptions { |
| 407 | w.WriteHeader(http.StatusOK) |
| 408 | return |
| 409 | } |
| 410 | |
| 411 | err := authkey.ValidateIncomingRequest(r) |
| 412 | if err != nil { |
| 413 | w.WriteHeader(http.StatusUnauthorized) |
| 414 | w.Write([]byte(fmt.Sprintf("error validating authkey: %v", err))) |
| 415 | return |
| 416 | } |
| 417 | fn(w, r) |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | func MakeTCPListener(serviceName string) (net.Listener, error) { |
| 422 | serverAddr := "127.0.0.1:" |
no test coverage detected