Esempio n. 1
0
void CWaveSession::PostSignOutRequest()
{
	if (m_lpRequest != NULL)
	{
		m_vOwnedRequests.push_back(m_lpRequest);
	}

	m_lpRequest = new CCurl(WAVE_URL_LOGOUT, m_lpTargetWindow);

	m_lpRequest->SetUserAgent(GetUserAgent());
	m_lpRequest->SetTimeout(WEB_TIMEOUT_SHORT);
	m_lpRequest->SetIgnoreSSLErrors(TRUE);
	m_lpRequest->SetCookies(m_lpCookies);
	m_lpRequest->SetAutoRedirect(TRUE);

	m_nRequesting = WSR_SIGN_OUT;

	CNotifierApp::Instance()->QueueRequest(m_lpRequest);

	if (m_lpCookies != NULL)
	{
		delete m_lpCookies;

		m_lpCookies = NULL;
	}
}
Esempio n. 2
0
void CWaveSession::PostRequests()
{
	// This method is responsible for deleting the requests because requests
	// may later be stored. Responses can be linked to a request and when this
	// becomes necessary, this function will take the requests for later retrieval.
	// Now, we just delete them.

	wstringstream szPostData;

	ASSERT(m_vRequestQueue.size() > 0);

	szPostData << L"count=" << m_vRequestQueue.size();

	INT nOffset = 0;

	for (TWaveRequestVectorConstIter iter = m_vRequestQueue.begin(); iter != m_vRequestQueue.end(); iter++)
	{
		szPostData << L"&req" << nOffset << L"_key=" << UrlEncode(SerializeRequest(*iter));

		nOffset++;
	}

	// Post the JSON to the channel.

	wstring szUrl = Format(WAVE_URL_CHANNEL_POST, m_szStickySessionID.c_str(), m_szSID.c_str(), m_nRID++, BuildHash().c_str());

	if (m_lpPostRequest != NULL)
	{
		m_vOwnedRequests.push_back(m_lpPostRequest);
	}

	m_lpPostRequest = new CCurl(szUrl, m_lpTargetWindow);

	m_lpPostRequest->SetUserAgent(GetUserAgent());
	m_lpPostRequest->SetTimeout(WEB_TIMEOUT_SHORT);
	m_lpPostRequest->SetIgnoreSSLErrors(TRUE);
	m_lpPostRequest->SetCookies(GetCookies());

	m_lpPostRequest->SetUrlEncodedPostData(szPostData.str());

	CNotifierApp::Instance()->QueueRequest(m_lpPostRequest);

	for (TWaveRequestVectorIter iter1 = m_vRequestQueue.begin(); iter1 != m_vRequestQueue.end(); iter1++)
	{
		ASSERT(*iter1 != NULL);

		(*iter1)->RequestCompleted();

		delete *iter1;
	}

	m_vRequestQueue.clear();
}
Esempio n. 3
0
void CWaveSession::PostSessionDetailsRequest()
{
	if (m_lpRequest != NULL)
	{
		m_vOwnedRequests.push_back(m_lpRequest);
	}

	m_lpRequest = new CCurl(WAVE_URL_WAVES, m_lpTargetWindow);

	m_lpRequest->SetUserAgent(GetUserAgent());
	m_lpRequest->SetTimeout(WEB_TIMEOUT_SHORT);
	m_lpRequest->SetIgnoreSSLErrors(TRUE);
	m_lpRequest->SetCookies(m_lpCookies);
	m_lpRequest->SetReader(new CCurlUTF8StringReader());

	m_nRequesting = WSR_SESSION_DETAILS;

	CNotifierApp::Instance()->QueueRequest(m_lpRequest);
}
Esempio n. 4
0
void CWaveSession::PostChannelRequest()
{
	if (m_lpRequest != NULL)
	{
		m_vOwnedRequests.push_back(m_lpChannelRequest);
	}

	m_lpChannelRequest = new CCurl(
		Format(WAVE_URL_CHANNEL, m_szStickySessionID.c_str(), m_szSID.c_str(), m_nAID, BuildHash().c_str()),
		m_lpTargetWindow
	);

	m_lpChannelRequest->SetUserAgent(GetUserAgent());
	m_lpChannelRequest->SetTimeout(WEB_TIMEOUT_CHANNEL);
	m_lpChannelRequest->SetIgnoreSSLErrors(TRUE);
	m_lpChannelRequest->SetCookies(GetCookies());
	m_lpChannelRequest->SetReader(new CWaveReader(this));

	CNotifierApp::Instance()->QueueRequest(m_lpChannelRequest);
}
Esempio n. 5
0
void CWaveSession::PostAuthCookieRequest()
{
	if (m_lpRequest != NULL)
	{
		m_vOwnedRequests.push_back(m_lpRequest);
	}

	m_lpRequest = new CCurl(
		Format(WAVE_URL_AUTH, UrlEncode(m_szAuthKey).c_str()),
		m_lpTargetWindow
	);

	m_lpRequest->SetUserAgent(GetUserAgent());
	m_lpRequest->SetTimeout(WEB_TIMEOUT_SHORT);
	m_lpRequest->SetIgnoreSSLErrors(TRUE);

	m_nRequesting = WSR_COOKIE;

	CNotifierApp::Instance()->QueueRequest(m_lpRequest);
}
Esempio n. 6
0
BOOL CWaveSession::Reconnect()
{
	if (m_szUsername.empty() || m_szPassword.empty())
	{
		return FALSE;
	}

	if (m_lpRequest != NULL)
	{
		m_vOwnedRequests.push_back(m_lpRequest);
	}

	m_lpRequest = new CCurl(WAVE_URL_CLIENTLOGIN, m_lpTargetWindow);

	m_lpRequest->SetUserAgent(GetUserAgent());
	m_lpRequest->SetTimeout(WEB_TIMEOUT_SHORT);
	m_lpRequest->SetIgnoreSSLErrors(TRUE);
	m_lpRequest->SetReader(new CCurlUTF8StringReader());

	wstringstream szPostData;

	szPostData
		<< L"accountType=GOOGLE&Email="
		<< UrlEncode(m_szUsername)
		<< L"&Passwd="
		<< UrlEncode(m_szPassword)
		<< L"&service=wave&source=net.sf.wave-notify";

	m_lpRequest->SetUrlEncodedPostData(szPostData.str());

	m_nRequesting = WSR_AUTH_KEY;

	CNotifierApp::Instance()->QueueRequest(m_lpRequest);

	return TRUE;
}
Esempio n. 7
0
void CWaveSession::PostSIDRequest()
{
	if (m_lpRequest != NULL)
	{
		m_vOwnedRequests.push_back(m_lpRequest);
	}

	m_lpRequest = new CCurl(
		Format(WAVE_URL_SESSIONID, m_szStickySessionID.c_str(), m_nRID++, BuildHash().c_str()),
		m_lpTargetWindow
	);

	m_lpRequest->SetUserAgent(GetUserAgent());
	m_lpRequest->SetTimeout(WEB_TIMEOUT_SHORT);
	m_lpRequest->SetIgnoreSSLErrors(TRUE);
	m_lpRequest->SetCookies(GetCookies());
	m_lpRequest->SetReader(new CCurlUTF8StringReader());

	m_lpRequest->SetUrlEncodedPostData(L"count=0");

	m_nRequesting = WSR_SID;

	CNotifierApp::Instance()->QueueRequest(m_lpRequest);
}
Esempio n. 8
0
namespace Downloads {

const String Constants::TAG("DownloadManager");

const String Constants::RETRY_AFTER_X_REDIRECT_COUNT("method");

const String Constants::OTA_UPDATE("otaupdate");

const String Constants::NO_SYSTEM_FILES("no_system");

const String Constants::ETAG("etag");

const String Constants::UID("uid");

const String Constants::ACTION_RETRY("android.intent.action.DOWNLOAD_WAKEUP");

const String Constants::ACTION_OPEN("android.intent.action.DOWNLOAD_OPEN");

const String Constants::ACTION_LIST("android.intent.action.DOWNLOAD_LIST");

const String Constants::ACTION_HIDE("android.intent.action.DOWNLOAD_HIDE");

const String Constants::DEFAULT_DL_FILENAME("downloadfile");

const String Constants::DEFAULT_DL_HTML_EXTENSION(".html");

const String Constants::DEFAULT_DL_TEXT_EXTENSION(".txt");

const String Constants::DEFAULT_DL_BINARY_EXTENSION(".bin");

const String Constants::PROVIDER_PACKAGE_NAME("com.android.providers.downloads");

const String Constants::FILENAME_SEQUENCE_SEPARATOR("-");

const String Constants::RECOVERY_DIRECTORY("recovery");

static String GetUserAgent()
{
    StringBuilder builder;

    Boolean validRelease = !TextUtils::IsEmpty(Build::VERSION::RELEASE);
    Boolean validId = !TextUtils::IsEmpty(Build::ID);
    Boolean includeModel = Build::VERSION::CODENAME.Equals("REL")
            && !TextUtils::IsEmpty(Build::MODEL);

    builder.Append("AndroidDownloadManager");
    if (validRelease) {
        builder.Append("/");
        builder.Append(Build::VERSION::RELEASE);
    }
    builder.Append(" (Linux; U; Android");
    if (validRelease) {
        builder.Append(" ");
        builder.Append(Build::VERSION::RELEASE);
    }
    if (includeModel || validId) {
        builder.Append(";");
        if (includeModel) {
            builder.Append(" ");
            builder.Append(Build::MODEL);
        }
        if (validId) {
            builder.Append(" Build/");
            builder.Append(Build::ID);
        }
    }
    builder.Append(")");

    String str = builder.ToString();
    return str;
}

const String Constants::DEFAULT_USER_AGENT = GetUserAgent();

const String Constants::MIMETYPE_APK("application/vnd.android.package");

const Int32 Constants::BUFFER_SIZE = 8192;

const Int32 Constants::MIN_PROGRESS_STEP = 65536;

const Int64 Constants::MIN_PROGRESS_TIME = 2000;

const Int32 Constants::MAX_RETRIES = 5;

const Int32 Constants::MIN_RETRY_AFTER = 30; // 30s

const Int32 Constants::MAX_RETRY_AFTER = 24 * 60 * 60; // 24h

const Int32 Constants::MAX_REDIRECTS = 5; // can't be more than 7.

const Int32 Constants::RETRY_FIRST_DELAY = 30;

const Boolean Constants::LOGX = FALSE;

const Boolean Constants::LOCAL_LOGV = FALSE;
const Boolean Constants::LOGV = LOCAL_LOGV;// && Log.isLoggable(TAG, Log.VERBOSE);

const Boolean Constants::LOCAL_LOGVV = FALSE;
const Boolean Constants::LOGVV = LOCAL_LOGVV && LOGV;

const String Constants::STORAGE_AUTHORITY("com.android.providers.downloads.documents");
const String Constants::STORAGE_ROOT_ID("downloads");

const String Constants::DIRECTORY_CACHE_RUNNING("partial_downloads");

} // namespace Downloads
Esempio n. 9
0
int CloHttpCurl::TransferProcPOSTFormData()
{
    THttpMemoryBuffer headerBuffer;
    THttpMemoryBuffer bodyBuffer;

    SetState( E_HTTPSTATUS_START );

    // 启动curl
    m_pAssoc->m_curlHandle = curl_easy_init();

    // 设置访问链接
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_VERBOSE, 1L);


    // 设置附加的Header
    struct curl_slist *chunk = NULL;
    if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 )
    {
        BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead);
        curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk);
    }

    // 添加POST文件信息
    struct curl_httppost *post=NULL;
    struct curl_httppost *last=NULL;

    // 设置Post数据
    if( m_pAssoc->m_pTransferParam->pData )
    {
        if( m_pAssoc->m_pTransferParam->bFormData )
        {   // 如果是表单,不要定义长度
            curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDS, m_pAssoc->m_pTransferParam->pData);
        }
        else
        {
            curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDS, m_pAssoc->m_pTransferParam->pData);
            curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDSIZE, m_pAssoc->m_pTransferParam->uDataLength);
        }
    }

    //
    if( m_pAssoc->m_pTransferParam->form.fformadd_cb )
    {
        m_pAssoc->m_pTransferParam->form.fformadd_cb( (void**)&post, (void**)&last , (fun_formadd_write_callback)curl_formadd , m_pAssoc->m_pTransferParam->form.pformdata );

        curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPPOST, post);
    }

    // 设置代理信息
    UpdateProxy(m_pAssoc->m_pTransferParam->cURL);

    // 设置传输状态回调
    SetTransferCallback((void *)&headerBuffer, (void *)&bodyBuffer);

    // 设置访问Agent
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, GetUserAgent() );
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1);		// 自动重定向
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1);

    // 启动获取
    CURLcode code = curl_easy_perform(m_pAssoc->m_curlHandle);

    GET_HTTP_CODE();

    ClearBuffer();

    // 结果处理
    if( code == CURLE_OK )
    {
        // 保存任务结果数据
        SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size);
        COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv);

        SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_BodyRecv,bodyBuffer.size,bodyBuffer.size);
        COPY_MEMORYBUFFER(&bodyBuffer,&m_pAssoc->m_BodyRecv);
    }

    // 清理缓存
    // 这里有点特别,因为是通过 BufferRealloc() 分配的
    SAFE_FREE_BUFFER( headerBuffer.buffer );
    SAFE_FREE_BUFFER( bodyBuffer.buffer );

    // 清理表单信息
    if( post != NULL )
        curl_formfree(post);

    // 清理附加的Header参数信息
    if( chunk != NULL )
    {
        curl_slist_free_all(chunk);
        chunk = 0;
    }

    // 释放curl
    curl_easy_cleanup(m_pAssoc->m_curlHandle);
    m_pAssoc->m_curlHandle = NULL;

    m_pAssoc->SetErrCode( code );
    TaskCode();

    // 任务结束
    SetState( E_HTTPSTATUS_STOP );

    return (code != CURLE_OK)?-1:0;
}
Esempio n. 10
0
int CloHttpCurl::TransferProcDOWNLOAD_TOBUFFER()
{
    THttpMemoryBuffer headerBuffer;

    SetState( E_HTTPSTATUS_START );

    // 启动curl
    m_pAssoc->m_curlHandle = curl_easy_init();

    // 设置访问链接
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL);


    // 设置附加的Header
    struct curl_slist *chunk = NULL;
    if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 )
    {
        BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead);
        curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk);
    }

    // 设置代理信息
    UpdateProxy(m_pAssoc->m_pTransferParam->cURL);

    // 设置访问Agent
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, GetUserAgent() );
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1);

    // 设置读取,保存数据的函数
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEFUNCTION, ToBufferCallback);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEDATA, this);

    // 先设置获取Header的函数
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERFUNCTION, ToHeaderCallback);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERDATA, (void *)&headerBuffer);

    // 设置progress回写
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOPROGRESS, 0);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSFUNCTION, ProgressCallback);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSDATA, this);

    // 开始获取
    CURLcode code = curl_easy_perform(m_pAssoc->m_curlHandle);

    GET_HTTP_CODE();

    ClearBuffer();
    // 结果处理
    if( code == CURLE_OK )
    {
        // 保存任务结果数据
        SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size);
        COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv);
    }

    // 清理缓存
    // 这里有点特别,因为是通过 BufferRealloc() 分配的
    SAFE_FREE_BUFFER( headerBuffer.buffer );

    // 清理附加的Header参数信息
    if( chunk != NULL ) {
        curl_slist_free_all(chunk);
        chunk = 0;
    }
    // 释放curl
    curl_easy_cleanup(m_pAssoc->m_curlHandle);
    m_pAssoc->m_curlHandle = NULL;

    m_pAssoc->SetErrCode( code );
    TaskCode();

    // 任务结束
    SetState( E_HTTPSTATUS_STOP );

    return (code != CURLE_OK)?-1:0;
}
Esempio n. 11
0
int CloHttpCurl::TransferProcDOWNLOAD()
{
    if( m_pAssoc->m_pTransferParam->bToBuffer )		// 下载到内存
    {
        return TransferProcDOWNLOAD_TOBUFFER();
    }

    ///////////////////////////////////////////////////////////////////
    // 下载到文件
    SetState( E_HTTPSTATUS_START );

    THttpMemoryBuffer headerBuffer;

    // 打开本地文件
    THttpFileBuffer fileBuffer;
    memset(&fileBuffer, 0, sizeof(THttpFileBuffer));

    // 关闭文件
#define FILE_CLOSE(p) if(p) { fclose(p);(p) = NULL;}

    // 看看文件夹是否有效
#ifdef _UNICODE
    if( !locom::CloFile::IsDirectoryExists( CA2T(m_pAssoc->m_pTransferParam->cSavePath) ) )
        locom::CloFile::CreateDirectory( CA2T(m_pAssoc->m_pTransferParam->cSavePath) );

#else
    if (!locom::CloFile::IsDirectoryExists(m_pAssoc->m_pTransferParam->cSavePath) )
        locom::CloFile::CreateDirectory(m_pAssoc->m_pTransferParam->cSavePath);

#endif

    // 启动curl
    m_pAssoc->m_curlHandle = curl_easy_init();

    // 设置访问链接
    CURLcode code = curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL);

    // 设置附加的Header
    struct curl_slist *chunk = NULL;
