http_upstream_profile.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package service
  2. import "context"
  3. // HTTPUpstreamProfile marks HTTP upstream requests that need provider-specific
  4. // transport policy.
  5. type HTTPUpstreamProfile string
  6. const (
  7. HTTPUpstreamProfileDefault HTTPUpstreamProfile = ""
  8. HTTPUpstreamProfileOpenAI HTTPUpstreamProfile = "openai"
  9. HTTPUpstreamProfileGrok HTTPUpstreamProfile = "grok"
  10. HTTPUpstreamProfileLongStream HTTPUpstreamProfile = "long_stream"
  11. )
  12. type httpUpstreamProfileContextKey struct{}
  13. type httpUpstreamDisableRedirectsContextKey struct{}
  14. type httpUpstreamPublicHostsOnlyContextKey struct{}
  15. // WithHTTPUpstreamProfile injects an upstream transport profile into ctx.
  16. func WithHTTPUpstreamProfile(ctx context.Context, profile HTTPUpstreamProfile) context.Context {
  17. if ctx == nil {
  18. ctx = context.Background()
  19. }
  20. if profile == HTTPUpstreamProfileDefault {
  21. return ctx
  22. }
  23. return context.WithValue(ctx, httpUpstreamProfileContextKey{}, profile)
  24. }
  25. // HTTPUpstreamProfileFromContext resolves the upstream transport profile from ctx.
  26. func HTTPUpstreamProfileFromContext(ctx context.Context) HTTPUpstreamProfile {
  27. if ctx == nil {
  28. return HTTPUpstreamProfileDefault
  29. }
  30. profile, ok := ctx.Value(httpUpstreamProfileContextKey{}).(HTTPUpstreamProfile)
  31. if !ok {
  32. return HTTPUpstreamProfileDefault
  33. }
  34. switch profile {
  35. case HTTPUpstreamProfileOpenAI, HTTPUpstreamProfileGrok, HTTPUpstreamProfileLongStream:
  36. return profile
  37. default:
  38. return HTTPUpstreamProfileDefault
  39. }
  40. }
  41. // WithHTTPUpstreamRedirectsDisabled prevents credential-bearing probes from
  42. // following redirects through the shared upstream client.
  43. func WithHTTPUpstreamRedirectsDisabled(ctx context.Context) context.Context {
  44. if ctx == nil {
  45. ctx = context.Background()
  46. }
  47. return context.WithValue(ctx, httpUpstreamDisableRedirectsContextKey{}, true)
  48. }
  49. func HTTPUpstreamRedirectsDisabled(ctx context.Context) bool {
  50. return ctx != nil && ctx.Value(httpUpstreamDisableRedirectsContextKey{}) == true
  51. }
  52. // WithHTTPUpstreamPublicHostsOnly marks a request whose destination, and every
  53. // redirect hop after it, must resolve to a public address. The shared upstream
  54. // client enforces it regardless of the security.url_allowlist configuration;
  55. // use it for fetches whose URL comes from an untrusted upstream response.
  56. func WithHTTPUpstreamPublicHostsOnly(ctx context.Context) context.Context {
  57. if ctx == nil {
  58. ctx = context.Background()
  59. }
  60. return context.WithValue(ctx, httpUpstreamPublicHostsOnlyContextKey{}, true)
  61. }
  62. func HTTPUpstreamPublicHostsOnly(ctx context.Context) bool {
  63. return ctx != nil && ctx.Value(httpUpstreamPublicHostsOnlyContextKey{}) == true
  64. }