# Module /web/app/middleware

Production HTTP middlewares: CSRF, CORS, session, rate limiting, security headers, auth, tracing, and scripts injection.

```tangu
(use /web/app/middleware)
```

## Constants

### *scripts-accumulator*

`Constant (Any)`

Dynamic accumulator collecting ES module script specifiers during SSR or Stage rendering.

### *scripts-seen*

`Constant (Any)`

Dynamic deduplication set tracking script specifiers already emitted for the current response.

### default-scripts-insertion-marker

`Constant (Any)`

Default HTML placeholder comment replaced with injected ES module `<script type="module">` tags.

## Functions & symbols

### allow-methods-middleware

`(allow-methods-middleware allowed-methods next-handler)`

Rejects methods outside allowlist with 405 and Allow header.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `allowed-methods` | `Any` | no | — |
| `next-handler` | `Any` | no | — |

### basic-auth-middleware

`(basic-auth-middleware ...args)`

Enforces HTTP Basic authentication using `verify-fn` (supports curried `(basic-auth-middleware verify-fn)` and direct forms).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### bearer-auth-middleware

`(bearer-auth-middleware verify-fn next-handler)`

Authenticates API requests via `Authorization: Bearer <token>`.
Supports both `(bearer-auth-middleware verify-fn next-handler)` and
curried `((bearer-auth-middleware verify-fn) next-handler)` forms.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `verify-fn` | `Any` | no | — |
| `next-handler` | `Any` | no | — |

### body-size-limit-middleware

`(body-size-limit-middleware max-bytes next-handler)`

Limits request payload size and returns 413 when known size exceeds max-bytes.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `max-bytes` | `Any` | no | — |
| `next-handler` | `Any` | no | — |

### cors-middleware

`(cors-middleware ...args)`

Middleware applying CORS headers and handling preflight `OPTIONS` requests according to `options`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### csrf-middleware

`(csrf-middleware ...args)`

Middleware validating synchronizer/double-submit CSRF tokens on state-changing HTTP requests.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### current-form-of

`(current-form-of req)`

Extracts the validated form struct stored on `req:attrs:_form` by `validate-form-middleware`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `req` | `Any` | no | — |

### etag-middleware

`(etag-middleware inner-handler)`

Middleware that generates Weak ETags (W/"<16-hex-hash>") using a-hash (15+ GB/s) for GET/HEAD 200 OK responses.
Returns 304 Not Modified with zero body payload when the client If-None-Match header matches.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `inner-handler` | `Any` | no | — |

### hreflang-alternates

`(hreflang-alternates spec)`

Renders `#html` `<link rel="alternate" hreflang="..." href="...">` elements from `{:base-url :path :alternates :x-default}`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `spec` | `Any` | no | — |

### i18n-middleware

`(i18n-middleware ...args)`

Web middleware negotiating locale and binding `*locale*` / `*bundle*`.
Supports `(i18n-middleware next-handler)`, `(i18n-middleware options)` (curried), and `(i18n-middleware options next-handler)`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### inject-scripts-into-html

`(inject-scripts-into-html html scripts insertion-marker)`

Injects script imports at the configured marker or before </body>.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `html` | `Any` | no | — |
| `scripts` | `Any` | no | — |
| `insertion-marker` | `Any` | no | — |

### method-override-middleware

`(method-override-middleware ...args)`

Middleware that rewrites `POST` requests to `PUT`, `PATCH`, or `DELETE` via `X-HTTP-Method-Override` header or `_method` form field.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### mount-localized-routes

`(mount-localized-routes prefixes ...rest)`

Mounts a list of localized route prefixes into `/web/app` `:scope` blocks matching `router.tan`'s contract.
Usage: `(mount-localized-routes prefixes routes)` or `(mount-localized-routes prefixes middlewares routes)`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `prefixes` | `Any` | no | — |
| `rest` | `Any` | yes | — |

### negotiate-locale

`(negotiate-locale req ...rest)`

Negotiates the active BCP 47 locale tag for `req`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `req` | `Any` | no | — |
| `rest` | `Any` | yes | — |

### negotiate-locale-info

`(negotiate-locale-info req ...rest)`

Negotiates the active locale and returns `{:locale tag :source :path | :resolver | :cookie | :accept-language | :default}`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `req` | `Any` | no | — |
| `rest` | `Any` | yes | — |

### parse-accept-language

`(parse-accept-language header-str)`

Parses an RFC 4647 `Accept-Language` header into quality-sorted `{:tag :language :region :q}` entries.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `header-str` | `Any` | no | — |

### rate-limit-middleware

`(rate-limit-middleware ...args)`

Bounded in-memory fixed-window rate limiter middleware.
Supports `(rate-limit-middleware options next-handler)`, `(rate-limit-middleware next-handler)`,
and curried `((rate-limit-middleware options) next-handler)`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### request-id-middleware

`(request-id-middleware next-handler)`

Ensures request-id is available in attrs and returned in response headers.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `next-handler` | `Any` | no | — |

### scripts-middleware

`(scripts-middleware ...args)`

Middleware that accumulates `(use-script ...)` module specifiers and injects them into HTML or Stage slot responses.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### security-headers-middleware

`(security-headers-middleware ...args)`

Middleware injecting defensive HTTP security headers (CSP, X-Frame-Options, Referrer-Policy, X-Content-Type-Options).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `args` | `Any` | yes | — |

### session-middleware

`(session-middleware store options)`

Session middleware factory.
Creates middleware that manages sessions via cookies.
Parameters:
store   - Session store instance (from construct-session-store)
options - Configuration map:
:cookie-name - Cookie name (default: "SESSION-ID")
:max-age     - Session lifetime in seconds (default: 3600)
:secure      - Set Secure flag on cookie (default: false)
:same-site   - SameSite policy: "Strict", "Lax", or "None" (default: "Lax")
:path        - Cookie path (default: "/")
:domain      - Cookie domain (default: not set)

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `store` | `Any` | no | — |
| `options` | `Any` | no | — |

### static-files-middleware

`(static-files-middleware next-handler)`

Middleware serving static files from `./public` before falling back to `next-handler`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `next-handler` | `Any` | no | — |

### traceparent-middleware

`(traceparent-middleware next-handler)`

Middleware propagating W3C `traceparent` / `x-trace-id` distributed tracing context headers.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `next-handler` | `Any` | no | — |

### tracing-middleware

`(tracing-middleware next-handler)`

Middleware logging request method, URI, response status, and elapsed duration.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `next-handler` | `Any` | no | — |

### use-script

`(use-script script)`

Registers an ES module script specifier to be imported on the current page or Stage slot payload.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `script` | `Any` | no | — |

### validate-form-middleware

`(validate-form-middleware struct-type ...rest)`

Route middleware that parses and validates `req` into `struct-type`, short-circuiting with 422 on `ValidationErrors` and storing the valid struct at `req:attrs:_form`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `struct-type` | `Any` | no | — |
| `rest` | `Any` | yes | — |
