Matching Reqs without suffix

14 views
Skip to first unread message

Gustav van der Merwe

unread,
Oct 3, 2015, 10:11:39 AM10/3/15
to lif...@googlegroups.com
Hey, I've noticed a horrible situation with Req. It matches suffixes with filenames that have one . but if there are two .s in the filename than it is treated as suffixless and the everything after the last / is treated as the filename.

"""
def serve: LiftRules.DispatchPF = {
case "files" :: filename :: Nil Get _ => {
"""

The above will match /files/data.dot with filename being "data" and /files/data.dot.dot with filename as being "data.dot.dot"

Is there any way to construct a match against a Req that doesn't care about suffixes? I know one can special case it via XmlGet etc but I want to go in the other direction, make it more general. Is there some way in which the last part of the URL can be treated uniformly?

Regards,
Gustav van der Merwe

signature.asc

Joe Barnes

unread,
Oct 3, 2015, 3:37:58 PM10/3/15
to Lift, gv...@gvdm.me
Hi Gustav,

So great news! Lift is totally configurable in this regard.  You can prepend your own URI splitter which does not mess with the suffix:

val splitter:SplitSuffixPF = {
case parts => // <= Parts is a List[String] of each part between slashes
// Since you want the entire filename without the suffix split, there is nothing to do
// but return the parts and an empty suffix
(parts, "")
}

And add it during Boot:

class Boot {
def boot {
    // Other stuff...

    LiftRules.suffixSplitters.prepend(RestApi.splitter)
}
}

Now filename will always have the suffix.

You can see this working in this sample project.  Run the application, and navigate to http://localhost:8080/test/my-file.txt

Joe

Joe Barnes

unread,
Oct 4, 2015, 11:08:25 AM10/4/15
to Lift, gv...@gvdm.me
BTW, I updated the example to make sure it is clear to you that you can customize the splitting for only when it matches your rest endpoint.  In this case, mine will only do our custom split when the URI starts with "test":

val splitter:SplitSuffixPF = {
case parts @ ("test" :: _) => // <= Parts is a List[String] of each part between slashes, matching only if starts with "test"
    
// Since you want the entire filename without the suffix split, there is nothing to do
// but return the parts and an empty suffix
(parts, "")
}

Joe

Gustav van der Merwe

unread,
Oct 4, 2015, 12:22:36 PM10/4/15
to lif...@googlegroups.com
On 10/04/2015 05:08 PM, Joe Barnes wrote:
> BTW, I updated the example to make sure it is clear to you that you can customize the splitting for only when it matches your rest endpoint. In this case, mine will only do our custom split when the URI starts with "test":
>
> val splitter:SplitSuffixPF = {
> case parts @ ("test" :: _) => // <= Parts is a List[String] of each part between slashes, matching only if starts with "test"
>
> // Since you want the entire filename without the suffix split, there is nothing to do
> // but return the parts and an empty suffix
> (parts, "")
> }
>

Awesome. Exactly what I need. Thanks Joe!
signature.asc

Joe Barnes

unread,
Oct 4, 2015, 3:03:53 PM10/4/15
to lif...@googlegroups.com
No problem, Gustav. Thanks for using Lift.

Joe


--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

---
You received this message because you are subscribed to a topic in the Google Groups "Lift" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/liftweb/yGZBKyF38Ok/unsubscribe.
To unsubscribe from this group and all its topics, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages