263 lines
8.2 KiB
C++
263 lines
8.2 KiB
C++
#include <common/mdifrm.hpp>
|
|
#include <getpage/entry.hpp>
|
|
#include <common/string.hpp>
|
|
#include <common/openfile.hpp>
|
|
#include <common/filemap.hpp>
|
|
#include <common/pview.hpp>
|
|
#include <common/openfile.hpp>
|
|
#include <common/profile.hpp>
|
|
#include <common/file.hpp>
|
|
#include <common/stdio.hpp>
|
|
#include <socket/hostent.hpp>
|
|
#include <socket/servent.hpp>
|
|
#include <socket/intsaddr.hpp>
|
|
#include <socket/socket.hpp>
|
|
#include <fileio/fileio.hpp>
|
|
|
|
WORD open(const String &hostName,const String &nameFile);
|
|
WORD open(const String &hostName,const String &nameString,const String &saveAs);
|
|
WORD makeFileName(String &pathFileName);
|
|
BOOL getEntryItems(const String &pathFileName,Block<EntryItem> &entryItems);
|
|
void replaceSpace(String &string);
|
|
|
|
class Console
|
|
{
|
|
public:
|
|
Console();
|
|
void writeLine(const String &string);
|
|
void read();
|
|
};
|
|
|
|
Console::Console()
|
|
{
|
|
}
|
|
|
|
void Console::writeLine(const String &string)
|
|
{
|
|
printf("%s\n",string.str());
|
|
}
|
|
|
|
void Console::read()
|
|
{
|
|
char buffer[32];
|
|
sscanf("%s",buffer);
|
|
}
|
|
|
|
Console winConsole;
|
|
|
|
|
|
// for when i do a post request
|
|
//POST /cgi-bin/phone_book.cgi HTTP/1.0
|
|
//
|
|
//Referer: http://www.somedomain.com/Direcory/file.html
|
|
//
|
|
//User-Agent: Mozilla/1.22 (Windows: I: 32bit)
|
|
//
|
|
//Accept */*
|
|
//
|
|
//Content-type: application/x-www-form-urlencoded
|
|
//
|
|
//Content-length: 29
|
|
//
|
|
//
|
|
//
|
|
//name=Selena+Sol&phone=7700404
|
|
//
|
|
//
|
|
|
|
|
|
|
|
int main(int argc,char **argv)
|
|
{
|
|
String pathFileName(argv[1]);
|
|
Block<EntryItem> entryItems;
|
|
|
|
if(pathFileName.isNull())
|
|
{
|
|
winConsole.writeLine("GETPAGE <inputfile>");
|
|
winConsole.writeLine("Input file is a text file that contains entries in the following format...");
|
|
winConsole.writeLine("<hostname> <pagename> <saveas>");
|
|
winConsole.writeLine("for example...");
|
|
winConsole.writeLine("\"www.developer.com\" \"http://www.developer.com/reference/foo.htm\" \"c:\\developer\\docs\\foo.html\"");
|
|
winConsole.writeLine("a '#' character located in the first position of any line indicates a comment, the line is ignored.");
|
|
winConsole.writeLine("Press ENTER to exit.");
|
|
winConsole.read();
|
|
return FALSE;
|
|
}
|
|
if(!getEntryItems(pathFileName,entryItems))
|
|
{
|
|
winConsole.writeLine("Failed to read input file");
|
|
winConsole.writeLine("Press ENTER to exit");
|
|
winConsole.read();
|
|
return FALSE;
|
|
}
|
|
for(int itemIndex=0;itemIndex<entryItems.size();itemIndex++)
|
|
{
|
|
EntryItem &entryItem=entryItems[itemIndex];
|
|
open(entryItem.hostName(),entryItem.pageName(),entryItem.saveAs());
|
|
}
|
|
winConsole.writeLine("Done...");
|
|
winConsole.writeLine("Press ENTER to exit.");
|
|
winConsole.read();
|
|
return FALSE;
|
|
}
|
|
|
|
WORD open(const String &hostName,const String &nameString)
|
|
{
|
|
INETSocketAddress internetSocketAddress;
|
|
String getRequest;
|
|
HostEnt hostEntry;
|
|
ServEnt serverEntry;
|
|
WSASystem wsaSystem;
|
|
Socket httpControl;
|
|
|
|
getRequest="GET ";
|
|
winConsole.writeLine(String("trying ")+hostName+String("..."));
|
|
if(!wsaSystem.isInitialized()){winConsole.writeLine("WINSOCK initialization failure.");return FALSE;}
|
|
InternetAddress internetAddress(hostName);
|
|
if(!internetAddress.isZero()){if(!hostEntry.hostByAddress(internetAddress)){return FALSE;}}
|
|
else if(!hostEntry.hostByName(hostName)){return FALSE;}
|
|
if(!internetAddress.isZero()){if(!hostEntry.hostByAddress(internetAddress)){winConsole.writeLine(String("no DNS entry for ")+hostName);return FALSE;}}
|
|
else if(!hostEntry.hostByName(hostName)){winConsole.writeLine(String("no DNS entry for ")+hostName);return FALSE;}
|
|
winConsole.writeLine(String("connect...")+String("'")+hostEntry.hostName()+String("' (")+(String)(hostEntry.addresses())[0]+String(")"));
|
|
internetSocketAddress.internetAddress((hostEntry.addresses())[0]);
|
|
if(serverEntry.serviceByName("http","tcp"))
|
|
{
|
|
if(!httpControl.create()){winConsole.writeLine("unable to create socket.");return FALSE;}
|
|
internetSocketAddress.family(PF_INET);
|
|
internetSocketAddress.port(serverEntry.port());
|
|
}
|
|
else
|
|
{
|
|
if(!httpControl.create()){winConsole.writeLine("unable to create socket.");return FALSE;}
|
|
internetSocketAddress.family(PF_INET);
|
|
internetSocketAddress.port(htons(80));
|
|
}
|
|
if(!httpControl.connect(internetSocketAddress)){winConsole.writeLine("unable to connect to http daemon");return FALSE;}
|
|
httpControl.getSocketName(internetSocketAddress);
|
|
getRequest+=nameString;
|
|
winConsole.writeLine(getRequest);
|
|
getRequest+="\n";
|
|
httpControl.send(getRequest);
|
|
TimeInfo timeInfo;
|
|
String saveFile(nameString);
|
|
makeFileName(saveFile);
|
|
winConsole.writeLine(String("'")+saveFile+String("'"));
|
|
httpControl.receiveBinary(saveFile,25000000,timeInfo);
|
|
return httpControl.isConnected();
|
|
}
|
|
|
|
WORD open(const String &hostName,const String &nameString,const String &saveAs)
|
|
{
|
|
INETSocketAddress internetSocketAddress;
|
|
String getRequest;
|
|
HostEnt hostEntry;
|
|
ServEnt serverEntry;
|
|
WSASystem wsaSystem;
|
|
Socket httpControl;
|
|
|
|
getRequest="GET ";
|
|
winConsole.writeLine(String("trying ")+hostName+String("..."));
|
|
if(!wsaSystem.isInitialized()){winConsole.writeLine("WINSOCK initialization failure.");return FALSE;}
|
|
InternetAddress internetAddress(hostName);
|
|
if(!internetAddress.isZero()){if(!hostEntry.hostByAddress(internetAddress)){return FALSE;}}
|
|
else if(!hostEntry.hostByName(hostName)){winConsole.writeLine(String("unable to contact host '")+hostName+String("'"));return FALSE;}
|
|
if(!internetAddress.isZero()){if(!hostEntry.hostByAddress(internetAddress)){winConsole.writeLine(String("no DNS entry for ")+hostName);return FALSE;}}
|
|
else if(!hostEntry.hostByName(hostName)){winConsole.writeLine(String("no DNS entry for ")+hostName);return FALSE;}
|
|
winConsole.writeLine(String("connect...")+String("'")+hostEntry.hostName()+String("' (")+(String)(hostEntry.addresses())[0]+String(")"));
|
|
internetSocketAddress.internetAddress((hostEntry.addresses())[0]);
|
|
if(serverEntry.serviceByName("http","tcp"))
|
|
{
|
|
if(!httpControl.create()){winConsole.writeLine("unable to create socket.");return FALSE;}
|
|
internetSocketAddress.family(PF_INET);
|
|
internetSocketAddress.port(serverEntry.port());
|
|
}
|
|
else
|
|
{
|
|
if(!httpControl.create()){winConsole.writeLine("unable to create socket.");return FALSE;}
|
|
internetSocketAddress.family(PF_INET);
|
|
internetSocketAddress.port(80);
|
|
}
|
|
if(!httpControl.connect(internetSocketAddress)){winConsole.writeLine("unable to connect to http daemon");return FALSE;}
|
|
httpControl.getSocketName(internetSocketAddress);
|
|
getRequest+=nameString;
|
|
getRequest+=" HTTP/1.0\n";
|
|
winConsole.writeLine(getRequest);
|
|
httpControl.send(getRequest);
|
|
TimeInfo timeInfo;
|
|
|
|
winConsole.writeLine(String("'")+saveAs+String("'"));
|
|
|
|
// char ch;
|
|
// int count=1;
|
|
// while(httpControl.receive(&ch,1,true))printf("%c",ch);
|
|
|
|
|
|
|
|
|
|
|
|
/* Block<String> receiveStrings;
|
|
httpControl.receivePage(receiveStrings);
|
|
for(int index=0;index<receiveStrings.size();index++)
|
|
{
|
|
winConsole.writeLine(receiveStrings[index]);
|
|
}
|
|
*/
|
|
|
|
httpControl.receiveImage(saveAs,timeInfo);
|
|
|
|
httpControl.receiveBinary(saveAs,25000000,timeInfo);
|
|
return httpControl.isConnected();
|
|
}
|
|
|
|
WORD makeFileName(String &pathFileName)
|
|
{
|
|
String tempString(pathFileName);
|
|
char *ptr;
|
|
|
|
ptr=::strchr(tempString,'.');
|
|
if(!ptr)return FALSE;
|
|
while(*ptr!='\\'&&*ptr!='/'&&*ptr!=':'&&ptr>=(char*)tempString)ptr--;
|
|
pathFileName=(++ptr);
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL getEntryItems(const String &pathFileName,Block<EntryItem> &entryItems)
|
|
{
|
|
File inFile;
|
|
String strLine;
|
|
|
|
entryItems.remove();
|
|
inFile.open(pathFileName,"rb");
|
|
if(!inFile.isOkay())return FALSE;
|
|
strLine.reserve(512);
|
|
while(inFile.readLine(strLine))
|
|
{
|
|
if('#'==*(char*)strLine)continue;
|
|
String host=strLine.betweenString('\"','\"');
|
|
host=host.trimLeft().trimRight();
|
|
String page=strLine.betweenString('\"',0).betweenString('\"',0).betweenString('\"','\"');
|
|
replaceSpace(page);
|
|
page=page.trimLeft().trimRight();
|
|
String saveAs=strLine.betweenString('\"',0).betweenString('\"',0).betweenString('\"',0).betweenString('\"',0).betweenString('\"','\"');
|
|
saveAs=saveAs.trimLeft().trimRight();
|
|
entryItems.insert(&EntryItem(host,page,saveAs));
|
|
}
|
|
return entryItems.size()?TRUE:FALSE;
|
|
}
|
|
|
|
void replaceSpace(String &string)
|
|
{
|
|
int position;
|
|
String replace="%20";
|
|
while(-1!=(position=string.strchr(' ')))
|
|
{
|
|
String tmp=string.substr(0,position-1);
|
|
String tmp2=string.substr(position+1,string.length());
|
|
winConsole.writeLine(String("'")+tmp+String("'"));
|
|
winConsole.writeLine(String("'")+tmp2+String("'"));
|
|
string=tmp+replace+tmp2;
|
|
winConsole.writeLine(String("'")+string+String("'"));
|
|
}
|
|
}
|