蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 .htm/.asp/.aspx 请求路由与 <% Response.Write(now()) %> 动态输出

PYTHONWindows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。explore-aspnet-pages.py
explore-aspnet-pages.py156 行
"""Offline ASP.NET WebForms verifier for Chapter 38 (pages)."""
from __future__ import annotations

import json
import re

VERSION = "CPython 3.12.14 offline ASP.NET WebForms verifier (IIS/ASP.NET runtime absent)"
FIXED_SERVER_NOW = "2026-12-01 09:30:00"


def route_request(filename: str, source: str, server_now: str = FIXED_SERVER_NOW) -> dict[str, object]:
    """Simulate IIS extension routing for .htm, .asp, and .aspx files."""
    lower = filename.lower()
    if lower.endswith((".htm", ".html")):
        return {
            "handler": "StaticFileModule",
            "executed_code": False,
            "body": source,
        }
    if lower.endswith(".asp"):
        handler = "ClassicAspEngine"
    elif lower.endswith(".aspx"):
        handler = "AspNetPageHandler"
    else:
        raise ValueError(f"Unsupported extension: {filename}")

    executed = False

    def eval_tag(match: re.Match[str]) -> str:
        nonlocal executed
        stmt = match.group(1).strip()
        if re.fullmatch(r"Response\.Write\s*\(\s*now\(\)\s*\)", stmt, flags=re.IGNORECASE):
            executed = True
            return server_now
        raise ValueError(f"Unsupported server expression: {stmt}")

    rendered = re.sub(r"<%(.*?)%>", eval_tag, source, flags=re.DOTALL)
    return {
        "handler": handler,
        "executed_code": executed,
        "body": rendered,
    }


def main() -> None:
    static_markup = (
        '<html>\n<body bgcolor="yellow">\n<center>\n'
        "<h2>Hello BlueSky!</h2>\n"
        "</center>\n</body>\n</html>"
    )
    dynamic_markup = (
        '<html>\n<body bgcolor="yellow">\n<center>\n'
        "<h2>Hello BlueSky!</h2>\n"
        "<p><%Response.Write(now())%></p>\n"
        "</center>\n</body>\n</html>"
    )
    expected_dynamic_body = (
        '<html>\n<body bgcolor="yellow">\n<center>\n'
        "<h2>Hello BlueSky!</h2>\n"
        f"<p>{FIXED_SERVER_NOW}</p>\n"
        "</center>\n</body>\n</html>"
    )

    res_htm = route_request("firstpage.htm", static_markup)
    res_static_aspx = route_request("firstpage.aspx", static_markup)
    res_dyn_asp = route_request("dynpage.asp", dynamic_markup)
    res_dyn_aspx = route_request("dynpage.aspx", dynamic_markup)

    cases = [
        {
            "name": "static_html_vs_aspx_page",
            "summary": "验证无服务端代码的 firstpage.htm 与 firstpage.aspx 返回相同 HTML,但处理管线不同",
            "expected": {
                "same_body": True,
                "htm_handler": "StaticFileModule",
                "aspx_handler": "AspNetPageHandler",
                "aspx_executed_code": False,
            },
            "actual": {
                "same_body": res_htm["body"] == res_static_aspx["body"],
                "htm_handler": res_htm["handler"],
                "aspx_handler": res_static_aspx["handler"],
                "aspx_executed_code": res_static_aspx["executed_code"],
            },
            "passed": (
                res_htm["body"] == res_static_aspx["body"]
                and res_htm["handler"] == "StaticFileModule"
                and res_static_aspx["handler"] == "AspNetPageHandler"
                and res_static_aspx["executed_code"] is False
            ),
        },
        {
            "name": "classic_asp_dynamic_response_write",
            "summary": "模拟经典 ASP(dynpage.asp)执行 <%Response.Write(now())%> 并移除服务端脚本标记",
            "expected": {
                "handler": "ClassicAspEngine",
                "executed_code": True,
                "has_server_delim": False,
                "body": expected_dynamic_body,
            },
            "actual": {
                "handler": res_dyn_asp["handler"],
                "executed_code": res_dyn_asp["executed_code"],
                "has_server_delim": "<%" in str(res_dyn_asp["body"]),
                "body": res_dyn_asp["body"],
            },
            "passed": (
                res_dyn_asp["handler"] == "ClassicAspEngine"
                and res_dyn_asp["executed_code"] is True
                and res_dyn_asp["body"] == expected_dynamic_body
            ),
        },
        {
            "name": "aspx_dynamic_response_write",
            "summary": "模拟 ASP.NET(dynpage.aspx)执行同构 <%Response.Write(now())%>,核对与经典 ASP 输出一致",
            "expected": {
                "handler": "AspNetPageHandler",
                "executed_code": True,
                "same_as_classic_asp": True,
                "body": expected_dynamic_body,
            },
            "actual": {
                "handler": res_dyn_aspx["handler"],
                "executed_code": res_dyn_aspx["executed_code"],
                "same_as_classic_asp": res_dyn_aspx["body"] == res_dyn_asp["body"],
                "body": res_dyn_aspx["body"],
            },
            "passed": (
                res_dyn_aspx["handler"] == "AspNetPageHandler"
                and res_dyn_aspx["executed_code"] is True
                and res_dyn_aspx["body"] == res_dyn_asp["body"] == expected_dynamic_body
            ),
        },
        {
            "name": "request_extension_routing",
            "summary": "确认 .htm 静态文件不会执行内嵌 <%...%> 代码块,必须改用 .asp/.aspx 才会触发服务端执行",
            "expected": {
                "htm_keeps_literal_tag": True,
                "htm_executed_code": False,
            },
            "actual": {
                "htm_keeps_literal_tag": "<%Response.Write(now())%>" in str(route_request("raw.htm", dynamic_markup)["body"]),
                "htm_executed_code": route_request("raw.htm", dynamic_markup)["executed_code"],
            },
            "passed": (
                "<%Response.Write(now())%>" in str(route_request("raw.htm", dynamic_markup)["body"])
                and route_request("raw.htm", dynamic_markup)["executed_code"] is False
            ),
        },
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。

入口文件:projects/web-pages-aspnet-pages-lab/explore-aspnet-pages.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-aspnet-pages.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Web Pages

课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a3037.zip

下载课程关联工程包:Web Pages

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3030.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习