From: rousskov <> Date: Sat, 6 Jun 1998 03:21:16 +0000 (+0000) Subject: - changed internal structure of HttpBody to use MemBuf; X-Git-Tag: SQUID_3_0_PRE1~3166 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1d21d91da41c51042b444b98958cb4a357a92869;p=thirdparty%2Fsquid.git - changed internal structure of HttpBody to use MemBuf; no more inconsistencies with body.size - removed HTML tags from mgr:http_headers dump --- diff --git a/src/HttpBody.cc b/src/HttpBody.cc index 340817192a..47f4298c19 100644 --- a/src/HttpBody.cc +++ b/src/HttpBody.cc @@ -2,7 +2,7 @@ /* - * $Id: HttpBody.cc,v 1.11 1998/05/27 22:51:39 rousskov Exp $ + * $Id: HttpBody.cc,v 1.12 1998/06/05 21:21:16 rousskov Exp $ * * DEBUG: section 56 HTTP Message Body * AUTHOR: Alex Rousskov @@ -34,63 +34,41 @@ #include "squid.h" -/* local constants */ - -/* local routines */ - - void httpBodyInit(HttpBody * body) { - body->buf = NULL; - body->size = 0; - body->freefunc = NULL; + body->mb = MemBufNull; } void httpBodyClean(HttpBody * body) { assert(body); - if (body->buf) { - assert(body->freefunc); - (*body->freefunc) (body->buf); - } - body->buf = NULL; - body->size = 0; + if (!memBufIsNull(&body->mb)) + memBufClean(&body->mb); } -/* set body, if freefunc is NULL the content will be copied, otherwise not */ +/* set body by absorbing mb */ void -httpBodySet(HttpBody * body, const char *buf, int size, FREE * freefunc) +httpBodySet(HttpBody * body, MemBuf *mb) { assert(body); - assert(!body->buf); - assert(buf); - assert(size); - assert(buf[size - 1] == '\0'); /* paranoid */ - if (!freefunc) { /* they want us to make our own copy */ - body->buf = xmalloc(size); - xmemcpy(body->buf, buf, size); - freefunc = &xfree; - } else { - /* @?@ @?@ Fix this cast: we should probably have two httpBodySet()s */ - body->buf = (char *) buf; - } - body->freefunc = freefunc; - body->size = size; + assert(memBufIsNull(&body->mb)); + body->mb = *mb; /* absorb */ } void httpBodyPackInto(const HttpBody * body, Packer * p) { assert(body && p); - /* assume it was a 0-terminated buffer */ - if (body->size) - packerAppend(p, body->buf, body->size - 1); + if (body->mb.size) + packerAppend(p, body->mb.buf, body->mb.size); } +#if UNUSED_CODE const char * httpBodyPtr(const HttpBody * body) { - return body->buf ? body->buf : ""; + return body->mb.buf ? body->mb.buf : ""; } +#endif diff --git a/src/HttpHeader.cc b/src/HttpHeader.cc index 4feb615f7a..d27ba9b71e 100644 --- a/src/HttpHeader.cc +++ b/src/HttpHeader.cc @@ -1,6 +1,6 @@ /* - * $Id: HttpHeader.cc,v 1.44 1998/06/05 19:45:17 rousskov Exp $ + * $Id: HttpHeader.cc,v 1.45 1998/06/05 21:21:17 rousskov Exp $ * * DEBUG: section 55 HTTP Header * AUTHOR: Alex Rousskov @@ -969,9 +969,8 @@ httpHeaderFieldStatDumper(StoreEntry * sentry, int idx, double val, double size, if (!visible && valid_id && dump_stat->owner_mask) visible = CBIT_TEST(*dump_stat->owner_mask, id); if (visible) - storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\t %d\n", - id, name, count, xdiv(count, dump_stat->busyDestroyedCount), - visible); + storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n", + id, name, count, xdiv(count, dump_stat->busyDestroyedCount)); } static void @@ -990,16 +989,16 @@ httpHeaderStatDump(const HttpHeaderStat * hs, StoreEntry * e) assert(hs && e); dump_stat = hs; - storeAppendPrintf(e, "\n

