NewProxy constructs a new proxy. The provided http RoundTripper will be used to fetch remote URLs. If nil is provided, http.DefaultTransport will be used.
(transport http.RoundTripper, cache Cache)
| 107 | // used to fetch remote URLs. If nil is provided, http.DefaultTransport will |
| 108 | // be used. |
| 109 | func NewProxy(transport http.RoundTripper, cache Cache) *Proxy { |
| 110 | if transport == nil { |
| 111 | transport, _ = aia.NewTransport() |
| 112 | } |
| 113 | if cache == nil { |
| 114 | cache = NopCache |
| 115 | } |
| 116 | |
| 117 | proxy := &Proxy{ |
| 118 | Cache: cache, |
| 119 | } |
| 120 | |
| 121 | client := new(http.Client) |
| 122 | client.Transport = &httpcache.Transport{ |
| 123 | Transport: &TransformingTransport{ |
| 124 | Transport: transport, |
| 125 | CachingClient: client, |
| 126 | log: func(format string, v ...any) { |
| 127 | if proxy.Verbose { |
| 128 | proxy.logf(format, v...) |
| 129 | } |
| 130 | }, |
| 131 | updateCacheHeaders: proxy.updateCacheHeaders, |
| 132 | }, |
| 133 | Cache: cache, |
| 134 | MarkCachedResponses: true, |
| 135 | } |
| 136 | |
| 137 | proxy.Client = client |
| 138 | |
| 139 | return proxy |
| 140 | } |
| 141 | |
| 142 | // updateCacheHeaders updates the cache-control headers in the provided headers. |
| 143 | // |