Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
9.4 KiB

  1. /*++
  2. Copyright (c) 1998-2002 Microsoft Corporation
  3. Module Name:
  4. C14n.h
  5. Abstract:
  6. URL canonicalization (c14n) routines
  7. Author:
  8. George V. Reilly (GeorgeRe) 10-Apr-2002
  9. Revision History:
  10. --*/
  11. #ifndef _C14N_H_
  12. #define _C14N_H_
  13. #define DEFAULT_C14N_ENABLE_NON_UTF8_URL TRUE
  14. #define DEFAULT_C14N_FAVOR_UTF8_URL TRUE
  15. #define DEFAULT_C14N_ENABLE_DBCS_URL FALSE
  16. #define DEFAULT_C14N_PERCENT_U_ALLOWED TRUE
  17. #define DEFAULT_C14N_ALLOW_RESTRICTED_CHARS FALSE
  18. // Maximum length of the AbsPath of a URL, in chars
  19. #define DEFAULT_C14N_URL_MAX_LENGTH UNICODE_STRING_MAX_WCHAR_LEN
  20. #ifndef MAX_PATH
  21. #define MAX_PATH 260
  22. #endif
  23. // Maximum length of an individual segment within a URL
  24. #define DEFAULT_C14N_URL_SEGMENT_MAX_LENGTH MAX_PATH
  25. #define C14N_URL_SEGMENT_UNLIMITED_LENGTH (0xFFFFFFFE - STRLEN_LIT("/"))
  26. // Maximum number of path segments within a URL
  27. #define DEFAULT_C14N_URL_SEGMENT_MAX_COUNT 255
  28. #define C14N_URL_SEGMENT_UNLIMITED_COUNT C14N_URL_SEGMENT_UNLIMITED_LENGTH
  29. // Maximum length of a label within a hostname; e.g., "www.example.com"
  30. // has three labels, with "example" being the longest at 7 octets.
  31. #define DEFAULT_C14N_MAX_LABEL_LENGTH 63
  32. // Maximum length of a hostname
  33. #define DEFAULT_C14N_MAX_HOSTNAME_LENGTH 255
  34. typedef enum _URL_PART
  35. {
  36. UrlPart_Scheme,
  37. UrlPart_HostName,
  38. UrlPart_UserInfo,
  39. UrlPart_AbsPath,
  40. UrlPart_QueryString,
  41. UrlPart_Fragment
  42. } URL_PART;
  43. typedef enum _URL_DECODE_ORDER
  44. {
  45. UrlDecode_Shift = 2,
  46. UrlDecode_Mask = ((1 << UrlDecode_Shift) - 1),
  47. #define URL_DECODE2(D1, D2) \
  48. ( UrlDecode_##D1 | (UrlDecode_##D2 << UrlDecode_Shift))
  49. #define URL_DECODE3(D1, D2, D3) \
  50. ( URL_DECODE2(D1, D2) | (UrlDecode_##D3 << (2 * UrlDecode_Shift)))
  51. UrlDecode_None = 0,
  52. // The following are the only valid permutations
  53. UrlDecode_Ansi = 1,
  54. UrlDecode_Dbcs = 2,
  55. UrlDecode_Utf8 = 3,
  56. UrlDecode_Ansi_Else_Dbcs = URL_DECODE2(Ansi, Dbcs),
  57. UrlDecode_Ansi_Else_Dbcs_Else_Utf8 = URL_DECODE3(Ansi, Dbcs, Utf8),
  58. UrlDecode_Ansi_Else_Utf8 = URL_DECODE2(Ansi, Utf8),
  59. UrlDecode_Ansi_Else_Utf8_Else_Dbcs = URL_DECODE3(Ansi, Utf8, Dbcs),
  60. UrlDecode_Dbcs_Else_Ansi = URL_DECODE2(Dbcs, Ansi),
  61. UrlDecode_Dbcs_Else_Ansi_Else_Utf8 = URL_DECODE3(Dbcs, Ansi, Utf8),
  62. UrlDecode_Dbcs_Else_Utf8 = URL_DECODE2(Dbcs, Utf8),
  63. UrlDecode_Dbcs_Else_Utf8_Else_Ansi = URL_DECODE3(Dbcs, Utf8, Ansi),
  64. UrlDecode_Utf8_Else_Ansi = URL_DECODE2(Utf8, Ansi),
  65. UrlDecode_Utf8_Else_Ansi_Else_Dbcs = URL_DECODE3(Utf8, Ansi, Dbcs),
  66. UrlDecode_Utf8_Else_Dbcs = URL_DECODE2(Utf8, Dbcs),
  67. UrlDecode_Utf8_Else_Dbcs_Else_Ansi = URL_DECODE3(Utf8, Dbcs, Ansi),
  68. UrlDecode_MaxMask = URL_DECODE3(Mask, Mask, Mask)
  69. #undef URL_DECODE2
  70. #undef URL_DECODE3
  71. // UrlDecode_Utf8_Else_Dbcs_Else_Ansi means:
  72. // * First attempt to decode the URL as UTF-8.
  73. // * If that fails, attempt to decode it as DBCS.
  74. // * If that too fails, attempt to decode it as ANSI.
  75. } URL_DECODE_ORDER, *PURL_DECODE_ORDER;
  76. typedef enum _URL_ENCODING_TYPE
  77. {
  78. UrlEncoding_Ansi = UrlDecode_Ansi,
  79. UrlEncoding_Dbcs = UrlDecode_Dbcs,
  80. UrlEncoding_Utf8 = UrlDecode_Utf8
  81. } URL_ENCODING_TYPE, *PURL_ENCODING_TYPE;
  82. typedef struct _URL_C14N_CONFIG
  83. {
  84. URL_DECODE_ORDER HostnameDecodeOrder;
  85. URL_DECODE_ORDER AbsPathDecodeOrder;
  86. BOOLEAN EnableNonUtf8;
  87. BOOLEAN FavorUtf8;
  88. BOOLEAN EnableDbcs;
  89. BOOLEAN PercentUAllowed;
  90. BOOLEAN AllowRestrictedChars;
  91. ULONG CodePage;
  92. ULONG UrlMaxLength;
  93. ULONG UrlSegmentMaxLength;
  94. ULONG UrlSegmentMaxCount;
  95. ULONG MaxLabelLength;
  96. ULONG MaxHostnameLength;
  97. } URL_C14N_CONFIG, *PURL_C14N_CONFIG;
  98. typedef enum
  99. {
  100. HttpUrlSite_None = 0,
  101. HttpUrlSite_Name, // named site
  102. HttpUrlSite_IP, // IPv4 or IPv6 literal hostname
  103. HttpUrlSite_NamePlusIP, // named site with Routing IP
  104. HttpUrlSite_WeakWildcard, // hostname = '*'
  105. HttpUrlSite_StrongWildcard, // hostname = '+'
  106. HttpUrlSite_Max
  107. } HTTP_URL_SITE_TYPE, *PHTTP_URL_SITE_TYPE;
  108. #define HTTP_PARSED_URL_SIGNATURE MAKE_SIGNATURE('PUrl')
  109. #define HTTP_PARSED_URL_SIGNATURE_X \
  110. MAKE_FREE_SIGNATURE(HTTP_PARSED_URL_SIGNATURE)
  111. #define IS_VALID_HTTP_PARSED_URL(p) \
  112. ((p) && ((p)->Signature == HTTP_PARSED_URL_SIGNATURE))
  113. typedef struct _HTTP_PARSED_URL
  114. {
  115. ULONG Signature; // HTTP_PARSED_URL_SIGNATURE
  116. HTTP_URL_SITE_TYPE SiteType; // Name, IP, or Weak/StrongWildCard
  117. //
  118. // These strings all point into the same buffer, of the form
  119. // "http://hostname:port/abs/path/" or
  120. // "http://hostname:port:IP/abs/path/".
  121. //
  122. PWSTR pFullUrl; // points to "http" or "https"
  123. PWSTR pHostname; // point to "hostname"
  124. PWSTR pPort; // point to "port"
  125. PWSTR pRoutingIP; // point to "IP" or NULL
  126. PWSTR pAbsPath; // points to "/abs/path"
  127. USHORT UrlLength; // length of pFullUrl
  128. USHORT HostnameLength; // length of pHostname
  129. USHORT PortLength; // length of pPort
  130. USHORT RoutingIPLength;// length of pRoutingIP
  131. USHORT AbsPathLength; // length of pAbsPath
  132. USHORT PortNumber; // value of pPort
  133. BOOLEAN Secure; // http or httpS?
  134. BOOLEAN Normalized; // In normalized form?
  135. BOOLEAN TrailingSlashReqd; // If TRUE => directory prefix
  136. union
  137. {
  138. SOCKADDR SockAddr; // Look at SockAddr.sa_family
  139. SOCKADDR_IN SockAddr4; // set if == TDI_ADDRESS_TYPE_IP
  140. SOCKADDR_IN6 SockAddr6; // set if == TDI_ADDRESS_TYPE_IP6
  141. };
  142. union
  143. {
  144. SOCKADDR RoutingAddr; // Look at RoutingAddr.sa_family
  145. SOCKADDR_IN RoutingAddr4; // set if == TDI_ADDRESS_TYPE_IP
  146. SOCKADDR_IN6 RoutingAddr6; // set if == TDI_ADDRESS_TYPE_IP6
  147. };
  148. } HTTP_PARSED_URL, *PHTTP_PARSED_URL;
  149. typedef enum _HOSTNAME_TYPE
  150. {
  151. Hostname_AbsUri = 1, // from Request-line
  152. Hostname_HostHeader, // from Host header
  153. Hostname_Transport // synthesized from transport's local IP address
  154. } HOSTNAME_TYPE, *PHOSTNAME_TYPE;
  155. VOID
  156. HttpInitializeDefaultUrlC14nConfig(
  157. PURL_C14N_CONFIG pCfg
  158. );
  159. VOID
  160. HttpInitializeDefaultUrlC14nConfigEncoding(
  161. PURL_C14N_CONFIG pCfg,
  162. BOOLEAN EnableNonUtf8,
  163. BOOLEAN FavorUtf8,
  164. BOOLEAN EnableDbcs
  165. );
  166. NTSTATUS
  167. HttpUnescapePercentHexEncoding(
  168. IN PCUCHAR pSourceChar,
  169. IN ULONG SourceLength,
  170. IN BOOLEAN PercentUAllowed,
  171. OUT PULONG pOutChar,
  172. OUT PULONG pBytesToSkip
  173. );
  174. NTSTATUS
  175. HttpValidateHostname(
  176. IN PURL_C14N_CONFIG pCfg,
  177. IN PCUCHAR pHostname,
  178. IN ULONG HostnameLength,
  179. IN HOSTNAME_TYPE HostnameType,
  180. OUT PSHORT pAddressType
  181. );
  182. NTSTATUS
  183. HttpCopyHost(
  184. IN PURL_C14N_CONFIG pCfg,
  185. OUT PWSTR pDestination,
  186. IN PCUCHAR pSource,
  187. IN ULONG SourceLength,
  188. OUT PULONG pBytesCopied,
  189. OUT PURL_ENCODING_TYPE pUrlEncodingType
  190. );
  191. NTSTATUS
  192. HttpCopyUrl(
  193. IN PURL_C14N_CONFIG pCfg,
  194. OUT PWSTR pDestination,
  195. IN PCUCHAR pSource,
  196. IN ULONG SourceLength,
  197. OUT PULONG pBytesCopied,
  198. OUT PURL_ENCODING_TYPE pUrlEncoding
  199. );
  200. NTSTATUS
  201. HttpCleanAndCopyUrl(
  202. IN PURL_C14N_CONFIG pCfg,
  203. IN URL_PART UrlPart,
  204. OUT PWSTR pDestination,
  205. IN PCUCHAR pSource,
  206. IN ULONG SourceLength,
  207. OUT PULONG pBytesCopied,
  208. OUT PWSTR * ppQueryString OPTIONAL,
  209. OUT PURL_ENCODING_TYPE pUrlEncoding
  210. );
  211. NTSTATUS
  212. HttpFindUrlToken(
  213. IN PURL_C14N_CONFIG pCfg,
  214. IN PCUCHAR pBuffer,
  215. IN ULONG BufferLength,
  216. OUT PUCHAR* ppTokenStart,
  217. OUT PULONG pTokenLength,
  218. OUT PBOOLEAN pRawUrlClean
  219. );
  220. NTSTATUS
  221. HttpParseUrl(
  222. IN PURL_C14N_CONFIG pCfg,
  223. IN PCWSTR pUrl,
  224. IN ULONG UrlLength,
  225. IN BOOLEAN TrailingSlashReqd,
  226. IN BOOLEAN ForceRoutingIP,
  227. OUT PHTTP_PARSED_URL pParsedUrl
  228. );
  229. NTSTATUS
  230. HttpNormalizeParsedUrl(
  231. IN OUT PHTTP_PARSED_URL pParsedUrl,
  232. IN PURL_C14N_CONFIG pCfg,
  233. IN BOOLEAN ForceCopy,
  234. IN BOOLEAN FreeOriginalUrl,
  235. IN BOOLEAN ForceRoutingIP,
  236. IN POOL_TYPE PoolType,
  237. IN ULONG PoolTag
  238. );
  239. PCSTR
  240. HttpSiteTypeToString(
  241. HTTP_URL_SITE_TYPE SiteType
  242. );
  243. #endif // _C14N_H_