Ejemplo n.º 1
0
int _ExecuteApp(HttpContextRef context, RouteRef route) {
	
	if (route->_application && !strcmp(route->_application,"AppManager")) {
		RAWLOG_INFO("Executing AppManager");
		return ExecuteAppManager(context,route);
	} else if (route->_application && !strcmp(route->_application,"system")) {
		if (context->_request->_method == METHOD_GET) {
			if (route->_model && !strcmp(route->_model,"geolocation")) {
				return HTTPSendReply(context,GeoGetLocation()); 	
			} else if (route->_model && !strcmp(route->_model,"syncdb")) {
				rho_sync_doSyncAllSources();
				return HTTPSendReply(context,"OK"); 	
			} else if (route->_model && !strcmp(route->_model,"redirect_to")) {
				if (context->_request->_query && !strncmp("url=",context->_request->_query,4)) {
					char* location = context->_request->_query+4;
					int len = strlen(location);
					HTTPUrlDecode(location, len, location, len+1);
					return HTTPRedirect(context, location);
				} else {
					return HTTPRedirect(context, "/app/");
				}
			}
		}
	} else if (route->_application && !strcmp(route->_application,"shared")) {
		return 0;
	} else {
		RAWLOG_INFO1( "Executing %s",route->_application);
		return _CallApplication(context, route);
	}
	return 0;
}
Ejemplo n.º 2
0
static void Init_RhoBlobs()
{
    const char* szBlobPath = rho_rhodesapp_getblobsdirpath();
    const char* szUserPath = rho_native_rhodbpath();
    RAWLOG_INFO1("Init_RhoBlobs: %s", szBlobPath );

    rho_file_recursive_createdir(szBlobPath, szUserPath);
}
Ejemplo n.º 3
0
static void Init_RhoBlobs()
{
  VALUE path = rb_str_new2(rho_rhodesapp_getblobsdirpath());

  RAWLOG_INFO1("Init_RhoBlobs: %s", RSTRING_PTR(path) );

  if ( rb_funcall(rb_cDir, rb_intern("exist?"), 1, path)==Qfalse )
    rb_funcall(rb_cDir, rb_intern("mkdir"), 1, path);

  RAWLOG_INFO("Init_RhoBlobs: done");
}
Ejemplo n.º 4
0
RHO_GLOBAL VALUE rho_bluetooth_get_device_name() {
    JNIEnv *env = jnienv();
    jclass cls = getJNIClass(RHODES_JAVA_CLASS_RHOBLUETOOTHMANAGER);
    if (!cls) return rho_ruby_get_NIL();
    jmethodID mid = getJNIClassStaticMethod(env, cls, "get_device_name", "()Ljava/lang/String;");
    if (!mid) return rho_ruby_get_NIL();
    jstring jname = static_cast<jstring>(env->CallStaticObjectMethod(cls, mid));
    if (!jname) return rho_ruby_get_NIL();
    std::string name = rho_cast<std::string>(env, jname); 
    RAWLOG_INFO1("rho_bluetooth_get_device_name() : %s", name.c_str());
    return rho_ruby_create_string(name.c_str());
}
Ejemplo n.º 5
0
VALUE callFramework(VALUE hashReq) {
    VALUE callres = rb_funcall(framework, framework_mid, 1, hashReq);
	
	if (TYPE(callres)!=T_STRING) {
		RAWLOG_INFO1("Method call result type = %s", rb_type_to_s(callres));
		return rb_str_new2("Error");//TBD: Supply html description of the error
	}

    rb_gc_register_mark_object(callres);
	//TBD: need to cleanup memory
	rb_gc();

	return callres;
}
Ejemplo n.º 6
0
VALUE callFramework(VALUE hashReq) {
    VALUE callres = rb_funcall(framework, framework_mid, 1, hashReq);

    if (TYPE(callres)!=T_STRING) {
        RAWLOG_INFO1("Method call result type = %s", rb_type_to_s(callres));
        return rb_str_new2("Error");//TBD: Supply html description of the error
    }
    if ( !rho_conf_getBool("log_skip_post") )
        RAWTRACE(RSTRING_PTR(callres));

    rb_gc_register_mark_object(callres);
    //TBD: need to cleanup memory
    //rb_gc();

    return callres;
}
Ejemplo n.º 7
0
VALUE callServeIndex(char* index_name) {
    VALUE callres;
    //RhoSetCurAppPath(index_name);
	callres = rb_funcall(framework, framework_mid2, 1, RhoPreparePath(rb_str_new2(index_name)));
	
	if (TYPE(callres)!=T_STRING) {
		RAWLOG_INFO1("Method call result type = %s", rb_type_to_s(callres));
		return rb_str_new2("Error");//TBD: Supply html description of the error
	}
    rb_gc_register_mark_object(callres);

	//TBD: need to cleanup memory
	rb_gc();
	
	return callres;
}
Ejemplo n.º 8
0
VALUE require_compiled(VALUE fname, VALUE* result)
{
    VALUE path;
    char* szName1 = 0;

    rb_funcall(fname, rb_intern("sub!"), 2, rb_str_new2(".rb"), rb_str_new2("") );

    szName1 = RSTRING_PTR(fname);
    if ( strcmp("strscan",szName1)==0 || strcmp("enumerator",szName1)==0 ||
        strcmp("stringio",szName1)==0 || strcmp("socket",szName1)==0 ||
        strcmp("digest.so",szName1)==0 || strcmp("openssl.so",szName1)==0 ||
        strcmp("fcntl",szName1)==0 || strcmp("digest/md5",szName1)==0 ||
        strcmp("digest/sha1",szName1)==0 )
        return Qtrue;

    if ( isAlreadyLoaded(fname) == Qtrue )
        return Qtrue;

    //RAWLOG_INFO1("find_file: %s", RSTRING_PTR(fname));
    path = find_file(fname);

    if ( path != 0 )
    {
        VALUE seq;

//        if ( isAlreadyLoaded(path) == Qtrue )
//            return Qtrue;

        RAWLOG_INFO1("require_compiled: %s", szName1);

        //optimize require
        //rb_ary_push(GET_VM()->loaded_features, path);
        rb_ary_push(GET_VM()->loaded_features, fname);

        rb_gc_disable();
        seq = loadISeqFromFile(path);
        rb_gc_enable();

        //*result = rb_funcall(seq, rb_intern("eval"), 0 );
        *result = rb_iseq_eval(seq);

        return Qtrue;
    }

    RAWLOG_ERROR1("require_compiled: error: can not find %s", RSTRING_PTR(fname));
    return Qnil;
}
Ejemplo n.º 9
0
VALUE callServeIndex(char* index_name, VALUE hashReq) {
    VALUE callres;
    //RhoSetCurAppPath(index_name);
    callres = rb_funcall(framework, framework_mid2, 2, RhoPreparePath(rb_str_new2(index_name)), hashReq);

    if (TYPE(callres)!=T_STRING) {
        RAWLOG_INFO1("Method call result type = %s", rb_type_to_s(callres));
        return rb_str_new2("Error");//TBD: Supply html description of the error
    }

    if ( !rho_conf_getBool("log_skip_post") )
        RAWTRACE(RSTRING_PTR(callres));

    rb_gc_register_mark_object(callres);

    //TBD: need to cleanup memory
    //rb_gc();

    return callres;
}
Ejemplo n.º 10
0
static int curl_trace(CURL *curl, curl_infotype type, char *data, size_t size, void *opaque)
{
    const char *text = "";
    switch (type) {
        case CURLINFO_TEXT:         text = "== Info"; break;
        case CURLINFO_HEADER_IN:    text = "<= Recv headers"; break;
        case CURLINFO_HEADER_OUT:   text = "=> Send headers"; break;
        case CURLINFO_DATA_IN:      text = "<= Recv data"; break;
        case CURLINFO_DATA_OUT:     text = "=> Send data"; break;
        case CURLINFO_SSL_DATA_IN:  text = "<= Recv SSL data"; break;
        case CURLINFO_SSL_DATA_OUT: text = "=> Send SSL data"; break;
        default:
            RAWLOG_INFO1("!! Unknown type of curl trace: %d", (int)type);
            return 0;
    }
    
    String strData(data, size);
    RAWLOG_INFO3("%s (%d bytes): %s", text, size, strData.c_str());
    return 0;
}
Ejemplo n.º 11
0
bool CHttpServer::process(SOCKET sock)
{
    m_sock = sock;

	// First of all, make socket non-blocking
#if defined(WINDOWS_PLATFORM)
	unsigned long optval = 1;
	if(::ioctlsocket(m_sock, FIONBIO, &optval) == SOCKET_ERROR) {
		if (verbose) RAWLOG_ERROR1("Can not set non-blocking socket mode: %d", RHO_NET_ERROR_CODE);
		return false;
	}
#else
	int flags = fcntl(m_sock, F_GETFL);
	if (flags == -1) {
		if (verbose) RAWLOG_ERROR1("Can not get current socket mode: %d", errno);
		return false;
	}
	if (fcntl(m_sock, F_SETFL, flags | O_NONBLOCK) == -1) {
		if (verbose) RAWLOG_ERROR1("Can not set non-blocking socket mode: %d", errno);
		return false;
	}
#endif

    // Read request from socket
    ByteVector request;

    String method, uri, query;
    HeaderList headers;
    String body;
    if (!parse_request(method, uri, query, headers, body)) {
        if (verbose) RAWLOG_ERROR("Parsing error");
        send_response(create_response("500 Internal Error"));
        return false;
    }

    if ( !String_endsWith( uri, "js_api_entrypoint" ) )
        if (verbose) RAWLOG_INFO1("Process URI: '%s'", uri.c_str());

    return decide(method, uri, query, headers, body);
}
Ejemplo n.º 12
0
bool CHttpServer::init()
{
    RAWTRACE("Open listening socket...");
    
    close_listener();
    m_listener = socket(AF_INET, SOCK_STREAM, 0);
    if (m_listener == INVALID_SOCKET) {
        RAWLOG_ERROR1("Can not create listener: %d", RHO_NET_ERROR_CODE);
        return false;
    }
    
    int enable = 1;
    if (setsockopt(m_listener, SOL_SOCKET, SO_REUSEADDR, (const char *)&enable, sizeof(enable)) == SOCKET_ERROR) {
        RAWLOG_ERROR1("Can not set socket option (SO_REUSEADDR): %d", RHO_NET_ERROR_CODE);
        close_listener();
        return false;
    }
    
    struct sockaddr_in sa;
    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons((uint16_t)m_port);
    sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    if (bind(m_listener, (const sockaddr *)&sa, sizeof(sa)) == SOCKET_ERROR) {
        RAWLOG_ERROR2("Can not bind to port %d: %d", m_port, RHO_NET_ERROR_CODE);
        close_listener();
        return false;
    }
    
    if (listen(m_listener, 128) == SOCKET_ERROR) {
        RAWLOG_ERROR1("Can not listen on socket: %d", RHO_NET_ERROR_CODE);
        close_listener();
        return false;
    }
    
    RAWLOG_INFO1("Listen for connections on port %d", m_port);
    return true;
}
Ejemplo n.º 13
0
void* rho_dispatch(struct conn *c, const char* path) {
  RouteRef route;
  
  if ( _shttpd_match_extension(c->uri,"css,js,html,htm,png,bmp,jpg") )
    return NULL;

#ifdef __SYMBIAN32__
  if ( strstr(_shttpd_known_http_methods[c->method].ptr, "GET" ) )
	  webview_set_current_location(c->uri);
#endif
  
  if ((route = _alloc_route(c->uri)) != NULL) {
    if (_parse_route(route)) {
      struct stat	st;
      //is this an actual file or folder
      if (_shttpd_stat(path, &st) != 0)
        return route;      
      //is this a folder
      if (S_ISDIR(st.st_mode)) {
        //check if there is controller.rb to run
        char	filename[FILENAME_MAX];
        int len = strlen(path);
        char* slash = path[len-1] == '\\' || path[len-1] == '/' ? "" : "/";
        _shttpd_snprintf(filename,sizeof(filename),"%s%s%s",path,slash,"controller.iseq");

        if ((_shttpd_stat(filename, &st) == 0)&&(!S_ISDIR(st.st_mode))) {
          RAWLOG_INFO1("Run controller on this url: %s", c->uri);
          return route;
        }
      }
    }
    _free_route(route);
  }

  return NULL;
}
Ejemplo n.º 14
0
INetResponse* CURLNetRequest::pushMultipartData(const String& strUrl, VectorPtr<CMultipartItem*>& arItems, IRhoSession* oSession, Hashtable<String,String>* pHeaders)
{
    int nRespCode = -1;
    String strRespBody;
    
    RAWLOG_INFO1("POST request (Push): %s", strUrl.c_str());
    rho_net_impl_network_indicator(1);
    
    curl_slist *hdrs = m_curl.set_options("POST", strUrl, String(), oSession, pHeaders);
    CURL *curl = m_curl.curl();
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strRespBody);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlBodyStringCallback);
	
    curl_httppost *post = NULL, *last = NULL;
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);

    for (size_t i = 0, lim = arItems.size(); i < lim; ++i) {
        CMultipartItem *mi = arItems[i];
        
        size_t cl;
        if (mi->m_strFilePath.empty())
            cl = mi->m_strBody.size();
        else {
            common::CRhoFile f;
            if (!f.open(mi->m_strFilePath.c_str(), common::CRhoFile::OpenReadOnly))
                cl = 0;
            else {
                cl = f.size();
                f.close();
            }
        }

        char buf[32];
        buf[sizeof(buf) - 1] = '\0';
        snprintf(buf, sizeof(buf) - 1, "Content-Length: %lu", (unsigned long)cl);
        curl_slist *fh = NULL;
        fh = curl_slist_append(fh, buf);

        const char *name = mi->m_strName.empty() ? "blob" : mi->m_strName.c_str();
        int opt = mi->m_strFilePath.empty() ? CURLFORM_COPYCONTENTS : CURLFORM_FILE;
        const char *data = mi->m_strFilePath.empty() ? mi->m_strBody.c_str() : mi->m_strFilePath.c_str();
        const char *ct = mi->m_strContentType.empty() ? NULL : mi->m_strContentType.c_str();
        if (ct) {
            curl_formadd(&post, &last,
                         CURLFORM_COPYNAME, name,
                         opt, data,
                         CURLFORM_CONTENTTYPE, ct,
                         CURLFORM_CONTENTHEADER, fh,
                         CURLFORM_END);
        }
        else {
            curl_formadd(&post, &last,
                         CURLFORM_COPYNAME, name,
                         opt, data,
                         CURLFORM_CONTENTHEADER, fh,
                         CURLFORM_END);
        }
    }
        
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
    
    CURLcode err = doCURLPerform(strUrl);
	
    curl_slist_free_all(hdrs);
    curl_formfree(post);
    
    rho_net_impl_network_indicator(0);
    
    nRespCode = getResponseCode(err, strRespBody, oSession);
    return makeResponse(strRespBody, nRespCode);
}
Ejemplo n.º 15
0
bool CHttpServer::init()
{
	if (verbose) RAWTRACE("Open listening socket...");

    close_listener();
    
    //m_listener = socket(AF_INET, SOCK_STREAM, 0);
    m_listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    if (m_listener == INVALID_SOCKET) {
        if (verbose) RAWLOG_ERROR1("Can not create listener: %d", RHO_NET_ERROR_CODE);
        return false;
    }

    int enable = 1;
    if (setsockopt(m_listener, SOL_SOCKET, SO_REUSEADDR, (const char *)&enable, sizeof(enable)) == SOCKET_ERROR) {
        if (verbose) RAWLOG_ERROR1("Can not set socket option (SO_REUSEADDR): %d", RHO_NET_ERROR_CODE);
        close_listener();
        return false;
    }
    
    struct sockaddr_in sa;
    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons((uint16_t)m_port);
    
    if (m_enable_external_access) {
        sa.sin_addr.s_addr = htonl(INADDR_ANY);
    }
    else {
        sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    }
    
    
    if (bind(m_listener, (const sockaddr *)&sa, sizeof(sa)) == SOCKET_ERROR) {
        if (verbose) RAWLOG_ERROR2("Can not bind to port %d: %d", m_port, RHO_NET_ERROR_CODE);
        close_listener();
        return false;
    }
    
    if (listen(m_listener, 128) == SOCKET_ERROR) {
        if (verbose) RAWLOG_ERROR1("Can not listen on socket: %d", RHO_NET_ERROR_CODE);
        close_listener();
        return false;
    }
    
    // detect local IP adress
    struct sockaddr_in sin;
    memset(&sin, 0, sizeof(sin));
    //sin.sin_len = sizeof(sin);
    sin.sin_family = AF_INET; // or AF_INET6 (address family)
    socklen_t len = sizeof(sin);
    if (getsockname(m_listener, (struct sockaddr *)&sin, &len) < 0) {
        // Handle error here
        if (verbose) RAWLOG_ERROR("Can not detect local IP adress");
    }
    else {
        m_IP_adress = inet_ntoa(sin.sin_addr);
    }
    
    if (verbose) RAWLOG_INFO1("Listen for connections on port %d", m_port);
    return true;
}
Ejemplo n.º 16
0
VALUE require_compiled(VALUE fname, VALUE* result, int bLoad)
{
    VALUE path;
    char* szName1 = 0;
    VALUE retval = Qtrue;
    
    if (TYPE(fname) != T_STRING)
        rb_raise(rb_eLoadError, "can not load non-string");

    szName1 = RSTRING_PTR(fname);

    if ( String_endsWith(szName1,".rb") ) 
    {
        rb_str_chop_bang(fname); rb_str_chop_bang(fname); rb_str_chop_bang(fname);
    }
    //rb_funcall(fname, rb_intern("sub!"), 2, rb_str_new2(".rb"), rb_str_new2("") );
    szName1 = RSTRING_PTR(fname);

    if ( strcmp("strscan",szName1)==0 || strcmp("enumerator",szName1)==0 ||
        strcmp("stringio",szName1)==0 || strcmp("socket",szName1)==0 )
        return Qtrue;

    RHO_LOCK(require_lock);

    if ( !bLoad && isAlreadyLoaded(fname) == Qtrue )
        goto RCompExit;

    path = find_file(fname);

    if ( path != 0 )
    {
        VALUE seq;

        RAWLOG_INFO1("require_compiled: %s", szName1);

        //optimize require
        //rb_ary_push(GET_VM()->loaded_features, path);
        rb_ary_push(GET_VM()->loaded_features, fname);

#ifdef RHODES_EMULATOR
        if ( strstr( RSTRING_PTR(path), ".rb") == 0 )
            rb_str_cat(path,".rb",3);

        GET_VM()->src_encoding_index = rb_utf8_encindex();
        rb_load(path, 0);

        if( rho_simconf_getBool("reload_app_changes") )
        {
            if ( strncmp( RSTRING_PTR(path), rho_native_rhopath(), strlen(rho_native_rhopath()) ) == 0 )
                rb_ary_delete(GET_VM()->loaded_features, fname);
        }
#else
        //rb_gc_disable();
        seq = loadISeqFromFile(path);
        

        //*result = rb_funcall(seq, rb_intern("eval"), 0 );
        *result = rb_iseq_eval(seq);
        
        //rb_gc_enable();
#endif
        goto RCompExit;
    }

    RAWLOG_ERROR1("require_compiled: error: can not find %s", RSTRING_PTR(fname));
    retval = Qnil;

RCompExit:
    RHO_UNLOCK(require_lock);
    return retval;
}