int mprPuts(MprFile *file, const char *writeBuf, uint count) { MprBuf *bp; char *buf; int total, bytes, len; mprAssert(file); /* * Buffer output and flush when full. */ if (file->buf == 0) { file->buf = mprCreateBuf(file, MPR_BUFSIZE, 0); if (file->buf == 0) { return MPR_ERR_CANT_ALLOCATE; } } bp = file->buf; if (mprGetBufLength(bp) > 0 && mprGetBufSpace(bp) < (int) count) { len = mprGetBufLength(bp); if (mprWrite(file, mprGetBufStart(bp), len) != len) { return MPR_ERR_CANT_WRITE; } mprFlushBuf(bp); } total = 0; buf = (char*) writeBuf; while (count > 0) { bytes = mprPutBlockToBuf(bp, buf, count); if (bytes <= 0) { return MPR_ERR_CANT_ALLOCATE; } count -= bytes; buf += bytes; total += bytes; mprAddNullToBuf(bp); if (count > 0) { len = mprGetBufLength(bp); if (mprWrite(file, mprGetBufStart(bp), len) != len) { return MPR_ERR_CANT_WRITE; } mprFlushBuf(bp); } } return total; }
int maWriteUserFile(MaServer *server, MaAuth *auth, char *path) { MprFile *file; MprHash *hp; MaUser *up; char buf[MA_MAX_PASS * 2]; char *tempFile; tempFile = mprGetTempPath(auth, NULL); if ((file = mprOpen(auth, tempFile, O_CREAT | O_TRUNC | O_WRONLY | O_TEXT, 0444)) == 0) { mprError(server, "Can't open %s", tempFile); mprFree(tempFile); return MPR_ERR_CANT_OPEN; } mprFree(tempFile); hp = mprGetNextHash(auth->users, 0); while (hp) { up = (MaUser*) hp->data; mprSprintf(buf, sizeof(buf), "%d: %s: %s: %s\n", up->enabled, up->name, up->realm, up->password); mprWrite(file, buf, (int) strlen(buf)); hp = mprGetNextHash(auth->users, hp); } mprFree(file); unlink(path); if (rename(tempFile, path) < 0) { mprError(server, "Can't create new %s", path); return MPR_ERR_CANT_WRITE; } return 0; }
void maWriteAccessLogEntry(MaHost *host, cchar *buf, int len) { static int once = 0; if (mprWrite(host->accessLog, (char*) buf, len) != len && once++ == 0) { mprError(host, "Can't write to access log %s", host->logPath); } }
int maWriteGroupFile(MaServer *server, MaAuth *auth, char *path) { MprHash *hp; MprFile *file; MaGroup *gp; char buf[MPR_MAX_STRING], *tempFile, *name; int next; tempFile = mprGetTempPath(server, NULL); if ((file = mprOpen(auth, tempFile, O_CREAT | O_TRUNC | O_WRONLY | O_TEXT, 0444)) == 0) { mprError(server, "Can't open %s", tempFile); mprFree(tempFile); return MPR_ERR_CANT_OPEN; } mprFree(tempFile); hp = mprGetNextHash(auth->groups, 0); while (hp) { gp = (MaGroup*) hp->data; mprSprintf(buf, sizeof(buf), "%d: %x: %s: ", gp->enabled, gp->acl, gp->name); mprWrite(file, buf, (int) strlen(buf)); for (next = 0; (name = mprGetNextItem(gp->users, &next)) != 0; ) { mprWrite(file, name, (int) strlen(name)); } mprWrite(file, "\n", 1); hp = mprGetNextHash(auth->groups, hp); } mprFree(file); unlink(path); if (rename(tempFile, path) < 0) { mprError(server, "Can't create new %s", path); return MPR_ERR_CANT_WRITE; } return 0; }
int mprStaticPrintf(MprCtx ctx, const char *fmt, ...) { va_list ap; char buf[MPR_MAX_STRING]; char *bufp; int len; MprApp *app; app = mprGetApp(ctx); va_start(ap, fmt); bufp = buf; len = mprSprintfCore(MPR_LOC_ARGS(0), &bufp, MPR_MAX_STRING, fmt, ap); va_end(ap); if (len >= 0) { len = mprWrite(app->console, buf, len); } return len; }
int mprFprintf(MprFile *file, const char *fmt, ...) { va_list ap; char *buf; int len; if (file == 0) { return MPR_ERR_BAD_HANDLE; } va_start(ap, fmt); len = mprAllocVsprintf(MPR_LOC_ARGS(file), &buf, 0, fmt, ap); va_end(ap); if (len >= 0) { len = mprWrite(file, buf, len); } mprFree(buf); return len; }
int mprErrorPrintf(MprCtx ctx, const char *fmt, ...) { va_list ap; char *buf; int len; MprApp *app; /* No asserts here as this is used as part of assert reporting */ app = mprGetApp(ctx); va_start(ap, fmt); len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, ap); va_end(ap); if (len >= 0 && app->error) { len = mprWrite(app->error, buf, len); } mprFree(buf); return len; }
void maLogRequest(MaConn *conn) { MaHost *logHost, *host; MaResponse *resp; MaRequest *req; MprBuf *buf; char keyBuf[80], *timeText, *fmt, *cp, *qualifier, *value, c; int len; resp = conn->response; req = conn->request; host = req->host; logHost = host->logHost; if (logHost == 0) { return; } fmt = logHost->logFormat; if (fmt == 0) { return; } if (req->method == 0) { return; } len = MPR_MAX_URL + 256; buf = mprCreateBuf(resp, len, len); while ((c = *fmt++) != '\0') { if (c != '%' || (c = *fmt++) == '%') { mprPutCharToBuf(buf, c); continue; } switch (c) { case 'a': /* Remote IP */ mprPutStringToBuf(buf, conn->remoteIpAddr); break; case 'A': /* Local IP */ mprPutStringToBuf(buf, conn->sock->listenSock->ipAddr); break; case 'b': if (resp->bytesWritten == 0) { mprPutCharToBuf(buf, '-'); } else { mprPutIntToBuf(buf, resp->bytesWritten); } break; case 'B': /* Bytes written (minus headers) */ mprPutIntToBuf(buf, resp->bytesWritten - resp->headerSize); break; case 'h': /* Remote host */ mprPutStringToBuf(buf, conn->remoteIpAddr); break; case 'n': /* Local host */ mprPutStringToBuf(buf, req->parsedUri->host); break; case 'l': /* Supplied in authorization */ mprPutStringToBuf(buf, req->user ? req->user : "******"); break; case 'O': /* Bytes written (including headers) */ mprPutIntToBuf(buf, resp->bytesWritten); break; case 'r': /* First line of request */ mprPutFmtToBuf(buf, "%s %s %s", req->methodName, req->parsedUri->originalUri, req->httpProtocol); break; case 's': /* Response code */ mprPutIntToBuf(buf, resp->code); break; case 't': /* Time */ mprPutCharToBuf(buf, '['); timeText = mprFormatLocalTime(conn, mprGetTime(conn)); mprPutStringToBuf(buf, timeText); mprFree(timeText); mprPutCharToBuf(buf, ']'); break; case 'u': /* Remote username */ mprPutStringToBuf(buf, req->user ? req->user : "******"); break; case '{': /* Header line */ qualifier = fmt; if ((cp = strchr(qualifier, '}')) != 0) { fmt = &cp[1]; *cp = '\0'; c = *fmt++; mprStrcpy(keyBuf, sizeof(keyBuf), "HTTP_"); mprStrcpy(&keyBuf[5], sizeof(keyBuf) - 5, qualifier); mprStrUpper(keyBuf); switch (c) { case 'i': value = (char*) mprLookupHash(req->headers, keyBuf); mprPutStringToBuf(buf, value ? value : "-"); break; default: mprPutStringToBuf(buf, qualifier); } *cp = '}'; } else { mprPutCharToBuf(buf, c); } break; case '>': if (*fmt == 's') { fmt++; mprPutIntToBuf(buf, resp->code); } break; default: mprPutCharToBuf(buf, c); break; } } mprPutCharToBuf(buf, '\n'); mprAddNullToBuf(buf); mprWrite(logHost->accessLog, mprGetBufStart(buf), mprGetBufLength(buf)); }