Search code examples
rubyrack

Are custom Rack environment variables discouraged?


I would like to store some values in the Rack environment to avoid re-calculating the values in every middleware. Is it discouraged to set your own Rack environment variables? Examples:

  • SERVER_NAME
  • SERVER_PORT
  • rack.errors
  • my_namespace.my_key
  • my_namespace.second_key
  • [etc.]

Solution

  • Custom Rack environments are NOT discouraged.

    This is discussed in the Rack Spec: http://rack.rubyforge.org/doc/SPEC.html as follows:

    The server or the application can store their own data in the environment, too. The keys must contain at least one dot, and should be prefixed uniquely. The prefix rack. is reserved for use with the Rack core distribution and other accepted specifications and must not be used otherwise. The environment must not contain the keys HTTP_CONTENT_TYPE or HTTP_CONTENT_LENGTH (use the versions without HTTP_). The CGI keys (named without a period) must have String values. There are the following restrictions . . .