zoukankan      html  css  js  c++  java
  • [转]URL的解析,C语言实现

    http://blog.csdn.net/cuishumao/article/details/10284463

    一 说明
    (1)应用情况:比如基于socket来实现http协议等,这时候就需要解析URL。
    (2)为了移植性,没有用非标准C库windows下的StrDup(linux下为strdup),用自己编写的dup_str。
    (3)编译环境:windows ,visual studio2010
    二 URL的格式:
    (协议)://(主机名):(端口号) / (文件路径)/(文件名) 
      例如:http://zj.qq.com/a/20130824/002507.htm#p=8
          http://www.itpub.net/kwrss/201211/wangzhiduankou.shtml
    三 实现

    [html] view plain copy
     
    1. #include <stdio.h>     //printf  
    2. #include <string.h>    //strchr strncmp ,memcpy  
    3. #include <malloc.h>    //malloc free  
    4. #include <stdlib.h>    //atoi  
    5. //将source开始空间的以NULL结尾的字符拷贝到dest中  
    6. //返回的指针需要free  
    7. char*dup_str(const char*source)  
    8. {  
    9.     if(source==NULL)  
    10.            return  NULL;  
    11.     int len = strlen(source);  
    12.     char *dest = (char*)malloc(len+1);  
    13.     memcpy(dest,source,len+1);  
    14.     return dest;  
    15. }  
    16. //函数功能:解析URL  
    17. //参数:host带回主机字符串,protocl协议,port端口,abs_path带回绝对路径  
    18. //使用完注意释放host和abs_path在堆上分配的内存  
    19. //备注:(1)先取到URL的一份拷贝,方面将该字符串截成几段,分别处理;  
    20. //      (2)用了指针引用,也可以使用二重指针来解决参数带回值的问题  
    21.   
    22. void parse_URL(const char*URL,const char*protocl,char*&host,unsigned int &port,char*&abs_path)  
    23. {  
    24.     if(URL == NULL)  
    25.         return ;  
    26.     char *url_dup = dup_str(URL);  
    27.     char *p_slash = NULL;//主机后第一个斜杠的位置  
    28.     char *p_colon = NULL;//主机后第一个冒号的位置  
    29.     char *start = 0;    //记录www开始的位置  
    30.     if(strncmp(url_dup,protocl,strlen(protocl))==0)  
    31.     {  
    32.        start = url_dup+strlen(protocl)+3;  
    33.         p_slash = strchr(start,'/');  
    34.        if(p_slash != NULL)  
    35.        {  
    36.           abs_path= dup_str(p_slash);  
    37.           *p_slash = '';  
    38.        }  
    39.        else  
    40.        {  
    41.           abs_path= dup_str("/");  
    42.        }  
    43.        p_colon = strchr(start,':');  
    44.        if(p_colon != NULL)  
    45.        {  
    46.            port = atoi(p_colon+1);  
    47.            *p_colon = '';  
    48.        }  
    49.        else  
    50.        port = 8080;//没有的话取默认的8080端口  
    51.        }  
    52.        host = dup_str(start);  
    53.     }  
    54.     if(url_dup != NULL)  
    55.     {  
    56.         free(url_dup);  
    57.         url_dup = NULL;  
    58.     }  
    59. }  
    60. int main()  
    61. {  
    62.     //这是一个伪造的地址,用于测试  
    63.     //char *URL = "http://www.xyz2013.com";  
    64.     //char *URL = "ftp://www.xyz2013.com:8080";  
    65.     char *URL = "https://www.xyz2013.com:1324/My/5201449.shtml";  
    66.     char*abs_path = NULL;  
    67.     char*host = NULL;  
    68.     unsigned int port;  
    69.       
    70.     parse_URL(URL,"https",host,port,abs_path);  
    71.     printf("主机地址:%s ",host);  
    72.     printf("端口号:%d ",port);  
    73.     printf("绝对路径:%s ",abs_path);  
    74.  //需要释放host,abs_path  
    75.     if(host!=NULL)   
    76.     {  
    77.         free(host);  
    78.         host = NULL;  
    79.     }  
    80.     if(abs_path!=NULL)   
    81.     {  
    82.         free(abs_path);  
    83.         abs_path=NULL;  
    84.     }  
    85.     getchar();  
    86. }  

    结果:

  • 相关阅读:
    用java批量重命名文件
    登入Tomcat Manager时出现401错误——解决方法
    bat启动/停止oracle服务
    java基本类型与字符串之间的转换
    java面向对象 要点
    理解main 方法
    第二章 变量,数据类型和运算符
    初始JAVA
    jdbcTemplate
    Aspectj
  • 原文地址:https://www.cnblogs.com/xunbu7/p/6226645.html
Copyright © 2011-2022 走看看