MCPcopy
hub / github.com/coder/websocket / ServeHTTP

Method ServeHTTP

dial_test.go:346–387  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

344}
345
346func (fc *forwardProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
347 ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
348 defer cancel()
349
350 r = r.WithContext(ctx)
351 r.RequestURI = ""
352 resp, err := fc.hc.Do(r)
353 if err != nil {
354 http.Error(w, err.Error(), http.StatusBadRequest)
355 return
356 }
357 defer resp.Body.Close()
358
359 maps.Copy(w.Header(), resp.Header)
360 w.Header().Set("PROXIED", "true")
361 w.WriteHeader(resp.StatusCode)
362
363 if resprw, ok := resp.Body.(io.ReadWriter); ok {
364 c, brw, err := w.(http.Hijacker).Hijack()
365 if err != nil {
366 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
367 return
368 }
369 brw.Flush()
370
371 errc1 := xsync.Go(func() error {
372 _, err := io.Copy(c, resprw)
373 return err
374 })
375 errc2 := xsync.Go(func() error {
376 _, err := io.Copy(resprw, c)
377 return err
378 })
379 select {
380 case <-errc1:
381 case <-errc2:
382 case <-r.Context().Done():
383 }
384 } else {
385 io.Copy(w, resp.Body)
386 }
387}
388
389func TestDialViaProxy(t *testing.T) {
390 t.Parallel()

Callers

nothing calls this directly

Calls 4

GoFunction · 0.92
ErrorMethod · 0.45
CloseMethod · 0.45
HijackMethod · 0.45

Tested by

no test coverage detected