linux url解码工具,Linux C语言实现urlencode和urldecode

本文主要记录一下urlencode和urldecode的C语言实现,作为一个简易工具使用。

1. urlencode编码的基本规则

URL编码做了如下操作:

字符"a"-"z","A"-"Z","0"-"9",".","-","*",和"_" 都不被编码,维持原值;

空格" "被转换为加号"+"。

其他每个字节都被表示成"%XY"格式的由3个字符组成的字符串,编码为UTF-8(特别需要注意: 这里是大写形式的hexchar)。

2. urlencode编码

#include

#include

#include

#include

#include

#include

#include

static unsigned char hexchars[] = "0123456789ABCDEF";

/**

* @brief URLEncode : encode the base64 string "str"

*

* @param str: the base64 encoded string

* @param strsz: the str length (exclude the last \0)

* @param result: the result buffer

* @param resultsz: the result buffer size(exclude the last \0)

*

* @return: >=0 represent the encoded result length

* <0 encode failure

*

* Note:

* 1) to ensure the result buffer has enough space to contain the encoded string, we'd better

* to set resultsz to 3*strsz

*

* 2) we don't check whether str has really been base64 encoded

*/

int URLEncode(const char *str, const int strsz, char *result, const int resultsz)

{

int i,j;

char ch;

if(strsz < 0 || resultsz < 0)

return -1;

for(i = 0,j = 0;i

{

ch = *(str + i);

if((ch >= 'A' && ch <= 'Z') ||

(ch >= 'a' && ch <= 'z') ||

(ch >= '0' && ch <= '9') ||

ch == '.' || ch == '-' || ch == '*' || ch == '_')

result[j++] = ch;

else if(ch == ' ')

result[j++] = '+';

else{

if(j + 3 <= resultsz)

{

result[j++] = '%';

result[j++] = hexchars[(unsigned char)ch >> 4];

result[j++] = hexchars[(unsigned char)ch & 0xF];

}

else{

return -2;

}

}

}

if(i == 0)

return 0;

else if(i == strsz)

return j;

return -2;

}

// return < 0: represent failure

int main(int argc,char *argv[])

{

int fd = -1;

char buf[1024],result[1024*3];

int ret;

int i = 0;

if(argc != 2)

{

printf("please input the encoding filename\n");

return -1;

}

if((fd = open(argv[1],O_RDONLY)) == -1)

{

printf("open file %s failure\n",argv[1]);

return -2;

}

while((ret = read(fd,buf,1024)) >= 0)

{

if(ret == 0)

break;

ret = URLEncode(buf,ret,result,1024*3);

if(ret < 0)

break;

for(i = 0;i

printf("%c",result[i]);

}

if(ret < 0)

{

printf("encode data failure\n");

}

close(fd);

return ret;

}

3. urldecode解码

#include

#include

#include

#include

#include

#include

#include

static unsigned char hexchars[] = "0123456789ABCDEF";

/**

* @brief URLDecode : decode the urlencoded str to base64 encoded string

*

* @param str: the urlencoded string

* @param strsz: the str length (exclude the last \0)

* @param result: the result buffer

* @param resultsz: the result buffer size(exclude the last \0)

*

* @return: >=0 represent the decoded result length

* <0 encode failure

*

* Note:

* 1) to ensure the result buffer has enough space to contain the decoded string, we'd better

* to set resultsz to strsz

*

*/

int URLDecode(const char *str, const int strsz, char *result, const int resultsz, const char **last_pos)

{

int i,j;

char ch;

char a;

*last_pos = str;

if(strsz < 0 || resultsz < 0)

return -1;

for(i = 0,j = 0;i

{

ch = *(str + i);

if(ch == '+')

{

result[j] = ' ';

i += 1;

}

else if(ch == '%')

{

if(i+3 <= strsz)

{

ch = *(str + i + 1);

if(ch >= 'A' && ch <= 'F')

{

a = (ch - 'A')+10;

}

else if(ch >= '0' && ch <= '9')

{

a = ch - '0';

}

else if(ch >= 'a' && ch <= 'f')

{

a = (ch - 'a') + 10;

}

else{

return -2;

}

a <<= 4;

ch = *(str + i + 2);

if(ch >= 'A' && ch <= 'F')

{

a |= (ch - 'A') + 10;

}

else if(ch >= '0' && ch <= '9')

{

a |= (ch - '0');

}

else if(ch >= 'a' && ch <= 'f')

{

a |= (ch - 'a') + 10;

}

else{

return -2;

}

result[j] = a;

i += 3;

}

else

break;

}

else if((ch >= 'A' && ch <= 'Z') ||

(ch >= 'a' && ch <= 'z') ||

(ch >= '0' && ch <= '9') ||

ch == '.' || ch == '-' || ch == '*' || ch == '_'){

result[j] = ch;

i+=1;

}

else{

return -2;

}

}

*last_pos = str + i;

return j;

}

// return < 0: represent failure

int main(int argc,char *argv[])

{

int fd = -1;

char buf[4096],result[4096];

char *start_pos;

const char * last_pos;

int ret,sz;

int i = 0;

if(argc != 2)

{

printf("please input the encoding filename\n");

return -1;

}

if((fd = open(argv[1],O_RDONLY)) == -1)

{

printf("open file %s failure\n",argv[1]);

return -2;

}

start_pos = buf;

last_pos = NULL;

while((ret = read(fd,start_pos,buf + 4096 - start_pos)) >= 0)

{

if(ret == 0)

{

if(start_pos == buf)

break;

else

{

ret = -3;

break;

}

}

sz = URLDecode(buf,start_pos - buf + ret,result,4096,&last_pos);

if(sz < 0)

{

ret = -4;

break;

}

if(last_pos != start_pos + ret)

{

memcpy(buf,last_pos,start_pos + ret - last_pos);

start_pos = buf + (start_pos + ret - last_pos);

}

else{

start_pos = buf;

}

for(i = 0;i

printf("%c",result[i]);

}

if(ret < 0)

{

printf("decode data failure\n");

}

close(fd);

return ret;

}

4. 说明

值得指出的是,实际上在对URL进行urlencode的时候(例如: http://127.0.0.1:8000/file/测试/只用于测试.txt?username=小明),不能简单的调用上面的URLEncode()函数,否则可能把:、/这样的字符也进行编码,而实际上

一个对如http://这样的部分是不需要进行修正的。

5. Go语言中的一个urlencode

package main

import (

"fmt"

"net/url"

)

func main(){

urlStr := "http://127.0.0.1:17480/userDownload/F6678E6FD4054150BA37521FBA8A67A6/tsp_test_file/批量上传走joss文件 -003-KZyxg.docx?certification=v1645f22bf4084cc7cf38092cd1b52ef6e3e"

urlObj, err := url.Parse(urlStr)

if err != nil{

fmt.Printf("part url failure: %s\n", err.Error())

return

}

fmt.Println(urlObj.String())

}

编译运行:

http://127.0.0.1:17480/userDownload/F6678E6FD4054150BA37521FBA8A67A6/tsp_test_file/%E6%89%B9%E9%87%8F%E4%B8%8A%E4%BC%A0%E8%B5%B0joss%E6%96%87%E4%BB%B6%20-003-KZyxg.docx?certification=v1645f22bf4084cc7cf38092cd1b52ef6e3e

[参看]:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python中的`urlencode`和`urldecode`是用于处理URL编码和解码的方法。 `urlencode`方法用于将一个字典形式的参数列表转换为URL编码的字符串。例如,假设有一个字典`params`包含以下键值对: ``` params = {"name": "张三", "age": 20, "city": "北京"} ``` 使用`urlencode`方法将字典转换为URL编码的字符串: ``` import urllib.parse url_encoded = urllib.parse.urlencode(params) print(url_encoded) ``` 输出结果为: ``` name=%E5%BC%A0%E4%B8%89&age=20&city=%E5%8C%97%E4%BA%AC ``` `urldecode`方法用于将URL编码的字符串解码为字典形式的参数列表。例如,将上面的URL编码字符串解码为字典: ```python import urllib.parse url_decoded = urllib.parse.parse_qs(url_encoded) print(url_decoded) ``` 输出结果为: ``` {'name': ['张三'], 'age': ['20'], 'city': ['北京']} ``` 可以通过访问字典的键来获取对应的值: ```python name = url_decoded['name'][0] age = url_decoded['age'][0] city = url_decoded['city'][0] print(f"姓名:{name},年龄:{age},城市:{city}") ``` 输出结果: ``` 姓名:张三,年龄:20,城市:北京 ``` 通过这两个方法,我们可以方便地进行URL编码和解码的操作,以便于在HTTP请求或其他URL相关的场景中使用。 ### 回答2: Python中的urlencode方法是将字典数据编码为URL参数的字符串。它将字典键值对转换为一组key=value对,并用&符号链接起来。这通常用于构建GET请求的URL参数部分。 例如,将以下字典数据编码为URL参数字符串: ``` params = {'name': '张三', 'age': 20, 'city': '北京'} ``` 通过使用urlencode方法,我们可以得到如下结果: ``` encoded_params = urllib.parse.urlencode(params) print(encoded_params) ``` 输出结果为:name=%E5%BC%A0%E4%B8%89&age=20&city=%E5%8C%97%E4%BA%AC urldecode的功能与urlencode相反,它用于将URL参数字符串解码为字典数据。例如,将上面编码后的字符串解码为字典数据: ``` decoded_params = urllib.parse.parse_qs(encoded_params) print(decoded_params) ``` 输出结果为:{'name': ['张三'], 'age': ['20'], 'city': ['北京']} 注意,这里使用了urllib.parse模块来进行编码和解码。在Python 3中,urllib模块已经被拆分为多个子模块,其中urllib.parse包含了URL编码和解码相关的功能。 综上所述,Python中的urlencodeurldecode方法提供了方便的对URL参数进行编码和解码的功能,可以用于构建和解析GET请求的URL参数部分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值