C++ 超强的URL解析算法

来源:互联网 发布:柯尼赛格one 1数据 编辑:程序博客网 时间:2024/06/10 21:08

int parse_url(char *url, char **serverstrp, int *portp, char **pathstrp)
{
  char buf[256];
  int serverlen, numread=0;
  /* go through the url */

  /* reset url to point PAST the http:// */

  /* assume it's always 7 chars! */

  url = url+7;
  /* no http:// now... server is simply up to the next / or : */

  sscanf(url, "%255[^/:]", buf);
  serverlen = strlen(buf);
  *serverstrp = (char *)malloc(serverlen+1);
  strcpy(*serverstrp, buf);
  if(url[serverlen]==':')
  {
    /* get the port */

    sscanf(&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;
}

原创粉丝点击