Skip to content

Commit

Permalink
jhttp: clean up leftover code
Browse files Browse the repository at this point in the history
After #82 removed support for parameter injection, support for plumbing the
HTTP request context into the client became pointless. This change removes the
vestiges of it, which can no longer serve any useful purpose.
  • Loading branch information
creachadair committed May 13, 2022
1 parent 58d4fee commit 195610d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 23 deletions.
21 changes: 1 addition & 20 deletions jhttp/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package jhttp

import (
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -34,10 +33,6 @@ import (
// If the request completes, whether or not there is an error, the HTTP
// response is 200 (OK) for ordinary requests or 204 (No Response) for
// notifications, and the response body contains the JSON-RPC response.
//
// The bridge attaches the inbound HTTP request to the context passed to the
// client, allowing an EncodeContext callback to retrieve state from the HTTP
// headers. Use jhttp.HTTPRequest to retrieve the request from the context.
type Bridge struct {
local server.Local
parseReq func(*http.Request) ([]*jrpc2.ParsedRequest, error)
Expand Down Expand Up @@ -123,9 +118,7 @@ func (b Bridge) serveInternal(w http.ResponseWriter, req *http.Request) error {
}

if len(spec) != 0 {
// Attach the HTTP request to the client context, so the encoder can see it.
ctx := context.WithValue(req.Context(), httpReqKey{}, req)
rsps, err := b.local.Client.Batch(ctx, spec)
rsps, err := b.local.Client.Batch(req.Context(), spec)
if err != nil {
return err
}
Expand Down Expand Up @@ -268,18 +261,6 @@ func (o *BridgeOptions) parseGETRequest() func(*http.Request) (string, interface
return o.ParseGETRequest
}

type httpReqKey struct{}

// HTTPRequest returns the HTTP request associated with ctx, or nil. The
// context passed to the JSON-RPC client by the Bridge will contain this value.
func HTTPRequest(ctx context.Context) *http.Request {
req, ok := ctx.Value(httpReqKey{}).(*http.Request)
if ok {
return req
}
return nil
}

// marshalError encodes an error response for an invalid request.
func marshalError(req *jrpc2.ParsedRequest) ([]byte, error) {
v, err := json.Marshal(req.Error)
Expand Down
4 changes: 1 addition & 3 deletions jhttp/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package jhttp

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
Expand Down Expand Up @@ -74,9 +73,8 @@ func (g Getter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

ctx := context.WithValue(req.Context(), httpReqKey{}, req)
var result json.RawMessage
if err := g.local.Client.CallResult(ctx, method, params, &result); err != nil {
if err := g.local.Client.CallResult(req.Context(), method, params, &result); err != nil {
var status int
switch code.FromError(err) {
case code.MethodNotFound:
Expand Down

0 comments on commit 195610d

Please sign in to comment.