Teapot and Regex

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Teapot and Regex

Pharo Smalltalk Users mailing list
Dear Smalltalkers (Pharonistas?),

while trying out teapot in Pharo4, I encountered the following: I have made
a plain HTML form which sends data to a teapot URL. Here is what the browser
is sending:

http://localhost:1701/bac/?a=88&r=0.7&kg=77

To answer this request via teapot, I wrote the following:

...
GET:'\/bac\/\?a=(\d*)&r=(\d*.\d*)&kg=(\d*)' asRegex -> [:req |
BloodAlcoholCalculator a:(req at:1)  r:(req at:2)  kg:(req at:3) ];
...

But the answer in the Browser looks like:

Not Found /bac/?r=0.7&a=88&kg=77

I tested the regular expression via 'matchesRegex' and it returned 'true'.
The funny thing is, that teapot changes the order of the values in the
answer (r and a are switched). If I leave out the '\?' and use the resulting
URL manually, teapot delivers the expected behaviour.

What could be the reason for this issue and where should I search to
understand it (System Browser)?

Thanks for the attention and kind regards

Marcus



--
View this message in context: http://forum.world.st/Teapot-and-Regex-tp4840892.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

jtuchel
Marcus,

I have no answers to your questions, but it seems like you expect the
URL parameters in a specified order.
I am nat sure this is a good idea at all. So independent on why the
order of the parameters is changed and by whom: you shouldn't rely on it.

Sure, this doesn't really help to understand or fix the problem at hand.
But there should be no problem if you handled parameters by name rather
than their order.

Are you sure the order of parameters is changed by Teapot?

Joachim

--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1


mtk
Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

mtk
This post was updated on .
Hi Joachim,

thanks for your feedback. Concerning the order of the values I rely solely on the teapot error message. Besides I have no idea how I could make this GET request independant from the order the HTML form generated. Maybe with Javascript? But I assume this simple scenario should work in teapot and I just didn't understand it correctly...

Kind regards

Marcus
Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

Pharo Smalltalk Users mailing list
In reply to this post by jtuchel
Hi Joachim,

thanks for your feedback. Concerning the order of the values I rely solely
on the teapot error message. Besides I have no idea how I could make this
GET request independant from the order the HTML form generated. Maybe with
Javascript? But I assume this simple scenario should work in teapot and I
just understand it correctly...

Kind regards

Marcus




--
View this message in context: http://forum.world.st/Teapot-and-Regex-tp4840893p4840908.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

jtuchel
Marcus,

>Hi Joachim,

> thanks for your feedback. Concerning the order of the values I rely solely
> on the teapot error message. 

The way I understand your code snippet, you access the url parameters by using "at: 1". I have never used Teapot, but would be surprised if it encourages this. In HTTP world, a parameter consists of its name and value, both being Strings. 

> Besides I have no idea how I could make this
> GET request independant from the order the HTML form generated. 

You shouldn't be required to do anything. In the Smalltalk web frameworks I know (Sst and Seaside mostly) an HTTPRequest object has a Dictionary like API for accessing parameters. Maybe something like "parameterAt: 'r'" should do the trick.

> Maybe with Javascript? 

Javascript is the source of a lot of evil, but here, it is innocent like Cinderella. 
Normal HTTP processing still works without javascript and I hope this won't change any time soon. This is simple HTTP back and forth, no javascript required nor desired.

> But I assume this simple scenario should work in teapot and I
> just understand it correctly... 

Hmm. As I said, don't access url parameters by index, only by name. I am sure Teapot offers methods for that, in the most basic case it gives you a Dictionary or parameters, but most likely some wrapper that provides a Dictionary-esque access methods.

Maybe this bit is a starting point (found here https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/Teapot/Teapot.html):

3.1. Parameters in URLs

The URL pattern may contain named parameters (e.g., <user> above), whose values are accessible via the request object. The request is an extension of ZnRequest with some extra methods.

