Skip to content

Commit 5c1828c

Browse files
committed
http: use the request context
CloseNotifier has been deprecated for a while. Also, ditch the "environment" context. We don't actually _need_ this.
1 parent 2e550a1 commit 5c1828c

File tree

4 files changed

+20
-41
lines changed

4 files changed

+20
-41
lines changed

executor.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ type Executor interface {
88
Execute(req *Request, re ResponseEmitter, env Environment) error
99
}
1010

11-
// Environment is the environment passed to commands. The only required method
12-
// is Context.
13-
type Environment interface {
14-
// Context returns the environment's context.
15-
Context() context.Context
16-
}
11+
// Environment is the environment passed to commands.
12+
type Environment interface{}
1713

1814
// MakeEnvironment takes a context and the request to construct the environment
1915
// that is passed to the command's Run function.

http/handler.go

+1-30
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package http
22

33
import (
44
"context"
5-
"crypto/rand"
6-
"encoding/base32"
75
"errors"
86
"net/http"
97
"runtime/debug"
@@ -97,12 +95,6 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9795
}
9896
}()
9997

100-
ctx := h.env.Context()
101-
if ctx == nil {
102-
log.Error("no root context found, using background")
103-
ctx = context.Background()
104-
}
105-
10698
if !allowOrigin(r, h.cfg) || !allowReferer(r, h.cfg) {
10799
w.WriteHeader(http.StatusForbidden)
108100
w.Write([]byte("403 - Forbidden"))
@@ -128,7 +120,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
128120
r.Body = bw
129121
}
130122

131-
req, err := parseRequest(ctx, r, h.root)
123+
req, err := parseRequest(r, h.root)
132124
if err != nil {
133125
if err == ErrNotFound {
134126
w.WriteHeader(http.StatusNotFound)
@@ -152,18 +144,6 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
152144
}
153145
defer cancel()
154146

155-
req.Context = logging.ContextWithLoggable(req.Context, uuidLoggable())
156-
if cn, ok := w.(http.CloseNotifier); ok {
157-
clientGone := cn.CloseNotify()
158-
go func() {
159-
select {
160-
case <-clientGone:
161-
case <-req.Context.Done():
162-
}
163-
cancel()
164-
}()
165-
}
166-
167147
re, err := NewResponseEmitter(w, r.Method, req, withRequestBodyEOFChan(bodyEOFChan))
168148
if err != nil {
169149
w.WriteHeader(http.StatusBadRequest)
@@ -186,15 +166,6 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
186166
h.root.Call(req, re, h.env)
187167
}
188168

189-
func uuidLoggable() logging.Loggable {
190-
ids := make([]byte, 16)
191-
rand.Read(ids)
192-
193-
return logging.Metadata{
194-
"requestId": base32.HexEncoding.EncodeToString(ids),
195-
}
196-
}
197-
198169
func sanitizedErrStr(err error) string {
199170
s := err.Error()
200171
s = strings.Split(s, "\n")[0]

http/parse.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package http
22

33
import (
4-
"context"
4+
"crypto/rand"
5+
"encoding/base32"
56
"fmt"
67
"io/ioutil"
78
"mime"
@@ -13,10 +14,11 @@ import (
1314

1415
"github.com/ipfs/go-ipfs-cmdkit"
1516
"github.com/ipfs/go-ipfs-files"
17+
logging "github.com/ipfs/go-log"
1618
)
1719

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

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

231234
return res, nil
232235
}
236+
237+
func uuidLoggable() logging.Loggable {
238+
ids := make([]byte, 16)
239+
rand.Read(ids)
240+
241+
return logging.Metadata{
242+
"requestId": base32.HexEncoding.EncodeToString(ids),
243+
}
244+
}

http/parse_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestParse(t *testing.T) {
3434
if err != nil {
3535
t.Fatal(err)
3636
}
37-
req, err := parseRequest(nil, r, root)
37+
req, err := parseRequest(r, root)
3838
if err != nil {
3939
t.Fatal(err)
4040
}
@@ -48,7 +48,7 @@ func TestParse(t *testing.T) {
4848
if err != nil {
4949
t.Fatal(err)
5050
}
51-
req, err = parseRequest(nil, r, root)
51+
req, err = parseRequest(r, root)
5252
if err != ErrNotFound {
5353
t.Errorf("expected ErrNotFound, got: %v", err)
5454
}
@@ -79,7 +79,7 @@ func (tc parseReqTestCase) test(t *testing.T) {
7979
}
8080
httpReq.URL.RawQuery = vs.Encode()
8181

82-
req, err := parseRequest(nil, httpReq, cmdRoot)
82+
req, err := parseRequest(httpReq, cmdRoot)
8383
if !errEq(err, tc.err) {
8484
t.Fatalf("expected error to be %v, but got %v", tc.err, err)
8585
}

0 commit comments

Comments
 (0)