httpget2_async_dns.cpp
#ifdef _WIN32
#define strcasecmp stricmp
#endif
#include <HttpGetSocket.h>
#include <HttpsGetSocket.h>
#include <SocketHandler.h>
#include <Parse.h>
void Get(SocketHandler& h,const std::string& url_in)
{
Parse pa(url_in,":/");
std::string protocol = pa.getword();
std::string host = pa.getword();
int port;
{
Parse pa(host,":");
pa.getword();
port = pa.getvalue();
}
std::string url = "/" + pa.getrest();
std::string file;
{
Parse pa(url,"/");
std::string tmp = pa.getword();
while (tmp.size())
{
file = tmp;
tmp = pa.getword();
}
}
if (!strcasecmp(protocol.c_str(),"http"))
{
HttpGetSocket *s = new HttpGetSocket(h,host,port ? port : 80,url,file);
s -> SetDeleteByHandler();
h.Add(s);
}
else
if (!strcasecmp(protocol.c_str(),"https"))
{
HttpsGetSocket *s = new HttpsGetSocket(h,host,port ? port : 443,url,file);
s -> SetDeleteByHandler();
h.Add(s);
}
else
{
printf("Unknown protocol: '%s'\n",protocol.c_str());
}
}
int main(int argc,char *argv[])
{
SocketHandler h;
// add this line to enable asynchronous dns
h.EnableResolver();
for (int i = 1; i < argc; i++)
{
Get(h,argv[i]);
}
h.Select(1,0);
while (h.GetCount())
{
h.Select(1,0);
}
}
|