Query parameters and Form parameters can be accessed the same way as path parameters (req at: #paramName). Teapot can perform conversions of parameters to a number, for example as follows:

Teapot on
	GET: '/user/<id:IsInteger>' -> [ :req | users findById: (req at: #id)]; 
	output: #ston;
	start.
But hey, now I see why you wanted to use Regex: Chapter 3.2 introduces them.... So what can you do if you want to use the reqex stuff? I'd start by adding a Breakpoint into the handler block and inspect the req object that gets passed in. Maybe somethins is wrong about your regex (I am far from being an expert on Regular Expressions) and Teapot is not doing any harm. Joachim
-- 
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          [hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

Pharo Smalltalk Users mailing list
Hi Joachim,

my first steps in teapot used the named parameters (and manually sending the URL), but as soon as I used the HTML form, I had to switch to regex (I thought). Maybe this was wrong, but your hint concerning the 'req' will certainly yield new insights. I will try to find the req object and check what is going on with it... :-)

thanks and kind regards

Marcus


[hidden email] <[hidden email]> schrieb am Di., 4. Aug. 2015 um 13:12 Uhr:
Marcus,

>Hi Joachim,

> thanks for your feedback. Concerning the order of the values I rely solely
> on the teapot error message. 

The way I understand your code snippet, you access the url parameters by using "at: 1". I have never used Teapot, but would be surprised if it encourages this. In HTTP world, a parameter consists of its name and value, both being Strings. 

> Besides I have no idea how I could make this
> GET request independant from the order the HTML form generated. 

You shouldn't be required to do anything. In the Smalltalk web frameworks I know (Sst and Seaside mostly) an HTTPRequest object has a Dictionary like API for accessing parameters. Maybe something like "parameterAt: 'r'" should do the trick.

> Maybe with Javascript? 

Javascript is the source of a lot of evil, but here, it is innocent like Cinderella. 
Normal HTTP processing still works without javascript and I hope this won't change any time soon. This is simple HTTP back and forth, no javascript required nor desired.

> But I assume this simple scenario should work in teapot and I
> just understand it correctly... 

Hmm. As I said, don't access url parameters by index, only by name. I am sure Teapot offers methods for that, in the most basic case it gives you a Dictionary or parameters, but most likely some wrapper that provides a Dictionary-esque access methods.

Maybe this bit is a starting point (found here https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/Teapot/Teapot.html):

3.1. Parameters in URLs

The URL pattern may contain named parameters (e.g., <user> above), whose values are accessible via the request object. The request is an extension of ZnRequest with some extra methods.

Query parameters and Form parameters can be accessed the same way as path parameters (req at: #paramName). Teapot can perform conversions of parameters to a number, for example as follows:

Teapot on
	GET: '/user/<id:IsInteger>' -> [ :req | users findById: (req at: #id)]; 
	output: #ston;
	start.
But hey, now I see why you wanted to use Regex: Chapter 3.2 introduces them.... So what can you do if you want to use the reqex stuff? I'd start by adding a Breakpoint into the handler block and inspect the req object that gets passed in. Maybe somethins is wrong about your regex (I am far from being an expert on Regular Expressions) and Teapot is not doing any harm. Joachim
-- 
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          [hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

jtuchel
Am 04.08.15 um 13:34 schrieb Marcus Kemper via Pharo-users:
Hi Marcus,

I am slowly getting what Teapot does...

So you setup an Association with a  String that matches the URL of an
incoming request as key and its handler block as value. So you should be
able to use "req at: #r" also when using the Regex as matching criteria.

Once you confirm this, the ordering is neither relevant nor a problem
for you and you can go forward. Just forget about indexed access to
request data for the future.

Please let us know if access by symbols works for you.

Joachim

--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1


mtk
Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

mtk
Hi Joachim,

thanks for your attention to this issue. I tried to change it to #a, #r or #kg, but this didn't work. It works with the following by leaving the '\?' out:

GET:'\/bac\/a=(\d*)&r=(\d*.\d*)&kg=(\d*)' asRegex -> [:req | BloodAlcoholCalculator a:(req at:1)  r:(req at:2)  kg:(req at:3) ];

But it works only by manually submitting it from the address input field of the browser. So I guess there is a problem with handling the '\?'.

I will investigate further! :-)

Best

Marcus  
Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

Pharo Smalltalk Users mailing list
In reply to this post by jtuchel
Hi Joachim,

thanks for your attention to this issue. I tried to change it to #a, #r or
#kg, but this didn't work. It works with the following by leaving the '\?'
out:

GET:'\/bac\/a=(\d*)&r=(\d*.\d*)&kg=(\d*)' asRegex -> [:req |
BloodAlcoholCalculator a:(req at:1)  r:(req at:2)  kg:(req at:3) ];

But it works only by manually submitting it from the address input field of
the browser. So I guess there is a problem with handling the '\?'.

I will investigate further! :-)

Best

Marcus  



--
View this message in context: http://forum.world.st/Teapot-and-Regex-tp4840893p4840921.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

Attila Magyar
In reply to this post by Pharo Smalltalk Users mailing list
Hi Marcus,

It seems you're trying to define a matcher on the query parameters. It's not going to work.

An url consist of path a the query part. These are separeted by a question mark.

http://localhost:1701/foo/bar?q1=xx&q2=yy

In this example /foo/bar is the path, and q1 and q2 are the query params.

When you define a route in Teapot you specify the path part (either with wildcards or with regexp). Teapot will select the matching route when a request comes in based on the path part, and it will parse the query part (in fact Zn does this), and make the query parameters accessible in the request object.

For example

GET: '/foo/bar' -> [:req | req at: #q1]

This matches to an url whose path part is /foo/bar, even if there is no query part, or if it has many query parameters.

In your example, you've defined the query part in the matcher, but since it is not part of the incoming url, you've got 404.

But if you share what you're trying to accomplish I may be able to help.

Attila

mtk
Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

mtk
Hi Attila,

many thanks for your explanation - now it works by simply putting:

GET:'/bac/' -> [:req | BloodAlcoholCalculator a:(req at:#a)  r:(req at:#r)  kg:(req at:#kg) ];

So you and Joachim were both right... :-) That makes it even easier to work with teapot.

Kind regards

Marcus
Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

Pharo Smalltalk Users mailing list
In reply to this post by Attila Magyar
Hi Attila,

many thanks for your explanation - now it works by simply putting:

GET:'/bac/' -> [:req | BloodAlcoholCalculator a:(req at:#a)  r:(req at:#r)
kg:(req at:#kg) ];

So you and Joachim were both right... :-) That makes it even easier to work
with teapot.

Kind regards

Marcus



--
View this message in context: http://forum.world.st/Teapot-and-Regex-tp4840893p4840965.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

jtuchel
Am 05.08.15 um 05:44 schrieb mtk via Pharo-users:
Hi Marcus,

good to hear you could make it work. Thankfully, Attila gave us the
final explanation for the regex mystery ;-)
So I guess it is best to stick with "normal" strings for the most part
for several reasons:

* performance: (matching strings is probably a lot faster than matching
a regex)
* security / stability: looking back at my own experiments with regular
expressions, you "never know" what exact urls are really being served.
You need to do extensive testing on which URLs match and which don't.
Sure this is not true if you know regex very well ;-)

Joachim

--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1


Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

jtuchel
In reply to this post by Pharo Smalltalk Users mailing list
Oh, and Marcus:

Please check your mail program: I only see your mail text as an
attachment, and on some mail readers (e.g. iPhone) it is not even
properly displayed. It is hard to cite your text and answer in the way
we're used to do on mailing lists...


Joachim

--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1


Reply | Threaded
Open this post in threaded view
|

Re: Teapot and Regex

Pharo Smalltalk Users mailing list

Good morning,

Thanks for the hint. I will check the settings.

Kind regards

Marcus


[hidden email] <[hidden email]> schrieb am Mi., 5. Aug. 2015 05:58:
Oh, and Marcus:

Please check your mail program: I only see your mail text as an
attachment, and on some mail readers (e.g. iPhone) it is not even
properly displayed. It is hard to cite your text and answer in the way
we're used to do on mailing lists...


Joachim

--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
Fliederweg 1                         http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1