GithubHelp home page GithubHelp logo

ring-middleware's Introduction

ring-middleware

Build Status

This project was originally adapted from tailrecursion's ring-proxy middleware, and is meant for use with the Trapperkeeper Jetty10 Webservice. It also contains common ring middleware between Puppet projects and helpers to be used with the middleware.

Usage

To use ring-middleware, add this project as a dependency in your leiningen project file:

Clojars Project

Schemas

  • ResponseType -- one of the two supported response types (:json, :plain) returned by many middleware.
  • RingRequest -- a map containing at least a :uri, optionally a valid certificate, and any number of keyword-Any pairs.
  • RingResponse -- a map with at least :status, :headers, and :body keys.

Non-Middleware Helpers

json-response

(json-response status body)

Creates a basic ring response with :status of status and a :body of body serialized to json.

plain-response

(plain-response status body)

Creates a basic ring response with :status of status and a :body of body set to UTF-8 plain text.

throw-bad-request!

(throw-bad-request! "Error Message")

Throws a :bad-request type slingshot error with the supplied message. See wrap-bad-request for middleware designed to compliment this function, also bad-request? for a function to help implement your own error handling.

throw-service-unavailable!

(throw-service-unavailable! "Error Message")

Throws a :service-unavailable type slingshot error with the supplied message. See wrap-service-unavailable for middleware designed to compliment this function, also service-unavailable? for a function to help implement your own error handling.

throw-data-invalid!

(throw-data-invalid! "Error Message")

Throws a :data-invalid type slingshot error with the supplied message. See wrap-data-errors for middleware designed to compliment this function, also data-invalid? for a function to help implement your own error handling.

bad-request?

(try+ (handler request)
  (catch bad-request? e
    (...handle a bad request...)))

Determines if the supplied slingshot error map is for a bad request.

service-unavailable?

(try+ (handler request)
  (catch service-unavailable? e
    (...handle service unavailability...)))

Determines if the supplied slingshot error map is for the service being unavailable.

data-invalid?

(try+ (handler request)
  (catch data-invalid? e
    (...handle invalid data...)))

Determines if the supplied slingshot error map is for invalid data.

schema-error?

(try+ (handler request)
  (catch schema-error? e
    (...handle schema error...)))

Determines if the supplied slingshot error map is for a schema error.

Middleware

wrap-request-logging

(wrap-request-logging handler)

Logs the :request-method and :uri at debug level, the full request at trace. At the trace level, attempts to remove sensitive auth information and replace client certificate with the client's common name.

wrap-response-logging

(wrap-response-logging handler)

Logs the response at the trace log level.

wrap-proxy

(wrap-proxy handler proxied-path remote-uri-base & [http-opts])

This function returns a ring handler that, when given a URL with a certain prefix, proxies the request to a remote URL specified by the remote-uri-base argument.

The arguments are as follows:

  • handler: A ring-handler that will be used if the provided url does not begin with the proxied-path prefix
  • proxied-path: The URL prefix of all requests that are to be proxied. This can be either a string or a regular expression pattern. Note that, when this is a regular expression, the entire request URI will be appended to remote-uri-base when the URI is being rewritten, whereas if this argument is a string, the proxied-path will not be included.
  • remote-uri-base: The base URL that you want to proxy requests with the proxied-path prefix to
  • http-opts: An optional list of options for an http client. This is used by the handler returned by wrap-proxy when it makes a proxied request to a remote URI. For a list of available options, please see the options defined for clj-http-client.

For example, the following:

(wrap-proxy handler "/hello-world" "http://localhost:9000/hello")

would return a ring handler that proxies all requests with URL prefix "/hello-world" to http://localhost:9000/hello.

The following:

