HTTP multiplexer

From HandWiki

HTTP Multiplexer or HTTP Request multiplexing is a technique by which a incoming HTTP request from a user agent is matched against registered patterns or routes, and the callback request handler is invoked for the best match.[1][note 1][2]

Functioning of the Multiplexer

The HTTP application router or multiplexer relies on a tree data structure, which makes use of Radix tree or prefix tree. For example:

Priority Path Request Handler
9 / Index
8 /s Index (no transition)
1 /search/ Search
1 /support/ Support
2 /blog/ BlogIndex
1 /blog/{\w+} BlogIndex (no transition)
1 /blog/{\w+}/ Blog
2 /contact-us/ GeneralContact
1 /contact-us/pr/ PRContact

Implementation

Every Request Handler is a callback function or a function pointer memory address that is registered to handle that HTTP endpoint. The placeholder or parameter for the endpoint is usually implemented using a state machine which accepts character until it reachers a sentinel value i.e. '/' (forward-slash). The transition between each states updates the request handler callback that will be invoked. Since a pattern ending in a slash name a rooted subtree, the pattern "/" matches all paths not matched by other registered patterns, not just the URL with Path == "/".

Common features

Few of the common features of the a HTTP multiplexer includes:

  1. Request routing based on URL host, request path, path prefix, schemes, header, query values, HTTP method declaration and wildcart routes.
  2. Registered routes can be used as subroutes i.e. nested routing is only tested if the parent route matches. This is useful when defining a group of routes that share common conditions like HTTP Host header and prefix path.
  3. URL hosts, request paths, and query values can have placeholders for dynamic request processing, with optional regular expressions.

External Links

Notes

  1. HTTP Multiplexer should not be confused with HTTP/2 RFC 7540 5.1.2 multiplexing

References