首页 > 代码库 > 解析http302重定向url
解析http302重定向url
bool urlparse(const u_char* data,u_int len)
{
ip_header *ih;
udp_header *uh;
tcp_header *th;
u_short sport,dport;
int ip_len = 0;
ih = (ip_header *)(data+0xE);
ip_len = (ih->ver_ihl & 0xf) * sizeof(unsigned long);
th = (tcp_header *) ((u_char*)ih + ip_len);
sport = ntohs( th->th_sport );
dport = ntohs( th->th_dport );
if ((ih->proto != IPPROTO_TCP))
return false;
std::string strdata((char*)th + sizeof(tcp_header),len - ip_len - sizeof(tcp_header)+1);
static char szTag0[] = "HTTP/1.1 302 Moved Temporarily\r\n";
static char szTag1[] = "Location: ";
static char szTag2[] = "\r\n\r\n";
size_t nP0 = strdata.find(szTag0);
if (nP0 == std::string::npos)
return false;
size_t nP1 = strdata.find(szTag1,nP0+strlen(szTag0));
if (nP1 == std::string::npos)
return false;
size_t nP2 = strdata.find(szTag2,nP1+strlen(szTag1));
if (nP2 == std::string::npos)
return false;
std::string strUrl = strdata.substr(nP1+strlen(szTag1),nP2-nP1-strlen(szTag1));
printf("url>%s\n",strUrl.c_str());
return true;
}
{
ip_header *ih;
udp_header *uh;
tcp_header *th;
u_short sport,dport;
int ip_len = 0;
ih = (ip_header *)(data+0xE);
ip_len = (ih->ver_ihl & 0xf) * sizeof(unsigned long);
th = (tcp_header *) ((u_char*)ih + ip_len);
sport = ntohs( th->th_sport );
dport = ntohs( th->th_dport );
if ((ih->proto != IPPROTO_TCP))
return false;
std::string strdata((char*)th + sizeof(tcp_header),len - ip_len - sizeof(tcp_header)+1);
static char szTag0[] = "HTTP/1.1 302 Moved Temporarily\r\n";
static char szTag1[] = "Location: ";
static char szTag2[] = "\r\n\r\n";
size_t nP0 = strdata.find(szTag0);
if (nP0 == std::string::npos)
return false;
size_t nP1 = strdata.find(szTag1,nP0+strlen(szTag0));
if (nP1 == std::string::npos)
return false;
size_t nP2 = strdata.find(szTag2,nP1+strlen(szTag1));
if (nP2 == std::string::npos)
return false;
std::string strUrl = strdata.substr(nP1+strlen(szTag1),nP2-nP1-strlen(szTag1));
printf("url>%s\n",strUrl.c_str());
return true;
}
解析http302重定向url
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。