(wrap-proxy handler #"^/hello-world" "http://localhost:9000/hello")

would return a ring handler that proxies all requests with a URL path matching the regex #^/hello-world" to http://localhost:9000/hello/[url-path].

Proxy Redirect Support

By default, all proxy requests using wrap-proxy will follow any redirects, including on POST and PUT requests. To allow redirects but restrict their use on POST and PUT requests, set the :force-redirects option to false in the http-opts map. To disable redirect following on proxy requests, set the :follow-redirects option to false in the http-opts map. Please not that if proxy redirect following is disabled, you may have to disable it on the client making the proxy request as well if the location returned by the redirect is relative.

SSL Support

wrap-proxy supports SSL. To add SSL support, you can set SSL options in the http-opts map as you would in a request made with clj-http-client. Simply set the :ssl-cert, :ssl-key, and :ssl-ca-cert options in the http-opts map to be paths to your .pem files.

wrap-with-certificate-cn

This middleware adds a :ssl-client-cn key to the request map if a :ssl-client-cert is present. If no client certificate is present, the key's value is set to nil. This makes for easier certificate whitelisting (using the cert whitelisting function from pl/kitchensink)

wrap-add-cache-headers

A utility middleware with the following signature:

(wrap-add-cache-headers handler)

This middleware adds Cache-Control: no-store headers to GET and PUT requests if they are handled by the handler.

wrap-add-x-frame-options-deny

A utility middleware with the following signature:

(wrap-add-x-frame-options-deny handler)

This middleware adds X-Frame-Options: DENY headers to requests if they are handled by the handler.

wrap-add-x-content-nosniff

A utility middleware with the following signature:

(wrap-add-x-content-nosniff handler)

This middleware adds X-Content-Type-Options: nosniff headers to requests if they are handled by the handler.

wrap-add-csp

A utility middleware with the following signature:

(wrap-add-csp handler csp-val)

This middleware adds Content-Security-Policy headers to requests if they are handled by the handler. The value of the header will be equivalent to the second argument passed, csp-val.

wrap-params

A utility middleware with the following signature:

(wrap-params handler & [options])

This middleware parses url-encoded parameters from the query string and form body and adds the following keys to the request map:

  • :query-params - a map of parameters from the query string
  • :form-params - a map of parameters from the body
  • :params - a map of all types of parameter

Accepts the following options:

  • :encoding - encoding to use for url-decoding. If not specified, uses the request character encoding, or "UTF-8" if no request charater encoding is set.

wrap-data-errors

(wrap-data-errors handler)

Always returns a status code of 400 to the client and logs the error message at the "error" log level. Catches and processes any exceptions thrown via slingshot/throw+ with a :type of one of:

  • :request-data-invalid
  • :user-data-invalid
  • :data-invalid
  • :service-status-version-not-found

Returns a basic ring response map with the :body set to the JSON serialized representation of the exception thrown wrapped in a map and accessible by the "error" key.

Example return body:

{
  "error": {
    "type": "user-data-invalid",
    "message": "Error Message From Thrower"
  }
}

Returns valid ResponseTypes, eg:

(wrap-data-errors handler :plain)

wrap-bad-request

(wrap-bad-request handler)

Always returns a status code of 400 to the client and logs the error message at the "error" log level. Catches and processes any exceptions thrown via slingshot/throw+ with a :type of one of:

  • :bad-request

Returns a basic ring response map with the :body set to the JSON serialized representation of the exception thrown wrapped in a map and accessible by the "error" key.

Example return body:

{
  "error": {
    "type": "bad-request",
    "message": "Error Message From Thrower"
  }
}

Returns valid ResponseTypes, eg:

(wrap-bad-request handler :plain)

wrap-schema-errors

(wrap-schema-errors handler)

Always returns a status code of 500 to the client and logs an message containing the schema error, expected value, and exception type at the "error" log level.

Returns a basic ring response map with the :body as the JSON serialized representation of helpful exception information wrapped in a map and accessible by the "error" key. Always returns an error type of "application-error".

Example return body:

{
  "error": {
    "type": "application-error",
    "message": "Something unexpected happened: {:error ... :value ... :type :schema.core/error}"
  }
}

Returns valid ResponseTypes, eg:

(wrap-schema-errors handler :plain)

wrap-uncaught-errors

(wrap-uncaught-errors handler)

Always returns a status code of 500 to the client and logs a message with the serialized Exception at the "error" log level.

Returns a basic ring response map with the :body set as the JSON serialized representation of helpful exception information wrapped in a map and accessible by the "error" key. Always returns an error type of "application-error".

Example return body:

{
  "error": {
    "type": "application-error",
    "message": "Internal Server Error: <serialized Exception>"
  }
}

Returns valid ResponseTypes, eg:

(wrap-uncaught-errors handler :plain)

Support

Please log tickets and issues at our Trapperkeeper Issue Tracker. In addition there is a #trapperkeeper channel on Freenode.

ring-middleware's People

Contributors

alawley avatar camlow325 avatar haus avatar jelinwils avatar jonathannewman avatar justinstoller avatar kevincorcoran avatar knbanderson avatar livinginsyn avatar magisus avatar mollykmcglone avatar mslilah avatar mwaggett avatar nmburgan avatar nwolfe avatar pcarlisle avatar puppetlabs-jenkins avatar rileynewton avatar rlinehan avatar steveax avatar theshanx avatar vilmibm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ring-middleware's Issues

Error from `lein repl`

Doing lein repl at the root of the project results in the following errors:

Tried to load org.apache.httpcomponents/httpcore version 4.1.2 but 4.4.4 was already loaded.
Tried to load org.apache.httpcomponents/httpclient version 4.1.2 but 4.5.3 was already loaded.
Tried to load org.apache.maven.wagon/wagon-http version 2.2 but 2.12 was already loaded.
Tried to load commons-codec version 1.4 but 1.9 was already loaded.
Tried to load org.codehaus.plexus/plexus-utils version 3.0 but 3.0.24 was already loaded.
Tried to load org.codehaus.plexus/plexus-component-annotations version 1.5.5 but 1.7.1 was already loaded.
Tried to load org.jsoup/jsoup version 1.6.1 but 1.7.2 was already loaded.
Tried to load org.tcrawley/dynapath version 0.2.3 but 0.2.5 was already loaded.
Tried to load org.codehaus.plexus/plexus-interpolation version 1.14 but 1.24 was already loaded.
Tried to load commons-io version 2.0.1 but 2.5 was already loaded.
Tried to load com.cemerick/pomegranate version 0.3.1 but 0.4.0-alpha1 was already loaded.
Tried to load org.apache.maven/maven-repository-metadata version 3.0.4 but 3.5.0 was already loaded.
Tried to load org.apache.maven.wagon/wagon-provider-api version 2.2 but 2.12 was already loaded.
Tried to load commons-logging version 1.1.1 but 1.2 was already loaded.
Tried to load org.apache.maven/maven-model-builder version 3.0.4 but 3.5.0 was already loaded.
Tried to load org.apache.maven/maven-model version 3.0.4 but 3.5.0 was already loaded.
Possibly confusing dependencies found:
[puppetlabs/i18n "0.7.1"] -> [org.clojure/clojure "1.6.0"]
 overrides
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-jetty-adapter "1.5.1"] -> [ring/ring-core "1.5.1"] -> [clj-time "0.11.0"] -> [org.clojure/clojure "1.7.0"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-devel "1.5.1"] -> [ring/ring-core "1.5.1"] -> [clj-time "0.11.0"] -> [org.clojure/clojure "1.7.0"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-core "1.5.1"] -> [clj-time "0.11.0"] -> [org.clojure/clojure "1.7.0"]
 and
