Skip to content

Commit

Permalink
Added WebSeed download from magnet
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan386 committed Dec 3, 2014
1 parent c1c9683 commit 8e5733e
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 94 deletions.
2 changes: 1 addition & 1 deletion shareaza/Download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ BOOL CDownload::OpenDownload()

SetModified();

if ( IsTorrent() )
if ( IsTorrent(TRUE) )
{
if ( Open( m_pTorrent ) )
return TRUE;
Expand Down
78 changes: 37 additions & 41 deletions shareaza/DownloadTransferHTTP.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DownloadTransferHTTP.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2013.
// Copyright (c) Shareaza Development Team, 2002-2014.
// This file is part of SHAREAZA (shareaza.sourceforge.net)
//
// Shareaza is free software; you can redistribute it
Expand Down Expand Up @@ -220,17 +220,30 @@ BOOL CDownloadTransferHTTP::StartNextFragment()
{
return SendRequest();
}
else if ( m_pDownload->NeedTigerTree() && m_sTigerTree.GetLength() )

if ( m_pDownload->NeedTigerTree() )
{
theApp.Message( MSG_INFO, IDS_DOWNLOAD_TIGER_REQUEST,
(LPCTSTR)m_pDownload->GetDisplayName(), (LPCTSTR)m_sAddress );

m_bTigerFetch = TRUE;
m_bTigerIgnore = TRUE;

return SendRequest();
if ( m_sTigerTree.IsEmpty() && m_pDownload->m_oTiger && Settings.Downloads.VerifyTiger && ! m_bTigerIgnore )
{
// Converting urn containing tiger tree root to
// "/gnutella/thex/v1?urn:tree:tiger/:{TIGER_ROOT}&depth={TIGER_HEIGHT}&ed2k={0/1}"
// in case if "X-Thex-URI" and "X-TigerTree-Path" headers will be absent or it is a "GIV" (push) connection
m_sTigerTree.Format( L"/gnutella/thex/v1?%s&depth=%u&ed2k=%d", (LPCTSTR)m_pDownload->m_oTiger.toUrn(),
Settings.Library.TigerHeight, ( Settings.Downloads.VerifyED2K ? 1 : 0 ) );
}

if ( ! m_sTigerTree.IsEmpty() )
{
theApp.Message( MSG_INFO,IDS_DOWNLOAD_TIGER_REQUEST, (LPCTSTR)m_pDownload->GetDisplayName(),(LPCTSTR)m_sAddress );

m_bTigerFetch = TRUE;
m_bTigerIgnore = TRUE;

return SendRequest();
}
}
else if ( m_pSource && ! m_pSource->m_bMetaIgnore && ! m_sMetadata.IsEmpty() )

if ( m_pSource && ! m_pSource->m_bMetaIgnore && ! m_sMetadata.IsEmpty() )
{
theApp.Message( MSG_INFO, IDS_DOWNLOAD_METADATA_REQUEST,
(LPCTSTR)m_pDownload->GetDisplayName(), (LPCTSTR)m_sAddress );
Expand All @@ -240,25 +253,22 @@ BOOL CDownloadTransferHTTP::StartNextFragment()

return SendRequest();
}
else if ( m_pDownload->GetFragment( this ) )

if ( m_pDownload->GetFragment( this ) )
{
ChunkifyRequest( &m_nOffset, &m_nLength, Settings.Downloads.ChunkSize, TRUE );

theApp.Message( MSG_INFO, IDS_DOWNLOAD_FRAGMENT_REQUEST,
m_nOffset, m_nOffset + m_nLength - 1,
(LPCTSTR)m_pDownload->GetDisplayName(), (LPCTSTR)m_sAddress );
theApp.Message( MSG_INFO, IDS_DOWNLOAD_FRAGMENT_REQUEST, m_nOffset, m_nOffset + m_nLength - 1, (LPCTSTR)m_pDownload->GetDisplayName(), (LPCTSTR)m_sAddress );

return SendRequest();
}
else
{
if ( m_pSource != NULL ) m_pSource->SetAvailableRanges( NULL );

if ( m_pSource != NULL ) m_pSource->SetAvailableRanges( NULL );

theApp.Message( MSG_INFO, IDS_DOWNLOAD_FRAGMENT_END, (LPCTSTR)m_sAddress );
Close( TRI_TRUE );
theApp.Message( MSG_INFO, IDS_DOWNLOAD_FRAGMENT_END, (LPCTSTR)m_sAddress );
Close( TRI_TRUE );

return FALSE;
}
return FALSE;
}

//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -342,6 +352,7 @@ BOOL CDownloadTransferHTTP::SendRequest()
}
else
{
m_nOffset = 0;
Write( _P("Range: bytes=0-\r\n") );
}

