Fork me on GitHub
#pedestal
<
2018-07-02
>
martinklepsch14:07:19

Is there a function in pedestal (or any of it’s dependencies) that will return the path part of a given URL?

martinklepsch14:07:39

To step back a bit: I’m trying to see if the referer url matches any of my defined routes. Any suggestions welcome 🙂

ddeaguiar14:07:04

@martinklepsch the key :path-info should be on the request map. I think that’s what you are looking for.

martinklepsch14:07:39

@ddeaguiar that works for the current incoming request but i’m interested in operating on the referer of the request

ddeaguiar15:07:02

as in the Referer header?

ddeaguiar15:07:38

AFAIK you’ll have to pull that header value directly

ddeaguiar15:07:14

i understand what you want

ddeaguiar15:07:23

sorry, you want to take a url and get the path-info essentially

martinklepsch15:07:26

more general problem to give some context: I want to allow users to use URLs like /some/thing/CURRENT where CURRENT is resolved based on the referer and a redirect to the actual “current” thing is returned

martinklepsch15:07:49

hope that makes some sense

martinklepsch15:07:59

> sorry, you want to take a url and get the path-info essentially yeah, and that’s the simpest part of it I guess, I duct-taped it with some regexes now but thought there’s probably something built in

ddeaguiar15:07:14

Path info is calculated on a per-container basis. There’s a ContainerRequest protocol which is implemented for servlet containers

ddeaguiar15:07:48

Essentially you’d need to know what the context path is for the referrer to do a similar thing

martinklepsch15:07:29

Ended up with this now which feels somewhat hacky but at least it gives me back a map with info about the parsed route:

(route/try-routing-for (routes identity {}) :map-tree "/d/reagent/reagent/0.8.1" :get)

ddeaguiar21:07:04

sample usage

(require '[io.pedestal.http.route.path :as route.path])

 (def parsed-path (route.path/parse-path "/foo/:id/:baz"))
 ;; {:path-constraints {:baz "([^/]+)", :id "([^/]+)"},
 ;;  :path-params      [:id :baz],
 ;;  :path-parts       ["foo" :id :baz]}

 (route.path/path-regex parsed-path)
 ;; #"/\Qfoo\E/([^/]+)/([^/]+)"

martinklepsch21:07:13

@ddeaguiar Interesting, are you suggesting to build my own match-route based on my route information and that function?

ddeaguiar21:07:52

preferably this would be easier to do. I’m looking into that now

ddeaguiar21:07:03

this fn gets you part of the way there

ddeaguiar21:07:44

to match routes you need a route specification

ddeaguiar21:07:52

both in bidi and in pedestal

ddeaguiar21:07:30

but I think in your case you are dealing with referer urls right? These are not routes in your app, correct?

martinklepsch21:07:53

they might not be but I’m specifically trying to handle the case that they are part of my app (for which I need to match them against my route table

ddeaguiar21:07:19

I brought up these fns as an option for parsing paths arbitrarily.

ddeaguiar21:07:08

I’m unclear as to what the route fn is in your example:

(route/try-routing-for (routes identity {}) :map-tree "/d/reagent/reagent/0.8.1" :get)

martinklepsch21:07:57

routes returns whatever expand-routes returns

ddeaguiar21:07:04

I see that now

ddeaguiar21:07:21

why do you need match-routes again? You can leverage a specific routing impl directly

martinklepsch21:07:25

I’m using that little indirection of passing a resolver to decouple the definition of the routes table from the actual handlers

ddeaguiar21:07:22

If you look at what try-routing-for does, it creates a router of a specific type and invokes the :enter impl

martinklepsch21:07:23

I need it because I want to determine route-name and path-params for an arbitrary path-info string like "/some/uri"

martinklepsch21:07:54

yes that’s what I ended up using 😄

martinklepsch21:07:47

It does what I need but it seems undocumented and that ;;; Help for debugging comment makes me think that I might be abusing it for things it wasn’t intended for

ddeaguiar21:07:21

I think it’s perfectly acceptable to use a router directly. I don’t think you need to bother with try-routing-for

ddeaguiar21:07:33

one sec, let me experiment

martinklepsch21:07:18

I’ll head to bed but thanks for looking into this. Maybe something along those lines would be a good addition to the API.

ddeaguiar21:07:31

k, I’ll leave comments here

ddeaguiar22:07:41

@martinklepsch you can create the router of your choice using the relevant constructor and then use the io.pedestal.http.route.router/find-route impl on the router to match routes

martinklepsch15:07:35

Considered creating a “container” thing to get the path info but some regex is doing the job fine for now…

martinklepsch15:07:23

(Looking for something akin to Bidi’s match-route I guess: https://github.com/juxt/bidi#route-patterns)

orestis18:07:52

Hi, is there any example on how to ensure an explicit content encoding (ie I want to be sure I always encode the body as utf-8), while also setting the correct content-length header?

orestis18:07:09

I have made a custom interceptor that encodes JSON via Cheshire, and I use (count (.getBytes s)) to get length in bytes, but I’d like to be 100% sure it does the right thing.

ddeaguiar19:07:02

@orestis you may want to consider using the built-in json-body interceptor instead.

ddeaguiar19:07:17

I don’t think pedestal sets the content-length header but it does support updating the servlet response if it’s set.

orestis19:07:29

Thanks! Already I see a bunch of functionality I can use to change my interceptor. I need some more fine grained control so I can’t use the built in one.

orestis19:07:06

Any idea about bytes/encoding etc? I guess this is a more generic thing, but in other languages I’d have to explicitly encode a string to utf-8, so passing strings around makes me worried. Though now I see that I can also use a function that will write to the stream. I might have to dig a little bit deeper there to see how it works.

orestis19:07:30

Aha, I have to use getBytes(Charset.UTF-8), then presumably count that as well.

orestis19:07:16

I might file an issue for this for further discussion, if that’s OK? I was half expecting for this to work out of the box.

orestis19:07:25

Thanks for the help!

ddeaguiar19:07:27

@orestis np. Filing an issue is definitely ok. Paul will see that and may have additional input.

orestis19:07:48

Thanks, I’ll do it tomorrow from the office.