Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad1e32f

Browse files
tswasttseaver
authored andcommitted
Don't URL-encode slashes in gRPC request headers. (#6310)
Per internal document go/api-url-encoding (approved on 2017-04-20), "the client library will %-encode everything except "/" and unreserved characters, and the server will %-decode everything except "%2F" and %2f" This is currently affecting a private API which passes a resource name (containing slashes) as a query parameter over gRPC. Toward #6326. (cherry picked from commit e368317)
1 parent 7527a4e commit ad1e32f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

api_core/google/api_core/gapic_v1/routing_header.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Generally, these headers are specified as gRPC metadata.
2121
"""
2222

23+
import sys
24+
2325
from six.moves.urllib.parse import urlencode
2426

2527
ROUTING_METADATA_KEY = 'x-goog-request-params'
@@ -35,7 +37,13 @@ def to_routing_header(params):
3537
Returns:
3638
str: The routing header string.
3739
"""
38-
return urlencode(params)
40+
if sys.version_info[0] < 3:
41+
# Python 2 does not have the "safe" parameter for urlencode.
42+
return urlencode(params).replace('%2F', '/')
43+
return urlencode(
44+
params,
45+
# Per Google API policy (go/api-url-encoding), / is not encoded.
46+
safe='/')
3947

4048

4149
def to_grpc_metadata(params):

api_core/tests/unit/gapic/test_routing_header.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def test_to_routing_header():
2222
assert value == "name=meep&book.read=1"
2323

2424

25+
def test_to_routing_header_with_slashes():
26+
params = [('name', 'me/ep'), ('book.read', '1&2')]
27+
value = routing_header.to_routing_header(params)
28+
assert value == "name=me/ep&book.read=1%262"
29+
30+
2531
def test_to_grpc_metadata():
2632
params = [('name', 'meep'), ('book.read', '1')]
2733
metadata = routing_header.to_grpc_metadata(params)

0 commit comments

Comments
 (0)