Skip to content

Commit

Permalink
http: use the request context
Browse files Browse the repository at this point in the history
CloseNotifier has been deprecated for a while.

Also, ditch the "environment" context. We don't actually _need_ this.
  • Loading branch information
Stebalien committed Apr 30, 2019
1 parent 2e550a1 commit 5c1828c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 41 deletions.
8 changes: 2 additions & 6 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ type Executor interface {
Execute(req *Request, re ResponseEmitter, env Environment) error
}

// Environment is the environment passed to commands. The only required method
// is Context.
type Environment interface {
// Context returns the environment's context.
Context() context.Context
}
// Environment is the environment passed to commands.
type Environment interface{}

// MakeEnvironment takes a context and the request to construct the environment
// that is passed to the command's Run function.
Expand Down
31 changes: 1 addition & 30 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package http

import (
"context"
"crypto/rand"
"encoding/base32"
"errors"
"net/http"
"runtime/debug"
Expand Down Expand Up @@ -97,12 +95,6 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}()

ctx := h.env.Context()
if ctx == nil {
log.Error("no root context found, using background")
ctx = context.Background()
}

if !allowOrigin(r, h.cfg) || !allowReferer(r, h.cfg) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("403 - Forbidden"))
Expand All @@ -128,7 +120,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Body = bw
}

req, err := parseRequest(ctx, r, h.root)
req, err := parseRequest(r, h.root)
if err != nil {
if err == ErrNotFound {
w.WriteHeader(http.StatusNotFound)
Expand All @@ -152,18 +144,6 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
defer cancel()

req.Context = logging.ContextWithLoggable(req.Context, uuidLoggable())
if cn, ok := w.(http.CloseNotifier); ok {
clientGone := cn.CloseNotify()
go func() {
select {
case <-clientGone:
case <-req.Context.Done():
}
cancel()
}()
}

re, err := NewResponseEmitter(w, r.Method, req, withRequestBodyEOFChan(bodyEOFChan))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -186,15 +166,6 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.root.Call(req, re, h.env)
}

func uuidLoggable() logging.Loggable {
ids := make([]byte, 16)
rand.Read(ids)

return logging.Metadata{
"requestId": base32.HexEncoding.EncodeToString(ids),
}
}

func sanitizedErrStr(err error) string {
s := err.Error()
s = strings.Split(s, "\n")[0]
Expand Down
16 changes: 14 additions & 2 deletions http/parse.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package http

import (
"context"
"crypto/rand"
"encoding/base32"
"fmt"
"io/ioutil"
"mime"
Expand All @@ -13,10 +14,11 @@ import (

"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-files"
logging "github.com/ipfs/go-log"
)

// parseRequest parses the data in a http.Request and returns a command Request object
func parseRequest(ctx context.Context, r *http.Request, root *cmds.Command) (*cmds.Request, error) {
func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
if r.URL.Path[0] == '/' {
r.URL.Path = r.URL.Path[1:]
}
Expand Down Expand Up @@ -135,6 +137,7 @@ func parseRequest(ctx context.Context, r *http.Request, root *cmds.Command) (*cm
return nil, fmt.Errorf("File argument '%s' is required", requiredFile)
}

ctx := logging.ContextWithLoggable(r.Context(), uuidLoggable())
req, err := cmds.NewRequest(ctx, pth, opts, args, f, root)
if err != nil {
return nil, err
Expand Down Expand Up @@ -230,3 +233,12 @@ func parseResponse(httpRes *http.Response, req *cmds.Request) (cmds.Response, er

return res, nil
}

func uuidLoggable() logging.Loggable {
ids := make([]byte, 16)
rand.Read(ids)

return logging.Metadata{
"requestId": base32.HexEncoding.EncodeToString(ids),
}
}
6 changes: 3 additions & 3 deletions http/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestParse(t *testing.T) {
if err != nil {
t.Fatal(err)
}
req, err := parseRequest(nil, r, root)
req, err := parseRequest(r, root)
if err != nil {
t.Fatal(err)
}
Expand All @@ -48,7 +48,7 @@ func TestParse(t *testing.T) {
if err != nil {
t.Fatal(err)
}
req, err = parseRequest(nil, r, root)
req, err = parseRequest(r, root)
if err != ErrNotFound {
t.Errorf("expected ErrNotFound, got: %v", err)
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func (tc parseReqTestCase) test(t *testing.T) {
}
httpReq.URL.RawQuery = vs.Encode()

req, err := parseRequest(nil, httpReq, cmdRoot)
req, err := parseRequest(httpReq, cmdRoot)
if !errEq(err, tc.err) {
t.Fatalf("expected error to be %v, but got %v", tc.err, err)
}
Expand Down

0 comments on commit 5c1828c

Please sign in to comment.