Skip to content

Commit

Permalink
api: fix the encoded path of region key (#2399)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx authored May 8, 2020
1 parent 592583a commit 829afb2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package api
import (
"container/heap"
"net/http"
"net/url"
"sort"
"strconv"

Expand Down Expand Up @@ -145,6 +146,11 @@ func (h *regionHandler) GetRegionByKey(w http.ResponseWriter, r *http.Request) {
rc := getCluster(r.Context())
vars := mux.Vars(r)
key := vars["key"]
key, err := url.QueryUnescape(key)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
regionInfo := rc.GetRegionInfoByKey([]byte(key))
h.rd.JSON(w, http.StatusOK, NewRegionInfo(regionInfo))
}
Expand Down
2 changes: 1 addition & 1 deletion server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func createRouter(ctx context.Context, prefix string, svr *server.Server) *mux.R

regionHandler := newRegionHandler(svr, rd)
clusterRouter.HandleFunc("/region/id/{id}", regionHandler.GetRegionByID).Methods("GET")
clusterRouter.HandleFunc("/region/key/{key}", regionHandler.GetRegionByKey).Methods("GET")
clusterRouter.UseEncodedPath().HandleFunc("/region/key/{key}", regionHandler.GetRegionByKey).Methods("GET")

srd := createStreamingRender()
regionsAllHandler := newRegionsHandler(svr, srd)
Expand Down
8 changes: 8 additions & 0 deletions tests/pdctl/region/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ func (s *regionTestSuite) TestRegion(c *C) {
c.Assert(json.Unmarshal(output, &regionInfo), IsNil)
c.Assert(&regionInfo, DeepEquals, api.NewRegionInfo(r2))

// issue #2351
args = []string{"-u", pdAddr, "region", "key", "--format=hex", "622f62"}
_, output, err = pdctl.ExecuteCommandC(cmd, args...)
c.Assert(err, IsNil)
regionInfo = api.RegionInfo{}
c.Assert(json.Unmarshal(output, &regionInfo), IsNil)
c.Assert(&regionInfo, DeepEquals, api.NewRegionInfo(r2))

// region startkey --format=raw <key> command
args = []string{"-u", pdAddr, "region", "startkey", "--format=raw", "b", "2"}
_, output, err = pdctl.ExecuteCommandC(cmd, args...)
Expand Down

0 comments on commit 829afb2

Please sign in to comment.