_recvHeader:

    int state = GetState();
    if( state != E_HTTPSTATUS_START ) // 防止 死loop ,
    {
        // 有错误发生

        FILE_CLOSE( fileBuffer.fp );

        // 清理缓存
        // 这里有点特别,因为是通过 BufferRealloc() 分配的
        SAFE_FREE_BUFFER( headerBuffer.buffer );
        // 清理附加的Header参数信息
        if( chunk != NULL ) {
            curl_slist_free_all(chunk);
            chunk = 0;
        }
        // 释放curl
        curl_easy_cleanup(m_pAssoc->m_curlHandle);
        m_pAssoc->m_curlHandle = NULL;

        m_pAssoc->SetErrCode(code);
        TaskCode();

        // 任务结束
        SetState( E_HTTPSTATUS_STOP );
        return -1;
    }

    if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 )
    {
        BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead);
        curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk);
    }

    // 设置代理信息
    UpdateProxy(m_pAssoc->m_pTransferParam->cURL);

    // 设置访问Agent
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, GetUserAgent() );
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1);

    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOBODY, 1);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADER, 1);

    // 先设置获取Header的函数
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERFUNCTION, ToHeaderCallback);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERDATA, (void *)&headerBuffer);

    curl_easy_perform(m_pAssoc->m_curlHandle);

    // 获取http code
    m_pAssoc->m_lhttpCode = 0;
    code = curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_HTTP_CODE, &m_pAssoc->m_lhttpCode);

    // 成功
    if( m_pAssoc->m_lhttpCode == 200  && CURLE_OK == code )
    {
        double contentLength = 0;
        char * url = NULL;

        curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentLength);

        curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_EFFECTIVE_URL, (void *)&url);

        // 如果没有指定文件名
        if( !m_pAssoc->m_pTransferParam->bRename || strlen(m_pAssoc->m_pTransferParam->cNewFileName) == 0 )
        {
            // 获取有效的文件名
            std::string filename = GetUrlFileName(url);
            if( filename.length() > 0 )
            {
                strcpy_s(m_pAssoc->m_pTransferParam->cNewFileName, 1024, filename.c_str());
            }
        }

        if( strlen(m_pAssoc->m_pTransferParam->cNewFileName) > 0 )
        {
            // 以新建方式,创建本地文件
            char cNewFile[ConstLocalFileLength];
            strcpy(cNewFile , m_pAssoc->m_pTransferParam->cSavePath );
            strcat( cNewFile  , "\\");
            strcat( cNewFile  , m_pAssoc->m_pTransferParam->cNewFileName );

            FILE * fp = fopen(cNewFile , "w+b");
            if( fp == 0 )
            {
                // 清理缓存
                // 这里有点特别,因为是通过 BufferRealloc() 分配的
                SAFE_FREE_BUFFER(headerBuffer.buffer);
                // 清理附加的Header参数信息
                if( chunk != NULL ) {
                    curl_slist_free_all(chunk);
                    chunk = 0;
                }
                // 释放curl
                curl_easy_cleanup(m_pAssoc->m_curlHandle);
                m_pAssoc->m_curlHandle = NULL;

                // 创建本地文件失败
                m_pAssoc->SetErrCode( HTTPE_CREATE_FILE );
                TaskCode();

                // 任务结束
                SetState( E_HTTPSTATUS_STOP );

                return -1;
            }

            // 准备写文件了
            fileBuffer.fp = fp;
        }
        else
        {
            //
            FILE_CLOSE( fileBuffer.fp );

            // 清理缓存
            // 这里有点特别,因为是通过 BufferRealloc() 分配的
            SAFE_FREE_BUFFER( headerBuffer.buffer );
            // 清理附加的Header参数信息
            if( chunk != NULL ) {
                curl_slist_free_all(chunk);
                chunk = 0;
            }
            // 释放curl
            curl_easy_cleanup(m_pAssoc->m_curlHandle);
            m_pAssoc->m_curlHandle = NULL;

            m_pAssoc->SetErrCode( HTTPE_FILE_NAME_NULL );
            TaskCode();

            // 任务结束
            SetState( E_HTTPSTATUS_STOP );

            return -1;
        }

        // 先保存一下Header
        SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size);
        COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv);

        goto _recvBody;
    }
    else if( m_pAssoc->m_lhttpCode == 302  && CURLE_OK == code )
    {   // 重定向

        char * pUrl = NULL;
        curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_REDIRECT_URL, (void *)&pUrl);

        curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, pUrl);
        // 清理附加的Header参数信息
        if( chunk != NULL )
        {
            curl_slist_free_all(chunk);
            chunk = 0;
        }
        goto _recvHeader;
    }
    else
    {   // 有错误发生

        FILE_CLOSE( fileBuffer.fp );

        // 清理缓存
        // 这里有点特别,因为是通过 BufferRealloc() 分配的
        SAFE_FREE_BUFFER( headerBuffer.buffer );
        // 清理附加的Header参数信息
        if( chunk != NULL )
            curl_slist_free_all(chunk);
        // 释放curl
        curl_easy_cleanup(m_pAssoc->m_curlHandle);
        m_pAssoc->m_curlHandle = NULL;

        m_pAssoc->SetErrCode(code);
        TaskCode();

        // 任务结束
        SetState( E_HTTPSTATUS_STOP );

        return -1;
    }

