25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

145 satır
4.7KB

  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. ' 404 fallback
  76. Resolve = Array("ErrorController", "NotFound", emptyParams)
  77. End Function
  78. '------------------------------------------------------------
  79. ' INTERNAL IsMatch(requestPath, routePattern, values())
  80. ' Returns True/False and fills values() with parameters
  81. '------------------------------------------------------------
  82. Private Function IsMatch(requestPath, routePattern, values)
  83. Dim reqParts, routeParts, i, paramCount
  84. reqParts = Split(requestPath, "/")
  85. routeParts = Split(routePattern, "/")
  86. If UBound(reqParts) <> UBound(routeParts) Then
  87. IsMatch = False : Exit Function
  88. End If
  89. paramCount = 0 : ReDim values(-1)
  90. For i = 0 To UBound(reqParts)
  91. If Left(routeParts(i), 1) = ":" Then
  92. ReDim Preserve values(paramCount)
  93. values(paramCount) = reqParts(i)
  94. paramCount = paramCount + 1
  95. ElseIf LCase(routeParts(i)) <> LCase(reqParts(i)) Then
  96. IsMatch = False : Exit Function
  97. End If
  98. Next
  99. If paramCount = 0 Then ReDim values(-1)
  100. IsMatch = True
  101. End Function
  102. '------------------------------------------------------------
  103. ' Optional lifecycle hooks
  104. '------------------------------------------------------------
  105. Private Sub Class_Terminate()
  106. Set routes = Nothing
  107. End Sub
  108. End Class
  109. ' Singleton accessor (same pattern as MVC()/ControllerRegistry()) so the router is visible
  110. ' inside class methods regardless of include order.
  111. Dim Router_Class__Singleton
  112. Function GetRouter()
  113. If IsEmpty(Router_Class__Singleton) Then
  114. Set Router_Class__Singleton = New Router_Class
  115. End If
  116. Set GetRouter = Router_Class__Singleton
  117. End Function
  118. Dim router : Set router = GetRouter()
  119. %>

Powered by TurnKey Linux.