00001 #include "gdSnarfHttp.h"
00002
00003 gdHttpURI::gdHttpURI (gURI& aURI, gString& sInProxy, t_gPort myPort)
00004 : uPort( myPort ),
00005 isHttpOk( false )
00006 {
00007 int parseOp = thisProcessHttpURI( aURI, sInProxy, uPort );
00008 isHttpOk = parseOp==0;
00009 }
00010
00011 gdHttpURI::gdHttpURI (gURI& aURI)
00012 : uPort( 80 ),
00013 isHttpOk( false )
00014 {
00015 gString sEmpty;
00016 int parseOp = thisProcessHttpURI( aURI, sEmpty, uPort );
00017 isHttpOk = parseOp==0;
00018 }
00019
00020 gdHttpURI::~gdHttpURI ()
00021 {
00022 }
00023
00024 bool gdHttpURI::IsOk ()
00025 {
00026 return isHttpOk;
00027 }
00028
00029 int gdHttpURI::thisProcessHttpURI (gURI& aURI, gString& sInProxy, t_gPort& myPort)
00030 {
00031 char* str = aURI.GetDomainStr();
00032 char* inHostName = aURI.IsValidDomain() ? str : nil;
00033 char* connectHostName = inHostName;
00034 gString sUriPath( aURI.GetPathStr() );
00035 t_gPort portURI=0;
00036 bool isPxyOk = true;
00037
00038
00039
00040 if ( sUriPath.IsEmpty() ) sUriPath.Set( "/" );
00041
00042 DBGPRINT("DBG: GetDomainStr()=%s, inHostName=%s, connectHostName=%s\n",str,inHostName,connectHostName);
00043
00044 if ( sInProxy.IsEmpty() ) {
00045 sPath = sUriPath;
00046 }
00047 else {
00048 inHostName = ParseProxyStr( sInProxy, isPxyOk, proxy );
00049 sPath.Set( "http://" );
00050 sPath.Add( aURI.GetDomainStr() );
00051 if ( myPort!=80 ) portURI = myPort;
00052 myPort = proxy.port;
00053 if ( portURI!=0 ) {
00054
00055 sPath.Add( ":" );
00056 sPath.Add( (unsigned)portURI );
00057 }
00058 sPath.Add( aURI.GetPathStr() );
00059 if ( isPxyOk==false ) return 1;
00060 }
00061
00062 if ( inHostName==nil || inHostName[0]==0 || connectHostName==nil || connectHostName[0]==0 ) return 1;
00063
00064 sConnectHost.Set( inHostName );
00065 sContentHost.Set( connectHostName );
00066
00067 DBGPRINT("DBG: sConnectHost=%s, sContentHost=%s, sPath='%s', port=%u\n",inHostName,connectHostName,sPath.Str(),myPort);
00068
00069 return 0;
00070 }
00071
00072 char* gdHttpURI::ParseProxyStr (gString& sInProxy, bool& isProxyOk, sOptProxy& resultProxy)
00073 {
00074 ;
00075
00076
00077
00078
00079 unsigned n;
00080 char* str = sInProxy.Str();
00081 gParam aParam( str, ":" );
00082
00083 n = aParam.N();
00084 resultProxy.Reset();
00085 isProxyOk = n==2;
00086 if ( isProxyOk==false ) return nil;
00087
00088 gString sHost( aParam.Str(1) );
00089 isProxyOk = sHost.FindExcept( "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_", true )==0;
00090 if ( isProxyOk==false ) return nil;
00091
00092 t_uint32 vRes=0;
00093 isProxyOk = gStrControl::Self().ConvertToUInt32( aParam.Str(2), vRes )==0;
00094 if ( isProxyOk==false ) return nil;
00095 isProxyOk = vRes>0 && vRes<=MAX_UINT16_U;
00096 resultProxy.sHost = sHost;
00097 resultProxy.port = (t_gPort)vRes;
00098 return resultProxy.sHost.Str();
00099 }
00100
00101 gdHttpSnarf::gdHttpSnarf (gTcpConnect& connection)
00102 : gXHttpCont( connection ),
00103 usedSize( 0 )
00104 {
00105 }
00106
00107 gdHttpSnarf::~gdHttpSnarf ()
00108 {
00109 }
00110
00111 gString& gdHttpSnarf::GetReply ()
00112 {
00113
00114 return sReply;
00115 }
00116
00117 int gdHttpSnarf::HttpCode ()
00118 {
00119
00120
00121
00122
00123
00124
00125
00126
00127 return replyHeader.htmlCode;
00128 }
00129
00130 unsigned gdHttpSnarf::GetHeaderLines (char* sKind, gList& resultL)
00131 {
00132 unsigned i, n, pos=0;
00133 gString s;
00134
00135 if ( sKind==nil ) return 0;
00136 for (i=1, n=replyHeader.kindsL.N(); i<=n; i++) {
00137 gString s( replyHeader.kindsL.Str(i) );
00138 if ( s.Match( sKind ) ) {
00139 if ( pos==0 ) pos = i;
00140 resultL.Add( replyHeader.linesL.Str(i) );
00141 }
00142 }
00143 return pos;
00144 }
00145
00146 bool gdHttpSnarf::SnarfContent (gString& sHost, gString& sPath)
00147 {
00148 bool resultOk = GetContent( e_Get, sHost, sPath, sReply );
00149 return resultOk;
00150 }
00151
00152 int gdHttpSnarf::FileInOut (gFileTemp& fT, FILE* fOut)
00153 {
00154 int fHandle = fT.fHandle;
00155 t_uchar c;
00156
00157 if ( fT.Rewind()==false ) return -1;
00158 for ( ; read(fHandle,&c,1)==1; ) {
00159 fprintf(fOut,"%c",c);
00160 }
00161 return 0;
00162 }
00163
00164 int gdHttpSnarf::DoPrint (FILE* fOut)
00165 {
00166 int thisError=0;
00167 t_uchar* uBuf = UBuffer();
00168 unsigned k, nod, nNodes, maxSize;
00169
00170
00171
00172
00173 switch ( BufferKind() ) {
00174 case gBigBuffer::e_NormalRAM:
00175 if ( IsText() ) {
00176 fprintf(fOut,"%s",uBuf);
00177 }
00178 else {
00179 for (k=0, maxSize=(unsigned)usedSize; k<maxSize; k++) {
00180 fprintf(fOut,"%c",uBuf[k]);
00181 }
00182 }
00183 case gBigBuffer::e_NodesRAM:
00184 for (nod=0, nNodes=GetBuffer().GetNumberNodes(); nod<nNodes; nod++) {
00185 uBuf = GetBuffer().GetNode(nod)->uBuf;
00186 for (k=0, maxSize=GetBuffer().GetNode(nod)->size; k<maxSize; k++)
00187 fprintf(fOut,"%c",uBuf[k]);
00188 }
00189 break;
00190 case gBigBuffer::e_InFile:
00191 thisError = FileInOut( GetBuffer().GetFile(), fOut )!=0;
00192 default:
00193 break;
00194 }
00195
00196 return thisError;
00197 }
00198
00199