Skip to content

juxt/pick

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

94 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pick

A Clojure library for HTTP server-driven content negotiation.

Warning

STATUS: Stable. In use.

Quick Start

Suppose you are writing a web service and want to provide proactive content negotiation based on the accept headers in the client request. Suppose also that you have established which representations you want to choose between.

A representation is a map that one or more of the following keys:

  • :juxt.http/content-type — the media-type and charset of the representation

  • :juxt.http/content-encoding — how the representation has been encoded (e.g. gzip)

  • :juxt.http/content-language — the natural language of the representation (e.g. es)

  • :juxt.http/content-length — the length of the payload

Example 1. An example representation
{
…
 {:juxt.http/content-type "text/html;charset=utf-8"
  :juxt.http/content-encoding "gzip"
  :juxt.http/content-language "en-US"
  :juxt.http/content-length 20578}
…
}

You can use pick to select the representation that is most acceptable to the user agent.

Here’s how:

(require '[juxt.pick.ring :refer [pick]])

(pick
  ;; A ring request (where headers indicate preferences)
  {:request-method :get
   :uri "/"
   :headers {"accept" "text/html"
             "accept-language" "de"}}

  ;; Possible representations
  [
   {:id 1
    :juxt.http/content-type "text/html;charset=utf-8"
    :juxt.http/content-language "en"}

   {:id 2
    :juxt.http/content-type "text/html;charset=utf-8"
    :juxt.http/content-language "de"}

   {:id 3
    :juxt.http/content-type "text/plain;charset=utf-8"}])

=>
{:juxt.pick/representation {…} ; most acceptable representation
 :juxt.pick/representations […] ; all representations (scored)
}

Introduction

Content negotiation is a feature of HTTP that allows different 'representations' to be served from the same URI.

pick is a Clojure library that determines quality values of each of the negotiable dimensions and corresponding rules specified in RFC 7231. These can be used by content negotiation algorithms to determine the most appropriate content to respond with.

pick also includes a built-in reference implementation based on Apache’s de-facto content negotiation algorithm.

Problem Statement

Content negotiation is one of the primary ways that the web is inclusive, supporting a wide range of user agents and other software-based clients.

Content negotiation can also contribute to improved performance, by allowing the compression of content for optimising network bandwidth and by cache integration.

Also, since content negotiation allows for gradual migration from old to new data formats it enables graceful and continuous improvement, balancing the needs for both innovation and solid dependability.

However, while content negotiation is often used for static resources, which is well implemented by web servers such as Apache’s httpd and nginx, it is often missing from, or poorly implemented by, the majority of web libraries and frameworks.

API Reference

The pick function 3 arguments:

  • The Ring request

  • A collection of possible representations

  • An option map

The option map supports the following entries:

:juxt.pick/explain?

if truthy, provide an explain in the return value

:juxt.pick/vary?

if truthy, compute the preferences that will vary the choice

API alternative: Use of metadata strings

Up until now, we have described a representation as a Clojure map with keywords in the juxt.http namespace. Alternatively, a representation can be any object with Clojure metadata. The metadata should contain the string keywords corresponding to the HTTP representation metadata.

keyword metadata string alternative

:juxt.http/content-type

"content-type"

:juxt.http/content-encoding

"content-encoding"

:juxt.http/content-language

"content-language"

:juxt.http/content-length

"content-length"

For example, here is a representation defined as a Clojure function.

^{"content-type" "text/html;charset=utf-8"
  "content-language" "en"}
(fn [] …)

Alternatives

Most Clojure alternatives only deal with media-types, and perhaps charsets. Note that pick fully implements all proactive (server-driven) content negotiation rules contained in RFC 7231, including media-types, charsets, encodings, languages, wildcards, qvalues and precedence rules. Where not prescribed specifically by the RFC, pick adopts the de-facto decisions made by the Apache httpd engine.

ring-middleware-format and Metosin’s Muuntaja library negotiate media-types and charsets, and perform automatic decoding/encoding of request/response bodies

Caution

pick only provides the decision of which representation(s) to select, given the context of an HTTP request and a set of representation to choose between.

Unlike Muuntaja, pick does not include support for format transformation or coercion, which is considered out of scope for this library.

References

RFC 7231 is the normative standard on content negotiation.

While pick attempts to be reasonably performant, due to the per-request nature of content negotiation some users may consider using a memoization strategy, making use of a memoization library such as clojure.core.memoize.

License

The MIT License (MIT)

Copyright © 2020-2023 JUXT LTD.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A Clojure library for HTTP server-driven content negotiation.

Topics

Resources

License

Stars

Watchers

Forks

Languages