-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace authn.Request for *http.Request #9
Changes from 1 commit
b3a3e19
7013291
478629f
a8cdbbd
95670f4
c30f2e6
ef16a43
08bcf75
be24644
a20d53d
4524997
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"context" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"connectrpc.com/connect" | ||
) | ||
|
@@ -69,6 +70,40 @@ func Errorf(template string, args ...any) *connect.Error { | |
return connect.NewError(connect.CodeUnauthenticated, fmt.Errorf(template, args...)) | ||
} | ||
|
||
// InferProtocol returns the inferred RPC protocol. It is one of | ||
// [connect.ProtocolConnect], [connect.ProtocolGRPC], or [connect.ProtocolGRPCWeb]. | ||
func InferProtocol(request *http.Request) string { | ||
ct := request.Header.Get("Content-Type") | ||
switch { | ||
case strings.HasPrefix(ct, "application/grpc-web"): | ||
return connect.ProtocolGRPCWeb | ||
case strings.HasPrefix(ct, "application/grpc"): | ||
return connect.ProtocolGRPC | ||
default: | ||
return connect.ProtocolConnect | ||
} | ||
} | ||
|
||
// InferProcedure returns the inferred RPC procedure. It is of the form | ||
// "/service/method". If the request path does not contain a procedure name, the | ||
// entire path is returned. | ||
func InferProcedure(request *http.Request) string { | ||
path := strings.TrimSuffix(request.URL.Path, "/") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we do this? Do connect RPC servers actually accept an invalid trailing slash like this? Pretty sure gRPC servers are usually strict and do not allow this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed allowing trailing suffix. |
||
ultimate := strings.LastIndex(path, "/") | ||
if ultimate < 0 { | ||
return request.URL.Path | ||
} | ||
penultimate := strings.LastIndex(path[:ultimate], "/") | ||
if penultimate < 0 { | ||
return request.URL.Path | ||
} | ||
procedure := path[penultimate:] | ||
if len(procedure) < 4 { // two slashes + service + method | ||
return request.URL.Path | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same remark as in connect-go PR. This would incorrectly allow "//foo". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a testcase. |
||
return procedure | ||
} | ||
|
||
// Middleware is server-side HTTP middleware that authenticates RPC requests. | ||
// In addition to rejecting unauthenticated requests, it can optionally attach | ||
// arbitrary information about the authenticated identity to the context. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no value in returning "unknown" (or empty string, etc) when the request doesn't look like any of these? Since this is middleware, it seems highly likely it could be used with a mux that has both connect and non-connect routes, so I think we do need better classification here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now return "", false.