ServeResourceHTTP dispatches an unauthenticated browser-navigable resource request to a plugin route.
(w http.ResponseWriter, r *http.Request)
| 284 | |
| 285 | // ServeResourceHTTP dispatches an unauthenticated browser-navigable resource request to a plugin route. |
| 286 | func (h *Host) ServeResourceHTTP(w http.ResponseWriter, r *http.Request) bool { |
| 287 | if h == nil || w == nil || r == nil || r.URL == nil { |
| 288 | return false |
| 289 | } |
| 290 | if !strings.EqualFold(r.Method, http.MethodGet) { |
| 291 | return false |
| 292 | } |
| 293 | key := managementRouteKey(http.MethodGet, r.URL.Path) |
| 294 | h.mu.Lock() |
| 295 | record, okRoute := h.resourceRoutes[key] |
| 296 | h.mu.Unlock() |
| 297 | if !okRoute || record.route.Handler == nil || h.isPluginFused(record.pluginID) { |
| 298 | return false |
| 299 | } |
| 300 | |
| 301 | resp, errHandle := h.callResourceHandler(r.Context(), record, pluginapi.ManagementRequest{ |
| 302 | Method: http.MethodGet, |
| 303 | Path: r.URL.Path, |
| 304 | Headers: cloneHeader(r.Header), |
| 305 | Query: cloneValues(r.URL.Query()), |
| 306 | }) |
| 307 | if errHandle != nil { |
| 308 | log.Warnf("pluginhost: resource handler %s failed: %v", record.pluginID, errHandle) |
| 309 | http.Error(w, "plugin resource handler failed", http.StatusBadGateway) |
| 310 | return true |
| 311 | } |
| 312 | |
| 313 | for keyHeader, values := range resp.Headers { |
| 314 | for _, value := range values { |
| 315 | w.Header().Add(keyHeader, value) |
| 316 | } |
| 317 | } |
| 318 | statusCode := resp.StatusCode |
| 319 | if statusCode == 0 { |
| 320 | statusCode = http.StatusOK |
| 321 | } |
| 322 | w.WriteHeader(statusCode) |
| 323 | if _, errWrite := w.Write(resp.Body); errWrite != nil { |
| 324 | log.Warnf("pluginhost: failed to write plugin resource response: %v", errWrite) |
| 325 | } |
| 326 | return true |
| 327 | } |
| 328 | |
| 329 | func (h *Host) callManagementHandler(ctx context.Context, record managementRouteRecord, req pluginapi.ManagementRequest) (resp pluginapi.ManagementResponse, err error) { |
| 330 | if h == nil || record.route.Handler == nil || h.isPluginFused(record.pluginID) || !h.pluginIdentityCurrent(record.pluginID, record.path, record.version) { |