http.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package prober
  4. import (
  5. "bytes"
  6. "context"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. )
  11. const maxHTTPBody = 4 << 20 // MiB
  12. // HTTP returns a Probe that healthchecks an HTTP URL.
  13. //
  14. // The ProbeFunc sends a GET request for url, expects an HTTP 200
  15. // response, and verifies that want is present in the response
  16. // body.
  17. func HTTP(url, wantText string) ProbeFunc {
  18. return func(ctx context.Context) error {
  19. return probeHTTP(ctx, url, []byte(wantText))
  20. }
  21. }
  22. func probeHTTP(ctx context.Context, url string, want []byte) error {
  23. req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
  24. if err != nil {
  25. return fmt.Errorf("constructing request: %w", err)
  26. }
  27. // Get a completely new transport each time, so we don't reuse a
  28. // past connection.
  29. tr := http.DefaultTransport.(*http.Transport).Clone()
  30. defer tr.CloseIdleConnections()
  31. c := &http.Client{
  32. Transport: tr,
  33. }
  34. resp, err := c.Do(req)
  35. if err != nil {
  36. return fmt.Errorf("fetching %q: %w", url, err)
  37. }
  38. defer resp.Body.Close()
  39. if resp.StatusCode != 200 {
  40. return fmt.Errorf("fetching %q: status code %d, want 200", url, resp.StatusCode)
  41. }
  42. bs, err := io.ReadAll(io.LimitReader(resp.Body, maxHTTPBody))
  43. if err != nil {
  44. return fmt.Errorf("reading body of %q: %w", url, err)
  45. }
  46. if !bytes.Contains(bs, want) {
  47. // Log response body, but truncate it if it's too large; the limit
  48. // has been chosen arbitrarily.
  49. if maxlen := 300; len(bs) > maxlen {
  50. bs = bs[:maxlen]
  51. }
  52. return fmt.Errorf("body of %q does not contain %q (got: %q)", url, want, string(bs))
  53. }
  54. return nil
  55. }