-
Notifications
You must be signed in to change notification settings - Fork 112
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
Add APIs to make and handle conditional GETs #494
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2021-2023 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package connect_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/bufbuild/connect-go" | ||
pingv1 "github.com/bufbuild/connect-go/internal/gen/connect/ping/v1" | ||
"github.com/bufbuild/connect-go/internal/gen/connect/ping/v1/pingv1connect" | ||
) | ||
|
||
// ExampleCachingServer is an example of how servers can take advantage the | ||
// Connect protocol's support for HTTP-level caching. The Protobuf | ||
// definition for this API is in proto/connect/ping/v1/ping.proto. | ||
type ExampleCachingPingServer struct { | ||
pingv1connect.UnimplementedPingServiceHandler | ||
} | ||
|
||
// Ping is idempotent and free of side effects (and the Protobuf schema | ||
// indicates this), so clients using the Connect protocol may call it with HTTP | ||
// GET requests. This implementation uses Etags to manage client-side caching. | ||
func (*ExampleCachingPingServer) Ping( | ||
_ context.Context, | ||
req *connect.Request[pingv1.PingRequest], | ||
) (*connect.Response[pingv1.PingResponse], error) { | ||
resp := connect.NewResponse(&pingv1.PingResponse{ | ||
Number: req.Msg.Number, | ||
}) | ||
// Our hashing logic is simple: we use the number in the PingResponse. | ||
hash := fmt.Sprint(resp.Msg.Number) | ||
// If the request was an HTTP GET (which always has URL query parameters), | ||
// we'll need to check if the client already has the response cached. | ||
if len(req.Peer().Query) > 0 { | ||
if req.Header().Get("If-None-Match") == hash { | ||
return nil, connect.NewNotModifiedError(http.Header{ | ||
"Etag": []string{hash}, | ||
}) | ||
} | ||
resp.Header().Set("Etag", hash) | ||
} | ||
return resp, nil | ||
} | ||
|
||
func ExampleNewNotModifiedError() { | ||
mux := http.NewServeMux() | ||
mux.Handle(pingv1connect.NewPingServiceHandler(&ExampleCachingPingServer{})) | ||
_ = http.ListenAndServe("localhost:8080", mux) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 it possible for the handler to detect whether the request was a GET vs a POST? Would that possibly be a better check here than just looking if the method is side-effect-free?
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.
@jchadwick-buf, do you know the answer to above? I think either way, we can probably go ahead and merge this PR. If the answer is "no, there isn't yet a way for a handler to detect if current request was GET vs POST", then that is something that can be tacked on later.
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.
Unfortunately, no - currently, there's no way for clients to easily detect whether the request was a GET or a POST. (The GET/POST decision happens after interceptors run, so there wasn't any need for such an API before.) We could add a way to detect GETs directly in a later PR - it's feasible, but would be a bit gross.
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.
I was just talking about handlers, not clients.
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.
There's not a great way to tell with handlers, though as a hack it is possible to inspect the
Query
value onPeer
.