Skip to content

Instantly share code, notes, and snippets.

@omasanori
Created December 15, 2011 12:14
Show Gist options
  • Save omasanori/1480887 to your computer and use it in GitHub Desktop.
Save omasanori/1480887 to your computer and use it in GitHub Desktop.
A small library to generate boundaries for multipart/form-data and so on.
(ns generate-boundary)
(defn- char-range
"Returns a lazy seq of characters between `start` and `end`.
Unlike clojure.core/range, `end` is included in the results.
e.g. (= \9 (last (char-range \0 \9)))"
[start end]
(map char (range (int start) (inc (int end)))))
(def ^:dynamic *characters*
"Seq of characters used for suffix in boundaries."
(concat (char-range \0 \9) (char-range \a \z) (char-range \A \Z) "-_"))
(defn generate-boundary
"Generate a boundary followed by `n` (40 by default) characters in *characters* randomly.
See also: http://www.ietf.org/rfc/rfc2388.txt"
([]
(generate-boundary 40))
([n]
(apply str "-------" (repeatedly n #(rand-nth *characters*)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment