MCPcopy Index your code
hub / github.com/tailscale/tailscale / ServeHTTP

Method ServeHTTP

control/controlhttp/http_test.go:450–492  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

448}
449
450func (h *httpProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
451 if r.Method != "CONNECT" {
452 if !h.allowHTTP {
453 http.Error(w, "http proxy not allowed", 500)
454 return
455 }
456 h.rp.ServeHTTP(w, r)
457 return
458 }
459
460 if !h.allowConnect {
461 http.Error(w, "connect not allowed", 500)
462 return
463 }
464
465 dst := r.RequestURI
466 c, err := h.dialAndRecord(context.Background(), "tcp", dst)
467 if err != nil {
468 http.Error(w, err.Error(), 500)
469 return
470 }
471 defer c.Close()
472
473 cc, ccbuf, err := w.(http.Hijacker).Hijack()
474 if err != nil {
475 http.Error(w, err.Error(), 500)
476 return
477 }
478 defer cc.Close()
479
480 io.WriteString(cc, "HTTP/1.1 200 OK\r\n\r\n")
481
482 errc := make(chan error, 1)
483 go func() {
484 _, err := io.Copy(cc, c)
485 errc <- err
486 }()
487 go func() {
488 _, err := io.Copy(c, ccbuf)
489 errc <- err
490 }()
491 <-errc
492}
493
494func (h *httpProxy) dialAndRecord(ctx context.Context, network, addr string) (net.Conn, error) {
495 var d net.Dialer

Callers 1

connectProxyToFunction · 0.45

Calls 5

dialAndRecordMethod · 0.95
WriteStringMethod · 0.80
ErrorMethod · 0.65
CloseMethod · 0.65
HijackMethod · 0.45

Tested by

no test coverage detected