Skip to content

Commit

Permalink
api: fix the encoded path of region key (tikv#2399)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Jun 24, 2020
1 parent d0963de commit 6d6af9b
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"
"strconv"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -119,6 +120,11 @@ func (h *regionHandler) GetRegionByKey(w http.ResponseWriter, r *http.Request) {
}
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 := cluster.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 @@ -94,7 +94,7 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {

regionHandler := newRegionHandler(svr, rd)
router.HandleFunc("/api/v1/region/id/{id}", regionHandler.GetRegionByID).Methods("GET")
router.HandleFunc("/api/v1/region/key/{key}", regionHandler.GetRegionByKey).Methods("GET")
router.UseEncodedPath().HandleFunc("/api/v1/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 @@ -235,6 +235,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 6d6af9b

Please sign in to comment.