Expand Down Expand Up @@ -861,24 +872,9 @@ BOOL CDownloadTransferHTTP::OnHeaderLine(CString& strHeader, CString& strValue)
&& ( !oMD5 .fromUrn( strValue ) || m_pSource->CheckHash( oMD5 ) )
&& ( !oBTH .fromUrn( strValue ) || ( m_pSource->CheckHash( oBTH ), TRUE ) ) )
{
if ( oTiger && Settings.Downloads.VerifyTiger && !m_bTigerIgnore && m_sTigerTree.IsEmpty()
&& ( _tcsistr( m_sUserAgent, L"shareaza 2.1.4" ) != NULL
|| _tcsistr( m_sUserAgent, L"shareaza 2.2.0" ) != NULL ) )
{
// Converting urn containing tiger tree root to
// "/gnutella/thex/v1?urn:tree:tiger/:{TIGER_ROOT}&depth={TIGER_HEIGHT}&ed2k={0/1}"
// in case if "X-Thex-URI" and "X-TigerTree-Path" headers
// will be absent (perfect workaround for "silent" Sareaza 2.2.0.0)
m_sTigerTree.Format( L"/gnutella/thex/v1?%s&depth=%u&ed2k=%d",
(LPCTSTR)oTiger.toUrn(),
Settings.Library.TigerHeight,
( Settings.Downloads.VerifyED2K ? 1 : 0 ) );
}
m_bHashMatch = m_bHashMatch || oSHA1 || oTiger || oED2K || oBTH || oMD5;
continue;
}
theApp.Message( MSG_ERROR, IDS_DOWNLOAD_WRONG_HASH, (LPCTSTR)m_sAddress,
(LPCTSTR)m_pDownload->GetDisplayName() );
theApp.Message( MSG_ERROR, IDS_DOWNLOAD_WRONG_HASH, (LPCTSTR)m_sAddress, (LPCTSTR)m_pDownload->GetDisplayName() );
Close( TRI_FALSE );
return FALSE;
}
Expand Down Expand Up @@ -1395,14 +1391,14 @@ BOOL CDownloadTransferHTTP::ReadContent()

if ( m_bRecvBackwards )
{
CAutoVectorPtr< BYTE >pBuffer( new BYTE[ nLength ] );
CAutoVectorPtr< BYTE >pBuffer( new BYTE[ (size_t)nLength ] );
if ( ! pBuffer )
{
// Out of memory
Close( TRI_TRUE );
return FALSE;
}
CBuffer::ReverseBuffer( pInput->m_pBuffer, pBuffer, nLength );
CBuffer::ReverseBuffer( pInput->m_pBuffer, pBuffer, (size_t)nLength );
bSubmit = m_pDownload->SubmitData( m_nOffset + m_nLength - m_nPosition - nLength, pBuffer, nLength );
}
else
Expand All @@ -1412,7 +1408,7 @@ BOOL CDownloadTransferHTTP::ReadContent()
}

if ( m_bChunked )
pInput->Remove( nLength );
pInput->Remove( (size_t)nLength );
else
pInput->Clear();

Expand Down Expand Up @@ -1572,7 +1568,7 @@ BOOL CDownloadTransferHTTP::ReadFlush()
QWORD nRemove = min( (QWORD)pInput->m_nLength, m_nContentLength );
m_nContentLength -= nRemove;

pInput->Remove( nRemove );
pInput->Remove( (size_t)nRemove );

if ( m_nContentLength == 0 )
{
Expand Down
5 changes: 3 additions & 2 deletions shareaza/DownloadWithFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,9 @@ CString CDownloadWithFile::GetDisplayName() const

BOOL CDownloadWithFile::IsPositionEmpty(QWORD nOffset)
{
return m_pFile.get() && m_pFile->IsValid() &&
m_pFile->IsPositionRemaining( nOffset );
return m_pFile.get()
&& m_pFile->IsValid()
&& m_pFile->IsPositionRemaining( nOffset );
}

//////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions shareaza/DownloadWithTorrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ bool CDownloadWithTorrent::IsSeeding() const
return m_bSeeding != 0;
}

bool CDownloadWithTorrent::IsTorrent() const
bool CDownloadWithTorrent::IsTorrent(BOOL bWithInfo) const
{
return m_pTorrent.IsAvailable();
return bWithInfo ? m_pTorrent.IsAvailableInfo() : m_pTorrent.IsAvailable();
}

