c语言 url字符串解析,HTTP协议URL解析的C语言实现

#include #define METHOD_DECODE(mode) mode?"POST":"GET"

static char data[] =

"GET /joyent/http-parser.txt HTTP/1.1\r\n"

"Host: github.com\r\n"

"DNT: 1\r\n"

"Accept-Encoding: gzip, deflate, sdch\r\n"

"Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n"

"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) "

"AppleWebKit/537.36 (KHTML, like Gecko) "

"Chrome/39.0.2171.65 Safari/537.36\r\n"

"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,"

"image/webp,*/*;q=0.8\r\n"

"Connection: keep-alive\r\n"

"Transfer-Encoding: chunked\r\n"

"Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n\r\n";

struct http_parser_urt

{

char filename[32];

char method; /* 0:GET;1:POST */

};

char parse_url(struct http_parser_urt * httpurl, char * strurl)

{

char ret = 0;

char * line_start = NULL;

char * line_end = NULL;

char * start_temp = NULL;

char * end_temp = NULL;

char * line_temp = NULL;

/* input check */

if( (httpurl == NULL)||(strurl == NULL) )

{

ret = 1;

return ret;

}

/* check http end flag */

line_temp = (char *)strstr(strurl, "\r\n\r\n");

if (line_temp == NULL)

{

ret = 1;

return ret;

}

/* ----------------------------------- first line ------------------------------*/

/* Determine the first and end addresses of a line */

line_start = strurl;

line_end = (char *)strstr(line_start, "\r\n");

if (line_end == NULL)

{

ret = 1;

return ret;

}

/* find method name */

if (strncmp(line_start, "GET ", 4) == 0)

{

httpurl->method = 0;

start_temp = line_start + 4;

}

else if (strncmp(strurl, "POST ", 5) == 0)

{

httpurl->method = 1;

start_temp = line_start + 5;

}

else

{

/* now do not suport method */

}

/* find file name */

start_temp++;

end_temp = (char *)strstr(line_start, " HTTP");

strncpy_s(httpurl->filename, start_temp, end_temp-start_temp);

return ret;

}

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

{

char ret1 = 0;

struct http_parser_urt url_test1;

ret1 = parse_url(&url_test1, data);

if (ret1 != 0)

{

printf("解析失败...\r\n");

return 0;

}

printf("解析成功..\r\n");

printf("方法名为:%s\r\n", METHOD_DECODE(url_test1.method));

printf("文件名为:%s\r\n", url_test1.filename);

system("pause");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值