home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1997 May
/
Pcwk0597.iso
/
borland
/
ib
/
setups
/
intrabld
/
data.z
/
NETCLASS.JS
< prev
next >
Wrap
Text File
|
1996-12-11
|
21KB
|
562 lines
/****************************************************************************\
* *
* NetClass.js -- These classes are designed to help organize the data we *
* send and receive over the internet. *
* *
* *
* Dependencies: localurl.qry *
* *
* * *
* Links to: *
* *
* Updated 8/21/96 by IntraBuilder Samples Group *
* $Revision: 1.2 $ *
* *
* Copyright (c) 1996, Borland International, Inc. All rights reserved. *
* *
\****************************************************************************/
function loadExternalFunctions() //call me first!!
{
//In order to use the net classes you must call this function.
//Immediately after you call _sys.scripts.load("netclass")
//call loadExternalFunctions()
extern char* requestUrl (char* /*url*/) "reqstURL.dll";
extern char* requestHead (char* /*url*/) "reqstURL.dll";
extern void getWebName(char*/*ip address*/,char*/*string to put name in*/)"reqsturl.dll";
}
class HtmlFile(input) {
//This class is a wrapper for HTML files
//MEMBER VARIABLES////////////////////////////////////////////////
this.text=new String(input); //a place to store the html text
this.baseUrl=new Url(""); //remember where it came from
this.url=new Url("");
//MEMBER FUNCTIONS/////////////////////////////////////////////////
function findBase() {
//looks for a tag like <base href="http://www.borland.com">
var openTagMark=0;
var closeTagMark=0;
var hrefMark=0;
var baseMark=0;
var endHeadMark=0;
var tempString=new String();
var i,k=0;
openTagMark=this.text.indexOf('<'); //find any tag between <>, eg <head>
closeTagMark=this.text.indexOf('>');
for( i=1; (openTagMark>=0 && closeTagMark>=0);i++)
{
tempString=this.text.substring(openTagMark+1,closeTagMark); //extract what is between the < > marks
tempString=tempString.toLowerCase(); //convert that to lower case
hrefMark=tempString.indexOf("href");
endHeadMark=tempString.indexOf("/head");
if (hrefMark>0) //we only want those with hrefs
{
k++;
baseMark=tempString.indexOf("base");
if ( (baseMark>=0) && (k==1) && (baseMark<hrefMark) ){ //first href might be "base"
//<BASE HREF="http://www.borland.com">
//if so "BASE" must be before "HREF"
var beginQuoteIndex=tempString.indexOf("\"",hrefMark);
var endQuoteIndex=tempString.lastIndexOf("\"");
var tempUrl=new Url(tempString.substring(beginQuoteIndex+1,endQuoteIndex)); //extract the Url from within the quotes
this.baseUrl=tempUrl; //store the base string (ie http://www.borland.com)
return true;
}else{
this.baseUrl=this.url;
return false;
}
}else if(endHeadMark>=0){//we've reached the end of the head element
this.baseUrl=this.url; //base can only be specified within <head></head>
return false;
}
this.text=this.text.substring(closeTagMark+1,this.text.length); //truncate the html string
openTagMark=this.text.indexOf('<');
closeTagMark=this.text.indexOf('>');
}//end for
this.baseUrl=this.url;
return false;
}//end function
function getReferences()
//This function finds all references to other urls
{
var tag=new Array(0);
var openTagMark=0;
var closeTagMark=0;
var hrefMark=0;
var noHrefMark=0;
var srcMark=0;
var textInBrackets=new String();
var lcTextInBrackets=new String();
var i;
if (this.findBase()==false && this.baseUrl.text.length==0) { //we must make sure that either
// 1. the document contains a <base href="www.x.com">
return; // 2. we have stored the url where the document came from
}
openTagMark=this.text.indexOf('<'); //find <> marks
closeTagMark=this.text.indexOf('>');
for( i=1; (openTagMark>=0 && closeTagMark>=0);i++)
{
badProtocol=false;
textInBrackets=this.text.substring(openTagMark+1,closeTagMark); //extract what is between the < > marks
lcTextInBrackets=textInBrackets.toLowerCase(); //convert that to lower case
srcMark=lcTextInBrackets.indexOf("src");
hrefMark=lcTextInBrackets.indexOf("href");
noHrefMark=lcTextInBrackets.indexOf("nohref");
if (hrefMark>=0 && noHrefMark<0) //we only want those with hrefs
{
if(lcTextInBrackets.indexOf("mailto:")<0 && lcTextInBrackets.indexOf("#")<0){ //no mailto or local links
tag.add(this.extractLink(textInBrackets,hrefMark)); //add the link to our tag array
}
}else if( srcMark>0){ //we're looking for something like <img src="picture.gif">
tag.add(this.extractLink(textInBrackets,srcMark)); //add frame references to our array
}
this.text=this.text.substring(closeTagMark+1,this.text.length); //truncate the html string
openTagMark=this.text.indexOf('<'); //find the next <A href="">
closeTagMark=this.text.indexOf('>'); // and move on
}//end for
return tag;
}//end getReferences()
function extractLink(text,mark){
text=charReplace(text,'\'','\"'); //change all ' to "
var openQuote=text.indexOf("\"",mark); //find first quote after 'href='
var closeQuote=text.indexOf("\"",openQuote+1); //find second quote
var refIndex;
var fullUrl="";
if (openQuote<0 || closeQuote<0){
return "";
}
var quotedText=new StringEx(text.substring(openQuote+1,closeQuote)); //extract the string between quotes
quotedText=stripSpace(quotedText); //remove any garbage white space
var lcQuotedText=quotedText.toLowerCase(); //use lower case version for comparison
//Let's convert all refs of the form "http:/index.html" to "/index.html"
if(lcQuotedText.indexOf("http:")==0 && lcQuotedText.indexOf("http://")<0){
quotedText=quotedText.substring(5,quotedText.length);
}
if (quotedText.charAt(0)=='/'){ //this is a relative url
fullUrl=this.baseUrl.base + quotedText; //make it absolute by adding base
}else if (quotedText.indexOf("://")>0){ //absolute url
fullUrl= quotedText; //just add it to array
}else { //relative url without the "/" to start with (this is bad form but people do use it).
fullUrl=this.baseUrl.baseDir +"/"+quotedText;
}//endif
return fullUrl;
}//end function extractLink
}//end class
class HeaderFile(info)
{//Pass this class an http header and it will tell you all kinds
//of good information.
//MEMBER VARIABLES////////////////////////////////////////////////
this.text=new StringEx(info.toLowerCase());
this.dateIndex=0;
this.serverIndex=0;
this.contentTypeIndex=0;
this.lastModIndex=0;
this.contentLengthIndex=0;
this.returnCodeIndex=0;
this.date="";
this.server="";
this.contentType="";
this.lastModified="";
this.contentLength="";
this.returnCode="";
this.nToR();
this.parse();
//MEMBER FUNCTIONS/////////////////////////////////////////////////
function parse(){
//parses the header file
with (this){
dateIndex= text.indexOf("date:");
serverIndex=text.indexOf("server:");
contentTypeIndex=text.indexOf("content-type:");
lastModIndex=text.indexOf("last-modified:");
contentLengthIndex=text.indexOf("content-length:");
returnCodeIndex=text.indexOf("http/1.0");
}//end with
this.text=this.text+'\r'; //add a \r to the end to make parsing easier
var endOfLineIndex=0;
if(this.returnCodeIndex<0){
this.returnCode ="err unable to retrieve file"; //if we don't even get a code, we must have timed out.
return; //no need to parse because we didn't get any text
}else{
endOfLineIndex=this.text.indexOf("\r",this.returnCodeIndex+1);
this.returnCode= this.text.substring(this.returnCodeIndex+8,endOfLineIndex);
}
endOfLineIndex=this.text.indexOf("\r",this.serverIndex+1);
if (this.serverIndex>=0){//server name is specified
this.server= this.text.substring(this.serverIndex+7,endOfLineIndex);
}else{
this.server="not available: may be password protected"
}
endOfLineIndex=this.text.indexOf("\r",this.dateIndex+1);
if (this.dateIndex>=0){ //date is specified
this.date=this.text.substring(this.dateIndex+5,endOfLineIndex);
}else{
this.date="not available"
}
endOfLineIndex=this.text.indexOf("\r",this.contentTypeIndex+1);
if (this.contentTypeIndex>=0){
this.contentType=this.text.substring(this.contentTypeIndex+13,endOfLineIndex);
}else{
this.contentType="not available";
}
endOfLineIndex=this.text.indexOf("\r",this.lastModIndex+1);
if (this.lastModIndex>=0){
this.lastModified=this.text.substring(this.lastModIndex+14,endOfLineIndex);
}else{
this.lastModified="not available";
}
endOfLineIndex=this.text.indexOf("\r",this.contentLengthIndex+1);
if (this.contentLengthIndex>=0){
this.contentLength=this.text.substring(this.contentLengthIndex+15,endOfLineIndex);
}else{
this.contentLength="not available";
}
//remove the \r we added earlier
this.text=this.text.substring( 0,this.text.length-1 )
}//end parse()
function nToR(){
//converts all \n to \r
//This makes it easier to parse later.
var newLinePos=this.text.indexOf('\n');
while(newLinePos>=0){
this.text=new StringEx(this.text.stuff(newLinePos,1,'\r'));
newLinePos= this.text.indexOf('\n');
}
return
}//end nToR
}//end HeaderFile class
class HttpResponse(url){
//This class retreives both the header and the html text for a Url
//Pass it a Url object and it takes care of the rest.
//MEMBER VARIABLES////////////////////////////////////////////////
this.header=new HeaderFile("");
this.html=new HtmlFile("");
this.text=requestUrl(url.text);
var textToProcess=this.text
this.parse(textToProcess);//fill header and html with proper info.
this.html.url=url;
this.html.findBase();
//MEMBER FUNCTIONS////////////////////////////////////////////////
function parse(input){
var testString=new StringEx(input);
var emptyLineIndex=0;
emptyLineIndex=this.findBlankLine(input);//look for blank line
testString=input.substring(0,emptyLineIndex);
this.header=new HeaderFile(input.substring(0,emptyLineIndex));
this.html= new HtmlFile(input.substring(emptyLineIndex+1,input.length));
}// end parse()
function findBlankLine(input){
//look for /n/n or /r/n/r/n or /r/r
var Key=new Array(4);
Key[0]=input.indexOf("\r\r");
Key[1]=input.indexOf("\r\n\r");
Key[2]=input.indexOf("\n\r\n");
Key[3]=input.indexOf("\n\n");
Key.sort(0);
for (i=0;i<Key.length;i++){
if (Key[i]>0){ //first key>0 is the first blank line.
return Key[i];
}
}
return -1;
}
}//end HttpResponse
class HeadResponse(url)
{
//This class retrieves and stores header information for a URL
//Pass it a Url object and it does the rest.
//MEMBER VARIABLES////////////////////////////////////////////////////////////
this.url=url
this.text=requestHead(url.text);
var textToProcess=this.text;
this.headerFile=new HeaderFile(textToProcess);
}//end HeadResponse
class IpAddress(addressText)
{
//This class is a wrapper for numeric ip addresses
//MEMBER VARIABLES////////////////////////////////////////////////////////////
this.text=new String(addressText);
this.name=" ";
this.isValid=this.checkValidity();
//MEMBER FUNCTIONS////////////////////////////////////////////////////////////
function checkValidity(){
//We are looking for something like 255.254.253.252
//There should be 3 periods separating 4 numbers.
var charNum=0;
var startingChar=0;
var count=0;
//look for 3 periods seperated by at least one character
while(charNum>-1){
charNum=this.text.indexOf(".",startingChar);
startingChar=charNum+2;//look for the next period two characters from where we found the first
if(charNum>=0){
count++;
}//endif
}//endwhile
if (count!=3){
return false;
}
//Ok it has 3 periods in it,
//now make sure each section is a number between 0 and 255
var section=new Array(4);
section=this.parse();
for (i=0;i<section.length;i++){ //put each section through 3 tests
if(section[i].indexOf(":") > 0){ // Strip off port number if exists
section[i]=section[i].substring(0,section[i].indexOf(":"));
}
if(section[i].length >3){ //check to make sure each section is only 3 characters
return false;
}//endif
if (parseInt(section[i])<0 || parseInt(section[i])>255){ //check that numeric value is in proper range
return false;
}//endif
var tempString=new StringEx(section[i]);
while(tempString.length>0){
if (tempString.isAlpha()){ //check that there are no alphabetical characters in the string.
return false;
}//end if
tempString=new StringEx(tempString.substring(1,tempString.length)); //chop off first character
}//end while
}//end for
//passed all the tests
return true;
}//end isValid()
function parse(){
var byteValue=new Array(4);
var period =new Array(4);
with(this){
period[0]=text.indexOf(".");
period[1]=text.indexOf(".",period[0]+1);
period[2]=text.indexOf(".",period[1]+1);
byteValue[0]=text.substring(0,period[0]);
byteValue[1]=text.substring(period[0]+1, period[1]);
byteValue[2]=text.substring(period[1]+1, period[2]);
byteValue[3]=text.substring(period[2]+1, text.length);
}
return byteValue;
}
function toDecimal(){
//calculates the numeric value of an ip address
var bytes=new Array(4);
bytes=this.parse();
var total=0;
var temp=0;
for (i=0;i<bytes.length;i++){
total+=parseInt(bytes[i])*Math.pow(256,3-i)
}
return total;
}//end toDecimal
function decimalToIp(number){
//converts a number to an IP address.
var num=number;
var bytes=new Array(4);
var i=0;
this.text="";
for (i=(bytes.length-1);i>=0;i--){
bytes[i]=Math.int(num%256);
num=Math.int(num/256);
}//endfor
for (i=0;i<bytes.length;i++){
this.text+=(""+bytes[i]);
if(i!=3){
this.text+="."; //we only need 3 periods when i=0,1 or 2
}//endif
}//endfor
return;
}//end decimalToIP();
function getName(){
var temp=new StringEx().space(75) ;
getWebName(this.text,temp);
this.name=temp;
return this.name;
}
}//end class IpAddress
class Url (input)
{//This class is a wrapper for Url's
//pass it a string of the form http://www.borland.com/tributeToElvis.html
//to initialize it.
this.text=input;
this.protocol="";
this.protIndex=0;
this.host="";
this.hostIndex=0;
this.fileName="";
this.fileIndex=0;
this.extension=""
this.extensionIndex=0;
this.isValid=true;
this.base="";
this.baseDir="";
this.port=80;
this.parse(); //parse it
//store these items in lower case
this.host=this.host.toLowerCase();
this.protocol=this.protocol.toLowerCase();
this.extension=this.extension.toLowerCase();
function parse(){
//extracts host, filename, protocol, base, and basedir from text.
var i=0;
var extensionEnd=-1;
with (this){
protIndex=text.indexOf('://');
hostIndex=text.indexOf('/',protIndex+3);
protocol= text.substring(0,protIndex); //http
host= text.substring(protIndex+3, hostIndex); //www.borland.com
fileName= text.substring(hostIndex,text.length);// /index.html
baseDir=text.substring(0,text.lastIndexOf("/") );
base=text.substring(0,hostIndex);
}//end with (this)
if (this.protIndex<0 || this.hostIndex<0 || this.fileName.length==0){
this.isValid=false;
return;
}
var portIndex= this.host.indexOf(":");
if (portIndex>0){
this.port=parseInt(this.host.substring(portIndex+1,this.host.length)); //extract the port number
this.host=this.host.substring(0,portIndex); //truncate the host string to www.anycorp.com
}
var periodIndex=this.fileName.lastIndexOf("."); //find where the extension begins
if(periodIndex<0){ //no extension
this.extension="";
return;
}
//To find extension
this.extension=this.fileName.substring(periodIndex+1,this.text.length); //extract everything beyond the period in the filename
var endIndex=new Array(2);
endIndex[0]= this.extension.indexOf("?"); //check to see if the file is being fed arguemnets (eg /file.exe?var=fred or /file.exe/var=fred)
endIndex[1]= this.extension.indexOf("/");
endIndex.sort();
//we need to choose the smallest endIndex value greater than zero.
//The next loops finds that.
for (i=0;i<endIndex.length;i++){
if (endIndex[i]>=0){
extensionEnd=endIndex[i]
}
}
if (extensionEnd>=0){ //if file is being fed arguments
this.extension=this.extension.substring(0,extensionEnd); //just extract extension and throw away the arguements
}
this.relativeToAbs();//convert filename from relative to absolute
return
}//end parse()
function relativeToAbs(){
//converts path navigation marks (..) within a baseDir to an absolute path.
var doubleDotsIndex = this.fileName.indexOf(".."); //look for relative path
//If the filename starts with a .., just remove the ... There is no way to move
//up a directory from the root dir.
while(doubleDotsIndex==0){
this.filename=this.fileName.substring(2,this.fileName.length);
doubleDotsIndex=this.fileName.indexOf("..");
}
while (doubleDotsIndex>0){
var prevDirIndex=this.fileName.lastIndexOf("/",doubleDotsIndex-2);
var part1= this.fileName.substring(0,prevDirIndex)
var part2= this.fileName.substring(doubleDotsIndex + 2,this.fileName.length);
this.fileName=part1+part2;
doubleDotsIndex=this.fileName.indexOf("..");
}//end while
this.text=this.base+this.fileName
}//end relativeToAbs()
}//end Url class
function charReplace(text,charOld,charNew){
//Pass a string with charOld in it and
//this function converts all charOld to charNew
var tempText=new StringEx(text);
var charOldPos=tempText.indexOf(charOld);
while(charOldPos>=0){
tempText=new StringEx(tempText.stuff(charOldPos,1,charNew));
charOldPos=tempText.indexOf(charOld);
}
return tempText;
}//end nToR
function stripSpace(text){
//removes all leading white space, all tabs, returns, and linefeeds
text=new StringEx(text.leftTrim()); //remove any leading spaces.
text=new StringEx(text.rightTrim()); //remove trailing spaces.
text=charReplace(text,'\n','');
text=charReplace(text,'\r','');
text=charReplace(text,'\t','');
return text;
}