Code Monkey home page Code Monkey logo

clout's Introduction

Clout

Build Status

Clout is a library for matching Ring HTTP requests. It uses the same routing syntax as used by popular Ruby web frameworks like Ruby on Rails and Sinatra.

Installation

Add the following to your project.clj dependencies:

[clout "2.2.1"]

Usage

Require Clout in the normal way:

(require '[clout.core :as clout])

These following examples also make use of the Ring-Mock library to generate Ring request maps:

(require '[ring.mock.request :as mock])

Routes can match by keyword:

(clout/route-matches
 "/article/:title"
 (mock/request :get "/article/clojure"))

=> {:title "clojure"}

Or with wildcards:

(clout/route-matches
 "/public/*"
 (mock/request :get "/public/style/screen.css"))
 
=> {:* "style/screen.css"}

Clout can also match absolute routes:

(clout/route-matches
 "http://subdomain.example.com/"
 (mock/request :get "http://subdomain.example.com/"))

=> {}

And scheme-relative routes:

(clout/route-matches
 "//subdomain.example.com/"
 (mock/request :get "http://subdomain.example.com/"))

=> {}

(clout/route-matches
 "//subdomain.example.com/"
 (mock/request :get "https://subdomain.example.com/"))
 
=> {}

Clout supports both keywords and wildcards. Keywords (like ":title") will match any character but the following: / . , ; ?. Wildcards (*) will match anything.

If a route does not match, nil is returned:

(clout/route-matches "/products" (mock/request :get "/articles"))

=> nil

For additional performance, you can choose to pre-compile a route:

(def user-route
  (clout/route-compile "/user/:id"))

(clout/route-matches user-route (mock/request :get "/user/10"))

=> {:id "10"}

When compiling a route, you can specify a map of regular expressions to use for different keywords. This allows more specific routing:

(def user-route
  (clout/route-compile "/user/:id" {:id #"\d+"}))

(clout/route-matches user-route (mock/request :get "/user/10"))

=> {:user "10"}

(clout/route-matches user-route (mock/request :get "/user/jsmith"))

=> nil

You can also specify regular expressions inline in braces after the keyword:

(def user-route
  (clout/route-compile "/user/:id{\\d+}"))

Note that regular expression escape sequences (like \d) need to be double-escaped when placed inline in a string.

License

Copyright © 2018 James Reeves

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

clout's People

Contributors

amalloy avatar duckyuck avatar gfredericks avatar joelittlejohn avatar pctiby avatar weavejester avatar xafizoff avatar yurrriq avatar

Stargazers

 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

Watchers

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

clout's Issues

Path segment URL decoding causes problems

I'm writing a HTTP proxy where I use a route match from clout as part of the proxy. The request contains a path with some URL encoded data. Clout decodes this, causing my proxy to deliver invalid URLs (the unencoded versions).

Example: My route matches "/proxy_". I do a request to /proxy/foo%20bar. In my code, I do the proxy with (perform-http-request ... (get-in req [:route-params :_])). The value of the route param is"foo bar", not what's actually in the URL,"foo%20"bar. And "foo bar" is not a valid URL value, causing problems down the road when I do the proxying.

Why is the URL modified in the first place? Would it make sense to provide the unescaped values?

Captured groups in patterns clobber following path parameters

If a regex uses capturing groups, path parameters following the pattern get assigned values from the captured group, not the value from the path.

;; Clout version 2.1.2
(clout/route-matches (clout/route-compile "/:foo/:bar" {:foo #"a(bcd|ef)g"})
(mock/request :get "/abcdg/1"))
=> {:bar "bcd", :foo "abcdg"}

expected:
=> {:bar "1", :foo "abcdg"}

Routes with negative lookahead regex always return nil

Hi there, I'm trying to create a route which has a regex with a negative lookahead, but I can't seem to get it to match any routes at all.

Basically I want to match all possible "/foo/:fragment" routes that do not contain "admin" in the fragment part. I defined a route like so:

(def user-route
  (clout/route-compile "/foo/:fragment" {:fragment #"^((?!admin).)*$"}))

However both the tests below return nil:

(clout/route-matches
  user-route
  (mock/request :get "/foo/user"))
=> nil

(clout/route-matches
  user-route
  (mock/request :get "/foo/admin"))
=> nil

I've tested this with simpler negative lookahead regexes and had the same result. I'd appreciate any help with this.

Routes Throwing 500 in API, fixed after cycling

After some time in production, a web service using clout started throwing errors of the form:

2013-09-26 18:00:55.934:WARN:oejs.ServletHandler:qtp1525259451-38221: Error for /route/omitted/12345
java.lang.NoClassDefFoundError: clout/core$re_groups_STAR_$iter_242246$fn247$fn_248
at clout.core$re_groups_STAR_$iter_242246$fn_247.invoke(core.clj:23)
at clojure.lang.LazySeq.sval(LazySeq.java:42)
at clojure.lang.LazySeq.seq(LazySeq.java:60)
at clojure.lang.RT.seq(RT.java:484)
at clojure.core$seq.invoke(core.clj:133)
at clojure.core$map$fn__4207.invoke(core.clj:2479)
at clojure.lang.LazySeq.sval(LazySeq.java:42)
at clojure.lang.LazySeq.seq(LazySeq.java:60)
at clojure.lang.RT.seq(RT.java:484)
at clojure.core$seq.invoke(core.clj:133)
at clojure.core$map$fn__4211.invoke(core.clj:2490)
at clojure.lang.LazySeq.sval(LazySeq.java:42)
at clojure.lang.LazySeq.seq(LazySeq.java:60)
at clojure.lang.RT.seq(RT.java:484)
at clojure.core$seq.invoke(core.clj:133)
at clojure.core.protocols$seq_reduce.invoke(protocols.clj:30)
at clojure.core.protocols$fn__6026.invoke(protocols.clj:54)
at clojure.core.protocols$fn_5979$G5974_5992.invoke(protocols.clj:13)
at clojure.core$reduce.invoke(core.clj:6177)
at clout.core$assoc_keys_with_groups.invoke(core.clj:54)
at clout.core.CompiledRoute.route_matches(core.clj:84)
at compojure.core$if_route$fn__515.invoke(core.clj:38)
at compojure.core$if_method$fn__508.invoke(core.clj:24)

Prod eng sent me this as well by email:

The health checks also seem to be not working predictably. When Nagios and LVS make health check requests it is returning 200 but when I make a call from the browser I get 500 as seen below:

$ tail -f access.20130927.log jvm-default.log
==> access.20130927.log <==
10.3.157.253 - - [27/Sep/2013:01:27:34 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.254 - - [27/Sep/2013:01:27:35 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.253 - - [27/Sep/2013:01:27:46 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.254 - - [27/Sep/2013:01:27:47 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.253 - - [27/Sep/2013:01:27:58 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.254 - - [27/Sep/2013:01:27:59 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.253 - - [27/Sep/2013:01:28:10 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.254 - - [27/Sep/2013:01:28:11 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.253 - - [27/Sep/2013:01:28:22 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.254 - - [27/Sep/2013:01:28:23 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"

==> jvm-default.log <==
[GC 1222344K(2027264K), 0.3139680 secs]
[GC 1222670K(2027264K), 0.2166150 secs]
[GC 1222670K(2027264K), 0.2900790 secs]
[GC 1222670K(2027264K), 0.3108980 secs]
[GC 1222670K(2027264K), 0.1812040 secs]
[GC 1222670K(2027264K), 0.2490730 secs]
[GC 1222670K(2027264K), 0.1809940 secs]
[GC 1223499K(2027264K), 0.2554620 secs]
[GC 1223499K(2027264K), 0.2609510 secs]
[GC 1223499K(2027264K), 0.2105590 secs]
[GC 1223499K(2027264K), 0.2915280 secs]
[GC 1223499K(2027264K), 0.4804340 secs]
[GC 1223499K(2027264K), 0.2758370 secs]

==> access.20130927.log <==
10.3.157.253 - - [27/Sep/2013:01:28:34 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.254 - - [27/Sep/2013:01:28:35 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"

==> jvm-default.log <==
[GC 1223524K(2027264K), 0.3079560 secs]
[GC 1223524K(2027264K), 0.3189180 secs]
2013-09-26 18:28:42.921:WARN:oejs.ServletHandler:qtp523298718-38244: Error for /vinws/internal/health
java.lang.NoClassDefFoundError: ring/middleware/cookies$parse_cookie_header$iter__937__941
at ring.middleware.cookies$parse_cookie_header.invoke(cookies.clj:42)
at ring.middleware.cookies$parse_cookies.invoke(cookies.clj:85)
at ring.middleware.cookies$wrap_cookies$fn__1029.invoke(cookies.clj:159)
at vinws.servlet$_service$fn__458.invoke(servlet.clj:1)
at ring.util.servlet$make_service_method$fn__50.invoke(servlet.clj:126)
at vinws.servlet$_service.invoke(servlet.clj:1)
at vinws.servlet.service(Unknown Source)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:698)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:505)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:138)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:564)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:213)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1094)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:432)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1028)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:258)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:445)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:267)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:224)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
at java.lang.Thread.run(Thread.java:722)

==> access.20130927.log <==
10.4.50.92 - - [27/Sep/2013:01:28:42 +0000] "GET /vinws/internal/health HTTP/1.1" 500 2477 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.65 Safari/537.36"
10.3.157.253 - - [27/Sep/2013:01:28:46 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"
10.3.157.254 - - [27/Sep/2013:01:28:47 +0000] "GET /vinws/internal/health HTTP/1.0" 200 - "-" "KeepAliveClient"

This implies that the app is caching responses for existing connections which is very bad.

The server was cycled and the routes worked fine. How could the no class def error be thrown? Is there a reason a long running web service without cycling could go down?

Support encoded path components

I'd like to be able to match paths like /a:b/12345 (where 12345 is the variable component) with clout, perhaps using a pattern like /a%3Ab/:id.

(Corollary: I'd like for the path /%61/%61 to be matched by the pattern /a/:x. That is, matching across sometimes-overzealous percent-encoding.)

[SOLVED] java.lang.IllegalArgumentException: No implementation of method: :route-matches of protocol: #'clout.core/Route found for class: clout.core.CompiledRoute

This is a reopen of: #27

Here are my dependencies:

 {org.clojure/clojure {:mvn/version "1.10.3"}
  org.clojure/tools.cli {:mvn/version "1.0.206"}
  http-kit/http-kit {:mvn/version "2.5.3"}
  compojure/compojure {:mvn/version "1.6.2"}
  }

I’m trying to make a separate build (as a NixOS derivation) where I kind of manually compile a couple of modules like this (without even using anything like deps.edn). I found an example here: https://stackoverflow.com/questions/29011295/compile-clojure-source-into-class-aot-from-command-line-not-using-lein#29012274

          java -cp ${esc backend-classpaths}:"$src"/src \
            -Dclojure.compile.path=build \
            clojure.lang.Compile \
            "$module"

Where ${esc backend-classpaths} is a list of JAR files of the dependencies described above and "$src"/src is where my source code is. Then I run it like this:

      java -cp ${esc backend-classpaths}:${backendBuiltClasses} clojure_todo_app.main

Where ${backendBuiltClasses} is a directory with those pre-built classes.

The application successfully starts, arguments get parsed, but when I do a test curl request I get this error:

java.lang.IllegalArgumentException: No implementation of method: :route-matches of protocol: #'clout.core/Route found for class: clout.core.CompiledRoute

Any ideas what’s wrong?

I also found https://stackoverflow.com/questions/31129470/java-lang-illegalargumentexception-no-implementation-of-method-route-matches but in my case this isn’t useful.

IllegalArgumentException with `:aot :all` and `lein trampoline run`

I get the following error when setting :aot :all and starting the application with lein trampoline run:

java.lang.IllegalArgumentException: No implementation of method: :route-matches of protocol: #'clout.core/Route found for class: clout.core.CompiledRoute
        at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:554)
        at clout.core$fn__3514$G__3509__3521.invoke(core.clj:39)
        at compojure.core$if_route$fn__3677.invoke(core.clj:40)

I can reproduce this problem if and only if the two conditions mentioned above are in place. So it is not hard to find a workaround but a fix would be appreciated (I did cost me about 2 hours to figure it out).

There seem to be other ways to get the same problem and it might help to see the underlying problem:

  1. http://stackoverflow.com/questions/31129470/java-lang-illegalargumentexception-no-implementation-of-method-route-matches
  2. http://stackoverflow.com/questions/18542956/can-anybody-explain-why-compojures-routing-macro-only-accepts-literal-vector-as

route-matches Doesn't Match from Servlet Root

(defmethod route-matches [::compiled-route Map]
  [route request]
  (route-matches route (if (:absolute? route)
                         (request-url request)
                         (:uri request))))

This method matches on (:uri request), which works fine when using a single servlet that is mapped to /, but with multiple servlets at different urls, it would be better to match from the root of the servlet. Replacing (:uri request) with (.getPathInfo (:servlet-request request)) should be enough:

(defmethod route-matches [::compiled-route Map]
  [route request]
  (route-matches route (if (:absolute? route)
                         (request-url request)
                         (.getPathInfo (:servlet-request request)))))

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.