MCPcopy Create free account
hub / github.com/Azure/peerd / Handle

Method Handle

pkg/handlers/files/handler.go:23–60  ·  view source on GitHub ↗

Handle handles a request for a file.

(c pcontext.Context)

Source from the content-addressed store, hash-verified

21
22// Handle handles a request for a file.
23func (h *FilesHandler) Handle(c pcontext.Context) {
24 log := pcontext.Logger(c).With().Str("blob", pcontext.BlobUrl(c)).Bool("p2p", pcontext.IsRequestFromAPeer(c)).Logger()
25 log.Debug().Msg("files handler start")
26 s := time.Now()
27 defer func() {
28 dur := time.Since(s)
29 h.metricsRecorder.RecordRequest(c.Request.Method, "files", float64(dur.Milliseconds()))
30 log.Debug().Dur("duration", dur).Msg("files handler stop")
31 }()
32
33 err := h.fill(c)
34 if err != nil {
35 log.Debug().Err(err).Msg("failed to fill context")
36 // nolint
37 c.AbortWithError(http.StatusBadRequest, err)
38 return
39 }
40
41 f, err := h.store.Open(c)
42 if err == os.ErrNotExist {
43 c.AbortWithStatus(http.StatusNotFound)
44 return
45 }
46 if err != nil {
47 // nolint
48 c.AbortWithError(http.StatusInternalServerError, err)
49 return
50 }
51
52 w := c.Writer
53
54 w.Header().Set("Content-Type", "application/octet-stream")
55 w.Header().Del("Content-Length")
56 w.Header().Set(pcontext.NodeHeaderKey, pcontext.NodeName)
57 w.Header().Set(pcontext.CorrelationHeaderKey, c.GetString(pcontext.CorrelationIdCtxKey))
58
59 http.ServeContent(w, c.Request, "file", time.Now(), f)
60}
61
62// fill fills the context with handler specific information.
63func (h *FilesHandler) fill(c pcontext.Context) error {

Callers 5

fileHandlerFunction · 0.80
TestNotFoundInP2PModeFunction · 0.80
serverCommandFunction · 0.80
ScannerFunction · 0.80

Calls 4

fillMethod · 0.95
SetMethod · 0.80
RecordRequestMethod · 0.65
OpenMethod · 0.65

Tested by 2

TestNotFoundInP2PModeFunction · 0.64