itchio / httpkit

🚐 Various HTTP utilities (internal)
MIT License
8 stars 2 forks source link

Add ability to set the request headers #1

Open fire opened 6 years ago

fire commented 6 years ago

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

https://github.com/itchio/httpkit/blob/50f60be27c88c7d8a3cdc01ea2029d465a830ceb/htfs/conn.go#L115

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

The actual use-case is setting a cookie.

fire commented 6 years ago
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 commented 6 years ago

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

ModifyRequest func (req *http.Request)

in htfs.Settings:

https://github.com/itchio/httpkit/blob/a507e904f82f9fc90c40eec62bb64eb453cfbc18/htfs/file.go#L114-L122

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 commented 6 years ago

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