Example #1
0
void EventSource::didReceiveResponse(NetResponse& response, const Hashtable<String,String>* headers)
{
    LOG(TRACE) + "didReceiveResponse";

    ASSERT(m_state==CONNECTING);

    int statusCode = response.getRespCode();
    bool mimeTypeIsValid = headers->get("content-type") == "text/event-stream";
    bool responseIsValid = statusCode == 200 && mimeTypeIsValid;
    if (responseIsValid) {
        const String& charset = headers->get("content-encoding");
        // If we have a charset, the only allowed value is UTF-8 (case-insensitive).
        responseIsValid = charset.empty() || (strcasecmp(charset.c_str(), "UTF-8")==0);
        if (!responseIsValid) {
          LOG(ERROR) + "EventSource's response has a charset (\"" + charset + "\") that is not UTF-8. Aborting the connection.";
        }
    } else {
        // To keep the signal-to-noise ratio low, we only log 200-response with an invalid MIME type.
        if (statusCode == 200 && !mimeTypeIsValid) {
          LOG(ERROR) + "EventSource's response has a MIME type (\"" + headers->get("content-type") + "\") that is not \"text/event-stream\". Aborting the connection.";
        }
    }

    if (responseIsValid) {
        m_state = OPEN;
        m_pReceiver->onOpen();
    } else {
        close();
        m_pReceiver->onError("");
    }
}
Example #2
0
boolean CRhodesApp::sendLog() 
{
    String strDevicePin = rho::sync::CClientRegister::getInstance() ? rho::sync::CClientRegister::getInstance()->getDevicePin() : "";
	String strClientID = rho::sync::CSyncThread::getSyncEngine().readClientID();

    String strLogUrl = RHOCONF().getPath("logserver");
    if ( strLogUrl.length() == 0 )
        strLogUrl = RHOCONF().getPath("syncserver");

	String strQuery = strLogUrl + "client_log?" +
	    "client_id=" + strClientID + "&device_pin=" + strDevicePin + "&log_name=" + RHOCONF().getString("logname");

    net::CMultipartItem oItem;
    oItem.m_strFilePath = LOGCONF().getLogFilePath();
    oItem.m_strContentType = "application/octet-stream";

    boolean bOldSaveToFile = LOGCONF().isLogToFile();
    LOGCONF().setLogToFile(false);
    NetRequest oNetRequest;
    oNetRequest.setSslVerifyPeer(false);

    NetResponse resp = getNetRequest(&oNetRequest).pushMultipartData( strQuery, oItem, &(rho::sync::CSyncThread::getSyncEngine()), null );
    LOGCONF().setLogToFile(bOldSaveToFile);

    if ( !resp.isOK() )
    {
        LOG(ERROR) + "send_log failed : network error - " + resp.getRespCode() + "; Body - " + resp.getCharData();
        return false;
    }

    return true;
}
boolean CClientRegister::doUnregister(CSyncEngine& oSync)
{
    String session = m_unregisterSession;
    if ( session.length() == 0 )
    {
        m_unregisterSession = "";
        m_unregisterClientID = "";

        LOG(INFO)+"Session is empty, don't need to unregister";
        return true;
    }    

    m_nPollInterval = POLL_INTERVAL_SECONDS;

	String client_id = m_unregisterClientID;
	if ( client_id.length() == 0 )
	{
        m_unregisterSession = "";
        m_unregisterClientID = "";

        LOG(WARNING)+"Sync client_id is empty, don't need to unregister";
		return true;
	}
        
    String strBody = getUnregisterBody(client_id);    
    LOG(INFO)+"Unregistered client with body = " + strBody;
    
    class UnregisterSession : public net::IRhoSession {
        String m_session;
        String m_contentType;
    public:
        UnregisterSession(const String& session, const String& contentType) : m_session(session), m_contentType(contentType) {}
      	virtual void logout() {}
        virtual const String& getSession() {
            return m_session;
        }
        virtual const String& getContentType() {
            return m_contentType;
        }
    };
    
    UnregisterSession unregSession(session, oSync.getProtocol().getContentType() );
    
    NetResponse resp = getNet().pushData( oSync.getProtocol().getClientRegisterUrl(client_id), strBody, &unregSession );
	if( resp.isOK() )
    {
        m_unregisterSession = "";
        m_unregisterClientID = "";

        CDBAdapter::getUserDB().executeSQL("UPDATE client_info SET token_sent=?", 0 );
		LOG(INFO)+"Unregistered client sucessfully...";
        
        return true;
        
    }
    
	LOG(WARNING)+"Network error: "+ resp.getRespCode();
	return false;
}
Example #4
0
void rho_do_send_log(rho::apiGenerator::CMethodResult& oResult)
{
    String strDevicePin, strClientID;
    net::IRhoSession* pSession = 0;
	if ( sync::RhoconnectClientManager::haveRhoconnectClientImpl() ) 
    {
	    strDevicePin = rho::sync::RhoconnectClientManager::clientRegisterGetDevicePin();
	    strClientID = rho::sync::RhoconnectClientManager::syncEnineReadClientID();
        pSession = rho::sync::RhoconnectClientManager::getRhoSession();
    }

    String strQuery;
    String strLogUrl = RHOCONF().getString("Log.destinationURI");
    if ( strLogUrl.length() == 0 )
    {
        strLogUrl = RHOCONF().getPath("logserver");
        if ( strLogUrl.length() == 0 )
            strLogUrl = RHOCONF().getPath("syncserver");
        
        strQuery = strLogUrl + "client_log?" +
            "client_id=" + strClientID + "&device_pin=" + strDevicePin + "&log_name=" + RHOCONF().getString("logname");
    }else
    {
        String strSign = "?";
        if ( strrchr(strLogUrl.c_str(), '?') )
            strSign = "&";

        strQuery = strLogUrl + strSign + "client_id=" + strClientID + "&device_pin=" + strDevicePin;
    }

    net::CMultipartItem oItem;
    oItem.m_strFilePath = LOGCONF().getLogFilePath();
    oItem.m_strContentType = "application/octet-stream";
    
    boolean bOldSaveToFile = LOGCONF().isLogToFile();
    LOGCONF().setLogToFile(false);
    NetRequest oNetRequest;
    oNetRequest.setSslVerifyPeer(false);
    
	NetResponse resp = getNetRequest(&oNetRequest).pushMultipartData( strQuery, oItem, pSession, null );

    LOGCONF().setLogToFile(bOldSaveToFile);
    
    boolean isOK = true;
    
    if ( !resp.isOK() )
    {
        LOG(ERROR) + "send_log failed : network error - " + resp.getRespCode() + "; Body - " + resp.getCharData();
        isOK = false;
    }
    
    Hashtable<String, String> hashRes;
    hashRes["status"] = isOK ? "ok" : "error";
    oResult.set(hashRes);
    
    RHODESAPPBASE().setSendingLog(false);
}
Example #5
0
void CRhodesApp::callUiDestroyedCallback()
{
    String strUrl = m_strHomeUrl + "/system/uidestroyed";
    NetResponse resp = getNetRequest().pullData( strUrl, null );
    if ( !resp.isOK() )
    {
        LOG(ERROR) + "UI destroy callback failed. Code: " + resp.getRespCode() + "; Error body: " + resp.getCharData();
    }
}
Example #6
0
void CAsyncHttp::CHttpCommand::callNotify(NetResponse& resp, int nError )
{
    m_strResBody = "rho_callback=1";
    m_strResBody += "&status=";
    if ( nError > 0 )
    {
    	m_strResBody += "error&error_code=" + nError;
    }else
    {
        if ( resp.isSuccess() )
        {
    	    m_strResBody += "ok";
            if ( resp.isResponseRecieved())
	            m_strResBody += "&http_error=" + convertToStringA(resp.getRespCode());
        }
        else
        {
    	    m_strResBody += "error&error_code=";
            m_strResBody += convertToStringA(RhoAppAdapter.getErrorFromResponse(resp));
            if ( resp.isResponseRecieved())
	            m_strResBody += "&http_error=" + convertToStringA(resp.getRespCode());
        }

        String cookies = resp.getCookies();
        if (cookies.length()>0)
            m_strResBody += "&cookies=" + URI::urlEncode(cookies);

        String strHeaders = makeHeadersString();
        if (strHeaders.length() > 0 )
            m_strResBody += "&" + strHeaders;

        m_strResBody += "&" + RHODESAPP().addCallbackObject(
            new CAsyncHttpResponse(resp, m_mapHeaders.get("content-type")), "body");
    }

    if ( m_strCallbackParams.length() > 0 )
        m_strResBody += "&" + m_strCallbackParams;

    if ( m_strCallback.length() > 0 )
    {
        String strFullUrl = m_NetRequest.resolveUrl(m_strCallback);
        getNet().pushData( strFullUrl, m_strResBody, null );
    }
}
boolean CClientRegister::doRegister(CSyncEngine& oSync)
{
    String session = oSync.loadSession();
    if ( session.length() == 0 )
    {
        m_nPollInterval = POLL_INTERVAL_INFINITE;
        LOG(INFO)+"Session is empty, do register later";
        return false;
    }
    
    if ( m_strDevicePin.length() == 0 )
    {
        notifyLoggedIn("","",session);
        m_nPollInterval = POLL_INTERVAL_INFINITE;
        LOG(INFO)+"Device PUSH pin is empty, do register later";
        return false;
    }
    m_nPollInterval = POLL_INTERVAL_SECONDS;

	String client_id = oSync.loadClientID();
	if ( client_id.length() == 0 )
	{
        LOG(WARNING)+"Sync client_id is empty, do register later";
		return false;
	}
    
    IDBResult res = CDBAdapter::getUserDB().executeSQL("SELECT token,token_sent from client_info");
    if ( !res.isEnd() ) {
		String token = res.getStringByIdx(0); 
		int token_sent = res.getIntByIdx(1) && !RHOCONF().getBool("register_push_at_startup");
                
		if ( m_strDevicePin.compare(token) == 0 && token_sent > 0 ) 
		{
			//token in db same as new one and it was already send to the server
			//so we do nothing
            
			return true; 
		}
    }
    
	String strBody = getRegisterBody(client_id);
    NetResponse resp = getNet().pushData( oSync.getProtocol().getClientRegisterUrl(client_id), strBody, &oSync );
	if( resp.isOK() )
    {
        CDBAdapter::getUserDB().executeSQL("UPDATE client_info SET token_sent=?, token=?", 1, m_strDevicePin );
		LOG(INFO)+"Registered client sucessfully...";
        
        return true;

    }

	LOG(WARNING)+"Network error: "+ resp.getRespCode();
	return false;
}
Example #8
0
void CRhodesApp::callAppActiveCallback(boolean bActive)
{
    LOG(INFO) + "callAppActiveCallback";
    if (bActive)
    {
        // Restart server each time when we go to foreground
/*        if (m_activateCounter++ > 0)
        {
#if !defined( OS_WINCE ) && !defined (OS_WINDOWS)
            m_httpServer->stop();
#endif
            this->stopWait();

        } */

        m_appCallbacksQueue->addQueueCommand(new CAppCallbacksQueue::Command(CAppCallbacksQueue::app_activated));
    }
    else
    {
        // Deactivation callback must be called in place (not in separate thread!)
        // This is because system can kill application at any time after this callback finished
        // So to guarantee user code is executed on deactivation, we must not exit from this function
        // until application finish executing of user-defined deactivation callback.
        // However, blocking UI thread can cause problem with API refering to UI (such as WebView.navigate etc)
        // To fix this problem, new mode 'deactivation' introduced. When this mode active, no UI operations allowed.
        // All such operation will throw exception in ruby code when calling in 'deactivate' mode.
        m_bDeactivationMode = true;
        m_appCallbacksQueue->addQueueCommand(new CAppCallbacksQueue::Command(CAppCallbacksQueue::app_deactivated));

        String strUrl = m_strHomeUrl + "/system/deactivateapp";
        NetResponse resp = getNetRequest().pullData( strUrl, null );
        if ( !resp.isOK() )
        {
            LOG(ERROR) + "deactivate app failed. Code: " + resp.getRespCode() + "; Error body: " + resp.getCharData();
        }else
        {
            const char* szData = resp.getCharData();
            boolean bStop = szData && strcmp(szData,"stop_local_server") == 0;

            if (bStop)
            {
#if !defined( OS_WINCE ) && !defined (OS_WINDOWS)
                LOG(INFO) + "Stopping local server.";
                m_httpServer->stop();
#endif
            }
        }

        m_bDeactivationMode = false;
    }
}
Example #9
0
    void run(common::CRhoThread &)
    {
        String strDevicePin = rho::sync::CClientRegister::getInstance() ? rho::sync::CClientRegister::getInstance()->getDevicePin() : "";
	    String strClientID = rho::sync::CSyncThread::getSyncEngine().readClientID();
        
        String strLogUrl = RHOCONF().getPath("logserver");
        if ( strLogUrl.length() == 0 )
            strLogUrl = RHOCONF().getPath("syncserver");
        
	    String strQuery = strLogUrl + "client_log?" +
            "client_id=" + strClientID + "&device_pin=" + strDevicePin + "&log_name=" + RHOCONF().getString("logname");
        
        net::CMultipartItem oItem;
        oItem.m_strFilePath = LOGCONF().getLogFilePath();
        oItem.m_strContentType = "application/octet-stream";
        
        boolean bOldSaveToFile = LOGCONF().isLogToFile();
        LOGCONF().setLogToFile(false);
        NetRequest oNetRequest;
        oNetRequest.setSslVerifyPeer(false);
        
        NetResponse resp = getNetRequest(&oNetRequest).pushMultipartData( strQuery, oItem, &(rho::sync::CSyncThread::getSyncEngine()), null );
        LOGCONF().setLogToFile(bOldSaveToFile);
        
        boolean isOK = true;
        
        if ( !resp.isOK() )
        {
            LOG(ERROR) + "send_log failed : network error - " + resp.getRespCode() + "; Body - " + resp.getCharData();
            isOK = false;
        }

        if (m_strCallback.length() > 0) 
        {
            const char* body = isOK ? "rho_callback=1&status=ok" : "rho_callback=1&status=error";

            rho_net_request_with_data(RHODESAPP().canonicalizeRhoUrl(m_strCallback).c_str(), body);
        }

        RHODESAPP().setSendingLog(false);
    }
Example #10
0
const String& CRhodesApp::getRhoMessage(int nError, const char* szName)
{
    String strUrl = m_strHomeUrl + "/system/getrhomessage?";
    if ( nError )
        strUrl += "error=" + convertToStringA(nError);
    else if ( szName && *szName )
    {
        strUrl += "msgid=";
        strUrl += szName;
    }

    NetResponse resp = getNetRequest().pullData( strUrl, null );
    if ( !resp.isOK() )
    {
        LOG(ERROR) + "getRhoMessage failed. Code: " + resp.getRespCode() + "; Error body: " + resp.getCharData();
        m_strRhoMessage = "";
    }
    else
        m_strRhoMessage = resp.getCharData();

    return m_strRhoMessage;
}