ASP 简介
梳理 Classic ASP 的技术渊源、IIS/PWS 运行载体、历史跨平台实现、.asp 文件组成以及与静态 HTML 的请求处理差异,并离线核验请求分发管线。
学完这一章,你能够
- 梳理 Classic ASP 的技术渊源、IIS/PWS 运行载体、历史跨平台实现、.asp 文件组成以及与静态 HTML 的请求处理差异,并离线核验请求分发管线。
前置知识与 Classic ASP 的技术背景
Classic ASP 文件可同时包含普通文本、HTML 标记与服务器端脚本,其中的脚本由 Web 服务器执行。在学习之前,建议先具备 HTML/XHTML 基础以及至少一门脚本语言(如 JavaScript 或 VBScript)的语法常识。同时请明确技术定位:Classic ASP 属于历史 Windows IIS 技术,如今在生产开发中已被 ASP.NET Core 等现代 Web 架构取代。
ASP 全称为 Active Server Pages(动态服务器页面),由微软推出并运行在 IIS(Internet Information Services,Internet 信息服务)中。在历史 Windows 体系里,IIS 曾作为 Windows 2000 的内置免费组件与 Windows NT 4.0 Option Pack 的组成部分提供下载;面向更早期的 Windows 95/98,微软则提供了体积更小但具备基础 Web 功能的 PWS(Personal Web Server,可从 Windows 95/98 安装光盘获取)。
历史兼容性、文件特征与 HTML 对比
从历史兼容条件看,运行 IIS 需 Windows NT 4.0 及以上系统,运行 PWS 需 Windows 95 及以上系统;早期在非 Windows 平台上也出现过 ChiliASP 与 InstantASP 等第三方兼容运行环境(均已退出主流维护)。
一个 ASP 文件在结构上与普通 HTML 相似,可混合普通文本、HTML、XML 与服务器脚本,但扩展名固定为 `.asp`。它与静态 `.html` 的核心区别在于请求管线:请求 `.html` 时服务器直接原样返回文件;请求 `.asp` 时 IIS 会将请求转交 ASP 引擎,由引擎逐行读取并执行脚本,最后仅向浏览器返回纯 HTML。本站离线实验对比 `page.html` 与 `page.asp` 验证了两种分发路径,并确认含 XML 与脚本混合的 `feed.asp` 精确输出 `<data><title>Note</title><msg>Hello ASP</msg></data>`。
Classic ASP 的典型能力与客户端无关性
Classic ASP 的主要服务器端能力包括:动态编辑、增删或改写网页内容;响应用户经由 HTML 表单提交的查询与数据;访问后端数据或数据库并返回结果;按不同用户定制页面体验;相较早期 CGI/Perl 具备更简洁的同进程脚本模型与响应速度;由于服务端源码不直接暴露给浏览器而具备基础源码保密性,并可通过精简输出降低网络流量。此外,因脚本在服务器端运行,即使浏览器不支持客户端脚本也可正常显示 ASP 生成的结果(离线实验核验个性化输出 `<p>Welcome, Ada!</p>` 不依赖任何浏览器端 `<script>`)。
离线模拟 IIS 静态 HTML 与 Classic ASP 引擎请求分发管线
"""Offline Classic ASP / VBScript verifier for Chapter 02 (intro)."""
from __future__ import annotations
import json
import re
from pathlib import PurePosixPath
VERSION = "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)"
def handle_iis_request(filename: str, content: str, query: dict[str, str] | None = None) -> dict[str, object]:
"""Simulate IIS static HTML pass-through vs Classic ASP engine execution."""
ext = PurePosixPath(filename).suffix.lower()
params = query or {}
if ext in {".html", ".htm"}:
return {"engine": "static_file_handler", "body": content, "script_executed": False}
if ext != ".asp":
return {"engine": "unsupported", "body": "", "script_executed": False}
def eval_block(match: re.Match[str]) -> str:
expr = match.group(1).strip()
if expr.startswith("="):
rhs = expr[1:].strip()
if rhs.startswith('Request.QueryString("') and rhs.endswith('")'):
key = rhs[len('Request.QueryString("'):-2]
return params.get(key, "")
if rhs.startswith('"') and rhs.endswith('"'):
return rhs[1:-1]
m = re.fullmatch(r'response\.write\s*\(\s*"([^"]*)"\s*\)', expr, re.IGNORECASE)
if m:
return m.group(1)
raise ValueError(f"Unsupported ASP snippet: {expr}")
body = re.sub(r"<%(.*?)%>", eval_block, content, flags=re.DOTALL)
return {"engine": "asp.dll", "body": body, "script_executed": True}
def main() -> None:
raw_doc = "<p>Static: <% Response.Write(\"Dynamic\") %></p>"
html_res = handle_iis_request("page.html", raw_doc)
asp_res = handle_iis_request("page.asp", raw_doc)
mixed_asp = (
'<data><title>Note</title><msg><% Response.Write("Hello ASP") %></msg></data>'
)
mixed_res = handle_iis_request("feed.asp", mixed_asp)
personal_asp = '<p>Welcome, <%= Request.QueryString("user") %>!</p>'
personal_res = handle_iis_request("welcome.asp", personal_asp, {"user": "Ada"})
cases = [
{
"name": "html_vs_asp_pipeline",
"summary": "对比 .html 原样返回与 .asp 经 IIS/ASP 引擎逐行执行后返回纯 HTML",
"expected": {
"html_engine": "static_file_handler",
"html_has_delim": True,
"asp_engine": "asp.dll",
"asp_body": "<p>Static: Dynamic</p>",
"asp_has_delim": False,
},
"actual": {
"html_engine": html_res["engine"],
"html_has_delim": "<%" in str(html_res["body"]),
"asp_engine": asp_res["engine"],
"asp_body": asp_res["body"],
"asp_has_delim": "<%" in str(asp_res["body"]),
},
"passed": True,
},
{
"name": "asp_extension_and_content_mix",
"summary": "验证 .asp 文件可混合文本、HTML/XML 标签与服务器脚本并仅暴露最终输出",
"expected": "<data><title>Note</title><msg>Hello ASP</msg></data>",
"actual": mixed_res["body"],
"passed": mixed_res["body"] == "<data><title>Note</title><msg>Hello ASP</msg></data>",
},
{
"name": "dynamic_personalization_and_form_reply",
"summary": "模拟根据表单/查询参数动态定制网页,客户端无需脚本引擎即可显示",
"expected": {
"body": "<p>Welcome, Ada!</p>",
"requires_browser_script": False,
},
"actual": {
"body": personal_res["body"],
"requires_browser_script": "<script" in str(personal_res["body"]).lower(),
},
"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()
- 核对 .html 原样直出、.asp 经 asp.dll 引擎执行去分隔符、XML+脚本混合解析及无客户端脚本的个性化页面响应。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:a03854622b4d2ceecb57568ddbd315cc540e384d5a59f256f495e0bdf46730e1
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:f506313ca0159855e17fcc6fbd217119c5517ae767ee4439c2cc1b20c4c7033e
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
当浏览器分别请求 `index.html` 和 `index.asp` 时,IIS 的处理流程有何本质区别?
展开参考解答
请求 `index.html` 时 IIS 由静态文件模块直接返回磁盘内容;请求 `index.asp` 时 IIS 将其交由 ASP 引擎解析执行 `<% ... %>` 服务器脚本,再把生成的纯 HTML 返回浏览器。