Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set the request headers #1

Open
fire opened this issue May 23, 2018 · 3 comments
Open

Add ability to set the request headers #1

fire opened this issue May 23, 2018 · 3 comments

Comments

@fire
Copy link

fire commented May 23, 2018

Hi, was wondering to add the ability to set request headers:

func (c *conn) tryConnect(offset int64) error {

For example the external site expects some headers and rejects all requests without it.

The actual use-case is setting a cookie.

@fire
Copy link
Author

fire commented May 23, 2018

func (c *conn) tryConnect(offset int64) error {
	hf := c.file

	req, err := http.NewRequest("GET", hf.currentURL, nil)
	if err != nil {
		return err
	}

        key := os.Getenv("EXAMPLE_API_KEY") //change
        req.Header.Set("Cookie", "example=" + key) //change

	byteRange := fmt.Sprintf("bytes=%d-", offset)
	req.Header.Set("Range", byteRange)

	res, err := hf.client.Do(req)
	if err != nil {
		return err
	}

This is what I got.

@fasterthanlime
Copy link
Contributor

The way I'd go about it would be to add a

ModifyRequest func (req *http.Request)

in htfs.Settings:

httpkit/htfs/file.go

Lines 114 to 122 in a507e90

// Settings allows passing additional settings to an File
type Settings struct {
Client *http.Client
RetrySettings *retrycontext.Settings
Log LogFunc
LogLevel int
ForbidBacktracking bool
DumpStats bool
}

Then File would be responsible to pass it down to conn, which would call it in tryConnect (if it's non-nil).

Do you want to give it a shot?

@fire
Copy link
Author

fire commented May 24, 2018

This seems better. Can push it down the chain more, but wanted review.

diff --git a/vendor/github.com/itchio/httpkit/htfs/conn.go b/vendor/github.com/itchio/httpkit/htfs/conn.go
index 9cfbed7..1ae66d1 100644
--- a/vendor/github.com/itchio/httpkit/htfs/conn.go
+++ b/vendor/github.com/itchio/httpkit/htfs/conn.go
@@ -24,6 +24,7 @@ type conn struct {
 	currentURL string
 
 	header        http.Header
+	modifyRequest func (req *http.Request)
 	requestURL    *url.URL
 	statusCode    int
 	contentLength int64
@@ -120,6 +121,10 @@ func (c *conn) tryConnect(offset int64) error {
 		return err
 	}
 
+	if hf.modifyRequest != nil {
+		hf.modifyRequest(req)
+	}	
+
 	byteRange := fmt.Sprintf("bytes=%d-", offset)
 	req.Header.Set("Range", byteRange)
 
diff --git a/vendor/github.com/itchio/httpkit/htfs/file.go b/vendor/github.com/itchio/httpkit/htfs/file.go
index caa7fb6..e105d0d 100644
--- a/vendor/github.com/itchio/httpkit/htfs/file.go
+++ b/vendor/github.com/itchio/httpkit/htfs/file.go
@@ -72,6 +72,7 @@ type File struct {
 	getURL        GetURLFunc
 	needsRenewal  NeedsRenewalFunc
 	client        *http.Client
+	modifyRequest func (req *http.Request)
 	retrySettings *retrycontext.Settings
 
 	Log      LogFunc
@@ -114,6 +115,7 @@ var _ io.Closer = (*File)(nil)
 // Settings allows passing additional settings to an File
 type Settings struct {
 	Client             *http.Client
+	ModifyRequest      func (req *http.Request)
 	RetrySettings      *retrycontext.Settings
 	Log                LogFunc
 	LogLevel           int
@@ -139,6 +141,7 @@ func Open(getURL GetURLFunc, needsRenewal NeedsRenewalFunc, settings *Settings)
 		retrySettings: &retryCtx.Settings,
 		needsRenewal:  needsRenewal,
 		client:        client,
+		modifyRequest: settings.ModifyRequest,
 		name:          "<remote file>",
 
 		conns: make(map[string]*conn),
diff --git a/vendor/github.com/itchio/wharf/eos/eos.go b/vendor/github.com/itchio/wharf/eos/eos.go
index 44f5a21..820c290 100644
--- a/vendor/github.com/itchio/wharf/eos/eos.go
+++ b/vendor/github.com/itchio/wharf/eos/eos.go
@@ -136,7 +136,15 @@ func realOpen(name string, opts ...option.Option) (File, error) {
 	switch u.Scheme {
 	case "http", "https":
 		res := &simpleHTTPResource{name}
-		hf, err := htfs.Open(res.GetURL, res.NeedsRenewal, htfsSettings())
+
+	   	key := os.Getenv("EXAMPLE_API_KEY")
+
+		settings := htfsSettings()
+		settings.ModifyRequest = func (req *http.Request) {
+        		req.Header.Set("Cookie", "example=" + key)
+    		}
+
+		hf, err := htfs.Open(res.GetURL, res.NeedsRenewal, settings)
 
 		if err != nil {
 			return nil, err

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants