Пример #1
0
static void report_error(const char *s, CFStreamError *e)
{
    if (e->domain == kCFStreamErrorDomainPOSIX)
        RAWLOG_ERROR2("%s, errno: %d", s, e->error);
    else if (e->domain == kCFStreamErrorDomainMacOSStatus)
        RAWLOG_ERROR2("%s, Mac OS status: %d", s, e->error);
    else
        RAWLOG_ERROR1("%s, unknown error", s);
}
Пример #2
0
int CURLNetRequest::getResponseCode(CURLcode err, char const *body, size_t bodysize, IRhoSession* oSession )	
{    
    //if (err != CURLE_OK)
    //    return -1;

    if (!body) {
        body = "";
        bodysize = 0;
    }
	
    long statusCode = 0;
    CURL *curl = m_curl.curl();
    if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &statusCode) != 0)
        statusCode = 500;
    
	if (statusCode == 416) {
		statusCode = 206;
	}
	
    if (statusCode >= 400) {
        RAWLOG_ERROR2("Request failed. HTTP Code: %d returned. HTTP Response: %s",
                      (int)statusCode, body);
        if (statusCode == 401)
            if (oSession)
                oSession->logout();
    }
    else {
        RAWTRACE1("RESPONSE----- (%d bytes)", bodysize);
        RAWTRACE(body);
        RAWTRACE("END RESPONSE-----");
    }

    return (int)statusCode;
}
Пример #3
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;
}
Пример #4
0
CURLcode CURLNetRequest::CURLHolder::perform()
{
    activate();
    
    int const CHUNK = 1;
    
    long noactivity = 0;
    
    CURLcode result;
    for(;;) {
        int running;
        CURLMcode err = curl_multi_perform(m_curlm, &running);
        if (err == CURLM_CALL_MULTI_PERFORM)
            continue;
        if (err != CURLM_OK) {
            RAWLOG_ERROR1("curl_multi_perform error: %d", (int)err);
        }
        else {
            if (running > 0 && noactivity < timeout) {
                RAWTRACE("we still have active transfers but no data ready at this moment; waiting...");
                fd_set rfd, wfd, efd;
                int n = 0;
                FD_ZERO(&rfd);
                FD_ZERO(&wfd);
                FD_ZERO(&efd);
                err = curl_multi_fdset(m_curlm, &rfd, &wfd, &efd, &n);
                if (err == CURLM_OK) {
                    if (n > 0) {
                        timeval tv;
                        tv.tv_sec = CHUNK;
                        tv.tv_usec = 0;
                        int e = select(n + 1, &rfd, &wfd, &efd, &tv);
                        if (e < 0) {
                            RAWLOG_ERROR1("select (on curl handles) error: %d", errno);
                        }
                        else {
                            if (e == 0) {
                                RAWTRACE("No activity on sockets, check them again");
                                noactivity += CHUNK;
                            }
                            else noactivity = 0;
                            continue;
                        }
                    }
                }
                else {
                    RAWLOG_ERROR1("curl_multi_fdset error: %d", (int)err);
                }

            }
        }
        int nmsgs;
        CURLMsg *msg = curl_multi_info_read(m_curlm, &nmsgs);
        result = CURLE_OK;
        if (msg && msg->msg == CURLMSG_DONE)
            result = msg->data.result;
        if (result == CURLE_OK && noactivity >= timeout)
            result = CURLE_OPERATION_TIMEDOUT;
        if (result == CURLE_OK || result == CURLE_PARTIAL_FILE)
            RAWTRACE("Operation completed successfully");
        else
            RAWLOG_ERROR2("Operation finished with error %d: %s", (int)result, curl_easy_strerror(result));
        break;
    }

    deactivate();
    return result;
}
Пример #5
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;
}
Пример #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
JNIEnv* MethodResultJni::jniInit(JNIEnv *env)
{
    RAWTRACE3("%s - env: 0x%.8x, functions: 0x%.8x", __FUNCTION__, env, env->functions);
    if(env && !s_methodResultClass)
    {
        String exceptionMessage;
        s_methodResultClass = getJNIClass(RHODES_JAVA_CLASS_METHODRESULT);
        if(!s_methodResultClass)
        {
            RAWLOG_ERROR1("Failed to find java class: %s", METHOD_RESULT_CLASS);
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("<init>");

        s_midMethodResult = env->GetMethodID(s_methodResultClass, "<init>", "(Z)V");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get %s constructor: $s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("getJson");
        s_midGetJson = env->GetMethodID(s_methodResultClass, "getJson", "()Ljava/lang/String;");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get method %s.getJson: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("getResultType");
        s_midGetResultType = env->GetMethodID(s_methodResultClass, "getResultType", "()I");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get method %s.getResultType: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("reset");
        s_midReset = env->GetMethodID(s_methodResultClass, "reset", "()V");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get method %s.reset: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mBooleanResult");
        s_fidBoolean = env->GetFieldID(s_methodResultClass, "mBooleanResult", "Z");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get field %s.mBooleanResult: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mIntegerResult");
        s_fidInteger = env->GetFieldID(s_methodResultClass, "mIntegerResult", "I");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get field %s.mIntegerResult: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mDoubleResult");
        s_fidDouble = env->GetFieldID(s_methodResultClass, "mDoubleResult", "D");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get fiekd %s.mDoubleResult: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mStrResult");
        s_fidString = env->GetFieldID(s_methodResultClass, "mStrResult", "Ljava/lang/String;");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get field %s.mStrResult: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mListResult");
        s_fidList = env->GetFieldID(s_methodResultClass, "mListResult", "Ljava/util/List;");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get field %s.mListResult: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mMapResult");
        s_fidMap = env->GetFieldID(s_methodResultClass, "mMapResult", "Ljava/util/Map;");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get field %s.mMapResult: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mStrCallback");
        s_fidStrCallback = env->GetFieldID(s_methodResultClass, "mStrCallback", "Ljava/lang/String;");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get field %s.mStrCallback: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mStrCallbackData");
        s_fidStrCallbackData = env->GetFieldID(s_methodResultClass, "mStrCallbackData", "Ljava/lang/String;");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get %s.mStrCallbackData field: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mResultParamName");
        s_fidResultParamName = env->GetFieldID(s_methodResultClass, "mResultParamName", "Ljava/lang/String;");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get %s.mResultParamName field: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mRubyProcCallback");
        s_fidRubyProcCallback = env->GetFieldID(s_methodResultClass, "mRubyProcCallback", "J");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get field  %s.mRubyProcCallback: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mTabId");
        s_fidTabId = env->GetFieldID(s_methodResultClass, "mTabId", "I");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get field  %s.mTabId: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mObjectClassPath");
        s_fidObjectClassPath = env->GetFieldID(s_methodResultClass, "mObjectClassPath", "Ljava/lang/String;");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get field %s.mObjectClassPath: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE("mRubyObjectClass");
        s_fidRubyObjectClass = env->GetFieldID(s_methodResultClass, "mRubyObjectClass", "J");
        if(env->ExceptionCheck() == JNI_TRUE)
        {
            RAWLOG_ERROR2("Failed to get field %s.mRubyObjectClass: %s", METHOD_RESULT_CLASS, clearException(env).c_str());
            s_methodResultClass = 0;
            return NULL;
        }
        RAWTRACE(__FUNCTION__);

    }
    return env;
}