Artifact 5ce43cafefd1ad906411313b7cbc1c0d301c2c88587e1082599bbcb118361979:
- File docs/urlmapping.md — part of check-in [c0bf5ea896] at 2024-04-15 10:28:22 on branch w3 — Rebrand as "W3" (user: drh size: 2693)
URL Mapping In W3
1.0 Anatomy Of A URL
A Uniform Resource Locator (URL) is divided into parts as follows:
https://w3.tcl.tk/demo/env.tcl/abc/def/ghi?a=5&b=22.425#point42 \___/ \_______/\_______________________/ \__________/ \_____/ | | | | | scheme authority path query fragment
Assuming that /demo/env.tcl is the script that implements the application, traditional CGI and SCGI provide the following breakdown:
https://w3.tcl.tk/demo/env.tcl/abc/def/ghi?a=5&b=22.425#point42 \_______/\___________/\__________/ \__________/ | | | | HTTP_HOST SCRIPT_NAME PATH_INFO QUERY_STRING
W3 provides additional variables not found in traditional CGI:
SELF_URL ______________|___________________ / \ https://w3.tcl.tk/demo/env.tcl/abc/def/ghi?a=5&b=22.425#point42 \____________________________/ \_/ \_____/ | | | BASE_URL PATH_HEAD '-- PATH_TAIL
2.0 URL Mapping
The URL Mapper is the function that determines which routine in the application should handle an HTTP request based on the URL. In W3, the default URL Mapper simply looks at PATH_HEAD and invokes the application-defined proc name "w3-page-$PATH_HEAD". If no such proc exists, then W3 invokes the application-defined proc "w3-default".
2.1 Customizing The URL Mapper
Just prior to dispatch of the HTTP request handler, W3 invokes a proc named "w3-before-dispatch-hook". This proc is normally a no-op. But, applications can redefine the "w3-before-dispatch-hook" proc to make modifications to the environment prior to dispatch. So, for example, a custom w3-before-dispatch-hook function can change the value of the PATH_HEAD parameter to cause a different request handler to be invoked.
The checklist application does this. See these lines for the implementation. If the original PATH_HEAD is really the name of a checklist database, then that name is moved to a new parameter called OBJECT, and PATH_HEAD is shifted to be the next element of PATH_TAIL. In this way, the PATH_INFO for checklist is parsed into OBJECT/METHOD rather than just a METHOD.
This is but one example. Applications can make creative use of the "w3-before-dispatch-hook" to make whatever changes are appropriate for the task at hand.