Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 0 additions & 9 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import (
"log"
"net/http"
"os"
"sync"
)

var (
cookies = map[string]map[string][]*http.Cookie{}
cookiesL = new(sync.RWMutex)

credentials = map[string]bool{}
credentialsL = new(sync.RWMutex)
)

func InitLogger(verbose bool) *log.Logger {
Expand Down
47 changes: 33 additions & 14 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/http"
"net/url"
"sync"
"time"
)

Expand All @@ -14,6 +15,14 @@ const (
RequestTimeout int = 2
)

var (
cookies = map[string]map[string][]*http.Cookie{}
cookiesL = new(sync.RWMutex)

privateUris = map[string]bool{}
privateUrisL = new(sync.RWMutex)
)

type Proxy struct {
HttpClient *http.Client
HttpAgentClient *http.Client
Expand Down Expand Up @@ -64,22 +73,25 @@ func (p *Proxy) Handler(w http.ResponseWriter, req *http.Request) {
user := req.Header.Get("User")

// check if we need to authenticate from the start
withCredentials := false
authenticated := false
if requiresAuth(req.URL.String()) {
withCredentials = true
authenticated = true
p.Log.Println("Request will use credentials for cached URI:", req.URL.String())
}

p.Log.Println("Proxying request for URI:", req.URL, "and user:", user, "using Agent:", p.Agent.WebID)

// build new response
var r *http.Response
r, err = p.NewRequest(req, user, withCredentials)
r, err = p.NewRequest(req, user, authenticated)
if err != nil {
p.ExecError(w, err)
return
}

// the resource might have turned public, no need to remember it anymore
if r.StatusCode >= 200 && r.StatusCode <= 400 {
forgetUri(req.URL.String())
}
// Retry with server credentials if authentication is required
if r.StatusCode == 401 {
// Close the response to reuse the connection
Expand All @@ -90,8 +102,8 @@ func (p *Proxy) Handler(w http.ResponseWriter, req *http.Request) {
p.Log.Println(req.URL.String(), "saved to auth list")
}
if len(user) > 0 && p.HttpAgentClient != nil {
withCredentials = true
r, err = p.NewRequest(req, user, withCredentials)
authenticated = true
r, err = p.NewRequest(req, user, authenticated)
if err != nil {
p.ExecError(w, err)
return
Expand Down Expand Up @@ -132,7 +144,7 @@ func CopyHeaders(from http.Header, to http.Header) {
}
}

func (p *Proxy) NewRequest(req *http.Request, user string, withCredentials bool) (*http.Response, error) {
func (p *Proxy) NewRequest(req *http.Request, user string, authenticated bool) (*http.Response, error) {
// prepare new request
request, err := http.NewRequest(req.Method, req.URL.String(), req.Body)
// copy headers
Expand All @@ -141,7 +153,7 @@ func (p *Proxy) NewRequest(req *http.Request, user string, withCredentials bool)
request.Header.Set("User-Agent", GetServerFullName())

// build new response
if !withCredentials || len(user) == 0 {
if !authenticated || len(user) == 0 {
return p.HttpClient.Do(request)
}

Expand Down Expand Up @@ -176,19 +188,26 @@ func (p *Proxy) NewRequest(req *http.Request, user string, withCredentials bool)
return r, err
}

//@TODO add a forgetUri() method that deletes the cache
func rememberUri(uri string) bool {
if !credentials[uri] {
credentialsL.Lock()
credentials[uri] = true
credentialsL.Unlock()
if !privateUris[uri] {
privateUrisL.Lock()
privateUris[uri] = true
privateUrisL.Unlock()
return true
}
return false
}

func forgetUri(uri string) bool {
if privateUris[uri] {
delete(privateUris, uri)
return true
}
return false
}

func requiresAuth(uri string) bool {
if len(credentials) > 0 && credentials[uri] {
if len(privateUris) > 0 && privateUris[uri] {
return true
}
return false
Expand Down
14 changes: 14 additions & 0 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

var (
testMockServer *httptest.Server
testUri = "https://example.org"
)

func init() {
Expand Down Expand Up @@ -284,3 +285,16 @@ func TestProxyNoAgent(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 401, resp.StatusCode)
}

func TestRememberUri(t *testing.T) {
assert.True(t, rememberUri(testUri))
}

func TestRequiresAuth(t *testing.T) {
assert.True(t, requiresAuth(testUri))
}

func TestForgetUri(t *testing.T) {
assert.True(t, forgetUri(testUri))
assert.False(t, requiresAuth(testUri))
}