Resource get_resource(const char* url){ char pack[1024]={0}; char buf[1024]={0}; char redirect[256]={0},new_url[256]={0},old_url[256]={0}; static int redirect_count=0; char* i; char* j; char* z; Resource res; Connection conn=open_url(url); if(!conn.avaliable){ return res; } sprintf(pack,"GET %s HTTP/1.1\nHost: %s\nAccept: */*\nReferer: http://%s\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\nPragma: no-cache\nCache-Control: no-cache\nConnection: close\n\n",conn.url_info.file,conn.url_info.host_name,conn.url_info.host_name); send(conn.sock,pack,strlen(pack),0); recv(conn.sock,buf,sizeof(buf),0); //printf("%s\n",buf); if(strstr(buf,"HTTP/1.1 404")!=NULL || strstr(buf,"HTTP/1.0 404")!=NULL){ return res; } i=(char *)strstr(buf,"Location:"); if(i!=NULL && redirect_count<5){ sscanf(i,"Location: %s",redirect); sprintf(old_url,"%s://%s:%d%s",conn.url_info.schema,conn.url_info.host_name,conn.url_info.port,conn.url_info.file); join_url(old_url,redirect,new_url); //printf("@#%s\n",new_url); redirect_count++; return get_resource(new_url); } i=(char *)strstr(buf,"Content-Length:"); if(i!=NULL){ sscanf(i,"Content-Length: %d",&res.file_size); } strcpy(res.file_url,url); //printf("#%d\n",res.file_size); for(z=(char*)url;(j=strstr(z,"/"))!=NULL;){ z=j+sizeof(char); } strcpy(res.file_name,z); close(conn.sock); return res; }
std::string join_url(std::string first, std::string second){ return join_url(ParsedUrl(first), ParsedUrl(second)); }