|
- <?xml version="1.0"?>
- <?component error="true" debug="false"?>
- <component>
- <registration
- description="WscMvc explicit route table"
- progid="WscMvc.Router"
- version="1.00"
- classid="{C92F9338-B478-4EAD-B865-892FFB1E1C51}">
- </registration>
-
- <public>
- <method name="Match">
- <parameter name="ctx"/>
- <parameter name="applicationName"/>
- <parameter name="statusLine"/>
- <parameter name="contentType"/>
- <parameter name="body"/>
- <parameter name="allowHeader"/>
- <parameter name="handlerKey"/>
- </method>
- </public>
-
- <script language="VBScript">
- <![CDATA[
- Option Explicit
-
- ' Explicit allowlisted route table. This component decides only route
- ' metadata; Application.wsc performs the corresponding hardcoded handler
- ' calls. No URL segment is ever treated as a ProgID or method name.
- Sub Match(ctx, applicationName, statusLine, contentType, body, allowHeader, handlerKey)
- Dim rawPath, normalizedPath, httpMethod
-
- statusLine = ""
- contentType = ""
- body = ""
- allowHeader = ""
- handlerKey = ""
-
- rawPath = ctx.Path
- httpMethod = UCase(Trim(CStr(ctx.HttpMethod)))
-
- If Not TryNormalizePath(rawPath, normalizedPath) Then
- BadRequest statusLine, contentType, body
- Exit Sub
- End If
-
- ' Literal routes are intentionally listed directly. Parameterized routes,
- ' when added, must be checked after these literal comparisons.
- If applicationName = "production" Then
- If normalizedPath = "/hello" Then
- MatchMethod httpMethod, "GET", "Home.Hello", statusLine, contentType, body, allowHeader, handlerKey
- Exit Sub
- End If
- ElseIf applicationName = "tests" Then
- If normalizedPath = "/self-test" Then
- MatchMethod httpMethod, "GET, POST", "SelfTest.RunSelfTest", statusLine, contentType, body, allowHeader, handlerKey
- Exit Sub
- End If
- End If
-
- statusLine = "404 Not Found"
- contentType = "text/plain; charset=utf-8"
- body = "Not Found"
- End Sub
-
- Function TryNormalizePath(rawPath, normalizedPath)
- Dim path
-
- TryNormalizePath = False
- normalizedPath = ""
-
- If IsNull(rawPath) Or IsEmpty(rawPath) Then
- path = "/"
- Else
- path = Trim(CStr(rawPath))
- If Len(path) = 0 Then path = "/"
- End If
-
- If Left(path, 1) <> "/" Then Exit Function
- If InStr(path, "\") > 0 Then Exit Function
- If InStr(path, "%") > 0 Then Exit Function
- If InStr(path, "?") > 0 Then Exit Function
- If InStr(path, "#") > 0 Then Exit Function
- If InStr(path, "//") > 0 Then Exit Function
- If InStr(path, "/./") > 0 Then Exit Function
- If InStr(path, "/../") > 0 Then Exit Function
- If Right(path, 2) = "/." Then Exit Function
- If Right(path, 3) = "/.." Then Exit Function
- If ContainsControlCharacter(path) Then Exit Function
-
- If Len(path) > 1 And Right(path, 1) = "/" Then
- path = Left(path, Len(path) - 1)
- End If
-
- normalizedPath = LCase(path)
- TryNormalizePath = True
- End Function
-
- Function ContainsControlCharacter(value)
- Dim i, code
- ContainsControlCharacter = False
- For i = 1 To Len(value)
- code = Asc(Mid(value, i, 1))
- If code < 32 Or code = 127 Then
- ContainsControlCharacter = True
- Exit Function
- End If
- Next
- End Function
-
- Sub MatchMethod(httpMethod, allowedMethods, matchedHandlerKey, statusLine, contentType, body, allowHeader, handlerKey)
- If IsAllowedMethod(httpMethod, allowedMethods) Then
- handlerKey = matchedHandlerKey
- Else
- statusLine = "405 Method Not Allowed"
- contentType = "text/plain; charset=utf-8"
- body = "Method Not Allowed"
- allowHeader = allowedMethods
- End If
- End Sub
-
- Function IsAllowedMethod(httpMethod, allowedMethods)
- Dim methods, i
- methods = Split(allowedMethods, ",")
- IsAllowedMethod = False
- For i = 0 To UBound(methods)
- If httpMethod = Trim(methods(i)) Then
- IsAllowedMethod = True
- Exit Function
- End If
- Next
- End Function
-
- Sub BadRequest(statusLine, contentType, body)
- statusLine = "400 Bad Request"
- contentType = "text/plain; charset=utf-8"
- body = "Bad Request"
- End Sub
- ]]>
- </script>
- </component>
|