bool CDownloadWithTorrent::IsSingleFileTorrent() const
Expand Down
2 changes: 1 addition & 1 deletion shareaza/DownloadWithTorrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class CDownloadWithTorrent : public CDownloadWithFile, public CTrackerEvent
void AddUpload(CUploadTransferBT* pUpload);
void RemoveUpload(CUploadTransferBT* pUpload);
bool IsSeeding() const;
bool IsTorrent() const;
bool IsTorrent(BOOL bWithInfo = 0) const;
bool IsSingleFileTorrent() const;
float GetRatio() const;
BOOL UploadExists(in_addr* pIP) const;
Expand Down
131 changes: 87 additions & 44 deletions shareaza/ShareazaURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,9 @@ BOOL CShareazaURL::ParseED2KFTP(LPCTSTR pszURL, BOOL bResolve)

if ( !m_oED2K.fromString( strHash ) ) return FALSE;

m_bSize = _stscanf( strURL, _T("%I64i"), &m_nSize ) == 1;
if ( ! m_bSize ) return FALSE;
QWORD nSize = 0;
if ( m_bSize = _stscanf( strURL, _T("%I64u"), &nSize ) != 1 || nSize == 0 ) return FALSE;
m_nSize = nSize;

nSlash = m_sAddress.Find( _T('@') );

Expand Down Expand Up @@ -478,8 +479,10 @@ BOOL CShareazaURL::ParseDCHub(LPCTSTR pszURL, BOOL bResolve)
if ( ! m_oTiger.fromString( strHash ) )
return FALSE;

if ( _stscanf( strURL, _T("/%I64i"), &m_nSize ) == 1 )
QWORD nSize = 0;
if ( _stscanf( strURL, _T("/%I64u"), &nSize ) == 1 && nSize != 0 )
{
m_nSize = nSize;
m_bSize = TRUE;
}
}
Expand Down Expand Up @@ -555,26 +558,46 @@ BOOL CShareazaURL::ParseMagnet(LPCTSTR pszURL)
CString strURL( pszURL );
CAutoPtr< CBTInfo > pTorrent( new CBTInfo() );

for ( strURL += '&' ; strURL.GetLength() ; )
// TODO: Add multi-file magnets
DWORD nMyNumber = (DWORD)-1;

