解析url
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
#include?<stdio.h> #include?<string.h> #include?<stdlib.h>//?解析url,作為示例,很多情況沒考慮,比如說user,pass之類的 int?parse_url(char?*url,?char?**serverstrp,?int?*portp,?char?**pathstrp) {char?buf[256];int?serverlen,?numread=0;//?跳過"http://"?,例"http://www.a.cn"->"www.a.cn"url?=?url+7;//?輸入url直到遇到'/'和':'符號sscanf(url,?"%255[^/:]",?buf);serverlen?=?strlen(buf);*serverstrp?=?(char?*)malloc(serverlen+1);strcpy(*serverstrp,?buf);if(url[serverlen]==':'){//?獲取portsscanf(&url[serverlen+1],?"%d%n",?portp,?&numread);/*?add?one?to?go?PAST?it?*/numread++;}else{*portp?=?80;}/*?the?path?is?a?pointer?into?the?rest?of?url?*/*pathstrp?=?&url[serverlen+numread];return?0; }int?main() {char?url[256]?=?"http://www.a.cn:80//index.html";char?pathstr[256]?=?{'/0'};char?szserverstrp[256]?=?{'/0'};char*?serverstrp?=?szserverstrp;char*?pathstrp?=?&pathstr[0];int?port;parse_url(url,&serverstrp,&port,&pathstrp);printf("%s/n%s/n%d/n%s/n",url,serverstrp,port,pathstrp);return?0; }轉(zhuǎn)載于:https://my.oschina.net/u/572632/blog/225396
總結(jié)
- 上一篇: 关于DDD中Domain的思考
- 下一篇: WebApi系列~自主宿主HttpSel