Skip to content

Commit ad94722

Browse files
authored
Merge pull request #163 from ipfs/fix/http-context
http: use the request context
2 parents 72ee15c + 8d7fadf commit ad94722

File tree

8 files changed

+20
-61
lines changed

8 files changed

+20
-61
lines changed

cli/run_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ type env struct {
2828
ch chan struct{}
2929
}
3030

31-
func (e env) Context() context.Context {
32-
return context.Background()
33-
}
34-
3531
func TestRunWaits(t *testing.T) {
3632
flag := make(chan struct{}, 1)
3733

examples/adder/remote/server/main.go

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
nethttp "net/http"
65

76
"github.com/ipfs/go-ipfs-cmds/examples/adder"
@@ -11,10 +10,6 @@ import (
1110

1211
type env struct{}
1312

14-
func (env) Context() context.Context {
15-
return context.TODO()
16-
}
17-
1813
func main() {
1914
h := http.NewHandler(env{}, adder.RootCmd, http.NewServerConfig())
2015

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.

executor_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ type wc struct {
3838

3939
type env int
4040

41-
func (e *env) Context() context.Context {
42-
return context.Background()
43-
}
44-
4541
func TestExecutor(t *testing.T) {
4642
env := env(42)
4743
req, err := NewRequest(context.Background(), []string{"test"}, nil, nil, nil, root)

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/handler_test.go

-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package http
22

33
import (
44
"bytes"
5-
"context"
65
"errors"
76
"fmt"
87
"io"
@@ -26,15 +25,10 @@ type VersionOutput struct {
2625

2726
type testEnv struct {
2827
version, commit, repoVersion string
29-
rootCtx context.Context
3028
t *testing.T
3129
wait chan struct{}
3230
}
3331

34-
func (env testEnv) Context() context.Context {
35-
return env.rootCtx
36-
}
37-
3832
func getCommit(env cmds.Environment) (string, bool) {
3933
tEnv, ok := env.(testEnv)
4034
return tEnv.commit, ok
@@ -307,7 +301,6 @@ func getTestServer(t *testing.T, origins []string) (cmds.Environment, *httptest.
307301
version: "0.1.2",
308302
commit: "c0mm17", // yes, I know there's no 'm' in hex.
309303
repoVersion: "4",
310-
rootCtx: context.Background(),
311304
t: t,
312305
wait: make(chan struct{}),
313306
}

http/parse.go

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

33
import (
4-
"context"
4+
"encoding/base32"
55
"fmt"
66
"io/ioutil"
7+
"math/rand"
78
"mime"
89
"net/http"
910
"strconv"
@@ -12,10 +13,11 @@ import (
1213
"github.com/ipfs/go-ipfs-cmds"
1314

1415
"github.com/ipfs/go-ipfs-files"
16+
logging "github.com/ipfs/go-log"
1517
)
1618

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

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

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

http/parse_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestParse(t *testing.T) {
3333
if err != nil {
3434
t.Fatal(err)
3535
}
36-
req, err := parseRequest(nil, r, root)
36+
req, err := parseRequest(r, root)
3737
if err != nil {
3838
t.Fatal(err)
3939
}
@@ -47,7 +47,7 @@ func TestParse(t *testing.T) {
4747
if err != nil {
4848
t.Fatal(err)
4949
}
50-
req, err = parseRequest(nil, r, root)
50+
req, err = parseRequest(r, root)
5151
if err != ErrNotFound {
5252
t.Errorf("expected ErrNotFound, got: %v", err)
5353
}
@@ -78,7 +78,7 @@ func (tc parseReqTestCase) test(t *testing.T) {
7878
}
7979
httpReq.URL.RawQuery = vs.Encode()
8080

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

0 commit comments

Comments
 (0)