# Reference Everything on one page, for looking things up. Each entry links to the page that explains it. ## What is promised From 1.0, Cylinder follows [semantic versioning](https://semver.org/). What this page lists is what it promises: a release that changes any of it in a way that an app could notice has a new major number. Every other name in `cylinder.py` is part of how Cylinder works, and can change in any release. So can the words of Cylinder's own log lines and error pages. Adding a [parameter](#handler-parameters) that Cylinder passes is a change too. No extra or placeholder may have the name of one, so a new parameter could take a name that an app already uses. A parameter is added only in a release with a new major number. The `request` and `response` that a handler is passed are Werkzeug's own, and what they offer is Werkzeug's to change. A Werkzeug with a new minor number can take away what an earlier one deprecated. A release of Cylinder that allows such a Werkzeug has a new minor number, and its changelog says so. Werkzeug warns about a call for a release or more before it takes the call away. See [Requirements](#requirements). A release that needs a newer Python or a newer Werkzeug than the one before it has a new minor number too, and its changelog says so. pip does not install a release where what it needs is missing, so no app that is installed stops working for it. ## Filenames A webapp named `webapp1` in the directory `my_webapps` consists of the directory `my_webapps/webapp1` and the files beside it whose names start with `webapp1.` | Filename pattern | Example | What it is | | ------------------------- | --------------------- | -------------------------------------------------------------- | | `.page..py` | `bar.page.get.py` | [Page handler](routing.md) for one HTTP method | | `.page.default.py` | `bar.page.default.py` | Page handler for any method without a file of its own | | `.tree..py` | `files.tree.get.py` | Page handler for its own path and [every path beneath][sub] | | `.tree.default.py` | `foo.tree.default.py` | The same, for any method without a file of its own | | `.hook.early.py` | `foo.hook.early.py` | [Early hook](hooks.md): runs before the page handler | | `.hook.late.py` | `foo.hook.late.py` | [Late hook](hooks.md): runs after the page handler | | `..py` | `webapp1.404.py` | [Error handler](errors.md#error-handler-files) for a status | | anything else | `logo.png` | A file, sent only [where a handler serves it](static-files.md) | [sub]: routing.md#handlers-for-a-whole-tree - `` is the last segment of the URL path, or the webapp's name for files at the root of the site. - A `` in braces, such as `{user_id}`, is a [placeholder](routing.md#placeholders): the file or directory answers for any one segment, and the segment is passed in as the parameter `user_id`. What is in the braces must be a Python identifier, and no other parameter may have its name. - `` is the HTTP method in lowercase. It must be alphabetic. `default` is reserved. - A `.page.` handler named `foo` serves `/foo` and nothing beneath it. A `.tree.` page handler named `foo` serves `/foo` and every path beneath it that has no closer match. - A hook named `foo` covers `/foo` and every path beneath it, together with the hooks above and below it. It is not named for a method, and runs [for every one](hooks.md#hooks-and-http-methods). - Every page handler and hook ends in three parts, of which the middle one says what the file is: `page`, `tree`, or `hook`. An error handler ends in its status. So a filename can be read in one way only. - An error handler named `foo` covers errors raised for `/foo` and every path beneath it. ## How a request is matched | Rule | Details | | ---------------------------------------------------------------------------- | ---------------------------- | | A deeper path wins over a shallower one | [Routing][match] | | At the same path, an exact method wins over `.default` | [Routing][match] | | For `HEAD` at the same path: `.head`, then `.get`, then `.default` | [HEAD requests][head] | | A `.page.` handler serves its own path; a `.tree.` one, those beneath too | [Dynamic paths][dynamic] | | At the same path and method, `.page.` wins over `.tree.` | [Routing][match] | | A name wins over a placeholder, and one placeholder is allowed per directory | [Placeholders][placeholder] | | A file is sent only where a page handler serves files | [Static files][static] | | Hidden paths, handler files, and copies of handler files are never sent | [What is never served][deny] | | Nothing is found through a symbolic link or a Windows junction | [What is never served][deny] | | Matching is case-sensitive on every operating system | [Routing](routing.md) | | A URL is served in one spelling: no stray slash, `.`, `..`, or backslash | [One spelling][spelling] | | Every hook from the site root to the URL runs, one of each kind per path | [Hooks][one] | | The most specific error handler for the status code runs | [Errors][scoped] | | No page at a URL gives `404`; none for the method, `405` with `Allow` | [Routing][nomatch] | [fmt]: operations.md#where-logs-go [partial]: static-files.md#caching-and-partial-downloads [match]: routing.md#how-requests-are-matched [dynamic]: routing.md#dynamic-paths [placeholder]: routing.md#placeholders [spelling]: routing.md#one-spelling-for-each-url [head]: routing.md#head-requests [static]: static-files.md [deny]: static-files.md#what-is-never-served [one]: hooks.md#every-hook-on-the-path-runs [scoped]: errors.md#different-error-pages-for-different-sections [nomatch]: routing.md#when-nothing-matches ## The order a request runs in 1. `app_map()` chooses the webapp and supplies extra parameters. 2. The early hooks run, from the site root down to the URL. 3. The page handler runs. If no page handler serves the request, a `404`, `405`, or `501` is raised here. 4. If anything raised an HTTP error, the matching error handler runs. An uncaught exception is a `500`, and its traceback is logged first. A redirect and `abort(response)` have no error handler. 5. The late hooks run, from the URL back up to the site root. 6. The response is sent. 7. Functions registered with `response.call_on_close()` run. A file is run from its first line when its turn comes, and not before. So none of the code in a page handler's file runs for a request that an early hook turned away. Each request runs a handler file as a new module, so nothing at the top of one is shared between requests: see [What belongs at the top of a handler file](handlers.md#what-belongs-at-the-top-of-a-handler-file). ## Handler parameters A handler, hook, or error handler is a function named `main()`. It receives the parameters it names. | Parameter | Value | | ------------ | -------------------------------------------------------------------------------------- | | `request` | The Werkzeug `Request` | | `response` | The Werkzeug `Response` to fill in and return | | `logger` | A logger whose records carry the request ID | | `abort` | `abort(status)`, `abort(status, description)`, or `abort(redirect_status, location)` | | `remainder` | List of URL path segments after the page handler's own path | | `e` | The exception being handled, or `None` | | `request_id` | The [ID of the request](operations.md#request-ids) that its log records carry, a `str` | | placeholder | The URL segment that a placeholder on the path stands for, as a `str` | | extras | Any key of the dictionary returned as the third value from `app_map()` | - Parameters are matched by name, in any order. None is required. A parameter after a `*` is passed by name, and `*args` and `**kwargs` are passed nothing. - `abort(response)` [ends the request with the response](errors.md#ending-a-request-with-the-response-itself) as it is: no error handler runs for it. - `abort()` takes a status that Werkzeug has a class for, a redirect, or a status added with [`abort_extra`](errors.md#custom-status-codes). Anything else is a `LookupError`, which becomes a `500`. - The argument after the status is the URL of a redirect, and the description of any other status, whatever its class takes first. Anything else that a class takes, such as the `valid_methods` of a `405`, is passed by name. - An error or a redirect keeps the headers and cookies that were set before it, except the ones that describe the body that it replaces: see [What survives an abort](errors.md#what-survives-an-abort). - No parameter affects routing. `remainder` is `[]` for a `.page.` handler. - Asking for a name that is not available raises `TypeError`, unless the parameter has a default value. - `app_map()` can ask for `request`, `response`, `logger`, `abort`, and `request_id`. - Only a page handler can [read the request body](handlers.md#only-a-page-handler-reads-the-request-body). `app_map()`, a hook, or an error handler has to set `request.shallow = False` first. See [Handlers](handlers.md#handler-parameters). ## Return values | A handler returns | Result | | --------------------------- | -------------------------------------------------------- | | The `response` it was given | Sent as it is | | A `str` or `bytes` | Becomes the body of the `response`; everything else kept | | Anything else | `ValueError`, which becomes a `500` | `app_map()` returns `(site_dir, site_name)` or `(site_dir, site_name, extras)`, as a tuple or a list. Anything else is a `ValueError`, which becomes a `500`. Its line in the log says what `app_map()` must return, and the types of what it returned. - `site_dir` is the directory that holds the webapps: an absolute path, or one relative to the working directory. A `str` or a `pathlib.Path`. One that is not a directory is a `ValueError`, which becomes a `500`, and its line in the log names the path. - `site_name` is the name of the webapp: one name, and not a path. It is looked up in `site_dir` as it is. One that names nothing there, such as a misspelled name or a path, is a `ValueError`, which becomes a `500`, and its line in the log names it. The directories on the way to the webapp go into `site_dir`. - `extras` is a dictionary of [extra parameters](composition.md#extra-parameters-on-main), or `None`. ## get_app options ```python cylinder.get_app( app_map, *, log_level=logging.DEBUG, log_handler=None, request_id_header=None, abort_extra=None, ) ``` | Option | Default | Purpose | | ------------------- | ---------------- | ----------------------------------------------------------------- | | `app_map` | required | Function that chooses the webapp for each request | | `log_level` | `logging.DEBUG` | [Level](operations.md#where-logs-go) of the app's logger | | `log_handler` | stream to stderr | Any `logging.Handler`, which [keeps a formatter of its own][fmt] | | `request_id_header` | `None` | [Header](operations.md#request-ids) that carries the request ID | | `abort_extra` | `None` | [Extra status codes](errors.md#custom-status-codes) for `abort()` | Every option but `app_map` is passed by name. Every app logs through the logger named `cylinder`. Its records carry two fields beside the standard ones, which a formatter can use: `request_id` and `timestamp`. See [Where logs go][fmt]. `get_app()` returns the WSGI application. It has these attributes: | Attribute | Purpose | | ------------------- | -------------------------------------------- | | `app.test_client()` | Returns a Werkzeug [test client](testing.md) | | `app.logger` | The app's logger | ## The file server `cylinder.static_file(request, response, abort)` is a page handler that sends the file at the path of the URL, or aborts with a `404` where there is none to send. A handler file uses it as its `main()`, with the one line `from cylinder import static_file as main`, or calls it from a `main()` of its own. See [Static files](static-files.md). ## Status codes Cylinder produces itself | Status | When | | -------------------------- | --------------------------------------------------------------------------- | | `500` | An uncaught exception, an invalid return value, or an unavailable parameter | | `500` | Two placeholders in one directory, or one with the name of a parameter | | `500` | An extra from `app_map()` with the name of a built-in parameter | | `500` | A `site_dir` from `app_map()` that is not a directory | | `500` | A `site_name` from `app_map()` that names nothing in `site_dir` | | `500` | `app_map()`, a hook, or an error handler reads the body without opting in | | `500` | A response that [cannot be sent](errors.md#cylinder-always-answers) | | `404` | The URL has no page handler for any method, or `static_file` has no file | | `405` | The URL has one, but not for the method of the request | | `501` | An HTTP method that is not alphabetic, or the reserved method `DEFAULT` | | `304`, `206`, `416`, `412` | `static_file`, for a [conditional or a range request][partial] | | others | Whatever a handler passes to `abort()` | Redirect statuses supported by `abort()`: `301`, `302`, `303`, `307`, `308`. Each needs the URL to send the client to, as a `str`: without one, with one that Werkzeug cannot read, such as `//[`, or with a line break in it, the redirect is a `500`. ## Requirements - Python 3.11 or newer - [Werkzeug](https://werkzeug.palletsprojects.com/) 3.1.7 or newer, and older than 3.2, which is installed with Cylinder - A WSGI server of your choice