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.

174 line
5.8KB

  1. <%
  2. ' Router_Class: pure-ASP port of router.wsc (AxonASP/Linux cannot load Windows Script Components)
  3. Class Router_Class
  4. '------------------------------------------------------------
  5. ' Private state
  6. '------------------------------------------------------------
  7. Private routes
  8. Private Sub Class_Initialize()
  9. Set routes = CreateObject("Scripting.Dictionary")
  10. End Sub
  11. '------------------------------------------------------------
  12. ' METHOD AddRoute(method, path, controller, action)
  13. '------------------------------------------------------------
  14. Public Sub AddRoute(method, path, controller, action)
  15. ' Input validation
  16. If IsEmpty(method) Or Len(Trim(method)) = 0 Then
  17. Err.Raise 5, "Router.AddRoute", "HTTP method parameter is required and cannot be empty"
  18. End If
  19. If IsEmpty(path) Then
  20. Err.Raise 5, "Router.AddRoute", "Path parameter is required"
  21. End If
  22. If IsEmpty(controller) Or Len(Trim(controller)) = 0 Then
  23. Err.Raise 5, "Router.AddRoute", "Controller parameter is required and cannot be empty"
  24. End If
  25. If IsEmpty(action) Or Len(Trim(action)) = 0 Then
  26. Err.Raise 5, "Router.AddRoute", "Action parameter is required and cannot be empty"
  27. End If
  28. ' Validate HTTP method (allow common methods)
  29. Dim validMethods, methodUpper, i, isValidMethod
  30. validMethods = Array("GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS")
  31. methodUpper = UCase(Trim(method))
  32. isValidMethod = False
  33. For i = 0 To UBound(validMethods)
  34. If validMethods(i) = methodUpper Then
  35. isValidMethod = True
  36. Exit For
  37. End If
  38. Next
  39. If Not isValidMethod Then
  40. Err.Raise 5, "Router.AddRoute", "Invalid HTTP method: " & method & ". Allowed: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS"
  41. End If
  42. Dim routeKey
  43. routeKey = methodUpper & ":" & LCase(Trim(path))
  44. If Not routes.Exists(routeKey) Then
  45. routes.Add routeKey, Array(Trim(controller), Trim(action))
  46. End If
  47. End Sub
  48. '------------------------------------------------------------
  49. ' METHOD Resolve(method, path) -> Array(controller, action, params())
  50. '------------------------------------------------------------
  51. Public Function Resolve(method, path)
  52. Dim routeKey, routeValue, values
  53. routeKey = UCase(method) & ":" & LCase(path)
  54. ' Always return a params array (empty by default)
  55. Dim emptyParams() : ReDim emptyParams(-1)
  56. ' Exact match first
  57. If routes.Exists(routeKey) Then
  58. routeValue = routes(routeKey)
  59. Resolve = Array(routeValue(0), routeValue(1), emptyParams)
  60. Exit Function
  61. End If
  62. ' Dynamic routes (e.g. /users/:id)
  63. Dim r, routeMethod, routePattern
  64. For Each r In routes.Keys
  65. routeMethod = Split(r, ":")(0)
  66. routePattern = Mid(r, Len(routeMethod) + 2) ' strip "METHOD:"
  67. If UCase(routeMethod) = UCase(method) Then
  68. If IsMatch(path, routePattern, values) Then
  69. routeValue = routes(r)
  70. Resolve = Array(routeValue(0), routeValue(1), values)
  71. Exit Function
  72. End If
  73. End If
  74. Next
  75. ' The path exists for other methods: 405 with the allowed methods (Allow header)
  76. Dim allow
  77. allow = AllowedMethods(path)
  78. If Len(allow) > 0 Then
  79. Resolve = Array("ErrorController", "MethodNotAllowed", Array(allow))
  80. Exit Function
  81. End If
  82. ' 404 fallback
  83. Resolve = Array("ErrorController", "NotFound", emptyParams)
  84. End Function
  85. '------------------------------------------------------------
  86. ' AllowedMethods(path) -> "GET, POST" (methods routed for this path), or ""
  87. '------------------------------------------------------------
  88. Public Function AllowedMethods(path)
  89. Dim r, routeMethod, routePattern, dummy, out
  90. out = ""
  91. For Each r In routes.Keys
  92. routeMethod = Split(r, ":")(0)
  93. routePattern = Mid(r, Len(routeMethod) + 2)
  94. If IsMatch(LCase(path), routePattern, dummy) Then
  95. If InStr(", " & out & ",", ", " & routeMethod & ",") = 0 Then
  96. If Len(out) > 0 Then
  97. out = out & ", "
  98. End If
  99. out = out & routeMethod
  100. End If
  101. End If
  102. Next
  103. AllowedMethods = out
  104. End Function
  105. '------------------------------------------------------------
  106. ' INTERNAL IsMatch(requestPath, routePattern, values())
  107. ' Returns True/False and fills values() with parameters
  108. '------------------------------------------------------------
  109. Private Function IsMatch(requestPath, routePattern, values)
  110. Dim reqParts, routeParts, i, paramCount
  111. reqParts = Split(requestPath, "/")
  112. routeParts = Split(routePattern, "/")
  113. If UBound(reqParts) <> UBound(routeParts) Then
  114. IsMatch = False : Exit Function
  115. End If
  116. paramCount = 0 : ReDim values(-1)
  117. For i = 0 To UBound(reqParts)
  118. If Left(routeParts(i), 1) = ":" Then
  119. ReDim Preserve values(paramCount)
  120. values(paramCount) = reqParts(i)
  121. paramCount = paramCount + 1
  122. ElseIf LCase(routeParts(i)) <> LCase(reqParts(i)) Then
  123. IsMatch = False : Exit Function
  124. End If
  125. Next
  126. If paramCount = 0 Then ReDim values(-1)
  127. IsMatch = True
  128. End Function
  129. '------------------------------------------------------------
  130. ' Optional lifecycle hooks
  131. '------------------------------------------------------------
  132. Private Sub Class_Terminate()
  133. Set routes = Nothing
  134. End Sub
  135. End Class
  136. ' Singleton accessor (same pattern as MVC()/ControllerRegistry()) so the router is visible
  137. ' inside class methods regardless of include order.
  138. Dim Router_Class__Singleton
  139. Function GetRouter()
  140. If IsEmpty(Router_Class__Singleton) Then
  141. Set Router_Class__Singleton = New Router_Class
  142. End If
  143. Set GetRouter = Router_Class__Singleton
  144. End Function
  145. Dim router : Set router = GetRouter()
  146. %>

Powered by TurnKey Linux.