_recvBody:
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOBODY, 0);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADER, 0);

    // 设置读取,保存数据的函数
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEFUNCTION, ToFileCallback);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEDATA, (void *)&fileBuffer);

    // 设置progress回写
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOPROGRESS, 0);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSFUNCTION, ProgressCallback);
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSDATA, this);

    // 开始获取
    code = curl_easy_perform(m_pAssoc->m_curlHandle);


    FILE_CLOSE( fileBuffer.fp );

    // 清理缓存
    // 这里有点特别,因为是通过 BufferRealloc() 分配的
    SAFE_FREE_BUFFER( headerBuffer.buffer );
    // 清理附加的Header参数信息
    if( chunk != NULL )
        curl_slist_free_all(chunk);
    // 释放curl
    curl_easy_cleanup(m_pAssoc->m_curlHandle);
    m_pAssoc->m_curlHandle = NULL;

    m_pAssoc->SetErrCode( code );
    TaskCode();

    // 任务结束
    SetState( E_HTTPSTATUS_STOP );

    return (code != CURLE_OK)?-1:0;
}
Esempio n. 12
0
int CloHttpCurl::TransferProcGET()
{
    THttpMemoryBuffer headerBuffer;
    THttpMemoryBuffer bodyBuffer;

    SetState( E_HTTPSTATUS_START );

    // 启动curl
    m_pAssoc->m_curlHandle = curl_easy_init();

    // 设置访问链接
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL);

    // 设置代理信息
    UpdateProxy(m_pAssoc->m_pTransferParam->cURL);

    // 设置附加的Header
    struct curl_slist *chunk = NULL;
    if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 )
    {
        BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead);
        curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk);
    }

    // 设置传输状态回调
    SetTransferCallback((void *)&headerBuffer, (void *)&bodyBuffer);

    // 设置访问Agent
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, GetUserAgent() );
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1);		// 自动重定向
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1);


    // 启动获取
    CURLcode code = curl_easy_perform(m_pAssoc->m_curlHandle);

    GET_HTTP_CODE();

    ClearBuffer();

    if( code == CURLE_OK )
    {
        // 保存任务结果数据

        // modified by loach 2009-07-27
        SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size);
        COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv);

        SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_BodyRecv,bodyBuffer.size,bodyBuffer.size);
        COPY_MEMORYBUFFER(&bodyBuffer,&m_pAssoc->m_BodyRecv);
    }

    // 这里有点特别,因为是通过 BufferRealloc() 分配的
    // 清理缓存
    SAFE_FREE_BUFFER(headerBuffer.buffer);
    SAFE_FREE_BUFFER(bodyBuffer.buffer);
    // 清理附加的Header参数信息
    if( chunk != NULL )
        curl_slist_free_all(chunk);
    // 释放curl
    curl_easy_cleanup(m_pAssoc->m_curlHandle);
    m_pAssoc->m_curlHandle = NULL;

    m_pAssoc->SetErrCode(code);
    TaskCode();

    // 任务结束
    SetState( E_HTTPSTATUS_STOP );

    return (code != 0 )?-1:0;
}
Esempio n. 13
0
int CloHttpCurl::TransferProcDELETE()
{
    THttpMemoryBuffer headerBuffer;
    THttpMemoryBuffer bodyBuffer;

    SetState( E_HTTPSTATUS_START );

    // 启动curl
    m_pAssoc->m_curlHandle = curl_easy_init();

    // 设置访问链接
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL);

    // 可能需要POST数据
    if( m_pAssoc->m_pTransferParam->pData != NULL && m_pAssoc->m_pTransferParam->uDataLength )
    {
        curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDS, m_pAssoc->m_pTransferParam->pData);
        curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDSIZE, m_pAssoc->m_pTransferParam->uDataLength);
    }

    // 设置附加动作
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_CUSTOMREQUEST, "DELETE");

    // 设置代理信息
    UpdateProxy(m_pAssoc->m_pTransferParam->cURL);

    // 设置附加的Header
    struct curl_slist *chunk = NULL;
    if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 )
    {
        BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead);
        curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk);
    }

    // 设置传输状态回调
    SetTransferCallback(&headerBuffer, &bodyBuffer);

    // 设置访问Agent
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, GetUserAgent() );
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1);		// 自动重定向
    curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1);


    // 启动获取
    CURLcode code = curl_easy_perform(m_pAssoc->m_curlHandle);

    GET_HTTP_CODE();

    ClearBuffer();
    // 结果处理
    if( code == CURLE_OK )
    {
        // 保存任务结果数据
        SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size);
        COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv);

        SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_BodyRecv,bodyBuffer.size,bodyBuffer.size);
        COPY_MEMORYBUFFER(&bodyBuffer,&m_pAssoc->m_BodyRecv);
    }

    // 清理缓存
    // 这里有点特别,因为是通过 BufferRealloc() 分配的
    SAFE_FREE_BUFFER( headerBuffer.buffer );
    SAFE_FREE_BUFFER( bodyBuffer.buffer );

    // 清理附加的Header参数信息
    if( chunk != NULL )
    {
        curl_slist_free_all(chunk);
        chunk = 0;
    }

    // 释放curl
    curl_easy_cleanup(m_pAssoc->m_curlHandle);
    m_pAssoc->m_curlHandle = NULL;

    m_pAssoc->SetErrCode( code );
    TaskCode();

    // 任务结束
    SetState( E_HTTPSTATUS_STOP );

    return (code != CURLE_OK)?-1:0;
}