percent_encoding.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPC_CORE_LIB_SLICE_PERCENT_ENCODING_H
  19. #define GRPC_CORE_LIB_SLICE_PERCENT_ENCODING_H
  20. /* Percent encoding and decoding of slices.
  21. Transforms arbitrary strings into safe-for-transmission strings by using
  22. variants of percent encoding (RFC 3986).
  23. Two major variants are supplied: one that strictly matches URL encoding,
  24. and another which applies percent encoding only to non-http2 header
  25. bytes (the 'compatible' variant) */
  26. #include <grpc/support/port_platform.h>
  27. #include <stdbool.h>
  28. #include <grpc/slice.h>
  29. /* URL percent encoding spec bitfield (usabel as 'unreserved_bytes' in
  30. grpc_percent_encode_slice, grpc_strict_percent_decode_slice).
  31. Flags [A-Za-z0-9-_.~] as unreserved bytes for the percent encoding routines
  32. */
  33. extern const uint8_t grpc_url_percent_encoding_unreserved_bytes[256 / 8];
  34. /* URL percent encoding spec bitfield (usabel as 'unreserved_bytes' in
  35. grpc_percent_encode_slice, grpc_strict_percent_decode_slice).
  36. Flags ascii7 non-control characters excluding '%' as unreserved bytes for the
  37. percent encoding routines */
  38. extern const uint8_t grpc_compatible_percent_encoding_unreserved_bytes[256 / 8];
  39. /* Percent-encode a slice, returning the new slice (this cannot fail):
  40. unreserved_bytes is a bitfield indicating which bytes are considered
  41. unreserved and thus do not need percent encoding */
  42. grpc_slice grpc_percent_encode_slice(grpc_slice slice,
  43. const uint8_t* unreserved_bytes);
  44. /* Percent-decode a slice, strictly.
  45. If the input is legal (contains no unreserved bytes, and legal % encodings),
  46. returns true and sets *slice_out to the decoded slice.
  47. If the input is not legal, returns false and leaves *slice_out untouched.
  48. unreserved_bytes is a bitfield indicating which bytes are considered
  49. unreserved and thus do not need percent encoding */
  50. bool grpc_strict_percent_decode_slice(grpc_slice slice_in,
  51. const uint8_t* unreserved_bytes,
  52. grpc_slice* slice_out);
  53. /* Percent-decode a slice, permissively.
  54. If a % triplet can not be decoded, pass it through verbatim.
  55. This cannot fail. */
  56. grpc_slice grpc_permissive_percent_decode_slice(grpc_slice slice_in);
  57. #endif /* GRPC_CORE_LIB_SLICE_PERCENT_ENCODING_H */