[squeak-dev] HTTP Encoding and decoding

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

[squeak-dev] HTTP Encoding and decoding

Robert Krahn
Hi,

the following expressions returns false in 3.9.1:

('mö:' encodeForHTTP unescapePercents) = 'mö:'

although #unescapePercents should be the opposite from #encodeForHTTP  
(as stated in the method comment).

I assume this is a bug?

Best,
Robert
Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] HTTP Encoding and decoding

Lincke, Jens
Hi,

I tried some examples to narrow it:

'ö' encodeForHTTP  = '%C3%B6'
'%C3%B6'unescapePercents  = 'ö'

':'  encodeForHTTP  = '%3A'
'%3A' unescapePercents =  ':'

'ö:' encodeForHTTP =  '%C3%B6%3A%B6'
'%C3%B6%3A%B6' unescapePercents => 'ö:¶'
'%C3%B6%3A' unescapePercents  => 'ö:'

the problem  happens in encodeForHTTPWithTextEncoding: encodingName
conditionBlock: conditionBlock

where it is assumed that "encodedStream text; reset." clears the contents.

I don't know what is the correct behavior of
MultiByteBinaryOrTextStream, but

encodedStream _ MultiByteBinaryOrTextStream on: (String new: 6).

solves the problem, for once... but it ist not fast.

with the attached fix, "('mö:' encodeForHTTP unescapePercents) = 'mö:' "
evals to true

- Jens -

Robert Krahn schrieb:

> Hi,
>
> the following expressions returns false in 3.9.1:
>
> ('mö:' encodeForHTTP unescapePercents) = 'mö:'
>
> although #unescapePercents should be the opposite from #encodeForHTTP
> (as stated in the method comment).
>
> I assume this is a bug?
>
> Best,
> Robert

'From Squeak3.9 of 7 November 2006 [latest update: #7067] on 12 May 2008 at 6:12:19 pm'!

!String methodsFor: 'converting' stamp: 'jl 5/12/2008 18:06'!
encodeForHTTPWithTextEncoding: encodingName conditionBlock: conditionBlock
        "change dangerous characters to their %XX form, for use in HTTP transactions"

        | httpSafeStream encodedStream cont |
        httpSafeStream _ WriteStream on: (String new).
        encodedStream _ MultiByteBinaryOrTextStream on: (String new: 6).
        encodedStream converter: (TextConverter newForEncoding: encodingName).
        self do: [:c |
                (conditionBlock value: c)
                        ifTrue: [httpSafeStream nextPut: (Character value: c charCode)]
                        ifFalse: [
                                encodedStream _ MultiByteBinaryOrTextStream on: (String new: 6).
                                encodedStream text; reset.
                                encodedStream nextPut: c.
                                encodedStream position: 0.
                                encodedStream binary.
                                cont _ encodedStream contents.
                                cont do: [:byte |
                                        httpSafeStream nextPut: $%.
                                        httpSafeStream nextPut: (byte // 16) asHexDigit.
                                        httpSafeStream nextPut: (byte \\ 16) asHexDigit.
                                ].
                        ].
        ].
        ^ httpSafeStream contents.
! !