URLEncode.m 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // URLEncode.m
  3. // TunneledWebRequest
  4. //
  5. /*
  6. Licensed under Creative Commons Zero (CC0).
  7. https://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. // NOTE: this file is shared by TunneledWebRequest and TunneledWebView
  10. #import "URLEncode.h"
  11. @implementation URLEncode
  12. // Encode all reserved characters. See: https://stackoverflow.com/a/34788364.
  13. //
  14. // From RFC 3986 (https://www.ietf.org/rfc/rfc3986.txt):
  15. //
  16. // 2.3. Unreserved Characters
  17. //
  18. // Characters that are allowed in a URI but do not have a reserved
  19. // purpose are called unreserved. These include uppercase and lowercase
  20. // letters, decimal digits, hyphen, period, underscore, and tilde.
  21. //
  22. // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  23. + (NSString*)encode:(NSString*)url {
  24. NSCharacterSet *queryParamCharsAllowed = [NSCharacterSet
  25. characterSetWithCharactersInString:
  26. @"abcdefghijklmnopqrstuvwxyz"
  27. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  28. "0123456789"
  29. "-._~"];
  30. return [url stringByAddingPercentEncodingWithAllowedCharacters:queryParamCharsAllowed];
  31. }
  32. @end