Consolidated ASP Classic MVC framework from best components
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.8KB

  1. <?xml version="1.0"?>
  2. <!-- RouterComponent.wsc -->
  3. <component>
  4. <!-- COM registration -->
  5. <registration
  6. description = "Classic ASP Router Component"
  7. progid = "App.Router"
  8. version = "1.0"
  9. classid = "{A1FC6EA8-519E-4E34-AC08-77788E3E5E44}" />
  10. <!-- Public interface -->
  11. <public>
  12. <method name="AddRoute"/>
  13. <method name="Resolve"/>
  14. </public>
  15. <!-- Give the component ASP intrinsic objects (Request, Response, Server …) -->
  16. <implements type="ASP"/>
  17. <!-- Implementation -->
  18. <script language="VBScript">
  19. <![CDATA[
  20. Option Explicit
  21. '------------------------------------------------------------
  22. ' Private state
  23. '------------------------------------------------------------
  24. Dim routes : Set routes = CreateObject("Scripting.Dictionary")
  25. '------------------------------------------------------------
  26. ' METHOD AddRoute(method, path, controller, action)
  27. '------------------------------------------------------------
  28. Public Sub AddRoute(method, path, controller, action)
  29. ' Input validation
  30. If IsEmpty(method) Or Len(Trim(method)) = 0 Then
  31. Err.Raise 5, "Router.AddRoute", "HTTP method parameter is required and cannot be empty"
  32. End If
  33. If IsEmpty(path) Then
  34. Err.Raise 5, "Router.AddRoute", "Path parameter is required"
  35. End If
  36. If IsEmpty(controller) Or Len(Trim(controller)) = 0 Then
  37. Err.Raise 5, "Router.AddRoute", "Controller parameter is required and cannot be empty"
  38. End If
  39. If IsEmpty(action) Or Len(Trim(action)) = 0 Then
  40. Err.Raise 5, "Router.AddRoute", "Action parameter is required and cannot be empty"
  41. End If
  42. ' Validate HTTP method (allow common methods)
  43. Dim validMethods, methodUpper, i, isValidMethod
  44. validMethods = Array("GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS")
  45. methodUpper = UCase(Trim(method))
  46. isValidMethod = False
  47. For i = 0 To UBound(validMethods)
  48. If validMethods(i) = methodUpper Then
  49. isValidMethod = True
  50. Exit For
  51. End If
  52. Next
  53. If Not isValidMethod Then
  54. Err.Raise 5, "Router.AddRoute", "Invalid HTTP method: " & method & ". Allowed: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS"
  55. End If
  56. Dim routeKey
  57. routeKey = methodUpper & ":" & LCase(Trim(path))
  58. If Not routes.Exists(routeKey) Then
  59. routes.Add routeKey, Array(Trim(controller), Trim(action))
  60. End If
  61. End Sub
  62. '------------------------------------------------------------
  63. ' METHOD Resolve(method, path) -> Array(controller, action, params())
  64. '------------------------------------------------------------
  65. Public Function Resolve(method, path)
  66. Dim routeKey, routeValue, values
  67. routeKey = UCase(method) & ":" & LCase(path)
  68. ' Always return a params array (empty by default)
  69. Dim emptyParams() : ReDim emptyParams(-1)
  70. ' Exact match first
  71. If routes.Exists(routeKey) Then
  72. routeValue = routes(routeKey)
  73. Resolve = Array(routeValue(0), routeValue(1), emptyParams)
  74. Exit Function
  75. End If
  76. ' Dynamic routes (e.g. /users/:id)
  77. Dim r, routeMethod, routePattern
  78. For Each r In routes.Keys
  79. routeMethod = Split(r, ":")(0)
  80. routePattern = Mid(r, Len(routeMethod) + 2) ' strip "METHOD:"
  81. If UCase(routeMethod) = UCase(method) Then
  82. If IsMatch(path, routePattern, values) Then
  83. routeValue = routes(r)
  84. Resolve = Array(routeValue(0), routeValue(1), values)
  85. Exit Function
  86. End If
  87. End If
  88. Next
  89. ' 404 fallback
  90. Resolve = Array("ErrorController", "NotFound", emptyParams)
  91. End Function
  92. '------------------------------------------------------------
  93. ' INTERNAL IsMatch(requestPath, routePattern, values())
  94. ' Returns True/False and fills values() with parameters
  95. '------------------------------------------------------------
  96. Private Function IsMatch(requestPath, routePattern, values)
  97. Dim reqParts, routeParts, i, paramCount
  98. reqParts = Split(requestPath, "/")
  99. routeParts = Split(routePattern, "/")
  100. If UBound(reqParts) <> UBound(routeParts) Then
  101. IsMatch = False : Exit Function
  102. End If
  103. paramCount = 0 : ReDim values(-1)
  104. For i = 0 To UBound(reqParts)
  105. If Left(routeParts(i), 1) = ":" Then
  106. ReDim Preserve values(paramCount)
  107. values(paramCount) = reqParts(i)
  108. paramCount = paramCount + 1
  109. ElseIf LCase(routeParts(i)) <> LCase(reqParts(i)) Then
  110. IsMatch = False : Exit Function
  111. End If
  112. Next
  113. If paramCount = 0 Then ReDim values(-1)
  114. IsMatch = True
  115. End Function
  116. '------------------------------------------------------------
  117. ' Optional lifecycle hooks
  118. '------------------------------------------------------------
  119. Private Sub Class_Terminate()
  120. Set routes = Nothing
  121. End Sub
  122. ]]>
  123. </script>
  124. </component>

Powered by TurnKey Linux.