parseurl.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Dominik Picheta
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## **Warnings:** This module is deprecated since version 0.10.2.
  10. ## Use the `uri <uri.html>`_ module instead.
  11. ##
  12. ## Parses & constructs URLs.
  13. {.deprecated.}
  14. import strutils
  15. type
  16. Url* = tuple[ ## represents a *Uniform Resource Locator* (URL)
  17. ## any optional component is "" if it does not exist
  18. scheme, username, password,
  19. hostname, port, path, query, anchor: string]
  20. {.deprecated: [TUrl: Url].}
  21. proc parseUrl*(url: string): Url {.deprecated.} =
  22. var i = 0
  23. var scheme, username, password: string = ""
  24. var hostname, port, path, query, anchor: string = ""
  25. var temp = ""
  26. if url[i] != '/': # url isn't a relative path
  27. while true:
  28. # Scheme
  29. if url[i] == ':':
  30. if url[i+1] == '/' and url[i+2] == '/':
  31. scheme = temp
  32. temp.setLen(0)
  33. inc(i, 3) # Skip the //
  34. # Authority(username, password)
  35. if url[i] == '@':
  36. username = temp
  37. let colon = username.find(':')
  38. if colon >= 0:
  39. password = username.substr(colon+1)
  40. username = username.substr(0, colon-1)
  41. temp.setLen(0)
  42. inc(i) #Skip the @
  43. # hostname(subdomain, domain, port)
  44. if url[i] == '/' or url[i] == '\0':
  45. hostname = temp
  46. let colon = hostname.find(':')
  47. if colon >= 0:
  48. port = hostname.substr(colon+1)
  49. hostname = hostname.substr(0, colon-1)
  50. temp.setLen(0)
  51. break
  52. temp.add(url[i])
  53. inc(i)
  54. if url[i] == '/': inc(i) # Skip the '/'
  55. # Path
  56. while true:
  57. if url[i] == '?':
  58. path = temp
  59. temp.setLen(0)
  60. if url[i] == '#':
  61. if temp[0] == '?':
  62. query = temp
  63. else:
  64. path = temp
  65. temp.setLen(0)
  66. if url[i] == '\0':
  67. if temp[0] == '?':
  68. query = temp
  69. elif temp[0] == '#':
  70. anchor = temp
  71. else:
  72. path = temp
  73. break
  74. temp.add(url[i])
  75. inc(i)
  76. return (scheme, username, password, hostname, port, path, query, anchor)
  77. proc `$`*(u: Url): string {.deprecated.} =
  78. ## turns the URL `u` into its string representation.
  79. result = ""
  80. if u.scheme.len > 0:
  81. result.add(u.scheme)
  82. result.add("://")
  83. if u.username.len > 0:
  84. result.add(u.username)
  85. if u.password.len > 0:
  86. result.add(":")
  87. result.add(u.password)
  88. result.add("@")
  89. result.add(u.hostname)
  90. if u.port.len > 0:
  91. result.add(":")
  92. result.add(u.port)
  93. if u.path.len > 0:
  94. result.add("/")
  95. result.add(u.path)
  96. result.add(u.query)
  97. result.add(u.anchor)