[lein-cljfmt "0.5.7"] -> [cljfmt "0.5.7"] -> [rewrite-cljs "0.4.3"] -> [org.clojure/clojurescript "1.7.228" :exclusions [org.apache.ant/ant]] -> [org.clojure/clojure "1.7.0"]
 and
[lein-cljfmt "0.5.7"] -> [cljfmt "0.5.7"] -> [rewrite-cljs "0.4.3"] -> [org.clojure/clojure "1.7.0"]
 and
[lein-kibit "0.1.6-beta2"] -> [jonase/kibit "0.1.6-beta2"] -> [org.clojure/clojure "1.8.0"]
 and
[lein-cljfmt "0.5.7"] -> [meta-merge "1.0.0"] -> [org.clojure/clojure "1.7.0"]
 and
[lein-cljfmt "0.5.7"] -> [cljfmt "0.5.7"] -> [org.clojure/clojure "1.7.0"]
 and
[jonase/eastwood "0.2.5"] -> [org.clojure/clojure "1.8.0"]

Consider using these exclusions:
[nightlight/lein-nightlight "1.9.2" :exclusions [org.clojure/clojure]]
[lein-cljfmt "0.5.7" :exclusions [org.clojure/clojure]]
[lein-kibit "0.1.6-beta2" :exclusions [org.clojure/clojure]]
[jonase/eastwood "0.2.5" :exclusions [org.clojure/clojure]]

