MCPcopy
hub / github.com/safing/portmaster / getUpdateResource

Function getUpdateResource

service/core/api.go:197–302  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

195}
196
197func getUpdateResource(w http.ResponseWriter, r *http.Request) {
198 // Get identifier from URL.
199 var identifier string
200 if ar := api.GetAPIRequest(r); ar != nil {
201 identifier = ar.URLVars["artifact_name"]
202 }
203 if identifier == "" {
204 http.Error(w, "no resource specified", http.StatusBadRequest)
205 return
206 }
207
208 // Get resource.
209 artifact, err := module.instance.BinaryUpdates().GetFile(identifier)
210 if err != nil {
211 intelArtifact, intelErr := module.instance.IntelUpdates().GetFile(identifier)
212 if intelErr == nil {
213 artifact = intelArtifact
214 } else {
215 http.Error(w, err.Error(), http.StatusNotFound)
216 return
217 }
218 }
219
220 // Open file for reading.
221 file, err := os.Open(artifact.Path())
222 if err != nil {
223 http.Error(w, err.Error(), http.StatusInternalServerError)
224 return
225 }
226 defer file.Close() //nolint:errcheck,gosec
227
228 // Assign file to reader
229 var reader io.Reader = file
230
231 // Add version and hash to header.
232 if artifact.Version != "" {
233 w.Header().Set("Resource-Version", artifact.Version)
234 }
235 if artifact.SHA256 != "" {
236 w.Header().Set("Resource-SHA256", artifact.SHA256)
237 }
238
239 // Set Content-Type.
240 contentType, _ := utils.MimeTypeByExtension(filepath.Ext(artifact.Path()))
241 w.Header().Set("Content-Type", contentType)
242
243 // Check if the content type may be returned.
244 accept := r.Header.Get("Accept")
245 if accept != "" {
246 mimeTypes := strings.Split(accept, ",")
247 // First, clean mime types.
248 for i, mimeType := range mimeTypes {
249 mimeType = strings.TrimSpace(mimeType)
250 mimeType, _, _ = strings.Cut(mimeType, ";")
251 mimeTypes[i] = mimeType
252 }
253 // Second, check if we may return anything.
254 var acceptsAny bool

Callers

nothing calls this directly

Calls 14

GetAPIRequestFunction · 0.92
MimeTypeByExtensionFunction · 0.92
ErrorfFunction · 0.92
GetFileMethod · 0.80
HeaderMethod · 0.80
WriteHeaderMethod · 0.80
ErrorMethod · 0.65
BinaryUpdatesMethod · 0.65
IntelUpdatesMethod · 0.65
PathMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected