// // Called to rotate the access log // void MaHost::rotateLog() { struct stat sbuf; char fileBuf[MPR_MAX_FNAME]; struct tm tm; time_t when; // // Rotate logs when full // if (fstat(logFd, &sbuf) == 0 && sbuf.st_mode & S_IFREG && (unsigned) sbuf.st_size > maxSize) { char bak[MPR_MAX_FNAME]; time(&when); mprGmtime(&when, &tm); mprSprintf(bak, sizeof(bak), "%s-%02d-%02d-%02d-%02d:%02d:%02d", logPath, tm->tm_mon, tm->tm_mday, tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec); close(logFd); rename(logPath, bak); unlink(logPath); logFd = open(logPath, O_CREAT | O_TRUNC | O_WRONLY | O_TEXT, 0664); logConfig(); } }
/* * Add a response cookie */ static void setCookie(void *handle, cchar *name, cchar *value, int lifetime, cchar *path, bool secure) { struct tm tm; time_t when; MprTime time; char dateStr[64]; if (path == 0) { path = "/"; } if (lifetime > 0) { time = mprGetTime(mpr); when = time + lifetime * MPR_TICKS_PER_SEC; mprGmtime(mpr, &tm, when); mprRfctime(mpr, dateStr, sizeof(dateStr), &tm); setHeader(handle, 1, "Set-Cookie", "%s=%s; path=%s; Expires=%s;%s", name, value, path, dateStr, secure ? " secure" : ""); } else { setHeader(handle, 1, "Set-Cookie", "%s=%s; path=%s;%s", name, value, path, secure ? " secure" : ""); } setHeader(handle, 0, "Cache-control", "no-cache=\"set-cookie\""); }
/* * Called to rotate the access log */ void maRotateAccessLog(MaHost *host) { MprFileInfo info; struct tm tm; MprTime when; char bak[MPR_MAX_FNAME]; /* * Rotate logs when full */ if (mprGetFileInfo(host, host->logPath, &info) == 0 && info.size > MA_MAX_ACCESS_LOG) { when = mprGetTime(host); mprGmtime(host, &tm, when); mprSprintf(bak, sizeof(bak), "%s-%02d-%02d-%02d-%02d:%02d:%02d", host->logPath, tm.tm_mon, tm.tm_mday, tm.tm_year, tm.tm_hour, tm.tm_min, tm.tm_sec); mprFree(host->accessLog); rename(host->logPath, bak); unlink(host->logPath); host->accessLog = mprOpen(host, host->logPath, O_CREAT | O_TRUNC | O_WRONLY | O_TEXT, 0664); } }
/* * Get a date string. If sbuf is non-null, get the modified time of that file. If null, then get the current system time. */ static char *getDateString(MprFileInfo *sbuf) { MprTime when; struct tm tm; char *dateStr; if (sbuf == 0) { when = mprGetTime(mpr); } else { when = (MprTime) sbuf->mtime * MPR_TICKS_PER_SEC; } dateStr = (char*) mprAlloc(mpr, 64); mprGmtime(mpr, &tm, when); mprStrftime(mpr, dateStr, 64, "%a, %d %b %Y %T %Z", &tm); return dateStr; }