for ( strURL += _T('&') ; strURL.GetLength() ; )
{
CString strPart = strURL.SpanExcluding( _T("&") );
strURL = strURL.Mid( strPart.GetLength() + 1 );

int nEquals = strPart.Find( '=' );
if ( nEquals < 0 ) continue;
int nEquals = strPart.Find( _T('=') );
if ( nEquals < 0 )
continue;

CString strKey = URLDecode( strPart.Left( nEquals ) );
CString strValue = URLDecode( strPart.Mid( nEquals + 1 ) );

SafeString( strKey );
SafeString( strValue );

if ( strKey.IsEmpty() || strValue.IsEmpty() ) continue;
// Parse numbered keys
DWORD nNumber = 0;
int nDot = strKey.Find( _T('.') );
if ( nDot > 0 )
{
if ( _istdigit( strKey.GetAt( nDot + 1 ) ) )
nNumber = _tcstoul( strKey.Mid( nDot + 1 ), NULL, 10 );
strKey = strKey.Left( nDot );
}
if ( nMyNumber == (DWORD)-1 )
nMyNumber = nNumber;

if ( strKey.IsEmpty() || strValue.IsEmpty() )
continue;

if ( _tcsicmp( strKey, _T("xt") ) == 0 ||
_tcsicmp( strKey, _T("xs") ) == 0 ||
_tcsicmp( strKey, _T("as") ) == 0 ||
_tcsnicmp( strKey, _T("tr"), 2 ) == 0 ) // "tr=", "tr.1=", "tr.2="...
strKey.MakeLower();

if ( strKey == _T("xt") || // "eXact Topic" (URN containing file hash)
strKey == _T("xs") || // "eXact Source" (p2p link)
strKey == _T("as") || // "Acceptable Source" (web link to the online file)
strKey == _T("ws") || // "WebSeed" (web link to the online file or folder/ for multifile-torrent)
strKey == _T("tr") ) // "TRacker address" (tracker URL for BitTorrent downloads)
{
if ( StartsWith( strValue, _PT("urn:") ) ||
StartsWith( strValue, _PT("sha1:") ) ||
Expand All @@ -584,12 +607,15 @@ BOOL CShareazaURL::ParseMagnet(LPCTSTR pszURL)
StartsWith( strValue, _PT("btih:") ) ||
StartsWith( strValue, _PT("ed2k:") ) )
{
if ( !m_oSHA1 ) m_oSHA1.fromUrn( strValue );
if ( !m_oTiger ) m_oTiger.fromUrn( strValue );
if ( !m_oMD5 ) m_oMD5.fromUrn( strValue );
if ( !m_oED2K ) m_oED2K.fromUrn( strValue );
if ( !m_oBTH ) m_oBTH.fromUrn( strValue );
if ( !m_oBTH ) m_oBTH.fromUrn< Hashes::base16Encoding >( strValue );
if ( nMyNumber == nNumber )
{
if ( ! m_oSHA1 ) m_oSHA1.fromUrn( strValue );
if ( ! m_oTiger ) m_oTiger.fromUrn( strValue );
if ( ! m_oMD5 ) m_oMD5.fromUrn( strValue );
if ( ! m_oED2K ) m_oED2K.fromUrn( strValue );
if ( ! m_oBTH ) m_oBTH.fromUrn( strValue );
if ( ! m_oBTH ) m_oBTH.fromUrn< Hashes::base16Encoding >( strValue );
}
}
else if ( StartsWith( strValue, _PT("http://") ) ||
StartsWith( strValue, _PT("http%3A//") ) ||
Expand All @@ -603,52 +629,69 @@ BOOL CShareazaURL::ParseMagnet(LPCTSTR pszURL)
strValue.Replace( _T(" "), _T("%20") );
strValue.Replace( _T("%3A//"), _T("://") );

if ( _tcsicmp( strKey, _T("xt") ) == 0 )
if ( strKey == _T("xt") ) // "eXact Topic" (URN containing file hash)
{
CString strURL = _T("@") + strValue;

if ( m_sURL.GetLength() )
m_sURL = strURL + _T(", ") + m_sURL;
else
m_sURL = strURL;
if ( nMyNumber == nNumber )
{
if ( m_sURL.GetLength() )
m_sURL = _T("@") + strURL + _T(", ") + m_sURL;
else
m_sURL = _T("@") + strURL;
}
}
else if( _tcsnicmp( strKey, _T("tr"), 2 ) == 0 ) // "tr=", "tr.1=", "tr.2="...
else if( strKey == _T("tr") ) // "TRacker address" (tracker URL for BitTorrent downloads)
{
// Trackers are common
pTorrent->SetTracker( strValue );
}
else
{
if ( m_sURL.GetLength() ) m_sURL += _T(", ");
m_sURL += strValue;
if ( nMyNumber == nNumber )
{
if ( m_sURL.GetLength() )
m_sURL = m_sURL + _T(", ") + strValue;
else
m_sURL = m_sURL + strValue;
}
}
}
}
else if ( _tcsicmp( strKey, _T("dn") ) == 0 )
else if ( strKey == _T("dn") ) // "Display Name" (filename)
{
m_sName = strValue;
if ( nMyNumber == nNumber )
{
m_sName = strValue;
}
}
else if ( _tcsicmp( strKey, _T("kt") ) == 0 )
else if ( strKey == _T("kt") ) // "Keyword Topic" (key words for search)
{
m_sName = strValue;
m_oSHA1.clear();
m_oTiger.clear();
m_oED2K.clear();
m_oMD5.clear();
m_oBTH.clear();
if ( nMyNumber == nNumber )
{
m_sName = strValue;
m_oSHA1.clear();
m_oTiger.clear();
m_oED2K.clear();
m_oMD5.clear();
m_oBTH.clear();
}
}
else if ( _tcsicmp( strKey, _T("xl") ) == 0 ||
_tcsicmp( strKey, _T("sz") ) == 0 || // Non-standard
_tcsicmp( strKey, _T("fs") ) == 0 ) // Foxy
else if ( strKey == _T("xl") || // "eXact Length" (size in bytes)
strKey == _T("sz") || // used by old Shareaza
strKey == _T("fs") ) // used by Foxy
{
QWORD nSize;
if ( ( ! m_bSize ) && ( _stscanf( strValue, _T("%I64i"), &nSize ) == 1 ) && ( nSize > 0 ) )
if ( nMyNumber == nNumber )
{
m_nSize = nSize;
m_bSize = TRUE;
QWORD nSize = 0;
if ( m_nSize == SIZE_UNKNOWN && _stscanf( strValue, _T("%I64u"), &nSize ) == 1 && nSize != 0 )
{
m_nSize = nSize;
m_bSize = TRUE;
}
}
}
else if ( _tcsicmp( strKey, _T("bn") ) == 0 )
else if ( strKey == _T("bn") ) // "Bittorrent Node" (BitTorrent node address for DHT bootstrapping)
{
// Nodes are common
pTorrent->SetNode( strValue );
}
else if ( _tcsicmp( strKey, _T("br") ) == 0 )
Expand Down
Loading

0 comments on commit 8e5733e

Please sign in to comment.