Initial
This commit is contained in:
173
http/MAINPAGE.CPP
Normal file
173
http/MAINPAGE.CPP
Normal file
@@ -0,0 +1,173 @@
|
||||
#include <common/mdifrm.hpp>
|
||||
#include <http/entry.hpp>
|
||||
#include <common/string.hpp>
|
||||
#include <common/console.hpp>
|
||||
#include <common/openfile.hpp>
|
||||
#include <common/filemap.hpp>
|
||||
#include <common/pview.hpp>
|
||||
#include <common/openfile.hpp>
|
||||
#include <common/profile.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);
|
||||
|
||||
Console winConsole;
|
||||
|
||||
int PASCAL WinMain(HINSTANCE /*hInstance*/,HINSTANCE /*hPrevInstance*/,LPSTR lpszCmdLine,int /*nCmdShow*/)
|
||||
{
|
||||
String pathFileName(lpszCmdLine);
|
||||
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,250000,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;
|
||||
winConsole.writeLine(getRequest);
|
||||
getRequest+="\n";
|
||||
httpControl.send(getRequest);
|
||||
TimeInfo timeInfo;
|
||||
winConsole.writeLine(String("'")+saveAs+String("'"));
|
||||
httpControl.receiveBinary(saveAs,250000,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)
|
||||
{
|
||||
FileIO inFile;
|
||||
String strLine;
|
||||
|
||||
entryItems.remove();
|
||||
inFile.open(pathFileName);
|
||||
if(!inFile.isOkay())return FALSE;
|
||||
strLine.reserve(512);
|
||||
while(inFile.readLine(strLine))
|
||||
{
|
||||
if('#'==*(char*)strLine)continue;
|
||||
entryItems.insert(&EntryItem(strLine.betweenString(0,' '),strLine.betweenString(' ',' '),strLine.betweenString(' ',0).betweenString(' ',0)));
|
||||
}
|
||||
return entryItems.size()?TRUE:FALSE;
|
||||
}
|
||||
Reference in New Issue
Block a user