newRequest extracts image url and processing options from request URL and verifies them
(req *http.Request)
| 95 | |
| 96 | // newRequest extracts image url and processing options from request URL and verifies them |
| 97 | func (h *Handler) newRequest(req *http.Request) (*request, *server.Error) { |
| 98 | // let's extract signature and valid request path from a request |
| 99 | path, signature, err := handlers.SplitPathSignature(req) |
| 100 | if err != nil { |
| 101 | return nil, server.NewError(errctx.Wrap(err), handlers.ErrCategoryPathParsing) |
| 102 | } |
| 103 | |
| 104 | // verify the signature (if any) |
| 105 | if err = h.Security().VerifySignature(req.Context(), signature, path); err != nil { |
| 106 | return nil, server.NewError(errctx.Wrap(err), handlers.ErrCategorySecurity) |
| 107 | } |
| 108 | |
| 109 | // parse image url and processing options |
| 110 | features := h.ClientFeaturesDetector().Features(req.Header) |
| 111 | o, imageURL, err := h.OptionsParser().ParsePath(req.Context(), path, &features) |
| 112 | if err != nil { |
| 113 | return nil, server.NewError(errctx.Wrap(err), handlers.ErrCategoryPathParsing) |
| 114 | } |
| 115 | |
| 116 | // get image origin and create monitoring meta object |
| 117 | imageOrigin := monitoring.MetaURLOrigin(imageURL) |
| 118 | |
| 119 | mm := monitoring.Meta{ |
| 120 | monitoring.MetaSourceImageURL: imageURL, |
| 121 | monitoring.MetaSourceImageOrigin: imageOrigin, |
| 122 | monitoring.MetaOptions: o.Map(), |
| 123 | } |
| 124 | |
| 125 | // set error reporting and monitoring context |
| 126 | errorreport.SetMetadata(req, "Source Image URL", imageURL) |
| 127 | errorreport.SetMetadata(req, "Source Image Origin", imageOrigin) |
| 128 | errorreport.SetMetadata(req, "Options", o.NestedMap()) |
| 129 | |
| 130 | h.Monitoring().SetMetadata(req.Context(), mm) |
| 131 | |
| 132 | // verify that image URL came from the valid source |
| 133 | err = h.Security().VerifySourceURL(imageURL) |
| 134 | if err != nil { |
| 135 | return nil, server.NewError(errctx.Wrap(err), handlers.ErrCategorySecurity) |
| 136 | } |
| 137 | |
| 138 | return &request{ |
| 139 | HandlerContext: h, |
| 140 | |
| 141 | imageURL: imageURL, |
| 142 | path: path, |
| 143 | opts: o, |
| 144 | features: &features, |
| 145 | monitoringMeta: mm, |
| 146 | req: req, |
| 147 | }, nil |
| 148 | } |
no test coverage detected