quick urlEncode

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

quick urlEncode

rush
Hello,

could anyone point me where I could quickly obtain urlEncode function? (I
know I could hack my own, but first I would need to find exact definiton,
and so on)

rush
--
http://www.templatetamer.com/
http://www.folderscavenger.com/


Reply | Threaded
Open this post in threaded view
|

Re: quick urlEncode

Chris Uppal-3
rush,

> could anyone point me where I could quickly obtain urlEncode function? (I
> know I could hack my own, but first I would need to find exact definiton,
> and so on)

If you're using Steve Waring's stuff then I think that SptHTTPUrl
class>>canonicalsize: and #uncanonicalize: do URL encoding and decoding.  The
decoding actually delegates to the builtin (in D5) String>>unescapePercents.
Package 'SW HTTPClient Support'.

The space handling might not be what you want (spaces in URLs can get encoded
as '%20' or '+' according to context).  What it does is sensible:

SptHTTPUrl canonicalize: 'Hello there' --> 'Hello%20there'
SptHTTPUrl uncanonicalize:'Hello%20there'  --> 'Hello there'
SptHTTPUrl uncanonicalize:'Hello+there'  --> 'Hello there'

Note also that the output is not suitable for embedding directly into HTML/XML
source text since it (correctly) doesn't change & into &

    -- chris


Reply | Threaded
Open this post in threaded view
|

Re: quick urlEncode

Fernando Rodríguez
In reply to this post by rush
Hello rush,


See if this helps:

| package |
package := Package name: 'internet'.
package paxVersion: 0;
        basicComment: 'General internet'.


package methodNames
        add: #Character -> #isSafeForHTTP;
        add: #String -> #decodeForHTTP;
        add: #String -> #encodeForHTTP;
        yourself.

package binaryGlobalNames: (Set new
        yourself).

package globalAliases: (Set new
        yourself).

package allResourceNames: (Set new
        yourself).

package setPrerequisites: (IdentitySet new
        add: '..\..\..\..\Documents and Settings\Fernando\Mis documentos\Dolphin
Smalltalk 5.1\Object Arts\Dolphin\Base\Dolphin';
        yourself).

package!

"Class Definitions"!


"Global Aliases"!


"Loose Methods"!

!Character methodsFor!

isSafeForHTTP
        "whether a character is 'safe', or needs to be escaped when used, eg, in
a URL"
        ^ asciiValue < 128
                and: [self isAlphaNumeric
                                or: ['.~-_' includes: self]]! !
!Character categoriesFor: #isSafeForHTTP!internet!public! !

!String methodsFor!

decodeForHTTP
       
        "change each %XY substring to the character with ASCII value XY in hex.
 This is the opposite of #encodeForHTTP"
        | ans c asciiVal pos oldPos specialChars |
        ans:= WriteStream on: String new.
        oldPos:= 1.
        specialChars:= '+%'.

        [pos:= self indexOfAnyOf: specialChars startingAt: oldPos. pos > 0]
        whileTrue: [
                ans nextPutAll: (self copyFrom: oldPos to: pos - 1).
                c:= self at: pos.
                c = $+ ifTrue: [ans nextPut: $ ] ifFalse: [
                        (c = $% and: [pos + 2 <= self size]) ifTrue: [
                                asciiVal:= (self at: pos+1) asUppercase digitValue * 16 +
                                        (self at: pos+2) asUppercase digitValue.
                                pos:= pos + 2.
                                asciiVal > 255 ifTrue: [^self]. "not really an escaped string"
                                ans nextPut: (Character value: asciiVal)]
                        ifFalse: [ans nextPut: c]].
                oldPos:= pos+1].
        ans nextPutAll: (self copyFrom: oldPos to: self size).
        ^ ans contents!

encodeForHTTP
        "change dangerous characters to their %XX form, for use in HTTP transactions"
        | encodedStream |
        encodedStream := WriteStream on: (String new).
       
        self do: [ :c |
                c isSafeForHTTP ifTrue: [ encodedStream nextPut: c ] ifFalse: [
                        encodedStream nextPut: $%.
                        encodedStream nextPut: (c asciiValue // 16) asHexDigit.
                        encodedStream nextPut: (c asciiValue \\ 16) asHexDigit.
                ]
        ].
        ^encodedStream contents. ! !
!String categoriesFor: #decodeForHTTP!internet!public! !
!String categoriesFor: #encodeForHTTP!internet!public! !

"End of package definition"!

"Source Globals"!

"Classes"!

"Binary Globals"!

"Resources"!


> Hello,
>
> could anyone point me where I could quickly obtain urlEncode function?
> (I know I could hack my own, but first I would need to find exact
> definiton, and so on)
>
> rush
> --
> http://www.templatetamer.com/
> http://www.folderscavenger.com/


Reply | Threaded
Open this post in threaded view
|

Re: quick urlEncode

rush
"Fernando Rodríguez" <[hidden email]> wrote in message
news:[hidden email]...
> Hello rush,
>
> See if this helps:

thanks to both!

rush
--
http://www.templatetamer.com/
http://www.folderscavenger.com/