Header Stats: %s

\n", hs->label); - storeAppendPrintf(e, "

Field type distribution

\n"); + storeAppendPrintf(e, "\nHeader Stats: %s\n", hs->label); + storeAppendPrintf(e, "\nField type distribution\n"); storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n", "id", "name", "count", "#/header"); statHistDump(&hs->fieldTypeDistr, e, httpHeaderFieldStatDumper); - storeAppendPrintf(e, "

Cache-control directives distribution

\n"); + storeAppendPrintf(e, "Cache-control directives distribution\n"); storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\n", "id", "name", "count", "#/cc_field"); statHistDump(&hs->ccTypeDistr, e, httpHdrCcStatDumper); - storeAppendPrintf(e, "

Number of fields per header distribution

\n"); + storeAppendPrintf(e, "Number of fields per header distribution\n"); storeAppendPrintf(e, "%2s\t %-5s\t %5s\t %6s\n", "id", "#flds", "count", "%total"); statHistDump(&hs->hdrUCountDistr, e, httpHeaderFldsPerHdrDumper); @@ -1026,8 +1025,8 @@ httpHeaderStoreReport(StoreEntry * e) httpHeaderStatDump(HttpHeaderStats + i, e); storeAppendPrintf(e, "%s\n", "
"); } - /* field stats */ - storeAppendPrintf(e, "

Http Fields Stats (replies and requests)

\n"); + /* field stats for all messages */ + storeAppendPrintf(e, "\nHttp Fields Stats (replies and requests)\n"); storeAppendPrintf(e, "%2s\t %-20s\t %5s\t %6s\t %6s\n", "id", "name", "#alive", "%err", "%repeat"); for (ht = 0; ht < HDR_ENUM_END; ht++) { diff --git a/src/MemBuf.cc b/src/MemBuf.cc index c63690c452..2771851849 100644 --- a/src/MemBuf.cc +++ b/src/MemBuf.cc @@ -1,5 +1,5 @@ /* - * $Id: MemBuf.cc,v 1.14 1998/06/03 15:52:16 rousskov Exp $ + * $Id: MemBuf.cc,v 1.15 1998/06/05 21:21:18 rousskov Exp $ * * DEBUG: section 59 auto-growing Memory Buffer with printf * AUTHOR: Alex Rousskov @@ -155,11 +155,9 @@ memBufReset(MemBuf * mb) { assert(mb); - if (!mb->buf && !mb->max_capacity && !mb->capacity && !mb->size) { - /* Null */ + if (memBufIsNull(mb)) { memBufDefInit(mb); } else { - assert(mb->buf); assert(mb->freefunc); /* not frozen */ /* reset */ memset(mb->buf, 0, mb->capacity); @@ -167,6 +165,18 @@ memBufReset(MemBuf * mb) } } +/* unfirtunate hack to test if the buffer has been Init()ialized */ +int +memBufIsNull(MemBuf * mb) +{ + assert(mb); + if (!mb->buf && !mb->max_capacity && !mb->capacity && !mb->size) + return 1; /* null, not initialized */ + assert(mb->buf && mb->max_capacity && mb->capacity); /* paranoid */ + return 0; +} + + /* calls memcpy, appends exactly size bytes, extends buffer if needed */ void memBufAppend(MemBuf * mb, const char *buf, mb_size_t sz) diff --git a/src/errorpage.cc b/src/errorpage.cc index 6da39703f5..22f41136b7 100644 --- a/src/errorpage.cc +++ b/src/errorpage.cc @@ -1,6 +1,6 @@ /* - * $Id: errorpage.cc,v 1.137 1998/06/02 21:50:21 rousskov Exp $ + * $Id: errorpage.cc,v 1.138 1998/06/05 21:21:19 rousskov Exp $ * * DEBUG: section 4 Error Generation * AUTHOR: Duane Wessels @@ -527,8 +527,8 @@ errorBuildReply(ErrorState * err) */ httpHeaderPutStrf(&rep->header, HDR_X_SQUID_ERROR, "%s %d", err_type_str[err->page_id], err->xerrno); - httpBodySet(&rep->body, content.buf, content.size + 1, NULL); - memBufClean(&content); + httpBodySet(&rep->body, &content); + /* do not memBufClean() the content, it was absorbed by httpBody */ return rep; } diff --git a/src/protos.h b/src/protos.h index 751c0e7047..ed96fc715d 100644 --- a/src/protos.h +++ b/src/protos.h @@ -265,9 +265,8 @@ extern void httpBodyInit(HttpBody * body); extern void httpBodyClean(HttpBody * body); /* get body ptr (always use this) */ extern const char *httpBodyPtr(const HttpBody * body); -/* set body, if freefunc is NULL the content will be copied, otherwise not */ -extern void httpBodySet(HttpBody * body, const char *content, int size, - FREE * freefunc); +/* set body, does not clone mb so you should not reuse it */ +extern void httpBodySet(HttpBody * body, MemBuf *mb); /* pack */ extern void httpBodyPackInto(const HttpBody * body, Packer * p); @@ -490,6 +489,8 @@ extern void memBufDefInit(MemBuf * mb); extern void memBufClean(MemBuf * mb); /* resets mb preserving (or initializing if needed) memory buffer */ extern void memBufReset(MemBuf * mb); +/* unfirtunate hack to test if the buffer has been Init()ialized */ +extern int memBufIsNull(MemBuf * mb); /* calls memcpy, appends exactly size bytes, extends buffer if needed */ extern void memBufAppend(MemBuf * mb, const char *buf, mb_size_t size); /* calls snprintf, extends buffer if needed */ diff --git a/src/structs.h b/src/structs.h index 45e1208228..66962257e6 100644 --- a/src/structs.h +++ b/src/structs.h @@ -483,6 +483,27 @@ struct _hash_table { int count; }; +/* auto-growing memory-resident buffer with printf interface */ +/* note: when updating this struct, update MemBufNULL #define */ +struct _MemBuf { + /* public, read-only */ + char *buf; + mb_size_t size; /* used space, does not count 0-terminator */ + + /* private, stay away; use interface function instead */ + mb_size_t max_capacity; /* when grows: assert(new_capacity <= max_capacity) */ + mb_size_t capacity; /* allocated space */ + FREE *freefunc; /* what to use to free the buffer, NULL after memBufFreeFunc() is called */ +}; + +/* see Packer.c for description */ +struct _Packer { + /* protected, use interface functions instead */ + append_f append; + vprintf_f vprintf; + void *real_handler; /* first parameter to real append and vprintf */ +}; + /* http status line */ struct _HttpStatusLine { /* public, read only */ @@ -492,14 +513,12 @@ struct _HttpStatusLine { }; /* - * Note: HttpBody is used only for messages with a small text content that is + * Note: HttpBody is used only for messages with a small content that is * known a priory (e.g., error messages). */ struct _HttpBody { - /* private, never dereference these */ - char *buf; /* null terminated _text_ buffer, not for binary stuff */ - FREE *freefunc; /* used to free() .buf */ - int size; + /* private */ + MemBuf mb; }; /* http header extention field */ @@ -998,28 +1017,6 @@ struct _iostats { } Http, Ftp, Gopher, Wais; }; -/* auto-growing memory-resident buffer with printf interface */ -/* note: when updating this struct, update MemBufNULL #define */ -struct _MemBuf { - /* public, read-only */ - char *buf; - mb_size_t size; /* used space, does not count 0-terminator */ - - /* private, stay away; use interface function instead */ - mb_size_t max_capacity; /* when grows: assert(new_capacity <= max_capacity) */ - mb_size_t capacity; /* allocated space */ - FREE *freefunc; /* what to use to free the buffer, NULL after memBufFreeFunc() is called */ -}; - -/* see Packer.c for description */ -struct _Packer { - /* protected, use interface functions instead */ - append_f append; - vprintf_f vprintf; - void *real_handler; /* first parameter to real append and vprintf */ -}; - - struct _mem_node { char *data; int len;