Skip to content

Commit

Permalink
feat: support percent-encoded grpc-message header
Browse files Browse the repository at this point in the history
  • Loading branch information
standy66 committed Feb 15, 2019
1 parent d2d461f commit c6636f4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
7 changes: 3 additions & 4 deletions src/purerpc/grpclib/connection.py
@@ -1,3 +1,4 @@
import urllib.parse
import logging
import datetime

Expand Down Expand Up @@ -275,8 +276,7 @@ def respond_status(self, stream_id: int, status: Status, content_type_suffix="",
("grpc-status", str(status.int_value)),
]
if status.status_message:
# TODO: should be percent encoded
trailers.append(("grpc-message", status.status_message))
trailers.append(("grpc-message", urllib.parse.quote(status.status_message, safe='')))
trailers.extend(sanitize_headers(custom_metadata))
self.h2_connection.send_headers(stream_id, trailers, end_stream=True)

Expand All @@ -285,7 +285,6 @@ def end_response(self, stream_id: int, status: Status, custom_metadata=()):
("grpc-status", str(status.int_value)),
]
if status.status_message:
# TODO: should be percent encoded
trailers.append(("grpc-message", status.status_message))
trailers.append(("grpc-message", urllib.parse.quote(status.status_message, safe='')))
trailers.extend(sanitize_headers(custom_metadata))
self.h2_connection.send_headers(stream_id, trailers, end_stream=True)
4 changes: 2 additions & 2 deletions src/purerpc/grpclib/events.py
@@ -1,3 +1,4 @@
import urllib.parse
import datetime

from .headers import HeaderDict
Expand Down Expand Up @@ -174,8 +175,7 @@ def parse_from_stream_id_and_headers_destructive(stream_id: int, headers: Header

status_code = int(headers.pop("grpc-status"))
if "grpc-message" in headers:
# TODO: is percent encoded
status_message = headers.pop("grpc-message")
status_message = urllib.parse.unquote(headers.pop("grpc-message"))
else:
status_message = ""

Expand Down
14 changes: 8 additions & 6 deletions tests/test_status_codes.py
@@ -1,6 +1,7 @@
import functools
import pickle
import base64
import string
import re

import pytest
Expand All @@ -11,15 +12,16 @@


def regex_and(first, second):
return re.compile(r"(?=.*{first})(?=.*{second}).*".format(first=first, second=second), flags=re.DOTALL)
return re.compile(r"(?=.*{first})(?=.*{second}).*".format(first=re.escape(first), second=re.escape(second)),
flags=re.DOTALL)


STATUS_CODES = [
(purerpc.CancelledError, "CANCELLED", "detailed message"),
(purerpc.UnknownError, "UNKNOWN", "detailed message"),
(purerpc.InvalidArgumentError, "INVALID_ARGUMENT", "detailed message"),
(purerpc.DeadlineExceededError, "DEADLINE_EXCEEDED", "detailed message"),
(purerpc.NotFoundError, "NOT_FOUND", "detailed message"),
(purerpc.CancelledError, "CANCELLED", "percent encoded message: %"),
(purerpc.UnknownError, "UNKNOWN", "привет"),
(purerpc.InvalidArgumentError, "INVALID_ARGUMENT", "\r\n"),
(purerpc.DeadlineExceededError, "DEADLINE_EXCEEDED", string.printable),
(purerpc.NotFoundError, "NOT_FOUND", "message:" + string.whitespace),
(purerpc.AlreadyExistsError, "ALREADY_EXISTS", "detailed message"),
(purerpc.PermissionDeniedError, "PERMISSION_DENIED", "detailed message"),
(purerpc.ResourceExhaustedError, "RESOURCE_EXHAUSTED", "detailed message"),
Expand Down

0 comments on commit c6636f4

Please sign in to comment.