Commit 4fe2ad4a authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add a WS_Printf() function and use it.

Other minor polish around workspaces.
parent 6df57da0
......@@ -1197,6 +1197,7 @@ char *WS_Alloc(struct ws *ws, unsigned bytes);
void *WS_Copy(struct ws *ws, const void *str, int len);
char *WS_Snapshot(struct ws *ws);
int WS_Overflowed(const struct ws *ws);
void *WS_Printf(struct ws *ws, const char *fmt, ...) __printflike(2, 3);
/* rfc2616.c */
void RFC2616_Ttl(struct busyobj *);
......
......@@ -718,20 +718,17 @@ static void
http_PutField(const struct http *to, int field, const char *string)
{
char *p;
unsigned l;
CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
l = strlen(string);
p = WS_Alloc(to->ws, l + 1);
p = WS_Copy(to->ws, string, -1);
if (p == NULL) {
VSLb(to->vsl, SLT_LostHeader, "%s", string);
to->hd[field].b = NULL;
to->hd[field].e = NULL;
to->hdf[field] = 0;
} else {
memcpy(p, string, l + 1L);
to->hd[field].b = p;
to->hd[field].e = p + l;
to->hd[field].e = strchr(p, '\0');
to->hdf[field] = 0;
http_VSLH(to, field);
}
......
......@@ -517,6 +517,7 @@ HTTP1_Write(const struct worker *w, const struct http *hp, int resp)
hp->hd[HTTP_HDR_STATUS].b = WS_Alloc(hp->ws, 4);
AN(hp->hd[HTTP_HDR_STATUS].b);
assert(hp->status >= 100 && hp->status <= 999);
sprintf(hp->hd[HTTP_HDR_STATUS].b, "%3d", hp->status);
hp->hd[HTTP_HDR_STATUS].e = hp->hd[HTTP_HDR_STATUS].b + 3;
......
......@@ -109,7 +109,10 @@ pan_ws(const struct ws *ws, int indent)
{
VSB_printf(pan_vsp, "%*sws = %p {", indent, "", ws);
if (VALID_OBJ(ws, WS_MAGIC)) {
if (!VALID_OBJ(ws, WS_MAGIC)) {
if (ws != NULL)
VSB_printf(pan_vsp, " BAD_MAGIC(0x%08x) ", ws->magic);
} else {
if (WS_Overflowed(ws))
VSB_printf(pan_vsp, " OVERFLOW");
VSB_printf(pan_vsp,
......@@ -128,8 +131,6 @@ pan_ws(const struct ws *ws, int indent)
VSB_printf(pan_vsp, ",+%ld", (long) (ws->e - ws->s));
else
VSB_printf(pan_vsp, ",%p", ws->e);
} else {
VSB_printf(pan_vsp, " BAD_MAGIC(0x%08x) ", ws->magic);
}
VSB_printf(pan_vsp, "},\n");
VSB_printf(pan_vsp, "%*s},\n", indent, "" );
......
......@@ -313,27 +313,17 @@ VRT_IP_string(const struct vrt_ctx *ctx, VCL_IP ip)
char *
VRT_INT_string(const struct vrt_ctx *ctx, long num)
{
char *p;
int size;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
size = snprintf(NULL, 0, "%ld", num) + 1;
AN(p = WS_Alloc(ctx->ws, size));
assert(snprintf(p, size, "%ld", num) < size);
return (p);
return (WS_Printf(ctx->ws, "%ld", num));
}
char *
VRT_REAL_string(const struct vrt_ctx *ctx, double num)
{
char *p;
int size;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
size = snprintf(NULL, 0, "%.3f", num) + 1;
AN(p = WS_Alloc(ctx->ws, size));
assert(snprintf(p, size, "%.3f", num) < size);
return (p);
return (WS_Printf(ctx->ws, "%.3f", num));
}
char *
......
......@@ -471,16 +471,12 @@ VRT_DO_EXP(beresp, ctx->bo->exp, keep, 0, ctx->bo->exp.t_origin,)
const char *
VRT_r_req_xid(const struct vrt_ctx *ctx)
{
char *p;
int size;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
// XXX ?
size = snprintf(NULL, 0, "%u", ctx->req->vsl->wid & VSL_IDENTMASK) + 1;
AN(p = WS_Alloc(ctx->req->http->ws, size));
assert(snprintf(p, size, "%u", ctx->req->vsl->wid & VSL_IDENTMASK) < size);
return (p);
return (WS_Printf(ctx->req->http->ws, "%u",
ctx->req->vsl->wid & VSL_IDENTMASK));
}
/*--------------------------------------------------------------------*/
......
......@@ -30,6 +30,9 @@
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include "cache.h"
void
......@@ -159,6 +162,28 @@ WS_Copy(struct ws *ws, const void *str, int len)
return (r);
}
void *
WS_Printf(struct ws *ws, const char *fmt, ...)
{
unsigned u, v;
va_list ap;
char *p;
WS_Assert(ws);
assert(ws->r == NULL);
u = WS_Reserve(ws, 0);
p = ws->f;
va_start(ap, fmt);
v = vsnprintf(p, u, fmt, ap);
if (v > u) {
WS_Release(ws, 0);
p = NULL;
} else {
WS_Release(ws, v);
}
return (p);
}
char *
WS_Snapshot(struct ws *ws)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment