forked from lox/apt-proxy
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
109 additions
and
100 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,65 @@ | ||
package proxy | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
"time" | ||
|
||
"github.com/lox/apt-proxy/ubuntu" | ||
) | ||
|
||
var ubuntuRewriter = ubuntu.NewRewriter() | ||
|
||
var defaultTransport http.RoundTripper = &http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
ResponseHeaderTimeout: time.Second * 45, | ||
DisableKeepAlives: true, | ||
} | ||
|
||
type AptProxy struct { | ||
Transport http.RoundTripper | ||
Rewriters []Rewriter | ||
CachePatterns CachePatternSlice | ||
Handler http.Handler | ||
Rules []Rule | ||
} | ||
|
||
func NewAptProxy() *AptProxy { | ||
func NewAptProxyFromDefaults() *AptProxy { | ||
return &AptProxy{ | ||
Transport: http.DefaultTransport, | ||
Rewriters: []Rewriter{ubuntu.NewRewriter()}, | ||
CachePatterns: CachePatternSlice{}, | ||
Rules: DefaultRules, | ||
Handler: &httputil.ReverseProxy{ | ||
Director: func(r *http.Request) {}, | ||
Transport: defaultTransport, | ||
}, | ||
} | ||
} | ||
|
||
func (ap *AptProxy) Handler() http.Handler { | ||
return &handler{ap: ap, Handler: &httputil.ReverseProxy{ | ||
Director: func(r *http.Request) { | ||
for _, rewrite := range ap.Rewriters { | ||
rewrite.Rewrite(r) | ||
} | ||
func (ap *AptProxy) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | ||
rule, match := matchingRule(r.URL.Path, ap.Rules) | ||
if match { | ||
r.Header.Del("Cache-Control") | ||
if rule.Rewrite { | ||
ap.rewriteRequest(r) | ||
} | ||
} | ||
|
||
r.Host = r.URL.Host | ||
}, | ||
Transport: &cachePatternTransport{ap.CachePatterns, ap.Transport}, | ||
}} | ||
ap.Handler.ServeHTTP(&responseWriter{rw, rule}, r) | ||
} | ||
|
||
type handler struct { | ||
http.Handler | ||
ap *AptProxy | ||
func (ap *AptProxy) rewriteRequest(r *http.Request) { | ||
before := r.URL.String() | ||
ubuntuRewriter.Rewrite(r) | ||
log.Printf("rewrote %q to %q", before, r.URL.String()) | ||
r.Host = r.URL.Host | ||
} | ||
|
||
type responseWriter struct { | ||
http.ResponseWriter | ||
rule *Rule | ||
} | ||
|
||
func (rw *responseWriter) WriteHeader(status int) { | ||
if rw.rule != nil && rw.rule.CacheControl != "" && | ||
(status == http.StatusOK || status == http.StatusNotFound) { | ||
rw.Header().Set("Cache-Control", rw.rule.CacheControl) | ||
} | ||
rw.ResponseWriter.WriteHeader(status) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package proxy | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
var DefaultRules = []Rule{ | ||
{Pattern: regexp.MustCompile(`deb$`), CacheControl: `max-age=100000`, Rewrite: true}, | ||
{Pattern: regexp.MustCompile(`udeb$`), CacheControl: `max-age=100000`, Rewrite: true}, | ||
{Pattern: regexp.MustCompile(`DiffIndex$`), CacheControl: `max-age=3600`}, | ||
{Pattern: regexp.MustCompile(`PackagesIndex$`), CacheControl: `max-age=3600`}, | ||
{Pattern: regexp.MustCompile(`Packages\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`}, | ||
{Pattern: regexp.MustCompile(`SourcesIndex$`), CacheControl: `max-age=3600`}, | ||
{Pattern: regexp.MustCompile(`Sources\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`}, | ||
{Pattern: regexp.MustCompile(`Release(\.gpg)?$`), CacheControl: `max-age=3600`}, | ||
{Pattern: regexp.MustCompile(`Translation-(en|fr)\.(gz|bz2|bzip2|lzma)$`), CacheControl: `max-age=3600`}, | ||
} | ||
|
||
type Rule struct { | ||
Pattern *regexp.Regexp | ||
CacheControl string | ||
Rewrite bool | ||
} | ||
|
||
func (r *Rule) String() string { | ||
return fmt.Sprintf("%s Cache-Control=%s Rewrite=%#v", | ||
r.Pattern.String(), r.CacheControl, r.Rewrite) | ||
} | ||
|
||
func matchingRule(subject string, rules []Rule) (*Rule, bool) { | ||
for _, rule := range rules { | ||
if rule.Pattern.MatchString(subject) { | ||
return &rule, true | ||
} | ||
} | ||
|
||
return nil, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters