static void calctime(const char *fname) { time_t t; struct tm *now; t = time(NULL); now = localtime(&t); if(!now) die("localtime():"); #define FTIME(s, fmt) \ if(!strftime(s, sizeof s, fmt, now)) \ die("strftime():"); FTIME(cpp_time, "\"%H:%M:%S\""); FTIME(cpp_date, "\"%b %d %G\""); if(fname){ struct stat st; if(stat(fname, &st) == 0){ now = localtime(&st.st_mtime); }else{ /* don't touch now - can't open fname, * should get an error later */ } }else{ /* stdin - don't touch 'now' */ } if(!strftime(cpp_timestamp, sizeof cpp_timestamp, "\"%a %b %d %H:%M:%S %Y\"", now)) die("strftime():"); }
static value cache_find( request_rec *r ) { cache *c = (cache*)local_get(cache_root); cache *prev = NULL; value fname = alloc_string(r->filename); while( c != NULL ) { if( val_compare(fname,c->file) == 0 ) { if( config.use_cache && FTIME(r) == c->time ) { c->hits++; return c->main; } if( prev == NULL ) local_set(cache_root,c->next); else prev->next = c->next; free_root((value*)c); // try to lower memory partitioning // when a module is updated c = NULL; gc_major(); break; } prev = c; c = c->next; } return NULL; }
static value init_module() { neko_vm *vm = neko_vm_current(); mcontext *ctx = CONTEXT(); value env = vm->env; ctx->main = NULL; val_call1(val_array_ptr(env)[0],val_array_ptr(env)[1]); cache_module(ctx->r->filename,FTIME(ctx->r),ctx->main); return val_null; }
/*! ************************************************************************************* * \brief System trace log output in Wels * * \param pCtx instance pointer * \param kiLevel log iLevel ( WELS_LOG_QUIET, ERROR, WARNING, INFO, DEBUG ) * \param kpFmtStr formated string to mount * \param argv pData string argument * * \return NONE * * \note N/A ************************************************************************************* */ void WelsLogDefault( void *pCtx, const int32_t kiLevel, const str_t *kpFmtStr, va_list argv ) { sWelsEncCtx *pEncCtx = (sWelsEncCtx *)pCtx; iWelsLogLevel iVal = (kiLevel & g_iLevelLog); if ( 0 == iVal || NULL == pEncCtx ) // such iLevel not enabled { return; } else { str_t pBuf[WELS_LOG_BUF_SIZE+1] = {0}; const int32_t kiBufSize = sizeof(pBuf) / sizeof(pBuf[0]) - 1; int32_t iCurUsed = 0; int32_t iBufUsed = 0; int32_t iBufLeft = kiBufSize - iBufUsed; if ( pEncCtx ){ time_t l_time; #if defined(WIN32) #if defined(_MSC_VER) #if _MSC_VER >= 1500 struct tm t_now; #else//VC6 struct tm* t_now; #endif//_MSC_VER >= 1500 #endif//_MSC_VER #else//__GNUC__ struct tm* t_now; #endif//WIN32 #if defined( WIN32 ) struct _timeb tb; time(&l_time); #ifdef _MSC_VER #if _MSC_VER >= 1500 LOCALTIME(&t_now, &l_time); #else t_now = LOCALTIME(&l_time); if ( NULL == t_now ) { return; } #endif//_MSC_VER >= 1500 #endif//_MSC_VER FTIME(&tb); #elif defined( __GNUC__ ) struct timeval tv; time(&l_time); t_now = (struct tm *)LOCALTIME(&l_time); gettimeofday(&tv,NULL); #endif//WIN32 if (iBufLeft > 0){ #ifdef _MSC_VER #if _MSC_VER >= 1500 iCurUsed = SNPRINTF( &pBuf[iBufUsed], iBufLeft, iBufLeft, "[0x%p @ ", pEncCtx ); // confirmed_safe_unsafe_usage #else iCurUsed = SNPRINTF( &pBuf[iBufUsed], iBufLeft, "[0x%p @ ", pEncCtx ); // confirmed_safe_unsafe_usage #endif//_MSC_VER >= 1500 #endif//_MSC_VER if (iCurUsed >= 0){ iBufUsed += iCurUsed; iBufLeft -= iCurUsed; } } else{ return; } if ( iBufLeft > 0 ){ iCurUsed = GetCodeName( &pBuf[iBufUsed], iBufLeft ); if ( iCurUsed > 0 ){ iBufUsed += iCurUsed; iBufLeft -= iCurUsed; } pBuf[iBufUsed] = ' '; ++ iBufUsed; -- iBufLeft; iCurUsed = GetLibName( &pBuf[iBufUsed], iBufLeft ); if ( iCurUsed > 0 ){ iBufUsed += iCurUsed; iBufLeft -= iCurUsed; } pBuf[iBufUsed] = ' '; ++ iBufUsed; -- iBufLeft; pBuf[iBufUsed] = 'v'; ++ iBufUsed; -- iBufLeft; iCurUsed = GetVerNum( &pBuf[iBufUsed], iBufLeft ); if ( iCurUsed > 0 ){ iBufUsed += iCurUsed; iBufLeft -= iCurUsed; } pBuf[iBufUsed] = ' '; ++ iBufUsed; -- iBufLeft; } if (iBufLeft > 0){ #if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER >= 1500) iCurUsed = strftime(&pBuf[iBufUsed], iBufLeft, "%y-%m-%d %H:%M:%S", &t_now); #else iCurUsed = strftime(&pBuf[iBufUsed], iBufLeft, "%y-%m-%d %H:%M:%S", t_now); #endif//WIN32.. if (iCurUsed > 0){ iBufUsed += iCurUsed; iBufLeft -= iCurUsed; } } else{ return; } if (iBufLeft > 0){ #if defined (WIN32) #ifdef _MSC_VER #if _MSC_VER >= 1500 iCurUsed = SNPRINTF(&pBuf[iBufUsed], iBufLeft, iBufLeft, ".%03.3u]: ", tb.millitm); // confirmed_safe_unsafe_usage #else iCurUsed = SNPRINTF(&pBuf[iBufUsed], iBufLeft, ".%3.3u]: ", tb.millitm); // confirmed_safe_unsafe_usage #endif//_MSC_VER >= 1500 #endif//_MSC_VER #elif defined (__GNUC__) iCurUsed = SNPRINTF(&pBuf[iBufUsed], iBufLeft, ".%3.3u]: ", tv.tv_usec/1000); // confirmed_safe_unsafe_usage #endif//WIN32 if (iCurUsed >= 0){ iBufUsed += iCurUsed; iBufLeft -= iCurUsed; } } else{ return; } } // fixed stack corruption issue on vs2008 if ( iBufLeft > 0 ){ int32_t i_shift = 0; str_t *pStr = NULL; pStr = GetLogTag( kiLevel, &i_shift ); if ( NULL != pCtx){ int32_t iLenTag = STRNLEN( pStr, 8 ); // confirmed_safe_unsafe_usage STRCAT( &pBuf[iBufUsed], iBufLeft, pStr ); // confirmed_safe_unsafe_usage iBufUsed += iLenTag; pBuf[iBufUsed] = ' '; iBufUsed++; ++iLenTag; iBufLeft -= iLenTag; } } if (iBufLeft > 0){ #if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER >= 1500) int32_t len = 0; len = _vscprintf( kpFmtStr, argv ) // _vscprintf doesn't count + 1; // terminating '\0' iCurUsed = VSPRINTF(&pBuf[iBufUsed], len, kpFmtStr, argv); // confirmed_safe_unsafe_usage #else iCurUsed = VSPRINTF(&pBuf[iBufUsed], kpFmtStr, argv); // confirmed_safe_unsafe_usage #endif//WIN32.. if (iCurUsed > 0){ iBufUsed += iCurUsed; iBufLeft -= iCurUsed; } } #ifdef ENABLE_TRACE_FILE if (NULL != pEncCtx && NULL != pEncCtx->pFileLog){ if ( pEncCtx->uiSizeLog > MAX_TRACE_LOG_SIZE){ if (0 == fseek(pEncCtx->pFileLog, 0L, SEEK_SET)) pEncCtx->uiSizeLog = 0; } if ( iBufUsed > 0 && iBufUsed < WELS_LOG_BUF_SIZE ) { iCurUsed = fwrite(pBuf, 1, iBufUsed, pEncCtx->pFileLog); fflush( pEncCtx->pFileLog ); if ( iCurUsed == iBufUsed ) pEncCtx->uiSizeLog += iBufUsed; } } else{ #if defined(WIN32) && defined(_DEBUG) OutputDebugStringA(pBuf); #endif } #endif//ENABLE_TRACE_FILE } }
static int neko_handler_rec( request_rec *r ) { mcontext ctx; neko_vm *vm; const char *ctype; value exc = NULL; /* Seems to crash on Windows. And on Linux, we rarely have libGC 7.x installed anyway # if defined(APACHE_2_X) || defined(NEKO_WINDOWS) // we are using threads, so let's make sure that the current thread is registered neko_thread_register(true); # endif */ config.hits++; ctx.r = r; ctx.main = cache_find(r); ctx.post_data = val_null; ctx.headers_sent = false; ctx.content_type = alloc_string("text/html"); r->content_type = val_string(ctx.content_type); if( ap_setup_client_block(r,REQUEST_CHUNKED_ERROR) != 0 ) { send_headers(&ctx); apache_error(APLOG_WARNING,r,"ap_setup_client_block failed"); return OK; } ctype = ap_table_get(r->headers_in,"Content-Type"); if( (!ctype || strstr(ctype,"multipart/form-data") == NULL) && ap_should_client_block(r) ) { # define MAXLEN 1024 char buf[MAXLEN]; int len; int tlen = 0; buffer b = alloc_buffer(NULL); while( (len = ap_get_client_block(r,buf,MAXLEN)) > 0 ) { if( tlen < config.max_post_size ) buffer_append_sub(b,buf,len); tlen += len; } if( tlen >= config.max_post_size ) { send_headers(&ctx); apache_error(APLOG_WARNING,r,"Maximum POST data exceeded. Try using multipart encoding"); return OK; } ctx.post_data = buffer_to_string(b); } vm = neko_vm_alloc(NULL); if( config.use_stats ) neko_vm_set_stats(vm,neko_stats_measure,config.use_prim_stats?neko_stats_measure:NULL); neko_vm_set_custom(vm,k_mod_neko,&ctx); if( config.use_jit && !neko_vm_jit(vm,1) ) { send_headers(&ctx); apache_error(APLOG_WARNING,r,"JIT required by env. var but not enabled in NekoVM"); return OK; } neko_vm_redirect(vm,request_print,&ctx); neko_vm_select(vm); if( ctx.main != NULL ) { value old = ctx.main; if( config.use_stats ) neko_stats_measure(vm,r->filename,1); val_callEx(val_null,old,NULL,0,&exc); if( config.use_stats ) neko_stats_measure(vm,r->filename,0); if( old != ctx.main ) cache_module(r->filename,FTIME(r),ctx.main); } else { char *base_uri = request_base_uri(r); value mload = neko_default_loader(&base_uri,1); value args[] = { alloc_string(r->filename), mload }; char *p = strrchr(val_string(args[0]),'.'); if( p != NULL ) *p = 0; val_callEx(mload,val_field(mload,val_id("loadmodule")),args,2,&exc); if( ctx.main != NULL && config.use_cache ) cache_module(r->filename,FTIME(r),ctx.main); } if( exc != NULL ) { buffer b = alloc_buffer(NULL); value v; int i; const char *p, *start; value st = neko_exc_stack(vm); val_buffer(b,exc); config.exceptions++; ap_soft_timeout("Client Timeout",r); send_headers(&ctx); v = buffer_to_string(b); p = val_string(v); start = p; ap_rprintf(r,"Uncaught exception - "); while( *p ) { if( *p == '<' || *p == '>' ) { ap_rwrite(start,(int)(p - start),r); ap_rwrite((*p == '<')?"<":">",4, r); start = p + 1; } p++; } ap_rwrite(start,(int)(p - start),r); ap_rprintf(r,"<br/><br/>"); for(i=0;i<val_array_size(st);i++) { value s = val_array_ptr(st)[i]; if( val_is_null(s) ) ap_rprintf(r,"Called from a C function<br/>"); else if( val_is_string(s) ) { ap_rprintf(r,"Called from %s (no debug available)<br/>",val_string(s)); } else if( val_is_array(s) && val_array_size(s) == 2 && val_is_string(val_array_ptr(s)[0]) && val_is_int(val_array_ptr(s)[1]) ) ap_rprintf(r,"Called from %s line %d<br/>",val_string(val_array_ptr(s)[0]),val_int(val_array_ptr(s)[1])); else { b = alloc_buffer(NULL); val_buffer(b,s); ap_rprintf(r,"Called from %s<br/>",val_string(buffer_to_string(b))); } } ap_kill_timeout(r); return OK; } send_headers(&ctx); return OK; }