示例#1
0
void CHTTPClient::OnConnect ( void* pSocketPtr, void* pClassPtr )
{
    if ( pSocketPtr != NULL )
    {
        CTCPClientSocket* pSocket = reinterpret_cast < CTCPClientSocket* > ( pSocketPtr );
        CHTTPClient* pClass = reinterpret_cast < CHTTPClient * > ( pClassPtr );

        pClass->m_strStatus = "connected to server";

        // Throw a HTTP request together
        SString strBuffer;
        if ( pClass->m_usPort != 80 ) // when port isn't 80, the host: part should specify it (otherwise it MUST not)
            strBuffer.Format ( "GET %s HTTP/1.0\r\nHost: %s:%d\r\nUser-Agent: MTASA_10\r\n\r\n", pClass->m_szPath, pClass->m_szHost, pClass->m_usPort );
        else
            strBuffer.Format ( "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: MTASA_10\r\n\r\n", pClass->m_szPath, pClass->m_szHost );
        size_t sizeRequest = strBuffer.length ();

        // Write it to the TCP stream
        pSocket->WriteBuffer ( strBuffer, sizeRequest );

        OnRead ( pSocketPtr, pClassPtr );
    }
}
示例#2
0
bool CHTTPClient::Get ( const char* szURL, const char* szDestinationFilename )
{
    // Parse the URL into protocol, host, port and path
    char szProtocol [64];
    char szHost [256];
    unsigned short usPort = 0;
    char szPath [1024];

    if ( !ParseURL ( szURL, szProtocol, 64, szHost, 256, usPort, szPath, 1024 ) )
    {
        strcpy ( m_szLastError, "Invalid URL" );
        return false;
    }

    // Default whatever optional values we got to their defaults if they weren't specified
    if ( usPort == 0 )
    {
        usPort = 80;
    }

    // If the protocol is empty, set it to http
    if ( szProtocol [0] == 0 )
    {
        strcpy ( szProtocol, "http" );
    }
    else if ( stricmp ( szProtocol, "http" ) != 0 )
    {
        strcpy ( m_szLastError, "Unsupported protocol" );
        return false;
    }

    // Empty hostname?
    if ( szHost [0] == 0 )
    {
        strcpy ( m_szLastError, "No hostname within the URL" );
        return false;
    }

    // Create a socket
    CTCPClientSocket* pSocket = m_TCP->CreateClient ();
    if ( !pSocket )
    {
        strcpy ( m_szLastError, m_TCP->GetLastError () );
        return false;
    }

    // Connect to the server
    if ( !pSocket->Connect ( szHost, usPort ) )
    {
        strcpy ( m_szLastError, pSocket->GetLastError () );
        delete pSocket;
        return false;
    }

    // Throw a HTTP request together
    char szBuffer [2048];
    if ( usPort != 80 ) // when port isn't 80, the host: part should specify it (otherwise it MUST not)
        snprintf ( szBuffer, 2048, "GET %s HTTP/1.0\r\nHost: %s:%d\r\nUser-Agent: MTASA_ERRORTOOL\r\n\r\n", szPath, szHost, usPort );
    else
        snprintf ( szBuffer, 2048, "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: MTASA_ERRORTOOL\r\n\r\n", szPath, szHost );
    szBuffer [2047] = 0;
    size_t sizeRequest = strlen ( szBuffer );

    // Write it to the TCP stream
    if ( pSocket->WriteBuffer ( szBuffer, sizeRequest ) < (int) sizeRequest )
    {
        strcpy ( m_szLastError, "Error writing to socket" );
        delete pSocket;
        return false;
    }

    // Read out the reply header
    char szReplyHeaderBuffer [2048];
    unsigned int uiHeaderSize;
    if ( !( uiHeaderSize = ReadHeader ( pSocket, szReplyHeaderBuffer, 2048 ) ) )
    {
        //strcpy ( m_szLastError, "Failed reading out response" );
        delete pSocket;
        //return false;
        return true;
    }

    // Parse the reply
    CHTTPResponse Response;
   /* if ( !Response.Parse ( szReplyHeaderBuffer, uiHeaderSize ) )
    {
        //strcpy ( m_szLastError, "Parsing response failed" );

        pSocket->Disconnect ();
        delete pSocket;
        //return false;
        return true;
    }*/

    // Errocode 200? (success)
    if ( Response.GetErrorCode () != 200 )
    {
        snprintf ( m_szLastError, sizeof ( m_szLastError ), "%u - %s", Response.GetErrorCode (), Response.GetErrorDescription () );

        pSocket->Disconnect ();
        delete pSocket;
        return false;
    }

    bool bWriteSuccess = false;
    if ( WriteSocketToFile(pSocket, szDestinationFilename) )
        bWriteSuccess = true;

    // Disconnect and delete the socket
    pSocket->Disconnect ();
    delete pSocket;

    return bWriteSuccess;
}