📄 uploadtransferhttp.cpp
字号:
//
// UploadTransferHTTP.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2004.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "StdAfx.h"
#include "Shareaza.h"
#include "Settings.h"
#include "Uploads.h"
#include "UploadFile.h"
#include "UploadFiles.h"
#include "UploadQueue.h"
#include "UploadQueues.h"
#include "UploadTransferHTTP.h"
#include "TransferFile.h"
#include "Transfers.h"
#include "Remote.h"
#include "ShellIcons.h"
#include "Statistics.h"
#include "Buffer.h"
#include "Schema.h"
#include "XML.h"
#include "Network.h"
#include "Library.h"
#include "SharedFile.h"
#include "Downloads.h"
#include "Download.h"
#include "LocalSearch.h"
#include "ImageServices.h"
#include "ThumbCache.h"
#include "Neighbours.h"
#include "Neighbour.h"
#include "G2Packet.h"
#include "GProfile.h"
#include "SHA.h"
#include "ED2K.h"
#include "TigerTree.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// CUploadTransferHTTP construction
CUploadTransferHTTP::CUploadTransferHTTP() : CUploadTransfer( PROTOCOL_HTTP )
{
m_bKeepAlive = TRUE;
m_nGnutella = 0;
}
CUploadTransferHTTP::~CUploadTransferHTTP()
{
}
//////////////////////////////////////////////////////////////////////
// CUploadTransferHTTP attach to connection
void CUploadTransferHTTP::AttachTo(CConnection* pConnection)
{
CUploadTransfer::AttachTo( pConnection );
theApp.Message( MSG_DEFAULT, IDS_UPLOAD_ACCEPTED, (LPCTSTR)m_sAddress );
m_mInput.pLimit = &Settings.Bandwidth.Request;
m_mOutput.pLimit = &m_nBandwidth;
m_nState = upsRequest;
m_tRequest = m_tConnected;
OnRead();
}
//////////////////////////////////////////////////////////////////////
// CUploadTransferHTTP read handler
BOOL CUploadTransferHTTP::OnRead()
{
CUploadTransfer::OnRead();
switch ( m_nState )
{
case upsRequest:
case upsQueued:
if ( ! ReadRequest() ) return FALSE;
if ( m_nState != upsHeaders ) break;
case upsHeaders:
return ReadHeaders();
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// CUploadTransferHTTP read : request line
BOOL CUploadTransferHTTP::ReadRequest()
{
CString strLine;
if ( ! m_pInput->ReadLine( strLine ) ) return TRUE;
if ( strLine.GetLength() > 512 ) strLine = _T("#LINE_TOO_LONG#");
if ( m_nState == upsQueued && m_pQueue != NULL )
{
DWORD tLimit = Settings.Uploads.QueuePollMin;
if ( UploadQueues.GetPosition( this, FALSE ) <= 2 ) tLimit /= 2;
if ( GetTickCount() - m_tRequest < tLimit )
{
theApp.Message( MSG_ERROR, IDS_UPLOAD_BUSY_FAST, (LPCTSTR)m_sAddress );
Close();
return FALSE;
}
}
int nChar = strLine.Find( _T(" HTTP/") );
if ( strLine.GetLength() < 14 || nChar < 5 ||
( strLine.Left( 4 ) != _T("GET ") && strLine.Left( 5 ) != _T("HEAD ") ) )
{
theApp.Message( MSG_ERROR, IDS_UPLOAD_NOHTTP, (LPCTSTR)m_sAddress );
Close();
return FALSE;
}
ClearRequest();
m_bHead = ( strLine.Left( 5 ) == _T("HEAD ") );
m_bConnectHdr = FALSE;
m_bKeepAlive = TRUE;
m_bHostBrowse = FALSE;
m_bDeflate = FALSE;
m_bBackwards = FALSE;
m_bRange = FALSE;
m_bQueueMe = FALSE;
m_bMetadata = FALSE;
m_bTigerTree = FALSE;
m_sLocations.Empty();
m_sRanges.Empty();
CString strRequest = strLine.Mid( m_bHead ? 5 : 4, nChar - ( m_bHead ? 5 : 4 ) );
if ( strRequest.GetLength() > 5 && strRequest.Right( 1 ) == _T("/") )
{
strRequest = strRequest.Left( strRequest.GetLength() - 1 );
}
strRequest = URLDecode( strRequest );
if ( strRequest != m_sRequest )
{
if ( m_sRequest.Find( _T("/gnutella/tigertree/") ) < 0 &&
strRequest.Find( _T("/gnutella/tigertree/") ) < 0 &&
m_sRequest.Find( _T("/gnutella/thex/") ) < 0 &&
strRequest.Find( _T("/gnutella/thex/") ) < 0 &&
m_sRequest.Find( _T("/gnutella/metadata/") ) < 0 &&
strRequest.Find( _T("/gnutella/metadata/") ) < 0 )
{
UploadQueues.Dequeue( this );
}
m_sRequest = strRequest;
}
theApp.Message( MSG_DEBUG, _T("%s: UPLOAD PATH: %s"), (LPCTSTR)m_sAddress, (LPCTSTR)m_sRequest );
m_nState = upsHeaders;
m_tRequest = GetTickCount();
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// CUploadTransferHTTP read : headers
BOOL CUploadTransferHTTP::OnHeaderLine(CString& strHeader, CString& strValue)
{
theApp.Message( MSG_DEBUG, _T("%s: UPLOAD HEADER: %s: %s"), (LPCTSTR)m_sAddress, (LPCTSTR)strHeader, strValue );
if ( strHeader.CompareNoCase( _T("Connection") ) == 0 )
{
if ( strValue.CompareNoCase( _T("close") ) == 0 ) m_bKeepAlive = FALSE;
m_bConnectHdr = TRUE;
}
else if ( strHeader.CompareNoCase( _T("Accept") ) == 0 )
{
strValue.MakeLower();
if ( strValue.Find( _T("application/x-gnutella-packets") ) >= 0 ) m_bHostBrowse = 1;
if ( strValue.Find( _T("application/x-gnutella2") ) >= 0 ) m_bHostBrowse = 2;
if ( strValue.Find( _T("application/x-shareaza") ) >= 0 ) m_bHostBrowse = 2;
}
else if ( strHeader.CompareNoCase( _T("Accept-Encoding") ) == 0 )
{
if ( _tcsistr( strValue, _T("deflate") ) ) m_bDeflate = TRUE;
if ( Settings.Uploads.AllowBackwards && _tcsistr( strValue, _T("backwards") ) ) m_bBackwards = TRUE;
}
else if ( strHeader.CompareNoCase( _T("Range") ) == 0 )
{
QWORD nFrom = 0, nTo = 0;
if ( _stscanf( strValue, _T("bytes=%I64i-%I64i"), &nFrom, &nTo ) == 2 )
{
m_nOffset = nFrom;
m_nLength = nTo + 1 - nFrom;
m_bRange = TRUE;
}
else if ( _stscanf( strValue, _T("bytes=%I64i-"), &nFrom ) == 1 )
{
m_nOffset = nFrom;
m_nLength = SIZE_UNKNOWN;
m_bRange = TRUE;
}
}
else if ( strHeader.CompareNoCase( _T("X-Gnutella-Content-URN") ) == 0 ||
strHeader.CompareNoCase( _T("X-Content-URN") ) == 0 ||
strHeader.CompareNoCase( _T("Content-URN") ) == 0 )
{
HashesFromURN( strValue );
m_nGnutella |= 1;
}
else if ( strHeader.CompareNoCase( _T("X-Gnutella-Alternate-Location") ) == 0 ||
strHeader.CompareNoCase( _T("Alt-Location") ) == 0 ||
strHeader.CompareNoCase( _T("X-Alt") ) == 0 )
{
if ( Settings.Library.SourceMesh )
{
if ( strValue.Find( _T("Zhttp://") ) < 0 ) m_sLocations = strValue;
}
m_nGnutella |= 1;
}
else if ( strHeader.CompareNoCase( _T("X-Queue") ) == 0 )
{
m_bQueueMe = TRUE;
if ( strValue == _T("1.0") ) m_bQueueMe = (BOOL)2;
m_nGnutella |= 1;
}
else if ( strHeader.CompareNoCase( _T("X-Nick") ) == 0 ||
strHeader.CompareNoCase( _T("X-Name") ) == 0 ||
strHeader.CompareNoCase( _T("X-UserName") ) == 0 )
{
m_sNick = URLDecode( strValue );
}
else if ( strHeader.CompareNoCase( _T("X-Features") ) == 0 )
{
if ( _tcsistr( strValue, _T("g2/") ) != NULL ) m_nGnutella |= 2;
if ( _tcsistr( strValue, _T("gnet2/") ) != NULL ) m_nGnutella |= 2;
if ( _tcsistr( strValue, _T("gnutella2/") ) != NULL ) m_nGnutella |= 2;
if ( m_nGnutella == 0 ) m_nGnutella = 1;
}
return CUploadTransfer::OnHeaderLine( strHeader, strValue );
}
//////////////////////////////////////////////////////////////////////
// CUploadTransferHTTP process request
BOOL CUploadTransferHTTP::OnHeadersComplete()
{
if ( Uploads.EnforcePerHostLimit( this, TRUE ) ) return FALSE;
if ( _tcsistr( m_sUserAgent, _T("shareaza") ) != NULL )
{
// Assume certain capabilitites for various Shareaza versions
m_nGnutella |= 3;
if ( m_bQueueMe == (BOOL)2 ) m_nGnutella = 1; // GTK
if ( m_sUserAgent == _T("Shareaza 1.4.0.0") ) m_bQueueMe = TRUE;
}
else if ( _tcsistr( m_sUserAgent, _T("trustyfiles") ) != NULL ||
_tcsistr( m_sUserAgent, _T("gnucdna") ) != NULL ||
_tcsistr( m_sUserAgent, _T("adagio") ) != NULL )
{
// Assume Gnutella2 capability for certain user-agents
m_nGnutella |= 3;
}
if ( m_sRequest == _T("/") || StartsWith( m_sRequest, _T("/gnutella/browse/v1") ) )
{
// Requests for "/" or the browse path are handled the same way
if ( ( m_bHostBrowse == 1 && ! Settings.Community.ServeFiles ) ||
( m_bHostBrowse == 2 && ! Settings.Community.ServeProfile && ! Settings.Community.ServeFiles ) )
{
theApp.Message( MSG_ERROR, IDS_UPLOAD_BROWSE_DENIED, (LPCTSTR)m_sAddress );
m_bHostBrowse = FALSE;
}
if ( m_bHostBrowse )
{
RequestHostBrowse();
}
else
{
theApp.Message( MSG_DEFAULT, IDS_UPLOAD_ABOUT, (LPCTSTR)m_sAddress, (LPCTSTR)m_sUserAgent );
SendResponse( IDR_HTML_ABOUT );
}
return TRUE;
}
else if ( StartsWith( m_sRequest, _T("/remote") ) )
{
// A web client can start requesting remote pages on the same keep-alive
// connection after previously requesting other system objects
if ( Settings.Remote.Enable )
{
m_pInput->Prefix( "GET /remote/ HTTP/1.0\r\n\r\n" );
new CRemote( this );
Remove( FALSE );
return FALSE;
}
}
else if ( IsAgentBlocked() )
{
if ( m_sFileName.IsEmpty() ) m_sFileName = _T("file");
SendResponse( IDR_HTML_BROWSER );
theApp.Message( MSG_ERROR, IDS_UPLOAD_BROWSER, (LPCTSTR)m_sAddress, (LPCTSTR)m_sFileName );
if ( m_sUserAgent.Find( _T("Mozilla") ) >= 0 ) return TRUE;
Remove( FALSE );
return FALSE;
}
else if ( IsNetworkDisabled() )
{
SendResponse( IDR_HTML_DISABLED );
theApp.Message( MSG_ERROR, IDS_UPLOAD_DISABLED, (LPCTSTR)m_sAddress, (LPCTSTR)m_sUserAgent );
Remove( FALSE );
return FALSE;
}
else if ( StartsWith( m_sRequest, _T("/gnutella/metadata/v1?urn:") ) && Settings.Uploads.ShareMetadata )
{
LPCTSTR pszURN = (LPCTSTR)m_sRequest + 22;
CXMLElement* pMetadata = NULL;
if ( CLibraryFile* pShared = LibraryMaps.LookupFileByURN( pszURN, TRUE, TRUE, TRUE ) )
{
if ( pShared->m_pMetadata != NULL )
{
m_sFileName = pShared->m_sName;
pMetadata = pShared->m_pSchema->Instantiate( TRUE );
pMetadata->AddElement( pShared->m_pMetadata->Clone() );
}
Library.Unlock();
}
else if ( CDownload* pDownload = Downloads.FindByURN( pszURN ) )
{
if ( pDownload->m_pXML != NULL )
{
m_sFileName = pDownload->m_sRemoteName;
pMetadata = pDownload->m_pXML->Clone();
}
}
if ( pMetadata != NULL ) return RequestMetadata( pMetadata );
}
else if ( StartsWith( m_sRequest, _T("/gnutella/tigertree/v3?urn:") ) && Settings.Uploads.ShareTiger )
{
LPCTSTR pszURN = (LPCTSTR)m_sRequest + 23;
if ( CLibraryFile* pShared = LibraryMaps.LookupFileByURN( pszURN, TRUE, TRUE, TRUE ) )
{
CTigerTree* pTigerTree = pShared->GetTigerTree();
m_sFileName = pShared->m_sName;
Library.Unlock();
return RequestTigerTreeRaw( pTigerTree, TRUE );
}
else if ( CDownload* pDownload = Downloads.FindByURN( pszURN ) )
{
if ( pDownload->GetTigerTree() != NULL )
{
m_sFileName = pDownload->m_sRemoteName;
return RequestTigerTreeRaw( pDownload->GetTigerTree(), FALSE );
}
}
}
else if ( StartsWith( m_sRequest, _T("/gnutella/thex/v1?urn:") ) && Settings.Uploads.ShareTiger )
{
LPCTSTR pszURN = (LPCTSTR)m_sRequest + 18;
DWORD nDepth = 0;
if ( LPCTSTR pszDepth = _tcsistr( m_sRequest, _T("depth=") ) )
{
_stscanf( pszDepth + 6, _T("%i"), &nDepth );
}
BOOL bHashset = ( _tcsistr( m_sRequest, _T("ed2k=1") ) != NULL );
if ( CLibraryFile* pShared = LibraryMaps.LookupFileByURN( pszURN, TRUE, TRUE, TRUE ) )
{
CTigerTree* pTigerTree = pShared->GetTigerTree();
CED2K* pHashset = bHashset ? pShared->GetED2K() : NULL;
m_sFileName = pShared->m_sName;
m_nFileSize = pShared->GetSize();
Library.Unlock();
return RequestTigerTreeDIME( pTigerTree, nDepth, pHashset, TRUE );
}
else if ( CDownload* pDownload = Downloads.FindByURN( pszURN ) )
{
if ( pDownload->GetTigerTree() != NULL )
{
m_sFileName = pDownload->m_sRemoteName;
m_nFileSize = pDownload->m_nSize;
return RequestTigerTreeDIME( pDownload->GetTigerTree(), nDepth,
bHashset ? pDownload->GetHashset() : NULL, FALSE );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -