parseurl.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. proc parseUrl*(url: string): Url {.deprecated.} =
  21. var i = 0
  22. var scheme, username, password: string = ""
  23. var hostname, port, path, query, anchor: string = ""
  24. var temp = ""
  25. if url[i] != '/': # url isn't a relative path
  26. while true:
  27. # Scheme
  28. if url[i] == ':':
  29. if url[i+1] == '/' and url[i+2] == '/':
  30. scheme = temp
  31. temp.setLen(0)
  32. inc(i, 3) # Skip the //
  33. # Authority(username, password)
  34. if url[i] == '@':
  35. username = temp
  36. let colon = username.find(':')
  37. if colon >= 0:
  38. password = username.substr(colon+1)
  39. username = username.substr(0, colon-1)
  40. temp.setLen(0)
  41. inc(i) #Skip the @
  42. # hostname(subdomain, domain, port)
  43. if url[i] == '/' or url[i] == '\0':
  44. hostname = temp
  45. let colon = hostname.find(':')
  46. if colon >= 0:
  47. port = hostname.substr(colon+1)
  48. hostname = hostname.substr(0, colon-1)
  49. temp.setLen(0)
  50. break
  51. temp.add(url[i])
  52. inc(i)
  53. if url[i] == '/': inc(i) # Skip the '/'
  54. # Path
  55. while true:
  56. if url[i] == '?':
  57. path = temp
  58. temp.setLen(0)
  59. if url[i] == '#':
  60. if temp[0] == '?':
  61. query = temp
  62. else:
  63. path = temp
  64. temp.setLen(0)
  65. if url[i] == '\0':
  66. if temp[0] == '?':
  67. query = temp
  68. elif temp[0] == '#':
  69. anchor = temp
  70. else:
  71. path = temp
  72. break
  73. temp.add(url[i])
  74. inc(i)
  75. return (scheme, username, password, hostname, port, path, query, anchor)
  76. proc `$`*(u: Url): string {.deprecated.} =
  77. ## turns the URL `u` into its string representation.
  78. result = ""
  79. if u.scheme.len > 0:
  80. result.add(u.scheme)
  81. result.add("://")
  82. if u.username.len > 0:
  83. result.add(u.username)
  84. if u.password.len > 0:
  85. result.add(":")
  86. result.add(u.password)
  87. result.add("@")
  88. result.add(u.hostname)
  89. if u.port.len > 0:
  90. result.add(":")
  91. result.add(u.port)
  92. if u.path.len > 0:
  93. result.add("/")
  94. result.add(u.path)
  95. result.add(u.query)
  96. result.add(u.anchor)