Raised This Month: $32 Target: $400
 8% 

[STOCK] Url encode stock


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 02-04-2010 , 08:00   [STOCK] Url encode stock
Reply With Quote #1

Sometimes you need to encode parameters before send it to an http server.

For example you might want to do this:
Code:
GET /some/script.php?parameter=Hello world!!!~
But server will get only this:
Code:
GET /some/script.php?parameter=Hello
How to fix that? Encode parameters values following some rules (spaces, non-printable characters, etc).

Here's a stock that makes this work for you

PHP Code:
new const sHexTable[] = "0123456789abcdef"

stock urlencode(const sString[], sResult[], len)
{
    new 
fromc
    
new to

    
while(from len)
    {
        
sString[from++]
        if(
== 0)
        {
            
sResult[to++] = c
            
break
        }
        else if(
== ' ')
        {
            
sResult[to++] = '+'
        
}
        else if((
'0' && != '-' && != '.') ||
                (
'A' && '9') ||
                (
'Z' && 'a' && != '_') ||
                (
'z'))
        {
            if((
to 3) > len)
            {
                
sResult[to] = 0
                
break
            }
            
sResult[to++] = '%'
            
sResult[to++] = sHexTable[>> 4]
            
sResult[to++] = sHexTable[15]
        }
        else
        {
            
sResult[to++] = c
        
}
    }

How to use?

PHP Code:
public somefunc()
{
    new 
source[64] = "Hello ~ world!"
    
new output[128]

    
urlencode(sourceoutputcharsmax(output)
    
server_print("original= %s"output)
    
server_print("encoded= %s"source)

And you will get

Quote:
original= Hello ~ world!
encoded= Hello+%7e+world!
Then you can use encoded value to add to your GET/POST request.
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.

Last edited by joropito; 06-20-2010 at 20:48.
joropito is offline
Send a message via MSN to joropito
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 02-04-2010 , 15:07   Re: [STOCK] Url encode stock
Reply With Quote #2

Nice, yesterday I was having problem with System.Web.Server.URLEncode and I create my func.

But your func work better than my. I will update
__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-04-2010 , 15:14   Re: [STOCK] Url encode stock
Reply With Quote #3

I wrote one awhile ago but never thought anyone would need it.
Yours actually has an error where you can exceed the "len" parameter in the output string.

Code:
stock StringURLEncode( const szInput[ ], szOutput[ ], const iLen ) {     static const HEXCHARS[ 16 ] = {         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,         0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66     };         new iPos, cChar, iFLen;     while( ( cChar = szInput[ iPos ] ) && iFLen < iLen )     {         if( cChar == 0x20 )         {             szOutput[ iFLen++ ] = 0x2B;         }         else if( !( 0x41 <= cChar <= 0x5A )         && !( 0x61 <= cChar <= 0x7A )         && !( 0x30 <= cChar <= 0x39 )         && cChar != 0x2D         && cChar != 0x2E         && cChar != 0x5F )         {             if( ( iFLen + 3 ) > iLen )             {                 break;             }             else if( cChar > 0xFF             || cChar < 0x00 )             {                 cChar = 0x2A;             }                         szOutput[ iFLen++ ] = 0x25;             szOutput[ iFLen++ ] = HEXCHARS[ cChar >> 4 ];             szOutput[ iFLen++ ] = HEXCHARS[ cChar & 15 ];         }         else         {             szOutput[ iFLen++ ] = cChar;         }                 iPos++;     }         szOutput[ iFLen ] = 0;     return iFLen; }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 11-02-2010 at 18:39.
Exolent[jNr] is offline
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 02-04-2010 , 15:14   Re: [STOCK] Url encode stock
Reply With Quote #4

Quote:
Originally Posted by AntiBots View Post
Nice, yesterday I was having problem with System.Web.Server.URLEncode and I create my func.

But your func work better than my. I will update
I copied the function from php source code.
You have urlencode function in php.
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 02-04-2010 , 15:48   Re: [STOCK] Url encode stock
Reply With Quote #5

Quote:
Originally Posted by joropito View Post
I copied the function from php source code.
You have urlencode function in php.
I have Http Solution also in .NET.

Yep, I see that function in a PHP Forum.
__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
Dygear
SourceMod Donor
Join Date: Apr 2004
Location: Levittown, NY
Old 02-05-2010 , 00:20   Re: [STOCK] Url encode stock
Reply With Quote #6

Quote:
Originally Posted by joropito View Post
PHP Code:
new const sHexTable[] = "0123456789abcdef" 
[...]
PHP Code:
        else if(('0' && != '-' && != '.') ||
                (
'A' && '9') ||
                (
'Z' && 'a' && != '_') ||
                (
'z'))
        {
            
sResult[to] = '%'
            
sResult[to+1] = sHexTable[>> 4]
            
sResult[to+2] = sHexTable[15]
            
to += 3
        

I'm perticually intrested in these two lines:

PHP Code:
            sResult[to+1] = sHexTable[>> 4]
            
sResult[to+2] = sHexTable[15
I understand that >> is an arithmetic shift to the right of c by 4 bits, as described in the Pawn Language Guide Pg. 106.
I understand that & is a bitwise logical "and" as described in the Pawn Language Guide Pg. 107.

Code:
1,000s  |                     1           
Hundreds|                     0521        
Tens    |                     2152621     
Units   |                     42684368421 
________|_______________________________________
         00000000000000000000000001111110 ( 126)| ASCII Charater '~'.
     SAR 00000000000000000000000000000100 (   4)| Signed Arithmetic Left (By 4).
       = 00000000000000000000000000000111 (   7)|
         --------------------------------=======+ 
         00000000000000000000000001111110 ( 126)| ASCII Charater '~'.
     AND 00000000000000000000000000001111 (  15)| Bitwise logical AND: 126 & 15.
       = 00000000000000000000000000001110 (  14)| sHexTable[14] = e.
         --------------------------------=======+
That's pretty cool!
__________________

Last edited by Dygear; 02-05-2010 at 00:27. Reason: That's awesome!
Dygear is offline
Send a message via AIM to Dygear Send a message via MSN to Dygear Send a message via Skype™ to Dygear
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 02-05-2010 , 07:48   Re: [STOCK] Url encode stock
Reply With Quote #7

Yeah, it's a pretty way to get:

Quote:
Originally Posted by Dygear View Post
sResult[to+1] = sHexTable[c >> 4]
4 most significant bits (left nibble)

Quote:
Originally Posted by Dygear View Post
sResult[to+2] = sHexTable[c & 15]
4 least significant bits (right nibble)
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 02-05-2010 , 08:14   Re: [STOCK] Url encode stock
Reply With Quote #8

Quote:
Originally Posted by Exolent[jNr] View Post
Yours actually has an error where you can exceed the "len" parameter in the output string.
Yeah, fixed.
Look that yours has an error too.
If you don't have enought room for that 3bytes, you break the while without putting 0 at end of szOutput, broken that string... I'm right?

I have a question, why you're putting 0x2a when you have 0x00 or 0xff?
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-20-2010 , 14:31   Re: [STOCK] Url encode stock
Reply With Quote #9

Wow. I'm surprised nobody found out neither of these two stocks work correctly.

Joropito's code:

Code:
			if((len + 3) > c)
			{
				sResult[c] = 0
				break
			}
This is far from correct variable "c" is a character in the string and you use it to compare to length AND as an index for sResult!

I believe it should be:

Code:
			if((to + 3) > len)
			{
				sResult[to] = 0
				break
			}
Exolent's code:

Code:
static const HEXCHARS[ 16 ] = {
        0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
        0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F
    };
HEXCHARS = "0123456789:;<=>?"

It should be:

Code:
    static const HEXCHARS[ 16 ] = {
        0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
        0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
    };
Also, shouldn't you terminate the string if you don't have the extra 3 spots for the encoding?

Code:
            if( ( iFLen + 3 ) > iLen )
            {
                break;
            }

Code:
            if( ( iFLen + 3 ) > iLen )
            {
                szOutput[ iFLen ] = 0;
                break;
            }
__________________
fysiks is offline
Alucard^
AMXX Moderator: Others
Join Date: Sep 2007
Location: Street
Old 06-20-2010 , 17:10   Re: [STOCK] Url encode stock
Reply With Quote #10

I have a little question...

I don't understand this simple thing in the code:

condition(c < 'a') for example... how a character can be MORE than other character? i need to reference by the alphabet? B > A? or what?

Sry for my english.

Oh and, good job joropito... this will help me when i need to use sockets.
__________________
Approved Plugins - Steam Profile

Public non-terminated projects:
All Admins Menu, HLTV parameters, Subnick,
Second Password (cool style), InfoZone,
Binary C4 plant/defuse, and more...

Private projects:
NoSpec (+menu), NV Surf Management,
PM Adanved System, KZ longjump2, and more...
Alucard^ is offline
Send a message via Skype™ to Alucard^
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:49.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode