Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

WaiExtra – Middleware Collection

Lean: Hale.WaiExtra | Haskell: wai-extra

API Reference: Hale.WaiExtra | AddHeaders | ForceSSL | Gzip | HttpAuth | Test

36 middleware modules for request/response transformation.

Middleware Composition

         +----------------------------------------------+
         |         Middleware Composition                 |
         |                                               |
         |   m1 . m2 . m3 : Middleware                   |
         |                                               |
         |   Algebraic Laws:                             |
         |     id . m = m        (left identity)         |
         |     m . id = m        (right identity)        |
         |     (f.g).h = f.(g.h) (associativity)         |
         +----------------------------------------------+

  Request --> m1 --> m2 --> m3 --> App --> Response
              |       |       |              |
              | modify| check |              | modify
              | headers auth  |              | headers
              v       v       v              v

Available Middleware

Request Modification

MiddlewareProven PropertiesDescription
methodOverrideOverride method from _method query param
methodOverridePostOverride method from POST body
acceptOverrideOverride Accept from _accept param
realIpExtract client IP from X-Forwarded-For
rewriteURL path rewriting rules

Response Modification

MiddlewareProven PropertiesDescription
addHeadersaddHeaders [] = idAdd headers to responses
stripHeadersstripHeaders [] = idRemove headers from responses
combineHeadersMerge duplicate headers
gzipGzip compression (framework)
streamFileConvert file->stream responses

Routing and Filtering

MiddlewareProven PropertiesDescription
selectselect (fun _ => none) = idConditional middleware
routedrouted (fun _ => true) m = m, routed (fun _ => false) = idPath-based routing
vhostVirtual host routing
urlMapURL prefix routing

Security

MiddlewareProven PropertiesDescription
forceSSLSecure requests pass throughRedirect HTTP->HTTPS
forceDomainRedirect to canonical domain
httpAuthHTTP Basic Authentication
localOnlyRestrict to localhost
requestSizeLimitReject oversized bodies (413)
validateHeadersReject invalid header chars (500)

Monitoring

MiddlewareDescription
requestLoggerApache/dev format logging
requestLogger.jsonStructured JSON logging
healthCheckHealth check endpoint (200 OK)
timeoutRequest timeout (503)

Protocol

MiddlewareDescription
autoheadHEAD->GET + strip body
cleanPathNormalize URL paths (301 redirect)
approotApplication root detection
eventSourceServer-Sent Events
jsonpJSONP callback wrapping

Proven Properties (11 theorems)

All proofs are in the source files, verified at compile time (no sorry):

AddHeaders Identity (3, in AddHeaders.lean)

TheoremStatement
addHeaders_nil_builderEmpty headers on builder = identity
addHeaders_nil_fileEmpty headers on file = identity
addHeaders_nil_streamEmpty headers on stream = identity

StripHeaders Identity (3, in StripHeaders.lean)

TheoremStatement
stripHeaders_nil_builderEmpty strip list on builder = identity
stripHeaders_nil_fileEmpty strip list on file = identity
stripHeaders_nil_streamEmpty strip list on stream = identity

Select (1, in Select.lean)

TheoremStatement
select_noneAlways-none selector = identity middleware

Routed (2, in Routed.lean)

TheoremStatement
routed_trueAlways-true predicate = apply middleware
routed_falseAlways-false predicate = identity middleware

ForceSSL (1, in ForceSSL.lean)

TheoremStatement
forceSSL_secureSecure requests pass through unchanged

HealthCheck (1, in HealthCheckEndpoint.lean)

TheoremStatement
healthCheck_passthroughNon-matching paths pass through to inner app

Files (36 modules)

FilePurpose
Middleware/AddHeaders.leanAdd headers + 3 identity proofs
Middleware/StripHeaders.leanRemove headers + 3 identity proofs
Middleware/Select.leanConditional middleware + 1 proof
Middleware/Routed.leanPath-based routing + 2 proofs
Middleware/ForceSSL.leanHTTP->HTTPS redirect + 1 proof
Middleware/HealthCheckEndpoint.leanHealth check + 1 proof
Middleware/Autohead.leanHEAD method handling
Middleware/AcceptOverride.leanAccept header override
Middleware/MethodOverride.leanMethod override (query param)
Middleware/MethodOverridePost.leanMethod override (POST body)
Middleware/Vhost.leanVirtual host routing
Middleware/Timeout.leanRequest timeout
Middleware/CombineHeaders.leanHeader deduplication
Middleware/StreamFile.leanFile->stream conversion
Middleware/Rewrite.leanURL rewriting
Middleware/CleanPath.leanPath normalization
Middleware/ForceDomain.leanDomain redirect
Middleware/Local.leanLocalhost restriction
Middleware/RealIp.leanClient IP extraction
Middleware/HttpAuth.leanBasic authentication
Middleware/RequestSizeLimit.leanBody size limit
Middleware/ValidateHeaders.leanHeader validation
Middleware/RequestLogger.leanRequest logging
Middleware/RequestLogger/JSON.leanJSON request logging
Middleware/Gzip.leanGzip compression
Middleware/Approot.leanApplication root
Middleware/Jsonp.leanJSONP support
UrlMap.leanURL prefix routing
Header.leanWAI header utilities
Request.leanRequest utilities
Parse.leanMultipart/form parsing
EventSource.leanServer-Sent Events
EventSource/EventStream.leanSSE stream types
Test.leanWAI test utilities
Test/Internal.leanTest internals
Middleware/RequestSizeLimit/Internal.leanSize limit internals