[lein-cljfmt "0.5.7"] -> [cljfmt "0.5.7"] -> [org.clojure/tools.reader "1.0.0-alpha4"]
 overrides
[lein-kibit "0.1.6-beta2"] -> [jonase/kibit "0.1.6-beta2"] -> [org.clojure/tools.reader "1.0.2"]

Consider using these exclusions:
[lein-kibit "0.1.6-beta2" :exclusions [org.clojure/tools.reader]]

[lein-parent "0.3.1"] -> [com.cemerick/pomegranate "0.3.1"] -> [org.apache.maven.wagon/wagon-http "2.2"] -> [org.apache.maven.wagon/wagon-http-shared4 "2.2"] -> [commons-io "2.0.1"]
 overrides
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-jetty-adapter "1.5.1"] -> [ring/ring-core "1.5.1"] -> [commons-fileupload "1.3.1"] -> [commons-io "2.2"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-devel "1.5.1"] -> [ring/ring-core "1.5.1"] -> [commons-fileupload "1.3.1"] -> [commons-io "2.2"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-jetty-adapter "1.5.1"] -> [ring/ring-core "1.5.1"] -> [commons-io "2.5"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-devel "1.5.1"] -> [ring/ring-core "1.5.1"] -> [commons-io "2.5"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-core "1.5.1"] -> [commons-fileupload "1.3.1"] -> [commons-io "2.2"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-core "1.5.1"] -> [commons-io "2.5"]

Consider using these exclusions:
[nightlight/lein-nightlight "1.9.2" :exclusions [commons-io]]

[lein-parent "0.3.1"] -> [com.cemerick/pomegranate "0.3.1"] -> [org.apache.maven.wagon/wagon-http "2.2"] -> [org.apache.httpcomponents/httpclient "4.1.2"] -> [commons-codec "1.4"]
 overrides
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-jetty-adapter "1.5.1"] -> [ring/ring-core "1.5.1"] -> [crypto-random "1.2.0"] -> [commons-codec "1.6"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-jetty-adapter "1.5.1"] -> [ring/ring-core "1.5.1"] -> [ring/ring-codec "1.0.1"] -> [commons-codec "1.6"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-devel "1.5.1"] -> [ring/ring-core "1.5.1"] -> [crypto-random "1.2.0"] -> [commons-codec "1.6"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-devel "1.5.1"] -> [ring/ring-core "1.5.1"] -> [ring/ring-codec "1.0.1"] -> [commons-codec "1.6"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-core "1.5.1"] -> [crypto-random "1.2.0"] -> [commons-codec "1.6"]
 and
[nightlight/lein-nightlight "1.9.2"] -> [nightlight "1.9.2" :exclusions [org.clojure/core.async]] -> [ring "1.5.1"] -> [ring/ring-core "1.5.1"] -> [ring/ring-codec "1.0.1"] -> [commons-codec "1.6"]

Consider using these exclusions:
[nightlight/lein-nightlight "1.9.2" :exclusions [commons-codec]]

Aborting due to :pedantic? :abort

net::ERR_INCOMPLETE_CHUNKED_ENCODING

Describe the Bug

I have a Luminus app using static ring-proxy to mediate between the browser and a back-end server, which works fine when running on its own, but I have used lein-uberwar to package it up and deploy it onto Tomcat, and there I see this error that a Content-Length header is set even though the response says it is chunked encoding, which the browser says contradicts itself and can't be processed. I looked though the http-opts, and nothing jumps out at me as a setting to change to avoid this. Any ideas?

Expected Behavior

The response from the servlet running in Tomcat has the chunked_encoding header, but not Content-Length

Steps to Reproduce

Steps to reproduce the behavior:

  1. lein new luminus app +war
  2. use (wrap-proxy ...) around the app-routes to forward anything matching "/api" to a server that returns chunked-encoding responses
  3. lein uberwar to compile to a .war
  4. deploy to Tomcat
  5. trigger AJAX calls from the browser to the URLs that are being proxied

Environment

  • I'm actually using this through [derekchiang/ring-proxy "1.0.1"]
  • Compiled on Ubuntu 20.10, run on Tomcat 9.0.44 on Windows 10

Additional Context

I hit the server from PostMan just to make sure it wasn't a problem with the browser or the ClojureScript app, but it also throws a parsing error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.