"""Offline Classic ASP / VBScript verifier for Chapter 12 (globalasa)."""
from __future__ import annotations
import json
import re
VERSION = "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)"
EVENT_ALLOWED_OBJECTS = {
"Application_OnStart": {"Server", "Application"},
"Application_OnEnd": {"Server", "Application"},
"Session_OnStart": {"Server", "Application", "Session", "Request", "Response"},
"Session_OnEnd": {"Server", "Application", "Session"},
}
TYPELIB_ERRORS = {
"ASP 0222": "Invalid type library specification",
"ASP 0223": "Type library not found",
"ASP 0224": "Type library cannot be loaded",
"ASP 0225": "Type library cannot be wrapped",
}
def validate_global_asa(source: str) -> dict[str, object]:
if "<%" in source or "%>" in source:
return {"valid": False, "error": "Global.asa cannot use <% %> script delimiters"}
script_blocks = re.findall(
r'<script[^>]*runat\s*=\s*"server"[^>]*>(.*?)</script>',
source,
flags=re.IGNORECASE | re.DOTALL,
)
for blk in script_blocks:
if re.search(r"<object\b", blk, re.IGNORECASE):
return {"valid": False, "error": "<object> must be declared outside <script>"}
objects: list[dict[str, str]] = []
for tag in re.findall(r"<object\b([^>]*)>", source, flags=re.IGNORECASE):
attrs = {k.lower(): v for k, v in re.findall(r'(\w+)\s*=\s*"([^"]*)"', tag)}
if "progid" not in attrs and "classid" not in attrs:
return {"valid": False, "error": "ProgID or ClassID must be specified on <object>"}
objects.append(attrs)
events = re.findall(
r"\bsub\s+(Application_OnStart|Application_OnEnd|Session_OnStart|Session_OnEnd)\b",
source,
flags=re.IGNORECASE,
)
return {"valid": True, "objects": objects, "events": events}
def validate_typelib_metadata(tag: str) -> dict[str, object]:
attrs = {k.lower(): v for k, v in re.findall(r'(\w+)\s*=\s*"([^"]*)"', tag)}
if attrs.get("type", "").lower() != "typelib":
return {"ok": False, "code": "ASP 0222"}
if not attrs.get("file") and not attrs.get("uuid"):
return {"ok": False, "code": "ASP 0222"}
if attrs.get("file") == "missing.dll":
return {"ok": False, "code": "ASP 0223"}
return {"ok": True, "attrs": attrs}
def main() -> None:
sample_global = """
<!--METADATA TYPE="TypeLib" uuid="{00000205-0000-0010-8000-00AA006D2EA4}" version="2.5" lcid="0"-->
<object runat="server" scope="session" id="MyAd" progid="MSWC.AdRotator"></object>
<object runat="server" scope="application" id="MyConnection" classid="Clsid:8AD3067A-B3FC-11CF-A560-00A0C9081C21"></object>
<script language="vbscript" runat="server">
Sub Application_OnStart
Application("visitors") = 0
End Sub
Sub Session_OnStart
Application.Lock
Application("visitors") = Application("visitors") + 1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors") = Application("visitors") - 1
Application.UnLock
End Sub
</script>
"""
parsed = validate_global_asa(sample_global)
bad_delim = validate_global_asa("<% Application(\"x\")=1 %>")
bad_obj_inside = validate_global_asa('<script runat="server"><object id="x" progid="A.B"></object></script>')
tl_ok = validate_typelib_metadata(
'<!--METADATA TYPE="TypeLib" uuid="{00000205-0000-0010-8000-00AA006D2EA4}" version="2.5" lcid="0"-->'
)
tl_missing_key = validate_typelib_metadata('<!--METADATA TYPE="TypeLib" version="2.5"-->')
tl_not_found = validate_typelib_metadata('<!--METADATA TYPE="TypeLib" file="missing.dll"-->')
# Simulate visitor counter lifecycle + helper sub
app_state: dict[str, object] = {}
app_state["visitors"] = 0
app_state["customers"] = ["Alfreds Futterkiste", "Around the Horn"]
sessions: dict[str, dict[str, str]] = {}
for sid in ("s1", "s2", "s3"):
sessions[sid] = {"started": "2026-12-01T00:00:00Z"}
app_state["visitors"] = int(app_state["visitors"]) + 1
sessions.pop("s2")
app_state["visitors"] = int(app_state["visitors"]) - 1
rendered = f"<p>There are {app_state['visitors']} online now!</p>"
cases = [
{
"name": "globalasa_structure_and_object_scope",
"summary": "解析 Global.asa 的 <script runat='server'> 与外部 <object> 声明并拦截 <% %> 或嵌套 <object>",
"expected": {
"valid": True,
"object_ids": ["MyAd", "MyConnection"],
"events": ["Application_OnStart", "Session_OnStart", "Session_OnEnd"],
"delim_rejected": True,
"nested_object_rejected": True,
},
"actual": {
"valid": parsed["valid"],
"object_ids": [o["id"] for o in parsed["objects"]],
"events": parsed["events"],
"delim_rejected": not bad_delim["valid"],
"nested_object_rejected": not bad_obj_inside["valid"],
},
"passed": True,
},
{
"name": "typelib_metadata_and_error_codes",
"summary": "核对 METADATA TypeLib 的 file/uuid 必选约束与 ASP 0222–0225 错误代码映射",
"expected": {
"valid_ok": True,
"missing_file_uuid_code": "ASP 0222",
"not_found_code": "ASP 0223",
"known_error_codes": ["ASP 0222", "ASP 0223", "ASP 0224", "ASP 0225"],
},
"actual": {
"valid_ok": tl_ok["ok"],
"missing_file_uuid_code": tl_missing_key["code"],
"not_found_code": tl_not_found["code"],
"known_error_codes": sorted(TYPELIB_ERRORS.keys()),
},
"passed": True,
},
{
"name": "event_object_access_restrictions",
"summary": "核对四类 Global.asa 事件子例程各自允许访问的 ASP 内建对象集合",
"expected": {
"app_onstart_has_response": False,
"session_onend_has_response": False,
"session_onstart_has_response": True,
"session_onend_count": 3,
},
"actual": {
"app_onstart_has_response": "Response" in EVENT_ALLOWED_OBJECTS["Application_OnStart"],
"session_onend_has_response": "Response" in EVENT_ALLOWED_OBJECTS["Session_OnEnd"],
"session_onstart_has_response": "Response" in EVENT_ALLOWED_OBJECTS["Session_OnStart"],
"session_onend_count": len(EVENT_ALLOWED_OBJECTS["Session_OnEnd"]),
},
"passed": True,
},
{
"name": "online_visitors_counter_lifecycle",
"summary": "模拟 Application_OnStart 初始化、3 次 Session_OnStart 与 1 次 Session_OnEnd 后的在线计数",
"expected": {
"visitors": 2,
"customers_cached": 2,
"html": "<p>There are 2 online now!</p>",
},
"actual": {
"visitors": app_state["visitors"],
"customers_cached": len(app_state["customers"]),
"html": rendered,
},
"passed": True,
},
]
for c in cases:
c["passed"] = c["actual"] == c["expected"]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/asp-globalasa-lab/explore-globalasa.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-globalasa.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a1425.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1431.zip