Skip to content
This repository was archived by the owner on Apr 18, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions cli/httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path"
"strings"

"github.com/lox/httpcache"
"github.com/lox/httpcache/httplog"
)

const (
defaultOrigin = "http://127.0.0.1:80"
defaultListen = "0.0.0.0:8080"
defaultDir = "./cachedata"
)

var (
origin string
host string
listen string
useDisk bool
private bool
Expand All @@ -26,6 +32,8 @@ var (
)

func init() {
flag.StringVar(&origin, "origin", defaultOrigin, "the origin url to proxy to")
flag.StringVar(&host, "host", "", "the host header to send")
flag.StringVar(&listen, "listen", defaultListen, "the host and port to bind to")
flag.StringVar(&dir, "dir", defaultDir, "the dir to store cache data in, implies -disk")
flag.BoolVar(&useDisk, "disk", false, "whether to store cache data to disk")
Expand All @@ -40,10 +48,23 @@ func init() {
}

func main() {
u, err := url.Parse(origin)
if err != nil {
log.Fatal(err)
}

proxy := &httputil.ReverseProxy{
Director: func(r *http.Request) {
r.URL.Scheme = "http"
r.URL.Host = "127.0.0.1:80"
r.URL.Scheme = u.Scheme
r.URL.Host = u.Host
if strings.HasSuffix(r.URL.Path, "/") {
r.URL.Path = path.Join(u.Path, r.URL.Path) + "/"
} else {
r.URL.Path = path.Join(u.Path, r.URL.Path)
}
if host != "" {
r.Host = host
}
},
}

Expand Down