You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Alexander Klimetschek <ak...@adobe.com> on 2014/01/14 05:26:03 UTC
Lost parameter order for form POSTs
Hi,
Sling currently does not allow to read request parameters in their original order. I need this for a migrated servlet code that used to run on other servlet containers and was able to preserve the order (see below), thus has URL schemes where paramter order is crucial.
Actually it is the java servlet spec that messed up things here: the HTML forms definition [1] clearly says that the order in the form encoding should be preserved (both for GET and POST), but the servlet API uses the "Map" interface and doesn't specify that this has to be an ordered map. Hence most/all the servlet containers implement this using a HashMap (Jetty [2], Adobe's CQSE; only Tomcat seems to be an exception [3]), and you can't rely on it.
Note that while Sling does try to _ensure_ the order of parameters as noted on [4], this only means it keeps the container order, and only for the special case of "multipart/form-data" POSTs, where Sling actually does the decoding itself and does not use the servlet containers implicit decoding. However, the majority of POSTs and the ones I care about are not in multipart.
Now, there can be a workaround: simply parse it yourself:
- GET: use request.getQueryString()
- POST: use request.getReader()
This is what the servlet code base I mention is doing.
However, the POST case relies on the fact that getReader() hasn't been called yet. But this is easily the case with form POSTs, since the first request.getParameter() will trigger decoding of the request body. And in Sling there will usually be at least one authentication handler that needs to do a request.getParameter(), and authentication handlers come before any servlet filter, so no luck to work around that.
The proper way IMO would be to have sling take care of the correctly-ordered-re-decoding. This would need to be a change mostly in ParameterSupport [5] of the sling engine. It should probably be disabled by default (otherwise all parameters would always get decoded twice, once in the container and once in Sling) and could be opt-in on a per servlet basis - via means of some "sling.servlet.orderedparameters" option. Since the servlet is not resolved at the time of the auth handlers, this might in fact be tricky to do.
WDYT?
[1] http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
[2] http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/util/MultiMap.html#33
[3] http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/http/Parameters.java?revision=1525696&view=markup
[4] http://sling.apache.org/documentation/the-sling-engine/request-parameters.html
[5] http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/parameters/ParameterSupport.java?view=markup
Cheers,
Alex
Re: Lost parameter order for form POSTs
Posted by Alexander Klimetschek <ak...@adobe.com>.
On 14.01.2014, at 09:59, Felix Meschberger <fm...@adobe.com> wrote:
> How about we move the ParameterSupport into a separate bundle and expose the ParameterSupport class as a utility class. This can then be leveraged by the Sling Engine itself but also by other pieces not running inside the Sling Engine.
Probably makes sense. But I'd first implement the changes for the ordering, to see what impact it has on the class.
Cheers,
Alex
Re: Lost parameter order for form POSTs
Posted by Felix Meschberger <fm...@adobe.com>.
Hi
Another thing: Currently ParameterSupport is an internal part of the Sling Engine.
How about we move the ParameterSupport into a separate bundle and expose the ParameterSupport class as a utility class. This can then be leveraged by the Sling Engine itself but also by other pieces not running inside the Sling Engine.
WDYT ?
Regards
Felix
Am 13.01.2014 um 22:17 schrieb Felix Meschberger <fm...@adobe.com>:
> Hi
>
> Well, all is not lost since, event if the AuthenticationHandler is reading the parameters the Sling Engine's ParameterSupport actually kicks in ! (We do this to support mutipart/form-data submission of login forms).
>
> Yet, I am a bit reluctant to replicate servlet container work here.
>
> Yet, on the other hand, we could maybe solve another problem more elegantly: the problem of parameter encoding, where we currently employ a "reencoding" technique which might be simplified if we do it ourselves.
>
> Yet, I still suggest you use multipart/form-data for any HTML form submission. The other forms have serious drawbacks, particularly the querystring method.
>
> Having said this, I could imagine taking a hybrid approach:
>
> (1) For url-encoded POST request, check whether InputStream is available
> (1a) if available:
> - decode query string
> - decode input stream
> (1b) if not available
> - take all parameters from servlet container
> (2) For form-data POST request
> - decode query string
> - decode POST input (as done today)
>
> WDYT ?
>
> Regards
> Felix
>
> Am 13.01.2014 um 21:26 schrieb Alexander Klimetschek <ak...@adobe.com>:
>
>> Hi,
>>
>> Sling currently does not allow to read request parameters in their original order. I need this for a migrated servlet code that used to run on other servlet containers and was able to preserve the order (see below), thus has URL schemes where paramter order is crucial.
>>
>> Actually it is the java servlet spec that messed up things here: the HTML forms definition [1] clearly says that the order in the form encoding should be preserved (both for GET and POST), but the servlet API uses the "Map" interface and doesn't specify that this has to be an ordered map. Hence most/all the servlet containers implement this using a HashMap (Jetty [2], Adobe's CQSE; only Tomcat seems to be an exception [3]), and you can't rely on it.
>>
>> Note that while Sling does try to _ensure_ the order of parameters as noted on [4], this only means it keeps the container order, and only for the special case of "multipart/form-data" POSTs, where Sling actually does the decoding itself and does not use the servlet containers implicit decoding. However, the majority of POSTs and the ones I care about are not in multipart.
>>
>> Now, there can be a workaround: simply parse it yourself:
>> - GET: use request.getQueryString()
>> - POST: use request.getReader()
>>
>> This is what the servlet code base I mention is doing.
>>
>> However, the POST case relies on the fact that getReader() hasn't been called yet. But this is easily the case with form POSTs, since the first request.getParameter() will trigger decoding of the request body. And in Sling there will usually be at least one authentication handler that needs to do a request.getParameter(), and authentication handlers come before any servlet filter, so no luck to work around that.
>>
>> The proper way IMO would be to have sling take care of the correctly-ordered-re-decoding. This would need to be a change mostly in ParameterSupport [5] of the sling engine. It should probably be disabled by default (otherwise all parameters would always get decoded twice, once in the container and once in Sling) and could be opt-in on a per servlet basis - via means of some "sling.servlet.orderedparameters" option. Since the servlet is not resolved at the time of the auth handlers, this might in fact be tricky to do.
>>
>> WDYT?
>>
>> [1] http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
>> [2] http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/util/MultiMap.html#33
>> [3] http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/http/Parameters.java?revision=1525696&view=markup
>> [4] http://sling.apache.org/documentation/the-sling-engine/request-parameters.html
>> [5] http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/parameters/ParameterSupport.java?view=markup
>>
>> Cheers,
>> Alex
>
Re: Lost parameter order for form POSTs
Posted by Alexander Klimetschek <ak...@adobe.com>.
On 20.01.2014, at 02:43, Felix Meschberger <fm...@adobe.com> wrote:
> Ok, I have created a request parameter processing prototype in my whiteboard [1]:
>
> - Create new "parameters" bundle to reuse request parameter support
> independently of the Sling Engine
> - Added new ParameterSupport method to retrieve java.util.List of
> request parameter name-value pairs (of all parameters extracted)
> - Updated to Servlet API 3.0 (still need to make sure that we can also
> deploy this on Servlet API 2.4|5 environments
>
> WDYT ?
Cool :)
Would be nice to have a patch to look at just the changes. And an issue to discuss it. Shall I create one?
Cheers,
Alex
Re: Lost parameter order for form POSTs
Posted by Felix Meschberger <fm...@adobe.com>.
Hi
Ok, I have created a request parameter processing prototype in my whiteboard [1]:
- Create new "parameters" bundle to reuse request parameter support
independently of the Sling Engine
- Added new ParameterSupport method to retrieve java.util.List of
request parameter name-value pairs (of all parameters extracted)
- Updated to Servlet API 3.0 (still need to make sure that we can also
deploy this on Servlet API 2.4|5 environments
WDYT ?
Regards
Felix
[1] https://svn.apache.org/repos/asf/sling/whiteboard/fmeschbe/parameters
Am 16.01.2014 um 23:11 schrieb Alexander Klimetschek <ak...@adobe.com>:
> On 16.01.2014, at 02:15, Bertrand Delacretaz <bd...@apache.org> wrote:
>
>> This looks like a mini language, so I'd say using multiple parameters
>> for that is a bad idea, I'd rather have
>>
>> commands="layer 0 ; img mypic.jpg ; rotate 10 ; layer 1 ; img anotherpic.jpg"
>>
>> or something like that, as a single parameter.
>
> Well, maybe, but I can’t change that.
>
>> I'm not even sure if proxies and other HTTP intermediaries are
>> supposed to keep the order of parameters, not having to rely on that
>> is certainly safer.
>
> Yes they are, the URL is fixed and the response body (for a POST) as well.
>
> As I referred to above, the HTML spec clearly says that the order is important and based exactly on the order of form fields in the HTML.
>
> The problem is only introduced by the java servlet API that apparently didn’t read the spec.
>
> Cheers,
> Alex
Re: Lost parameter order for form POSTs
Posted by Carsten Ziegeler <cz...@apache.org>.
Yes, I would go with the whiteboard
http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html#using-the-whiteboard
Carsten
2014/1/17 Alexander Klimetschek <ak...@adobe.com>
> On 17.01.2014, at 01:54, Bertrand Delacretaz <bd...@apache.org>
> wrote:
>
> > You might generate the "commands" parameter In a Filter, maybe? At
> > least as a workaround until this is fixed in Sling, which might not be
> > that simple (I haven't looked in detail).
>
> If you mean Carsten's proposal of a osgi httpservice-level filter - yes,
> that could work. But I'd prefer a sling solution (over time), the necessary
> changes would be minimal.
>
> I found this [1] - either using org.apache.felix.http.api.ExtHttpService
> or the whiteboard approach to register filters on this level. Is this the
> right way?
>
> [1]
> http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html
>
> Cheers,
> Alex
--
Carsten Ziegeler
cziegeler@apache.org
Re: Lost parameter order for form POSTs
Posted by Alexander Klimetschek <ak...@adobe.com>.
On 17.01.2014, at 01:54, Bertrand Delacretaz <bd...@apache.org> wrote:
> You might generate the "commands" parameter In a Filter, maybe? At
> least as a workaround until this is fixed in Sling, which might not be
> that simple (I haven't looked in detail).
If you mean Carsten's proposal of a osgi httpservice-level filter - yes, that could work. But I'd prefer a sling solution (over time), the necessary changes would be minimal.
I found this [1] - either using org.apache.felix.http.api.ExtHttpService or the whiteboard approach to register filters on this level. Is this the right way?
[1] http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html
Cheers,
Alex
Re: Lost parameter order for form POSTs
Posted by Bertrand Delacretaz <bd...@apache.org>.
On Thu, Jan 16, 2014 at 11:11 PM, Alexander Klimetschek
<ak...@adobe.com> wrote:
> On 16.01.2014, at 02:15, Bertrand Delacretaz <bd...@apache.org> wrote:
>>...I'd rather have
>>
>> commands="layer 0 ; img mypic.jpg ; rotate 10 ; layer 1 ; img anotherpic.jpg"
>>
>> or something like that, as a single parameter.
>
> Well, maybe, but I can’t change that....
You might generate the "commands" parameter In a Filter, maybe? At
least as a workaround until this is fixed in Sling, which might not be
that simple (I haven't looked in detail).
-Bertrand
Re: Lost parameter order for form POSTs
Posted by Alexander Klimetschek <ak...@adobe.com>.
On 16.01.2014, at 02:15, Bertrand Delacretaz <bd...@apache.org> wrote:
> This looks like a mini language, so I'd say using multiple parameters
> for that is a bad idea, I'd rather have
>
> commands="layer 0 ; img mypic.jpg ; rotate 10 ; layer 1 ; img anotherpic.jpg"
>
> or something like that, as a single parameter.
Well, maybe, but I can’t change that.
> I'm not even sure if proxies and other HTTP intermediaries are
> supposed to keep the order of parameters, not having to rely on that
> is certainly safer.
Yes they are, the URL is fixed and the response body (for a POST) as well.
As I referred to above, the HTML spec clearly says that the order is important and based exactly on the order of form fields in the HTML.
The problem is only introduced by the java servlet API that apparently didn’t read the spec.
Cheers,
Alex
Re: Lost parameter order for form POSTs
Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,
On Wed, Jan 15, 2014 at 11:43 PM, Alexander Klimetschek
<ak...@adobe.com> wrote:
> In my case it is Adobe Scene7 - which uses rendering commands and layers with URLs such as this:
>
> "layer=0&img=mypic.jpg&rotate=10&layer=1&img=anotherpic.jpg"
>
> The location of the individual "layer" or "img" parts is important....
This looks like a mini language, so I'd say using multiple parameters
for that is a bad idea, I'd rather have
commands="layer 0 ; img mypic.jpg ; rotate 10 ; layer 1 ; img anotherpic.jpg"
or something like that, as a single parameter.
I'm not even sure if proxies and other HTTP intermediaries are
supposed to keep the order of parameters, not having to rely on that
is certainly safer.
-Bertrand
Re: Lost parameter order for form POSTs
Posted by Carsten Ziegeler <cz...@apache.org>.
Sure, you can register a plain http filter which runs before Sling.
The code can be generic, so be used by everyone who needs it, but it can be
configured to only run on specific requests (whatever the pattern is to
identify those) as the majority of requests does not need this
And by having a http filter doing the stuff, it's available to all java
based webapps and not tied to Sling
Carsten
2014/1/15 Alexander Klimetschek <ak...@adobe.com>
> In my case it is Adobe Scene7 - which uses rendering commands and layers
> with URLs such as this:
>
> "layer=0&img=mypic.jpg&rotate=10&layer=1&img=anotherpic.jpg"
>
> The location of the individual "layer" or "img" parts is important.
>
> It comes up whenever you want to map tree hierarchies (with ordered
> children) onto query parameters.
>
> Another use case I had was the Adobe CQ querybuilder, which I had to
> design with ugly number prefixes to work around the issue (1_property &
> 2_property and 3_group.1_property & 3_group.2_property).
>
> Regarding the filter outside Sling
> - is that possible to do within a Sling setup?
> - custom URL parsing to decide where to use it is bad when Sling should do
> it
> - discussion has shown it can be tricky (query vs. POST body, url vs.
> form-data)
> - custom query parsing in application code is not ideal
> - general availability of this with a clear API would be great (as a fix
> to the servlet API oversight here)
>
> Cheers,
> Alex
>
> On 15.01.2014, at 05:46, Mark Adamcin <ad...@gmail.com> wrote:
>
> > One example off the top of my head: Adobe Search&Promote relies on the
> order of query string parameters to persist the ordering of breadcrumbs
> across requests.
> >
> > Mark Adamcin
> > Acquity Group
> >
> > Sent from a mobile device.
> >
> >> On Jan 15, 2014, at 3:36 AM, Carsten Ziegeler <cz...@apache.org>
> wrote:
> >>
> >> I'm wondering if this is really a general use case? So far I've never
> >> encountered a web application which needs the order of the request
> >> parameters.
> >>
> >> As your use case even requires to get the order in the case of a POST we
> >> would need to add the additional logic you mention which is executed for
> >> all post requests no matter if the information is used.
> >>
> >> I'm wondering if its worth the effort - can't you simply register a
> servlet
> >> filter (not a Sling servlet filter) which does the magic for you for
> those
> >> URLs where it matters?
> >>
> >> Regards
> >> Carsten
> >>
> >>
> >> 2014/1/15 Alexander Klimetschek <ak...@adobe.com>
> >>
> >>>> On 14.01.2014, at 18:05, Felix Meschberger <fm...@adobe.com>
> wrote:
> >>>>
> >>>> Am 14.01.2014 um 18:44 schrieb Alexander Klimetschek <
> aklimets@adobe.com
> >>>> :
> >>>>
> >>>>> /do?move=10steps&turnLeft=45deg&move=20steps
> >>>>>
> >>>>> [...]
> >>>>>
> >>>>> This could be easily solved by extending Sling's RequestParameterMap
> >>> [0], e.g. with a method returning an key-value iterator:
> >>>>>
> >>>>> Iterator<KeyValuePair> getParameters()
> >>>>
> >>>> Well, that is a bit esoteric IMHO and I would not go as far as this.
> >>>
> >>> It's not esoteric – only if java servlet apis or other web frameworks
> >>> trained you to see parameters as a hash and not as an ordered key-value
> >>> pair list, what they really are!
> >>>
> >>> My use case here is an instruction set, where it does make a difference
> >>> where same-name commands (such as "move" in my above example) appear.
> >>> Unfortunately I can't change this, it is an existing system that is
> >>> migrated to Sling, with many clients out there, and changing the URL
> format
> >>> is not an option (the other option then would be to ditch Sling ;(.
> >>>
> >>>> In the case of a multipart/form-data the input is in general not a
> >>> string and can get quite big, particularly if files are involved.
> >>>
> >>> Right, that keep-the-string option would only apply to the url-encoded
> >>> case. I don't like it either.
> >>>
> >>> Another option would be to just change Sling so that I can extend the
> >>> parameter handling with some special service (i.e. an alternative to
> >>> wrapping the request object with a filter - what is done now in my
> case and
> >>> works in servlet engines).
> >>>
> >>> I would prefer the above Iterator API though (given the querystring
> format
> >>> in my case is compatible and I can change to request parameter api).
> >>>
> >>> Cheers,
> >>> Alex
> >>
> >>
> >>
> >>
> >> --
> >> Carsten Ziegeler
> >> cziegeler@apache.org
>
>
--
Carsten Ziegeler
cziegeler@apache.org
Re: Lost parameter order for form POSTs
Posted by Alexander Klimetschek <ak...@adobe.com>.
In my case it is Adobe Scene7 - which uses rendering commands and layers with URLs such as this:
"layer=0&img=mypic.jpg&rotate=10&layer=1&img=anotherpic.jpg"
The location of the individual "layer" or "img" parts is important.
It comes up whenever you want to map tree hierarchies (with ordered children) onto query parameters.
Another use case I had was the Adobe CQ querybuilder, which I had to design with ugly number prefixes to work around the issue (1_property & 2_property and 3_group.1_property & 3_group.2_property).
Regarding the filter outside Sling
- is that possible to do within a Sling setup?
- custom URL parsing to decide where to use it is bad when Sling should do it
- discussion has shown it can be tricky (query vs. POST body, url vs. form-data)
- custom query parsing in application code is not ideal
- general availability of this with a clear API would be great (as a fix to the servlet API oversight here)
Cheers,
Alex
On 15.01.2014, at 05:46, Mark Adamcin <ad...@gmail.com> wrote:
> One example off the top of my head: Adobe Search&Promote relies on the order of query string parameters to persist the ordering of breadcrumbs across requests.
>
> Mark Adamcin
> Acquity Group
>
> Sent from a mobile device.
>
>> On Jan 15, 2014, at 3:36 AM, Carsten Ziegeler <cz...@apache.org> wrote:
>>
>> I'm wondering if this is really a general use case? So far I've never
>> encountered a web application which needs the order of the request
>> parameters.
>>
>> As your use case even requires to get the order in the case of a POST we
>> would need to add the additional logic you mention which is executed for
>> all post requests no matter if the information is used.
>>
>> I'm wondering if its worth the effort - can't you simply register a servlet
>> filter (not a Sling servlet filter) which does the magic for you for those
>> URLs where it matters?
>>
>> Regards
>> Carsten
>>
>>
>> 2014/1/15 Alexander Klimetschek <ak...@adobe.com>
>>
>>>> On 14.01.2014, at 18:05, Felix Meschberger <fm...@adobe.com> wrote:
>>>>
>>>> Am 14.01.2014 um 18:44 schrieb Alexander Klimetschek <aklimets@adobe.com
>>>> :
>>>>
>>>>> /do?move=10steps&turnLeft=45deg&move=20steps
>>>>>
>>>>> [...]
>>>>>
>>>>> This could be easily solved by extending Sling's RequestParameterMap
>>> [0], e.g. with a method returning an key-value iterator:
>>>>>
>>>>> Iterator<KeyValuePair> getParameters()
>>>>
>>>> Well, that is a bit esoteric IMHO and I would not go as far as this.
>>>
>>> It's not esoteric – only if java servlet apis or other web frameworks
>>> trained you to see parameters as a hash and not as an ordered key-value
>>> pair list, what they really are!
>>>
>>> My use case here is an instruction set, where it does make a difference
>>> where same-name commands (such as "move" in my above example) appear.
>>> Unfortunately I can't change this, it is an existing system that is
>>> migrated to Sling, with many clients out there, and changing the URL format
>>> is not an option (the other option then would be to ditch Sling ;(.
>>>
>>>> In the case of a multipart/form-data the input is in general not a
>>> string and can get quite big, particularly if files are involved.
>>>
>>> Right, that keep-the-string option would only apply to the url-encoded
>>> case. I don't like it either.
>>>
>>> Another option would be to just change Sling so that I can extend the
>>> parameter handling with some special service (i.e. an alternative to
>>> wrapping the request object with a filter - what is done now in my case and
>>> works in servlet engines).
>>>
>>> I would prefer the above Iterator API though (given the querystring format
>>> in my case is compatible and I can change to request parameter api).
>>>
>>> Cheers,
>>> Alex
>>
>>
>>
>>
>> --
>> Carsten Ziegeler
>> cziegeler@apache.org
Re: Lost parameter order for form POSTs
Posted by Mark Adamcin <ad...@gmail.com>.
One example off the top of my head: Adobe Search&Promote relies on the order of query string parameters to persist the ordering of breadcrumbs across requests.
Mark Adamcin
Acquity Group
Sent from a mobile device.
> On Jan 15, 2014, at 3:36 AM, Carsten Ziegeler <cz...@apache.org> wrote:
>
> I'm wondering if this is really a general use case? So far I've never
> encountered a web application which needs the order of the request
> parameters.
>
> As your use case even requires to get the order in the case of a POST we
> would need to add the additional logic you mention which is executed for
> all post requests no matter if the information is used.
>
> I'm wondering if its worth the effort - can't you simply register a servlet
> filter (not a Sling servlet filter) which does the magic for you for those
> URLs where it matters?
>
> Regards
> Carsten
>
>
> 2014/1/15 Alexander Klimetschek <ak...@adobe.com>
>
>>> On 14.01.2014, at 18:05, Felix Meschberger <fm...@adobe.com> wrote:
>>>
>>> Am 14.01.2014 um 18:44 schrieb Alexander Klimetschek <aklimets@adobe.com
>>> :
>>>
>>>> /do?move=10steps&turnLeft=45deg&move=20steps
>>>>
>>>> [...]
>>>>
>>>> This could be easily solved by extending Sling's RequestParameterMap
>> [0], e.g. with a method returning an key-value iterator:
>>>>
>>>> Iterator<KeyValuePair> getParameters()
>>>
>>> Well, that is a bit esoteric IMHO and I would not go as far as this.
>>
>> It's not esoteric – only if java servlet apis or other web frameworks
>> trained you to see parameters as a hash and not as an ordered key-value
>> pair list, what they really are!
>>
>> My use case here is an instruction set, where it does make a difference
>> where same-name commands (such as "move" in my above example) appear.
>> Unfortunately I can't change this, it is an existing system that is
>> migrated to Sling, with many clients out there, and changing the URL format
>> is not an option (the other option then would be to ditch Sling ;(.
>>
>>> In the case of a multipart/form-data the input is in general not a
>> string and can get quite big, particularly if files are involved.
>>
>> Right, that keep-the-string option would only apply to the url-encoded
>> case. I don't like it either.
>>
>> Another option would be to just change Sling so that I can extend the
>> parameter handling with some special service (i.e. an alternative to
>> wrapping the request object with a filter - what is done now in my case and
>> works in servlet engines).
>>
>> I would prefer the above Iterator API though (given the querystring format
>> in my case is compatible and I can change to request parameter api).
>>
>> Cheers,
>> Alex
>
>
>
>
> --
> Carsten Ziegeler
> cziegeler@apache.org
Re: Lost parameter order for form POSTs
Posted by Carsten Ziegeler <cz...@apache.org>.
I'm wondering if this is really a general use case? So far I've never
encountered a web application which needs the order of the request
parameters.
As your use case even requires to get the order in the case of a POST we
would need to add the additional logic you mention which is executed for
all post requests no matter if the information is used.
I'm wondering if its worth the effort - can't you simply register a servlet
filter (not a Sling servlet filter) which does the magic for you for those
URLs where it matters?
Regards
Carsten
2014/1/15 Alexander Klimetschek <ak...@adobe.com>
> On 14.01.2014, at 18:05, Felix Meschberger <fm...@adobe.com> wrote:
>
> > Am 14.01.2014 um 18:44 schrieb Alexander Klimetschek <aklimets@adobe.com
> >:
> >
> >> /do?move=10steps&turnLeft=45deg&move=20steps
> >>
> >> [...]
> >>
> >> This could be easily solved by extending Sling's RequestParameterMap
> [0], e.g. with a method returning an key-value iterator:
> >>
> >> Iterator<KeyValuePair> getParameters()
> >
> > Well, that is a bit esoteric IMHO and I would not go as far as this.
>
> It's not esoteric – only if java servlet apis or other web frameworks
> trained you to see parameters as a hash and not as an ordered key-value
> pair list, what they really are!
>
> My use case here is an instruction set, where it does make a difference
> where same-name commands (such as "move" in my above example) appear.
> Unfortunately I can't change this, it is an existing system that is
> migrated to Sling, with many clients out there, and changing the URL format
> is not an option (the other option then would be to ditch Sling ;(.
>
> > In the case of a multipart/form-data the input is in general not a
> string and can get quite big, particularly if files are involved.
>
> Right, that keep-the-string option would only apply to the url-encoded
> case. I don't like it either.
>
> Another option would be to just change Sling so that I can extend the
> parameter handling with some special service (i.e. an alternative to
> wrapping the request object with a filter - what is done now in my case and
> works in servlet engines).
>
> I would prefer the above Iterator API though (given the querystring format
> in my case is compatible and I can change to request parameter api).
>
> Cheers,
> Alex
--
Carsten Ziegeler
cziegeler@apache.org
Re: Lost parameter order for form POSTs
Posted by Alexander Klimetschek <ak...@adobe.com>.
On 14.01.2014, at 18:05, Felix Meschberger <fm...@adobe.com> wrote:
> Am 14.01.2014 um 18:44 schrieb Alexander Klimetschek <ak...@adobe.com>:
>
>> /do?move=10steps&turnLeft=45deg&move=20steps
>>
>> [...]
>>
>> This could be easily solved by extending Sling's RequestParameterMap [0], e.g. with a method returning an key-value iterator:
>>
>> Iterator<KeyValuePair> getParameters()
>
> Well, that is a bit esoteric IMHO and I would not go as far as this.
It's not esoteric – only if java servlet apis or other web frameworks trained you to see parameters as a hash and not as an ordered key-value pair list, what they really are!
My use case here is an instruction set, where it does make a difference where same-name commands (such as "move" in my above example) appear. Unfortunately I can't change this, it is an existing system that is migrated to Sling, with many clients out there, and changing the URL format is not an option (the other option then would be to ditch Sling ;(.
> In the case of a multipart/form-data the input is in general not a string and can get quite big, particularly if files are involved.
Right, that keep-the-string option would only apply to the url-encoded case. I don't like it either.
Another option would be to just change Sling so that I can extend the parameter handling with some special service (i.e. an alternative to wrapping the request object with a filter - what is done now in my case and works in servlet engines).
I would prefer the above Iterator API though (given the querystring format in my case is compatible and I can change to request parameter api).
Cheers,
Alex
Re: Lost parameter order for form POSTs
Posted by Felix Meschberger <fm...@adobe.com>.
Hi
Am 14.01.2014 um 18:44 schrieb Alexander Klimetschek <ak...@adobe.com>:
> I forgot 2 important points ;) (see below for responses to the existing discussion)
>
> (1)
>
> Just using an ordered map instead of a hash map doesn't solve the problem, as multi-value parameters still get merged and their invidual position lost. For example, an URL like this (which is similar to my case):
>
> /do?move=10steps&turnLeft=45deg&move=20steps
>
> Would become this ordered map:
> move => 10steps,20steps
> turnLeft => 45deg
>
> but I really want to get the original order.
>
> This could be easily solved by extending Sling's RequestParameterMap [0], e.g. with a method returning an key-value iterator:
>
> Iterator<KeyValuePair> getParameters()
Well, that is a bit esoteric IMHO and I would not go as far as this.
>
> (2)
>
> OTOH, I notice that the code I am looking at does its own parameter parsing anyway, and so basically requires the query string as input, even if it is a POST - for that it currently does request.getReader() and converts it into a string in a filter. It doesn't use any request parameter API. Changing it might lead to issues, I am not sure if it uses the exact same parsing logic that servlet engines are supposed to do…
>
> So another solution would be to allow access to the request body as string even if already parsed for parameters. I guess Sling doesn't store it, so adding this might give a memory usage penalty by storing that possibly long string (again, because you'd have to always do it before you even know if a servlet asks for it). Not sure…
In the case of a multipart/form-data the input is in general not a string and can get quite big, particularly if files are involved.
I am also not sure about the benefit of this and the use case.
Regards
Felix
>
> On 14.01.2014, at 16:27, Felix Meschberger <fm...@adobe.com> wrote:
>
>>> The question is:
>>> Is it ok to always redecode query strings (in addition to the parsing done by the servlet container)?
>>
>> I would imagine that we would grab request.getQueryString() and parse and decode it (in two goes — we first have to find whether there is a _charset_ parameter)
>
> Right, the _charset_ needs to be checked first.
>
>> For inside the Sling Engine, this is already taken care of by the Authentication Handlers actually getting a wrapped request object which hooks into the Sling Engine's parameter support.
>
> Good.
>
>> For outside the sling engine, this is not the case yet.
>
> I don't care :)
>
>> I think, once we are in control of parsing the parameters, we don't let the servlet container do it.
>
> I was under the assumption that servlet containers will parse the query string in any case and only do POST body parsing on demand (i.e. when a getParameter etc. gets called). But that was probably wrong, Jetty for example does it lazily [1].
>
> This simplifies it a lot and we can always ensure the right order for all servlets in sling without sacrifying performance.
>
>> In fact, I could as another option, even imagine that the ParameterSupport is registered as a filter, which does not even allow the servlet container to do anything — at least for the Http Service inside of Sling.
>
> I would not do it as a filter but as part of the sling engine i.e. the core sling servlet request impl which wraps the original request anyway. Sling already does decoding to some degree, we just improve it with keeping the order.
>
> We would need to update theAPI docs then to say that the getRequestParameterMap() (sling specific) and getParameterMap() (from servlet api) return an ordered map.
>
> [0] http://sling.apache.org/apidocs/sling5/org/apache/sling/api/request/RequestParameterMap.html
> [1] http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/server/Request.html#244
>
> Cheers,
> Alex
>
Re: Lost parameter order for form POSTs
Posted by Alexander Klimetschek <ak...@adobe.com>.
I forgot 2 important points ;) (see below for responses to the existing discussion)
(1)
Just using an ordered map instead of a hash map doesn't solve the problem, as multi-value parameters still get merged and their invidual position lost. For example, an URL like this (which is similar to my case):
/do?move=10steps&turnLeft=45deg&move=20steps
Would become this ordered map:
move => 10steps,20steps
turnLeft => 45deg
but I really want to get the original order.
This could be easily solved by extending Sling's RequestParameterMap [0], e.g. with a method returning an key-value iterator:
Iterator<KeyValuePair> getParameters()
(2)
OTOH, I notice that the code I am looking at does its own parameter parsing anyway, and so basically requires the query string as input, even if it is a POST - for that it currently does request.getReader() and converts it into a string in a filter. It doesn't use any request parameter API. Changing it might lead to issues, I am not sure if it uses the exact same parsing logic that servlet engines are supposed to do...
So another solution would be to allow access to the request body as string even if already parsed for parameters. I guess Sling doesn't store it, so adding this might give a memory usage penalty by storing that possibly long string (again, because you'd have to always do it before you even know if a servlet asks for it). Not sure...
On 14.01.2014, at 16:27, Felix Meschberger <fm...@adobe.com> wrote:
>> The question is:
>> Is it ok to always redecode query strings (in addition to the parsing done by the servlet container)?
>
> I would imagine that we would grab request.getQueryString() and parse and decode it (in two goes — we first have to find whether there is a _charset_ parameter)
Right, the _charset_ needs to be checked first.
> For inside the Sling Engine, this is already taken care of by the Authentication Handlers actually getting a wrapped request object which hooks into the Sling Engine's parameter support.
Good.
> For outside the sling engine, this is not the case yet.
I don't care :)
> I think, once we are in control of parsing the parameters, we don't let the servlet container do it.
I was under the assumption that servlet containers will parse the query string in any case and only do POST body parsing on demand (i.e. when a getParameter etc. gets called). But that was probably wrong, Jetty for example does it lazily [1].
This simplifies it a lot and we can always ensure the right order for all servlets in sling without sacrifying performance.
> In fact, I could as another option, even imagine that the ParameterSupport is registered as a filter, which does not even allow the servlet container to do anything — at least for the Http Service inside of Sling.
I would not do it as a filter but as part of the sling engine i.e. the core sling servlet request impl which wraps the original request anyway. Sling already does decoding to some degree, we just improve it with keeping the order.
We would need to update theAPI docs then to say that the getRequestParameterMap() (sling specific) and getParameterMap() (from servlet api) return an ordered map.
[0] http://sling.apache.org/apidocs/sling5/org/apache/sling/api/request/RequestParameterMap.html
[1] http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/server/Request.html#244
Cheers,
Alex
Re: Lost parameter order for form POSTs
Posted by Felix Meschberger <fm...@adobe.com>.
Hi
Am 14.01.2014 um 12:37 schrieb Alexander Klimetschek <ak...@adobe.com>:
> On 13.01.2014, at 21:17, Felix Meschberger <fm...@adobe.com> wrote:
>
>> Having said this, I could imagine taking a hybrid approach:
>>
>> (1) For url-encoded POST request, check whether InputStream is available
>> (1a) if available:
>> - decode query string
>> - decode input stream
>> (1b) if not available
>> - take all parameters from servlet container
>> (2) For form-data POST request
>> - decode query string
>> - decode POST input (as done today)
>
> Sounds good - some additions:
>
> The GET case is missing:
>
> (0) GET request
> - decode query string
Right
>
> And I think in a normal Sling setup, (1b) should never be the case, as nobody else should read the input stream beforehand (or does the OSGi http service allow for earlier "hooks"?). So that's ok.
Yes, and see below for more on this
>
> The question is:
> Is it ok to always redecode query strings (in addition to the parsing done by the servlet container)?
I would imagine that we would grab request.getQueryString() and parse and decode it (in two goes — we first have to find whether there is a _charset_ parameter)
>
> For POST bodies, it can happen only once, as done for form-data today already (afaics), by carefully not triggering the servlet container parsing with the first call to getParameter() etc.
>
> If redecoding is not ok, and should only happen when a servlet needs it, we probably have to handle authentication handlers separately from once the servlet has been selected and the per-servlet-config is clear.
For inside the Sling Engine, this is already taken care of by the Authentication Handlers actually getting a wrapped request object which hooks into the Sling Engine's parameter support.
For outside the sling engine, this is not the case yet.
I think, once we are in control of parsing the parameters, we don't let the servlet container do it. In fact, I could as another option, even imagine that the ParameterSupport is registered as a filter, which does not even allow the servlet container to do anything — at least for the Http Service inside of Sling.
Regards
Felix
> This means a mixed approach, for which I see two options:
>
> (I) avoid for GET only
> - do (1) and (2) early on already (it has to be read by Sling anyway)
> - but (0) (the GET query redecoding) only happens later, so the request parameter map must be updated once the servlet config says "order params"
>
> (II) avoid always
> - (1) and (2) will not redecode the query string, but keep decoding the input stream (with order) and remember that part
> - (0) GET query redecoding happens later
> - later (1) and (2) will be done again, by redecoding the query string and merging it with the existing decoded input stream
>
> This minimizes performance impact (if implemented efficiently), but will give you no full order for a POST with a query and a body in the early stage; it's very rare, so nothing we need to optimize for, and authentication handlers would never care. IMO it is important to not add a performance penalty for _all_ existing servlets.
>
> Cheers,
> Alex
Re: Lost parameter order for form POSTs
Posted by Alexander Klimetschek <ak...@adobe.com>.
On 13.01.2014, at 21:17, Felix Meschberger <fm...@adobe.com> wrote:
> Having said this, I could imagine taking a hybrid approach:
>
> (1) For url-encoded POST request, check whether InputStream is available
> (1a) if available:
> - decode query string
> - decode input stream
> (1b) if not available
> - take all parameters from servlet container
> (2) For form-data POST request
> - decode query string
> - decode POST input (as done today)
Sounds good - some additions:
The GET case is missing:
(0) GET request
- decode query string
And I think in a normal Sling setup, (1b) should never be the case, as nobody else should read the input stream beforehand (or does the OSGi http service allow for earlier "hooks"?). So that's ok.
The question is:
Is it ok to always redecode query strings (in addition to the parsing done by the servlet container)?
For POST bodies, it can happen only once, as done for form-data today already (afaics), by carefully not triggering the servlet container parsing with the first call to getParameter() etc.
If redecoding is not ok, and should only happen when a servlet needs it, we probably have to handle authentication handlers separately from once the servlet has been selected and the per-servlet-config is clear. This means a mixed approach, for which I see two options:
(I) avoid for GET only
- do (1) and (2) early on already (it has to be read by Sling anyway)
- but (0) (the GET query redecoding) only happens later, so the request parameter map must be updated once the servlet config says "order params"
(II) avoid always
- (1) and (2) will not redecode the query string, but keep decoding the input stream (with order) and remember that part
- (0) GET query redecoding happens later
- later (1) and (2) will be done again, by redecoding the query string and merging it with the existing decoded input stream
This minimizes performance impact (if implemented efficiently), but will give you no full order for a POST with a query and a body in the early stage; it's very rare, so nothing we need to optimize for, and authentication handlers would never care. IMO it is important to not add a performance penalty for _all_ existing servlets.
Cheers,
Alex
Re: Lost parameter order for form POSTs
Posted by Felix Meschberger <fm...@adobe.com>.
Hi
Well, all is not lost since, event if the AuthenticationHandler is reading the parameters the Sling Engine's ParameterSupport actually kicks in ! (We do this to support mutipart/form-data submission of login forms).
Yet, I am a bit reluctant to replicate servlet container work here.
Yet, on the other hand, we could maybe solve another problem more elegantly: the problem of parameter encoding, where we currently employ a "reencoding" technique which might be simplified if we do it ourselves.
Yet, I still suggest you use multipart/form-data for any HTML form submission. The other forms have serious drawbacks, particularly the querystring method.
Having said this, I could imagine taking a hybrid approach:
(1) For url-encoded POST request, check whether InputStream is available
(1a) if available:
- decode query string
- decode input stream
(1b) if not available
- take all parameters from servlet container
(2) For form-data POST request
- decode query string
- decode POST input (as done today)
WDYT ?
Regards
Felix
Am 13.01.2014 um 21:26 schrieb Alexander Klimetschek <ak...@adobe.com>:
> Hi,
>
> Sling currently does not allow to read request parameters in their original order. I need this for a migrated servlet code that used to run on other servlet containers and was able to preserve the order (see below), thus has URL schemes where paramter order is crucial.
>
> Actually it is the java servlet spec that messed up things here: the HTML forms definition [1] clearly says that the order in the form encoding should be preserved (both for GET and POST), but the servlet API uses the "Map" interface and doesn't specify that this has to be an ordered map. Hence most/all the servlet containers implement this using a HashMap (Jetty [2], Adobe's CQSE; only Tomcat seems to be an exception [3]), and you can't rely on it.
>
> Note that while Sling does try to _ensure_ the order of parameters as noted on [4], this only means it keeps the container order, and only for the special case of "multipart/form-data" POSTs, where Sling actually does the decoding itself and does not use the servlet containers implicit decoding. However, the majority of POSTs and the ones I care about are not in multipart.
>
> Now, there can be a workaround: simply parse it yourself:
> - GET: use request.getQueryString()
> - POST: use request.getReader()
>
> This is what the servlet code base I mention is doing.
>
> However, the POST case relies on the fact that getReader() hasn't been called yet. But this is easily the case with form POSTs, since the first request.getParameter() will trigger decoding of the request body. And in Sling there will usually be at least one authentication handler that needs to do a request.getParameter(), and authentication handlers come before any servlet filter, so no luck to work around that.
>
> The proper way IMO would be to have sling take care of the correctly-ordered-re-decoding. This would need to be a change mostly in ParameterSupport [5] of the sling engine. It should probably be disabled by default (otherwise all parameters would always get decoded twice, once in the container and once in Sling) and could be opt-in on a per servlet basis - via means of some "sling.servlet.orderedparameters" option. Since the servlet is not resolved at the time of the auth handlers, this might in fact be tricky to do.
>
> WDYT?
>
> [1] http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
> [2] http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/util/MultiMap.html#33
> [3] http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/http/Parameters.java?revision=1525696&view=markup
> [4] http://sling.apache.org/documentation/the-sling-engine/request-parameters.html
> [5] http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/parameters/ParameterSupport.java?view=markup
>
> Cheers,
> Alex