Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

SourceURL.cpp

Go to the documentation of this file.
00001 //
00002 // SourceURL.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "Network.h"
00026 #include "SourceURL.h"
00027 #include "SHA.h"
00028 #include "ED2K.h"
00029 
00030 #ifdef _DEBUG
00031 #undef THIS_FILE
00032 static char THIS_FILE[]=__FILE__;
00033 #define new DEBUG_NEW
00034 #endif
00035 
00036 
00038 // CSourceURL construction
00039 
00040 CSourceURL::CSourceURL(LPCTSTR pszURL)
00041 {
00042         Clear();
00043         if ( pszURL != NULL ) Parse( pszURL );
00044 }
00045 
00047 // CSourceURL clear
00048 
00049 void CSourceURL::Clear()
00050 {
00051         m_sURL.Empty();
00052         m_nProtocol = PROTOCOL_NULL;
00053         m_sAddress.Empty();
00054         m_pAddress.S_un.S_addr = 0;
00055         m_nPort = 0;
00056         m_pServerAddress.S_un.S_addr = 0;
00057         m_nServerPort = 0;
00058         m_sPath.Empty();
00059         m_bSHA1 = FALSE;
00060         m_bED2K = FALSE;
00061         m_bBTH  = FALSE;
00062         m_bBTC  = FALSE;
00063         m_bSize = FALSE;
00064         m_sLogin.Empty ();
00065         m_sPassword.Empty ();
00066 }
00067 
00069 // CSourceURL root parse
00070 
00071 BOOL CSourceURL::Parse(LPCTSTR pszURL, BOOL bResolve)
00072 {
00073         Clear();
00074         if ( ParseHTTP( pszURL, bResolve ) ) return TRUE;
00075         Clear();
00076         if ( ParseFTP( pszURL, bResolve ) ) return TRUE;
00077         Clear();
00078         if ( ParseED2KFTP( pszURL, bResolve ) ) return TRUE;
00079         Clear();
00080         if ( ParseBTC( pszURL, bResolve ) ) return TRUE;
00081         Clear();
00082         return FALSE;
00083 }
00084 
00086 // CSourceURL HTTP
00087 
00088 BOOL CSourceURL::ParseHTTP(LPCTSTR pszURL, BOOL bResolve)
00089 {
00090         if ( _tcsncmp( pszURL, _T("http://"), 7 ) != 0 ) return FALSE;
00091         
00092         CString strURL = pszURL + 7;
00093         
00094         int nSlash = strURL.Find( _T('/') );
00095         
00096         if ( nSlash >= 0 )
00097         {
00098                 m_sAddress      = strURL.Left( nSlash );
00099                 m_sPath         = strURL.Mid( nSlash );
00100         }
00101         else
00102         {
00103                 m_sAddress = strURL;
00104                 m_sPath = _T("/");
00105         }
00106         
00107         int nAt = m_sAddress.Find( _T('@') );
00108         if ( nAt >= 0 ) m_sAddress = m_sAddress.Mid( nAt + 1 );
00109         
00110         if ( m_sAddress.IsEmpty() ) return FALSE;
00111         
00112         if ( _tcsnicmp( m_sPath, _T("/uri-res/N2R?urn:sha1:"), 22 ) == 0 )
00113         {
00114                 m_bSHA1 = CSHA::HashFromURN( m_sPath.Mid( 13 ), &m_pSHA1 );
00115         }
00116         else if ( _tcsnicmp( m_sPath, _T("/uri-res/N2R?urn:bitprint:"), 26 ) == 0 )
00117         {
00118                 m_bSHA1 = CSHA::HashFromURN( m_sPath.Mid( 13 ), &m_pSHA1 );
00119         }
00120         
00121         SOCKADDR_IN saHost;
00122         
00123         BOOL bResult = Network.Resolve( m_sAddress, INTERNET_DEFAULT_HTTP_PORT, &saHost, bResolve );
00124         
00125         m_pAddress      = saHost.sin_addr;
00126         m_nPort         = htons( saHost.sin_port );
00127         
00128         m_sURL          = pszURL;
00129         m_nProtocol     = PROTOCOL_HTTP;
00130         
00131         return bResult;
00132 }
00133 
00135 // CSourceURL FTP
00136 
00137 BOOL CSourceURL::ParseFTP(LPCTSTR pszURL, BOOL bResolve)
00138 {
00139         // URI format
00140         // ftp://[user[:password]@]host[:port][/path]
00141 
00142         if ( _tcsncmp( pszURL, _T("ftp://"), 6 ) != 0 ) return FALSE;
00143         
00144         CString strURL ( pszURL + 6 );
00145         
00146         int nSlash = strURL.Find( _T('/') );
00147         
00148         if ( nSlash >= 0 )
00149         {
00150                 m_sAddress      = strURL.Left( nSlash );
00151                 m_sPath         = strURL.Mid( nSlash );
00152         }
00153         else
00154         {
00155                 m_sAddress = strURL;
00156                 m_sPath = _T("/");
00157         }
00158         
00159         int nAt = m_sAddress.Find( _T('@') );
00160         if ( nAt >= 0 )
00161         {
00162                 m_sLogin = m_sAddress.Left( nAt );
00163                 m_sAddress = m_sAddress.Mid( nAt + 1 );
00164         
00165                 int nColon = m_sLogin.Find( _T(':') );
00166                 if ( nColon >= 0 )
00167                 {
00168             m_sPassword = m_sLogin.Mid( nColon + 1 );                   
00169             m_sLogin = m_sLogin.Left( nColon );                 
00170                 }
00171         }
00172         else
00173         {
00174                 m_sLogin = _T("anonymous");
00175                 m_sPassword = _T("[email protected]");
00176         }
00177 
00178         if ( m_sAddress.IsEmpty() || m_sLogin.IsEmpty() )
00179                 return FALSE;
00180         
00181         SOCKADDR_IN saHost;
00182         
00183         BOOL bResult = Network.Resolve( m_sAddress, INTERNET_DEFAULT_FTP_PORT, &saHost, bResolve );
00184         
00185         m_pAddress      = saHost.sin_addr;
00186         m_nPort         = htons( saHost.sin_port );
00187         
00188         m_sURL          = pszURL;
00189         m_nProtocol     = PROTOCOL_FTP;
00190         
00191         return bResult;
00192 }
00193 
00195 // CSourceURL ED2KFTP
00196 
00197 BOOL CSourceURL::ParseED2KFTP(LPCTSTR pszURL, BOOL bResolve)
00198 {
00199         if ( _tcsnicmp( pszURL, _T("ed2kftp://"), 10 ) != 0 ) return FALSE;
00200         
00201         CString strURL = pszURL + 10;
00202         BOOL bPush = FALSE;
00203         
00204         int nSlash = strURL.Find( _T('/') );
00205         if ( nSlash < 7 ) return FALSE;
00206 
00207         m_sAddress      = strURL.Left( nSlash );
00208         strURL          = strURL.Mid( nSlash + 1 );
00209         
00210         nSlash = strURL.Find( _T('/') );
00211         if ( nSlash != 32 ) return FALSE;
00212         
00213         CString strHash = strURL.Left( 32 );
00214         strURL                  = strURL.Mid( 33 );
00215         
00216         if ( ! CED2K::HashFromString( strHash, &m_pED2K ) ) return FALSE;
00217         
00218         m_bSize = _stscanf( strURL, _T("%I64i"), &m_nSize ) == 1;
00219         if ( ! m_bSize ) return FALSE;
00220         
00221         nSlash = m_sAddress.Find( _T('@') );
00222         
00223         if ( nSlash > 0 )
00224         {
00225                 strHash = m_sAddress.Left( nSlash );
00226                 m_sAddress = m_sAddress.Mid( nSlash + 1 );
00227                 if ( _stscanf( strHash, _T("%lu"), &m_pAddress.S_un.S_addr ) != 1 ) return FALSE;
00228                 bPush = TRUE;
00229         }
00230         
00231         SOCKADDR_IN saHost;
00232         BOOL bResult = Network.Resolve( m_sAddress, ED2K_DEFAULT_PORT, &saHost, bResolve );
00233         
00234         if ( bPush )
00235         {
00236                 m_pServerAddress        = saHost.sin_addr;
00237                 m_nServerPort           = htons( saHost.sin_port );
00238                 m_nPort                         = 0;
00239         }
00240         else
00241         {
00242                 m_pAddress      = saHost.sin_addr;
00243                 m_nPort         = htons( saHost.sin_port );
00244         }
00245         
00246         m_sURL          = pszURL;
00247         m_nProtocol     = PROTOCOL_ED2K;
00248         
00249         return bResult;
00250 }
00251 
00253 // CSourceURL BTC
00254 
00255 BOOL CSourceURL::ParseBTC(LPCTSTR pszURL, BOOL bResolve)
00256 {
00257         if ( _tcsnicmp( pszURL, _T("btc://"), 6 ) != 0 ) return FALSE;
00258         
00259         CString strURL = pszURL + 6;
00260         BOOL bPush = FALSE;
00261         
00262         int nSlash = strURL.Find( _T('/') );
00263         if ( nSlash < 7 ) return FALSE;
00264 
00265         m_sAddress      = strURL.Left( nSlash );
00266         strURL          = strURL.Mid( nSlash + 1 );
00267         
00268         nSlash = strURL.Find( _T('/') );
00269         m_bBTC = FALSE;
00270         
00271         if ( nSlash == 32 )
00272         {
00273                 CString strGUID = strURL.Left( 32 );
00274                 m_bBTC = CSHA::HashFromString( strGUID, &m_pBTC );
00275         }
00276         else if ( nSlash < 0 ) return FALSE;
00277         
00278         strURL = strURL.Mid( nSlash + 1 );
00279         
00280         if ( ! CSHA::HashFromString( strURL, &m_pBTH ) ) return FALSE;
00281         
00282         SOCKADDR_IN saHost;
00283         BOOL bResult = Network.Resolve( m_sAddress, ED2K_DEFAULT_PORT, &saHost, bResolve );
00284         
00285         m_pAddress      = saHost.sin_addr;
00286         m_nPort         = htons( saHost.sin_port );
00287         
00288         m_sURL          = pszURL;
00289         m_nProtocol     = PROTOCOL_BT;
00290         
00291         return bResult;
00292 }

Generated on Thu Dec 15 10:39:50 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2