예제 #1
0
/*static*/ void CRhoFile::recursiveCreateDir(const char* szFolderPath, const char* szBasePath)
{
    String strRelPath = String(szFolderPath).substr(strlen(szBasePath), strlen(szFolderPath) );
    String strPath = szBasePath;
    CTokenizer oTokenizer( strRelPath, "/\\" );
	while (oTokenizer.hasMoreTokens()) 
    {
		String tok = oTokenizer.nextToken();
        strPath = CFilePath::join(strPath, tok);
        createFolder(strPath.c_str());
    }
}
예제 #2
0
void callback_system_update_bundle(void *arg, rho::String const &strQuery)
{
    const char* s = strQuery.c_str();
    
    rho::String qURL = "";
    rho::String qServerIP = "";
    rho::String qServerPort = "";
    bool isRefresh = false;
    
    
    rho::common::CTokenizer oTokenizer(strQuery, "&");
    while (oTokenizer.hasMoreTokens())
    {
        rho::String tok = oTokenizer.nextToken();
        if (tok.length() == 0)
        continue;
        
        size_t nPos = tok.find("package_url=");
        if ( nPos != rho::String::npos )
        {
            qURL = strQuery.substr(nPos+12);
        }
        if ( rho::String_startsWith(tok, "package_url=") )
        {
            qURL = tok.substr(12);
        }else if ( rho::String_startsWith( tok, "server_ip=") )
        {
            qServerIP = tok.substr(10);
        }else if ( rho::String_startsWith( tok, "server_port=") )
        {
            qServerPort = tok.substr(12);
        }else if ( rho::String_startsWith( tok, "refresh_after_update=") )
        {
            rho::String srefresh = tok.substr(21);
            if ((srefresh.compare("false") != 0) && (srefresh.compare("0") != 0) && (srefresh.compare("FALSE") != 0)) {
                isRefresh = true;
            }
        }
    }

    rho::String our_responce_server_url = "http://";
    our_responce_server_url = our_responce_server_url + qServerIP + ":" + qServerPort + "/response_from_device";
    
    rho_http_sendresponse(arg, "");
    
    
    BundleUpdateThreadQueue::BUCommand* cmd = new BundleUpdateThreadQueue::BUCommand(qURL, our_responce_server_url, isRefresh);
    getBundleUpdateThreadQueueSignletone()->addQueueCommand(cmd);
    
}
예제 #3
0
void LogSettings::setExcludeFilter( const String& strExcludeFilter )
{
    if ( strExcludeFilter.length() > 0 )
    {
        rho::common::CTokenizer oTokenizer( strExcludeFilter, "," );
	    while (oTokenizer.hasMoreTokens()) 
        {
            String tok = rho::String_trim(oTokenizer.nextToken());
		    if (tok.length() == 0)
			    continue;

            //m_arExcludeAttribs.addElement( "\"" + tok + "\"=>\"" );
            m_arExcludeAttribs.addElement( tok );
        } 
    }
    else
    	m_arExcludeAttribs.removeAllElements();
}
예제 #4
0
파일: RhoFile.cpp 프로젝트: KlearXos/rhodes
/*static*/ void CRhoFile::recursiveCreateDir(const char* szFolderPath, const char* szBasePath)
{
    String strRelPath = String(szFolderPath).substr(strlen(szBasePath), strlen(szFolderPath) );
    String strPath = szBasePath;
    // handle case when szBasePath is empty and szFolderPath is starting with /
    if (strPath.length() == 0 && strRelPath.length() > 0 && strRelPath[0] == '/')
    {
        strPath += '/';
    }
    
    CTokenizer oTokenizer( strRelPath, "/\\" );
	while (oTokenizer.hasMoreTokens()) 
    {
		String tok = oTokenizer.nextToken();
        strPath = CFilePath::join(strPath, tok);
        
        if (!isDirectory(strPath.c_str()))
        {
            createFolder(strPath.c_str());
        }
    }
}
예제 #5
0
void callback_system_update_bundle_callback(void *arg, rho::String const &strQuery) {
    const char* s = strQuery.c_str();
    
    // status    ok,error,need_sync
    // message   "bla bla bla"
    
    rho::String qStatus = "";
    rho::String qMessage = "";
    
    
    rho::common::CTokenizer oTokenizer(strQuery, "&");
    while (oTokenizer.hasMoreTokens())
    {
        rho::String tok = oTokenizer.nextToken();
        if (tok.length() == 0)
        continue;
        
        if ( rho::String_startsWith(tok, "status=") )
        {
            qStatus = tok.substr(7);
        }else if ( rho::String_startsWith( tok, "message=") )
        {
            qMessage = tok.substr(8);
        }
    }
    
    rho::String query = "";
    
    bool our_refresh_webview = false;
    rho::String our_responce_server_url = "";
    
    BundleUpdateThreadQueue::BUCommand* cmd = getBundleUpdateThreadQueueSignletone()->getCurrentBUCommand();
    if (cmd != NULL) {
        our_refresh_webview = cmd->refresh_webview;
        our_responce_server_url = cmd->responce_url;
    }
    
    rho::String message = "Unrecognizing Situation during update bundle! Restart application and reconnect device to server !";
    if (qStatus.compare("ok") == 0) {
       message = "Update bundle was finished !";
       query = "&status=ok";
        
        if (our_refresh_webview) {
            rho_webview_refresh(-1);
        }
    }
    if (qStatus.compare("error") == 0) {
        message = "Error when update Bundle : " + qMessage + " !";
        query = "&status=error&message="+message;
    }
    if (qStatus.compare("need_sync") == 0) {
        message = "Your application files too old. Request for full Bundle update was sended to server !";
        query = "&status=need_full_update";
    }
    
    
    alert_show_status("Development Extras", message.c_str(), "OK");
    
    //alert_show_popup(&p);
    
    rho_http_sendresponse(arg, "");
    
    // send responce to server
    char* norm_url = rho_http_normalizeurl(our_responce_server_url.c_str());
    query = query + make_info_string(false);
    rho_net_request_with_data_in_separated_thread(norm_url, query.c_str());
    rho_http_free(norm_url);
    
    // remove temporary files
    rho::String fileZipLocalDir = rho::common::CFilePath::join(RHODESAPPBASE().getRhoUserPath(), "RhoBundle");
    //rho_file_impl_delete_folder(fileZipLocalDir.c_str());
    rho::common::CRhoFile::deleteFolder(fileZipLocalDir.c_str());
    
    if (cmd != NULL) {
        cmd->canContinue = true;
    }
}
예제 #6
0
bool CHttpServer::send_file(String const &path, HeaderList const &hdrs)
{
    String fullPath = CFilePath::normalizePath(path);

    if (String_startsWith(fullPath,"/app/db/db-files") )
        fullPath = CFilePath::join( rho_native_rhodbpath(), path.substr(4) );
    else if (fullPath.find(m_root) != 0 && fullPath.find(m_strRhoRoot) != 0 && fullPath.find(m_strRuntimeRoot) != 0 && fullPath.find(m_userroot) != 0 && fullPath.find(m_strRhoUserRoot) != 0)
        fullPath = CFilePath::join( m_root, path );
	
    struct stat st;
    bool bCheckExist = true;
#ifdef RHODES_EMULATOR
    String strPlatform = RHOSIMCONF().getString("platform");
    if ( strPlatform.length() > 0 )
    {
        String fullPath1 = fullPath;
        int nDot = fullPath1.rfind('.');
        if ( nDot >= 0 )
            fullPath1.insert(nDot, String(".") + strPlatform);
        else
            fullPath1 += String(".") + strPlatform;

        if (stat(fullPath1.c_str(), &st) == 0 && S_ISREG(st.st_mode))
        {
            fullPath = fullPath1;
            bCheckExist = false;
        }
    }

#endif

    bool doesNotExists = bCheckExist && (stat(fullPath.c_str(), &st) != 0 || !S_ISREG(st.st_mode));
    if ( doesNotExists ) {
        // looking for files at 'rho/apps' at runtime folder
        fullPath = CFilePath::join( m_strRuntimeRoot, path );
    }

    if (verbose) RAWTRACE1("Sending file %s...", fullPath.c_str());

    if ( doesNotExists ) {
        if ( stat(fullPath.c_str(), &st) != 0 || !S_ISREG(st.st_mode) ) {
            doesNotExists = true;
        }else
            doesNotExists = false;
    }

#ifdef RHODES_EMULATOR
    if ( doesNotExists )
    {
        CTokenizer oTokenizer( RHOSIMCONF().getString("ext_path"), ";" );
	    while (oTokenizer.hasMoreTokens()) 
        {
		    String tok = oTokenizer.nextToken();
		    tok = String_trim(tok);
            
            String fullPath1 = CFilePath::join( tok, path );
            if (stat(fullPath1.c_str(), &st) == 0 && S_ISREG(st.st_mode))
            {
                fullPath = fullPath1;
                doesNotExists = false;
                break;
            }

        }
    }
#endif

    if ( doesNotExists ) {
        if (verbose) RAWLOG_ERROR1("The file %s was not found", path.c_str());
        String error = "<!DOCTYPE html><html><font size=\"+4\"><h2>404 Not Found.</h2> The file " + path + " was not found.</font></html>";
        send_response(create_response("404 Not Found",error));
        return false;
    }

    PROF_START("LOW_FILE");
    FILE *fp = fopen(fullPath.c_str(), "rb");
    PROF_STOP("LOW_FILE");
    if (!fp) {
        if (verbose) RAWLOG_ERROR1("The file %s could not be opened", path.c_str());
        String error = "<!DOCTYPE html><html><font size=\"+4\"><h2>404 Not Found.</h2> The file " + path + " could not be opened.</font></html";
        send_response(create_response("404 Not Found",error));
        return false;
    }
    
    HeaderList headers;
    
    // Detect mime type
    headers.push_back(Header("Content-Type", get_mime_type(path)));
    if ( String_startsWith(path, "/public") )
    {
        headers.push_back(Header("Expires", "Thu, 15 Apr 2020 20:00:00 GMT") );
        headers.push_back(Header("Cache-Control", "max-age=2592000") );
    }

    // Content length
    char* buf = new char[FILE_BUF_SIZE];
    
    String start_line;
    
    size_t file_size = st.st_size;
    size_t range_begin = 0, range_end = file_size - 1;
    size_t content_size = file_size;
    if (parse_range(hdrs, &range_begin, &range_end))
    {
        if (range_end >= file_size)
            range_end = file_size - 1;
        if (range_begin >= range_end)
            range_begin = range_end - 1;
        content_size = range_end - range_begin + 1;
        
        if (fseek(fp, range_begin, SEEK_SET) == -1) {
            RAWLOG_ERROR1("Can not seek to specified range start: %lu", (unsigned long)range_begin);
			snprintf(buf, FILE_BUF_SIZE, "bytes */%lu", (unsigned long)file_size);
			headers.push_back(Header("Content-Range", buf));
			send_response(create_response("416 Request Range Not Satisfiable",headers));
            fclose(fp);
            delete[] buf;
            return false;
        }
		
		snprintf(buf, FILE_BUF_SIZE, "bytes %lu-%lu/%lu", (unsigned long)range_begin,
                 (unsigned long)range_end, (unsigned long)file_size);
        headers.push_back(Header("Content-Range", buf));
        
        start_line = "206 Partial Content";
    }
    else {
        start_line = "200 OK";
    }

    
    snprintf(buf, FILE_BUF_SIZE, "%lu", (unsigned long)content_size);
    headers.push_back(Header("Content-Length", buf));
    
    // Send headers
    if (!send_response(create_response(start_line, headers))) {
        if (verbose) RAWLOG_ERROR1("Can not send headers while sending file %s", path.c_str());
        fclose(fp);
        delete[] buf;
        return false;
    }
    
    // Send body
    for (size_t start = range_begin; start < range_end + 1;) {
        size_t need_to_read = range_end - start + 1;
        if (need_to_read == 0)
            break;
        
        if (need_to_read > FILE_BUF_SIZE)
            need_to_read = FILE_BUF_SIZE;

PROF_START("LOW_FILE");
        size_t n = fread(buf, 1, need_to_read, fp);//fread(buf, 1, need_to_read, fp);
PROF_STOP("LOW_FILE");
        if (n < need_to_read) {
			if (ferror(fp) ) {
				if (verbose) RAWLOG_ERROR2("Can not read part of file (at position %lu): %s", (unsigned long)start, strerror(errno));
			} else if ( feof(fp) ) {
				if (verbose) RAWLOG_ERROR1("End of file reached, but we expect data (%lu bytes)", (unsigned long)need_to_read);
			}
            fclose(fp);
            delete[] buf;
            return false;
        }
        
        start += n;
        
        if (!send_response_body(String(buf, n))) {
            if (verbose) RAWLOG_ERROR1("Can not send part of data while sending file %s", path.c_str());
            fclose(fp);
            delete[] buf;
            return false;
        }
    }

PROF_START("LOW_FILE");
    fclose(fp);
PROF_STOP("LOW_FILE");
    delete[] buf;
    if (verbose) RAWTRACE1("File %s was sent successfully", path.c_str());
    return false;
}
예제 #7
0
bool CHttpServer::dispatch(String const &uri, Route &route)
{
    if (isknowntype(uri))
        return false;
    
    // Trying to parse route
    if (!parse_route(uri, route))
        return false;
    
    // Convert CamelCase to underscore_case
    // There has to be a better way?
    char tmp[3];
    const char *tmpstr = route.model.c_str();
    String controllerName = "";
    for(int i = 0; tmpstr[i] != '\0'; i++) {
        if(tmpstr[i] >= 'A' && tmpstr[i] <= 'Z') {
            if(i == 0) {
                tmp[0] = tmpstr[i] + 0x20;
                tmp[1] = '\0';
            } else {
                tmp[0] = '_';
                tmp[1] = tmpstr[i] + 0x20;
                tmp[2] = '\0';
            }
        } else {
            tmp[0] = tmpstr[i];
            tmp[1] = '\0';
        }
        controllerName += tmp;
    }
        
    //check if there is controller.rb to run
	struct stat st;

    String newfilename = CFilePath::join(m_root, route.application) + "/" + route.model + "/" + controllerName + "_controller" RHO_RB_EXT;
    String filename = CFilePath::join(m_root, route.application) + "/" + route.model + "/controller" RHO_RB_EXT;

    //look for controller.rb or model_name_controller.rb
    if ((stat(filename.c_str(), &st) != 0 || !S_ISREG(st.st_mode)) && (stat(newfilename.c_str(), &st) != 0 || !S_ISREG(st.st_mode)))
    {
#ifdef RHODES_EMULATOR
        CTokenizer oTokenizer( RHOSIMCONF().getString("ext_path"), ";" );
	    while (oTokenizer.hasMoreTokens()) 
        {
		    String tok = oTokenizer.nextToken();
		    tok = String_trim(tok);

            newfilename = CFilePath::join(tok, route.application) + "/" + route.model + "/" + controllerName + "_controller" RHO_RB_EXT;
            filename = CFilePath::join(tok, route.application) + "/" + route.model + "/controller" RHO_RB_EXT;

            //look for controller.rb or model_name_controller.rb
            if ((stat(filename.c_str(), &st) != 0 || !S_ISREG(st.st_mode)) && (stat(newfilename.c_str(), &st) != 0 || !S_ISREG(st.st_mode)))
                continue;

            return true;
        }
#endif

        return false;
    }

    return true;
}