PUBLIC ssize httpWrite(HttpQueue *q, cchar *fmt, ...) { va_list vargs; char *buf; va_start(vargs, fmt); buf = sfmtv(fmt, vargs); va_end(vargs); return httpWriteString(q, buf); }
static void readyCacheHandler(HttpQueue *q) { HttpConn *conn; HttpTx *tx; cchar *data; conn = q->conn; tx = conn->tx; if (tx->cachedContent) { mprLog(3, "cacheHandler: write cached content for '%s'", conn->rx->uri); if ((data = setHeadersFromCache(conn, tx->cachedContent)) != 0) { tx->length = slen(data); httpWriteString(q, data); } } httpFinalize(conn); }
ssize httpWriteCached(HttpConn *conn) { MprTime modified; cchar *cacheKey, *data, *content; if (!conn->tx->cache) { return MPR_ERR_CANT_FIND; } cacheKey = makeCacheKey(conn); if ((content = mprReadCache(conn->host->responseCache, cacheKey, &modified, 0)) == 0) { mprLog(3, "No cached data for ", cacheKey); return 0; } mprLog(5, "Used cached ", cacheKey); data = setHeadersFromCache(conn, content); httpSetHeader(conn, "Etag", mprGetMD5(cacheKey)); httpSetHeader(conn, "Last-Modified", mprFormatUniversalTime(MPR_HTTP_DATE, modified)); conn->tx->cacheBuffer = 0; httpWriteString(conn->writeq, data); httpFinalize(conn); return slen(data); }
PUBLIC ssize httpWriteSafeString(HttpQueue *q, cchar *s) { return httpWriteString(q, mprEscapeHtml(s)); }
static ssize writeBody(HttpConn *conn, MprList *files) { MprFile *file; char buf[ME_MAX_BUFFER], *path, *pair; ssize bytes, len, count, nbytes, sofar; int next; if (app->upload) { if (httpWriteUploadData(conn, app->files, app->formData) < 0) { return MPR_ERR_CANT_WRITE; } } else { if (app->formData) { count = mprGetListLength(app->formData); for (next = 0; (pair = mprGetNextItem(app->formData, &next)) != 0; ) { len = strlen(pair); if (next < count) { len = slen(pair); if (httpWriteString(conn->writeq, pair) != len || httpWriteString(conn->writeq, "&") != 1) { return MPR_ERR_CANT_WRITE; } } else { if (httpWrite(conn->writeq, pair, len) != len) { return MPR_ERR_CANT_WRITE; } } } } if (files) { assert(mprGetListLength(files) == 1); for (next = 0; (path = mprGetNextItem(files, &next)) != 0; ) { if (strcmp(path, "-") == 0) { file = mprAttachFileFd(0, "stdin", O_RDONLY | O_BINARY); } else { file = mprOpenFile(path, O_RDONLY | O_BINARY, 0); } if (file == 0) { mprLog("error http", 0, "Cannot open \"%s\"", path); return MPR_ERR_CANT_OPEN; } app->inFile = file; if (app->verbose) { mprPrintf("uploading: %s\n", path); } while ((bytes = mprReadFile(file, buf, sizeof(buf))) > 0) { sofar = 0; while (bytes > 0) { if ((nbytes = httpWriteBlock(conn->writeq, &buf[sofar], bytes, HTTP_BLOCK)) < 0) { mprCloseFile(file); return MPR_ERR_CANT_WRITE; } bytes -= nbytes; sofar += nbytes; assert(bytes >= 0); } } httpFlushQueue(conn->writeq, HTTP_BLOCK); mprCloseFile(file); app->inFile = 0; } } if (app->bodyData) { len = mprGetBufLength(app->bodyData); if (httpWriteBlock(conn->writeq, mprGetBufStart(app->bodyData), len, HTTP_BLOCK) != len) { return MPR_ERR_CANT_WRITE; } } } return 0; }