MCPcopy
hub / github.com/gofiber/fiber / handlerFunc

Function handlerFunc

middleware/adaptor/adaptor.go:243–352  ·  view source on GitHub ↗
(app *fiber.App, h ...fiber.Handler)

Source from the content-addressed store, hash-verified

241}
242
243func handlerFunc(app *fiber.App, h ...fiber.Handler) http.HandlerFunc {
244 return func(w http.ResponseWriter, r *http.Request) {
245 req := fasthttp.AcquireRequest()
246 defer fasthttp.ReleaseRequest(req)
247
248 // Convert net/http -> fasthttp request with size limit
249 maxBodySize := int64(app.Config().BodyLimit)
250 if r.Body != nil {
251 if r.ContentLength > maxBodySize {
252 http.Error(w, utils.StatusMessage(fiber.StatusRequestEntityTooLarge), fiber.StatusRequestEntityTooLarge)
253 return
254 }
255 limit := maxBodySize
256 if limit < math.MaxInt64 {
257 limit++
258 }
259 limitedReader := io.LimitReader(r.Body, limit)
260 n, err := io.Copy(req.BodyWriter(), limitedReader)
261 if err != nil {
262 http.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)
263 return
264 }
265
266 if n > maxBodySize {
267 http.Error(w, utils.StatusMessage(fiber.StatusRequestEntityTooLarge), fiber.StatusRequestEntityTooLarge)
268 return
269 }
270
271 req.Header.SetContentLength(int(n))
272 }
273 req.Header.SetMethod(r.Method)
274 req.SetRequestURI(r.RequestURI)
275 req.SetHost(r.Host)
276 req.Header.SetHost(r.Host)
277
278 for key, val := range r.Header {
279 for _, v := range val {
280 req.Header.Set(key, v)
281 }
282 }
283
284 remoteAddr, err := resolveRemoteAddr(r.RemoteAddr, r.Context().Value(http.LocalAddrContextKey))
285 if err != nil {
286 remoteAddr = nil // Fallback to nil
287 }
288
289 // New fasthttp Ctx from pool
290 fctx := ctxPool.Get().(*fasthttp.RequestCtx) //nolint:forcetypeassert,errcheck // not needed
291 fctx.Response.Reset()
292 fctx.Request.Reset()
293 defer ctxPool.Put(fctx)
294 fctx.Init(req, remoteAddr, &disableLogger{})
295
296 if len(h) > 0 {
297 // New fiber Ctx
298 ctx := app.AcquireCtx(fctx)
299 defer app.ReleaseCtx(ctx)
300

Callers 3

FiberHandlerFuncFunction · 0.85
FiberAppFunction · 0.85

Calls 15

resolveRemoteAddrFunction · 0.85
SetMethodMethod · 0.80
AcquireCtxMethod · 0.80
ReleaseCtxMethod · 0.80
ErrorHandlerMethod · 0.80
HandlerMethod · 0.80
BodyStreamMethod · 0.80
ConfigMethod · 0.65
ErrorMethod · 0.65
SetMethod · 0.65
ValueMethod · 0.65
ContextMethod · 0.65

Tested by 1