HTTP协议URL解析的C语言实现
2017-12-21 17:10
281 查看
/**
* 日期:2017年12月21日
* 作者:jack
* 原因:没有看到合适的解析URL代码,自己写一个。
* 应用:WIFI模块的Webserver
* 心情:可能不做嵌入式开发了,转向Java 的Hadoop
* 特点:不依赖很多东西,小巧稳定
*/
运行结果:
解析成功..
方法名为:GET
文件名为:joyent/http-parser.txt
请按任意键继续. . .
* 日期:2017年12月21日
* 作者:jack
* 原因:没有看到合适的解析URL代码,自己写一个。
* 应用:WIFI模块的Webserver
* 心情:可能不做嵌入式开发了,转向Java 的Hadoop
* 特点:不依赖很多东西,小巧稳定
*/
#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #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) { 4000 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"); }
运行结果:
解析成功..
方法名为:GET
文件名为:joyent/http-parser.txt
请按任意键继续. . .
相关文章推荐
- URL的解析,C语言实现
- [转]URL的解析,C语言实现
- ARM汇编实现C语言switch语句其中对PC寄存器取值的解析
- AVL树(一)之 图文解析 和 C语言的实现
- 轻量级C语言实现的minixml解析库入门教程
- 使用C语言解析URL
- 左倾堆(一)之 图文解析 和 C语言的实现
- DNS解析协议的C语言简单实现
- [置顶] 【BigData】Jsoup+FusionCharts实现根据网页url解析网页数据,并用图表显示
- C语言实现XML生成解析库(XML扩展)
- java实现对url解析
- 谷歌开源Gumbo:纯C语言实现的HTML5解析库
- AVL树(一)之 图文解析 和 C语言的实现
- 深度优先搜索DFS-C语言实现、思路/解析-简笔
- php中使用parse_url()对网址进行解析的实现代码(parse_url详解)
- 自己实现Http服务器:一、Http协议解析
- 二叉堆(一)之 图文解析 和 C语言的实现
- 左倾堆(一)之 图文解析 和 C语言的实现
- 使用C语言实现二维,三维绘图算法(2)-解析曲面的显示
- 伸展树(一)之 图文解析 和 C语言的实现