Skip to content

Commit

Permalink
implement arbitrary HTTP header support
Browse files Browse the repository at this point in the history
this commit adds the ability to specify arbitrary HTTP headers
for either the Gateway or the API. simply set the desired headers
on the config:

    ipfs config --json API.HTTPHeaders.X-MyHdr '["meow :)"]'
    ipfs config --json Gateway.HTTPHeaders.X-MyHdr '["meow :)"]'

License: MIT
Signed-off-by: Juan Batiz-Benet <juan@benet.ai>


This commit was moved from ipfs/kubo@4a571b0
  • Loading branch information
jbenet committed Jul 29, 2015
1 parent 2bb3cf2 commit c993cec
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions gateway/core/corehttp/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func addCORSFromEnv(c *cmdsHttp.ServerConfig) {
}
}

func addCORSFromConfig(c *cmdsHttp.ServerConfig, nc *config.Config) {
func addHeadersFromConfig(c *cmdsHttp.ServerConfig, nc *config.Config) {
log.Info("Using API.HTTPHeaders:", nc.API.HTTPHeaders)

if acao := nc.API.HTTPHeaders["Access-Control-Allow-Origin"]; acao != nil {
Expand All @@ -53,6 +53,8 @@ func addCORSFromConfig(c *cmdsHttp.ServerConfig, nc *config.Config) {
c.CORSOpts.AllowCredentials = (strings.ToLower(v) == "true")
}
}

c.Headers = nc.API.HTTPHeaders
}

func CommandsOption(cctx commands.Context) ServeOption {
Expand All @@ -64,7 +66,7 @@ func CommandsOption(cctx commands.Context) ServeOption {
},
}

addCORSFromConfig(cfg, n.Repo.Config())
addHeadersFromConfig(cfg, n.Repo.Config())
addCORSFromEnv(cfg)

cmdHandler := cmdsHttp.NewHandler(cctx, corecommands.Root, cfg)
Expand Down
4 changes: 4 additions & 0 deletions gateway/core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Gateway struct {
}

type GatewayConfig struct {
Headers map[string][]string
BlockList *BlockList
Writable bool
}
Expand All @@ -27,6 +28,9 @@ func NewGateway(conf GatewayConfig) *Gateway {

func (g *Gateway) ServeOption() ServeOption {
return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
// pass user's HTTP headers
g.Config.Headers = n.Repo.Config().Gateway.HTTPHeaders

gateway, err := newGatewayHandler(n, g.Config)
if err != nil {
return nil, err
Expand Down
11 changes: 11 additions & 0 deletions gateway/core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
return
}

i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("X-IPFS-Path", urlPath)

// Suborigin header, sandboxes apps from each other in the browser (even
Expand Down Expand Up @@ -229,6 +230,7 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
return
}

i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("IPFS-Hash", k.String())
http.Redirect(w, r, ipfsPathPrefix+k.String(), http.StatusCreated)
}
Expand All @@ -242,6 +244,7 @@ func (i *gatewayHandler) putEmptyDirHandler(w http.ResponseWriter, r *http.Reque
return
}

i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("IPFS-Hash", key.String())
http.Redirect(w, r, ipfsPathPrefix+key.String()+"/", http.StatusCreated)
}
Expand Down Expand Up @@ -340,6 +343,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
return
}

i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("IPFS-Hash", key.String())
http.Redirect(w, r, ipfsPathPrefix+key.String()+"/"+strings.Join(components, "/"), http.StatusCreated)
}
Expand Down Expand Up @@ -411,10 +415,17 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
return
}

i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("IPFS-Hash", key.String())
http.Redirect(w, r, ipfsPathPrefix+key.String()+"/"+strings.Join(components[:len(components)-1], "/"), http.StatusCreated)
}

func (i *gatewayHandler) addUserHeaders(w http.ResponseWriter) {
for k, v := range i.config.Headers {
w.Header()[k] = v
}
}

func webError(w http.ResponseWriter, message string, err error, defaultCode int) {
if _, ok := err.(path.ErrNoLink); ok {
webErrorWithCode(w, message, err, http.StatusNotFound)
Expand Down

0 comments on commit c993cec

Please sign in to comment.