From: rousskov <> Date: Sun, 31 May 1998 01:42:57 +0000 (+0000) Subject: - Replaced "complex" (offset accounting) calls to snprintf() with MemBuf. X-Git-Tag: SQUID_3_0_PRE1~3205 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=137ee196e3be76d5b96458b603e3d9aa78e314f1;p=thirdparty%2Fsquid.git - Replaced "complex" (offset accounting) calls to snprintf() with MemBuf. - cleanup --- diff --git a/ChangeLog b/ChangeLog index 57b3ee9c85..4c6520111c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -14,6 +14,9 @@ - Added while loop in icpHandleUdp(). - Fixed some small memory leaks. - Fixed single-bit-int flag checks (Henrik Nordstrom). + - Replaced "complex" (offset accounting) calls to snprintf with MemBuf + - Do not send only-if-cached cc directive with requests + for peer's digests. Changes to squid-1.2.beta21 (May 22, 1998): diff --git a/src/HttpHeaderTools.cc b/src/HttpHeaderTools.cc index a0c60611a7..3868e7ff9b 100644 --- a/src/HttpHeaderTools.cc +++ b/src/HttpHeaderTools.cc @@ -1,5 +1,5 @@ /* - * $Id: HttpHeaderTools.cc,v 1.15 1998/05/28 05:12:42 wessels Exp $ + * $Id: HttpHeaderTools.cc,v 1.16 1998/05/30 19:42:58 rousskov Exp $ * * DEBUG: section 66 HTTP Header Tools * AUTHOR: Alex Rousskov @@ -135,18 +135,11 @@ httpHeaderPutStrf(va_alist) static void httpHeaderPutStrvf(HttpHeader * hdr, http_hdr_type id, const char *fmt, va_list vargs) { -#if OLD_CODE - LOCAL_ARRAY(char, buf, 4096); - buf[0] = '\0'; - vsnprintf(buf, 4096, fmt, vargs); - httpHeaderPutStr(hdr, id, buf); -#else MemBuf mb; memBufDefInit(&mb); memBufVPrintf(&mb, fmt, vargs); httpHeaderPutStr(hdr, id, mb.buf); memBufClean(&mb); -#endif } diff --git a/src/MemBuf.cc b/src/MemBuf.cc index 92d0321f16..6df022420c 100644 --- a/src/MemBuf.cc +++ b/src/MemBuf.cc @@ -1,5 +1,5 @@ /* - * $Id: MemBuf.cc,v 1.11 1998/05/28 17:32:41 rousskov Exp $ + * $Id: MemBuf.cc,v 1.12 1998/05/30 19:42:59 rousskov Exp $ * * DEBUG: section 59 auto-growing Memory Buffer with printf * AUTHOR: Alex Rousskov @@ -147,6 +147,25 @@ memBufClean(MemBuf * mb) mb->size = mb->capacity = 0; } +/* cleans the buffer without changing its capacity + * if called with a Null buffer, calls memBufDefInit() */ +void +memBufReset(MemBuf * mb) +{ + assert(mb); + + if (!mb->buf && !mb->max_capacity && !mb->capacity && !mb->size) { + /* Null */ + memBufDefInit(mb); + } else { + assert(mb->buf); + assert(mb->freefunc); /* not frozen */ + /* reset */ + memset(mb->buf, 0, mb->capacity); + mb->size = 0; + } +} + /* calls memcpy, appends exactly size bytes, extends buffer if needed */ void memBufAppend(MemBuf * mb, const char *buf, mb_size_t sz) @@ -197,7 +216,6 @@ memBufVPrintf(MemBuf * mb, const char *fmt, va_list vargs) assert(mb && fmt); assert(mb->buf); assert(mb->freefunc); /* not frozen */ - /* @?@ we do not init buf with '\0', do we have to for vsnprintf?? @?@ */ /* assert in Grow should quit first, but we do not want to have a scary infinite loop */ while (mb->capacity <= mb->max_capacity) { mb_size_t free_space = mb->capacity - mb->size; @@ -206,17 +224,19 @@ memBufVPrintf(MemBuf * mb, const char *fmt, va_list vargs) /* check for possible overflow */ /* snprintf on Linuz returns -1 on overflows */ /* snprintf on FreeBSD returns at least free_space on overflows */ - if (sz < 0 || sz + 32 >= free_space) /* magic constant 32, ARGH! @?@ */ + if (sz < 0 || sz >= free_space) memBufGrow(mb, mb->capacity + 1); else break; } - /* snprintf on FreeBSD and linux do not count terminating '\0' as "character stored" */ - if (!sz || mb->buf[mb->size+sz-1]) - assert(!mb->buf[mb->size+sz]); - else - sz--; /* we cut 0-terminator as store does */ mb->size += sz; + /* on Linux and FreeBSD, '\0' is not counted in return value */ + /* on XXX it might be counted */ + /* check that '\0' is appended and not counted */ + if (!mb->size || mb->buf[mb->size-1]) + assert(!mb->buf[mb->size]); + else + mb->size--; } /* @@ -244,6 +264,8 @@ static void memBufGrow(MemBuf * mb, mb_size_t min_cap) { mb_size_t new_cap; + MemBuf old_mb; + assert(mb); assert(mb->capacity < min_cap); @@ -259,18 +281,48 @@ memBufGrow(MemBuf * mb, mb_size_t min_cap) if (new_cap > mb->max_capacity) new_cap = mb->max_capacity; - assert(new_cap <= mb->max_capacity); /* no overflow */ + assert(new_cap <= mb->max_capacity);/* no overflow */ assert(new_cap > mb->capacity); /* progress */ - /* finally [re]allocate memory */ - if (!mb->buf) { - mb->buf = xmalloc(new_cap); - mb->freefunc = &xfree; + old_mb = *mb; + + /* allocate new memory */ + switch (new_cap) { + case 2048: + mb->buf = memAllocate(MEM_2K_BUF); + mb->freefunc = &memFree2K; + break; + case 4096: + mb->buf = memAllocate(MEM_4K_BUF); + mb->freefunc = &memFree4K; + break; + case 8192: + mb->buf = memAllocate(MEM_8K_BUF); + mb->freefunc = &memFree8K; + break; + default: + /* recycle if old buffer was not "pool"ed */ + if (old_mb.freefunc == &xfree) { + mb->buf = xrealloc(old_mb.buf, new_cap); + old_mb.buf = NULL; + old_mb.freefunc = NULL; + /* init tail, just in case */ + memset(mb->buf + mb->size, 0, new_cap - mb->size); + } else { + mb->buf = xcalloc(1, new_cap); + mb->freefunc = &xfree; + } + } + + /* copy and free old buffer if needed */ + if (old_mb.buf && old_mb.freefunc) { + memcpy(mb->buf, old_mb.buf, old_mb.size); + (*old_mb.freefunc)(old_mb.buf); } else { - assert(mb->freefunc == &xfree); /* for now */ - mb->buf = xrealloc(mb->buf, new_cap); + assert(!old_mb.buf && !old_mb.freefunc); } - memset(mb->buf + mb->size, 0, new_cap - mb->size); /* just in case */ + + /* done */ mb->capacity = new_cap; } diff --git a/src/access_log.cc b/src/access_log.cc index e996739c29..f7f1c470f4 100644 --- a/src/access_log.cc +++ b/src/access_log.cc @@ -1,7 +1,7 @@ /* - * $Id: access_log.cc,v 1.31 1998/05/27 22:51:47 rousskov Exp $ + * $Id: access_log.cc,v 1.32 1998/05/30 19:43:00 rousskov Exp $ * * DEBUG: section 46 Access Log * AUTHOR: Duane Wessels @@ -35,8 +35,8 @@ static void accessLogOpen(const char *fname); static char *log_quote(const char *header); -static int accessLogSquid(AccessLogEntry * al); -static int accessLogCommon(AccessLogEntry * al); +static void accessLogSquid(AccessLogEntry * al, MemBuf *mb); +static void accessLogCommon(AccessLogEntry * al, MemBuf *mb); const char *log_tags[] = { @@ -83,7 +83,6 @@ static int LogfileStatus = LOG_DISABLE; static int LogfileFD = -1; static char LogfileName[SQUID_MAXPATHLEN]; #define LOG_BUF_SZ (MAX_URL<<2) -static char log_buf[LOG_BUF_SZ]; static const char c2x[] = "000102030405060708090a0b0c0d0e0f" @@ -154,16 +153,15 @@ log_quote(const char *header) return buf; } -static int -accessLogSquid(AccessLogEntry * al) +static void +accessLogSquid(AccessLogEntry * al, MemBuf *mb) { const char *client = NULL; if (Config.onoff.log_fqdn) client = fqdncache_gethostbyaddr(al->cache.caddr, 0); if (client == NULL) client = inet_ntoa(al->cache.caddr); - return snprintf(log_buf, LOG_BUF_SZ, - "%9d.%03d %6d %s %s/%03d %d %s %s %s %s%s/%s %s\n", + memBufPrintf(mb, "%9d.%03d %6d %s %s/%03d %d %s %s %s %s%s/%s %s", (int) current_time.tv_sec, (int) current_time.tv_usec / 1000, al->cache.msec, @@ -180,16 +178,15 @@ accessLogSquid(AccessLogEntry * al) al->http.content_type); } -static int -accessLogCommon(AccessLogEntry * al) +static void +accessLogCommon(AccessLogEntry * al, MemBuf *mb) { const char *client = NULL; if (Config.onoff.log_fqdn) client = fqdncache_gethostbyaddr(al->cache.caddr, 0); if (client == NULL) client = inet_ntoa(al->cache.caddr); - return snprintf(log_buf, LOG_BUF_SZ, - "%s %s - [%s] \"%s %s\" %d %d %s:%s\n", + memBufPrintf(mb, "%s %s - [%s] \"%s %s\" %d %d %s:%s", client, al->cache.ident, mkhttpdlogtime(&squid_curtime), @@ -217,9 +214,10 @@ accessLogOpen(const char *fname) void accessLogLog(AccessLogEntry * al) { - int l; + MemBuf mb; char *t; char *xbuf = NULL; + if (LogfileStatus != LOG_ENABLE) return; if (al->url == NULL) @@ -244,28 +242,23 @@ accessLogLog(AccessLogEntry * al) al->private.method_str = RequestMethodStr[al->http.method]; if (al->hier.host[0] == '\0') xstrncpy(al->hier.host, dash_str, SQUIDHOSTNAMELEN); + + memBufDefInit(&mb); + if (Config.onoff.common_log) - l = accessLogCommon(al); + accessLogCommon(al, &mb); else - l = accessLogSquid(al); + accessLogSquid(al, &mb); if (Config.onoff.log_mime_hdrs) { char *ereq = log_quote(al->headers.request); char *erep = log_quote(al->headers.reply); - if (LOG_BUF_SZ - l > 0) { - l--; - l += snprintf(log_buf + l, LOG_BUF_SZ - l, " [%s] [%s]\n", - ereq, erep); - } + memBufPrintf(&mb, " [%s] [%s]\n", ereq, erep); safe_free(ereq); safe_free(erep); + } else { + memBufPrintf(&mb, "\n"); } - file_write(LogfileFD, - -1, - xstrdup(log_buf), - l, - NULL, - NULL, - xfree); + file_write_mbuf(LogfileFD, -1, mb, NULL, NULL); safe_free(xbuf); } diff --git a/src/acl.cc b/src/acl.cc index d80cd6ccb6..37dcbce878 100644 --- a/src/acl.cc +++ b/src/acl.cc @@ -1,6 +1,6 @@ /* - * $Id: acl.cc,v 1.165 1998/05/28 20:47:52 wessels Exp $ + * $Id: acl.cc,v 1.166 1998/05/30 19:43:00 rousskov Exp $ * * DEBUG: section 28 Access Control * AUTHOR: Duane Wessels @@ -1936,22 +1936,23 @@ aclDumpIpList(acl_ip_data * ip) #else wordlist *W = NULL; wordlist **T = &W; - wordlist *w; - char buf[128]; - off_t o; + MemBuf mb; + + memBufDefInit(&mb); while (ip != NULL) { - o = 0; - o += snprintf(buf + o, 128 - o, "%s", inet_ntoa(ip->addr1)); + wordlist *w = xcalloc(1, sizeof(wordlist)); + memBufReset(&mb); + memBufPrintf(&mb, "%s", inet_ntoa(ip->addr1)); if (ip->addr2.s_addr != any_addr.s_addr) - o += snprintf(buf + o, 128 - o, "-%s", inet_ntoa(ip->addr2)); + memBufPrintf(&mb, "-%s", inet_ntoa(ip->addr2)); if (ip->mask.s_addr != no_addr.s_addr) - o += snprintf(buf + o, 128 - o, "/%s", inet_ntoa(ip->mask)); - w = xcalloc(1, sizeof(wordlist)); - w->key = xstrdup(buf); + memBufPrintf(&mb, "/%s", inet_ntoa(ip->mask)); + w->key = xstrdup(mb.buf); *T = w; T = &w->next; ip = ip->next; } + memBufClean(&mb); return W; #endif } @@ -1971,10 +1972,10 @@ aclDumpTimeSpecList(acl_time_data * t) { wordlist *W = NULL; wordlist **T = &W; - wordlist *w; char buf[128]; while (t != NULL) { - snprintf(buf, 128, "%c%c%c%c%c%c%c %02d:%02d-%02d:%02d", + wordlist *w = xcalloc(1, sizeof(wordlist)); + snprintf(buf, sizeof(buf), "%c%c%c%c%c%c%c %02d:%02d-%02d:%02d", t->weekbits & ACL_SUNDAY ? 'S' : '-', t->weekbits & ACL_MONDAY ? 'M' : '-', t->weekbits & ACL_TUESDAY ? 'T' : '-', @@ -1986,7 +1987,6 @@ aclDumpTimeSpecList(acl_time_data * t) t->start % 60, t->stop / 60, t->stop % 60); - w = xcalloc(1, sizeof(wordlist)); w->key = xstrdup(buf); *T = w; T = &w->next; @@ -2016,11 +2016,10 @@ aclDumpIntlistList(intlist * data) { wordlist *W = NULL; wordlist **T = &W; - wordlist *w; char buf[32]; while (data != NULL) { - snprintf(buf, 32, "%d", data->i); - w = xcalloc(1, sizeof(wordlist)); + wordlist *w = xcalloc(1, sizeof(wordlist)); + snprintf(buf, sizeof(buf), "%d", data->i); w->key = xstrdup(buf); *T = w; T = &w->next; @@ -2084,11 +2083,10 @@ aclDumpProxyAuthList(acl_proxy_auth * data) { wordlist *W = NULL; wordlist **T = &W; - wordlist *w; char buf[MAXPATHLEN]; + wordlist *w = xcalloc(1, sizeof(wordlist)); assert(data != NULL); - snprintf(buf, MAXPATHLEN, "%s %d\n", data->filename, data->check_interval); - w = xcalloc(1, sizeof(wordlist)); + snprintf(buf, sizeof(buf), "%s %d\n", data->filename, data->check_interval); w->key = xstrdup(buf); *T = w; T = &w->next; @@ -2388,13 +2386,12 @@ aclDumpArpList(acl_arp_data * data) { wordlist *W = NULL; wordlist **T = &W; - wordlist *w; char buf[24]; while (data != NULL) { - snprintf(buf, 24, "%02x:%02x:02x:02x:02x:02x", + wordlist *w = xcalloc(1, sizeof(wordlist)); + xsnprintf(buf, sizeof(buf), "%02x:%02x:02x:02x:02x:02x", data->eth[0], data->eth[1], data->eth[2], data->eth[3], data->eth[4], data->eth[5]); - w = xcalloc(1, sizeof(wordlist)); w->key = xstrdup(buf); *T = w; T = &w->next; diff --git a/src/asn.cc b/src/asn.cc index 99d5a584b1..eac32cf70f 100644 --- a/src/asn.cc +++ b/src/asn.cc @@ -1,5 +1,5 @@ /* - * $Id: asn.cc,v 1.37 1998/05/27 22:51:48 rousskov Exp $ + * $Id: asn.cc,v 1.38 1998/05/30 19:43:01 rousskov Exp $ * * DEBUG: section 53 AS Number handling * AUTHOR: Duane Wessels, Kostas Anagnostakis @@ -424,7 +424,7 @@ whoisConnectDone(int fd, int status, void *data) comm_close(fd); return; } - snprintf(buf, 128, "%s\r\n", strBuf(p->request->urlpath) + 1); + snprintf(buf, sizeof(buf), "%s\r\n", strBuf(p->request->urlpath) + 1); debug(53, 3) ("whoisConnectDone: FD %d, '%s'\n", fd, strBuf(p->request->urlpath) + 1); comm_write(fd, xstrdup(buf), strlen(buf), NULL, p, xfree); commSetSelect(fd, COMM_SELECT_READ, whoisReadReply, p, 0); diff --git a/src/cache_cf.cc b/src/cache_cf.cc index cd483d7fd2..902ba95965 100644 --- a/src/cache_cf.cc +++ b/src/cache_cf.cc @@ -1,6 +1,6 @@ /* - * $Id: cache_cf.cc,v 1.282 1998/05/21 03:59:34 wessels Exp $ + * $Id: cache_cf.cc,v 1.283 1998/05/30 19:43:02 rousskov Exp $ * * DEBUG: section 3 Configuration File Parsing * AUTHOR: Harvest Derived @@ -52,7 +52,6 @@ static const char *const B_GBYTES_STR = "GB"; static const char *const list_sep = ", \t\n\r"; -static char fatal_str[BUFSIZ]; static void self_destruct(void); static void wordlistAdd(wordlist **, const char *); @@ -78,9 +77,8 @@ static OBJH dump_config; static void self_destruct(void) { - snprintf(fatal_str, BUFSIZ, "Bungled %s line %d: %s", + fatalf("Bungled %s line %d: %s", cfg_filename, config_lineno, config_input_line); - fatal(fatal_str); } void @@ -117,19 +115,13 @@ wordlistAdd(wordlist ** list, const char *key) } } -char * -wordlistCat(const wordlist * w) +void +wordlistCat(const wordlist * w, MemBuf *mb) { - LOCAL_ARRAY(char, buf, 16384); - int o = 0; - buf[0] = '\0'; while (NULL != w) { - if (o + strlen(w->key) > 16384) - break; - o += snprintf(buf + o, 16384 - o, "%s\n", w->key); + memBufPrintf(mb, "%s\n", w->key); w = w->next; } - return buf; } void @@ -175,11 +167,9 @@ parseConfigFile(const char *file_name) char *tmp_line; free_all(); default_all(); - if ((fp = fopen(file_name, "r")) == NULL) { - snprintf(fatal_str, BUFSIZ, "Unable to open configuration file: %s: %s", + if ((fp = fopen(file_name, "r")) == NULL) + fatalf("Unable to open configuration file: %s: %s", file_name, xstrerror()); - fatal(fatal_str); - } cfg_filename = file_name; if ((token = strrchr(cfg_filename, '/'))) cfg_filename = token + 1; @@ -287,10 +277,10 @@ configDoConfigure(void) vhost_mode = 1; if (Config.Port.http == NULL) fatal("No http_port specified!"); - snprintf(ThisCache, SQUIDHOSTNAMELEN << 1, "%s:%d (Squid/%s)", + snprintf(ThisCache, sizeof(ThisCache), "%s:%d (%s)", getMyHostname(), (int) Config.Port.http->i, - SQUID_VERSION); + full_appname_string); if (!Config.udpMaxHitObjsz || Config.udpMaxHitObjsz > SQUID_UDP_SO_SNDBUF) Config.udpMaxHitObjsz = SQUID_UDP_SO_SNDBUF; if (Config.appendDomain) @@ -1346,10 +1336,7 @@ static void requirePathnameExists(const char *name, const char *path) { struct stat sb; - char buf[MAXPATHLEN]; assert(path != NULL); - if (stat(path, &sb) < 0) { - snprintf(buf, MAXPATHLEN, "%s: %s", path, xstrerror()); - fatal(buf); - } + if (stat(path, &sb) < 0) + fatalf("%s: %s", path, xstrerror()); } diff --git a/src/cachemgr.cc b/src/cachemgr.cc index 2b78e489ca..ad9d48ed63 100644 --- a/src/cachemgr.cc +++ b/src/cachemgr.cc @@ -1,6 +1,6 @@ /* - * $Id: cachemgr.cc,v 1.78 1998/03/31 05:37:35 wessels Exp $ + * $Id: cachemgr.cc,v 1.79 1998/05/30 19:43:03 rousskov Exp $ * * DEBUG: section 0 CGI Cache Manager * AUTHOR: Duane Wessels @@ -282,7 +282,7 @@ static char * menu_url(cachemgr_request * req, const char *action) { static char url[1024]; - snprintf(url, 1024, "%s?host=%s&port=%d&user_name=%s&operation=%s&auth=%s", + snprintf(url, sizeof(url), "%s?host=%s&port=%d&user_name=%s&operation=%s&auth=%s", script_name, req->hostname, req->port, diff --git a/src/client_db.cc b/src/client_db.cc index df6f5496a1..d4b5107f24 100644 --- a/src/client_db.cc +++ b/src/client_db.cc @@ -1,6 +1,6 @@ /* - * $Id: client_db.cc,v 1.33 1998/05/28 05:20:43 wessels Exp $ + * $Id: client_db.cc,v 1.34 1998/05/30 19:43:04 rousskov Exp $ * * DEBUG: section 0 Client Database * AUTHOR: Duane Wessels @@ -200,13 +200,13 @@ int meshCtblGetRowFn(oid * New, oid * Oid) { ClientInfo *c = NULL; - static char key[15]; if (!Oid[0] && !Oid[1] && !Oid[2] && !Oid[3]) { hash_first(client_table); c = (ClientInfo *) hash_next(client_table); } else { - snprintf(key, 15, "%d.%d.%d.%d", Oid[0], Oid[1], Oid[2], Oid[3]); + char key[15]; + snprintf(key, sizeof(key), "%d.%d.%d.%d", Oid[0], Oid[1], Oid[2], Oid[3]); c = (ClientInfo *) hash_lookup(client_table, key); if (NULL != c) c = c->next; @@ -230,7 +230,7 @@ snmp_meshCtblFn(variable_list * Var, snint * ErrP) Answer = snmp_var_new(Var->name, Var->name_length); *ErrP = SNMP_ERR_NOERROR; - snprintf(key, 15, "%d.%d.%d.%d", Var->name[11], Var->name[12], + snprintf(key, sizeof(key), "%d.%d.%d.%d", Var->name[11], Var->name[12], Var->name[13], Var->name[14]); debug(49, 5) ("snmp_meshCtblFn: [%s] requested!\n", key); c = (ClientInfo *) hash_lookup(client_table, key); diff --git a/src/comm.cc b/src/comm.cc index a5535a5dd7..e0429a6ba1 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -1,7 +1,7 @@ /* - * $Id: comm.cc,v 1.266 1998/05/28 23:35:31 wessels Exp $ + * $Id: comm.cc,v 1.267 1998/05/30 19:43:05 rousskov Exp $ * * DEBUG: section 5 Socket Functions * AUTHOR: Harvest Derived @@ -1383,7 +1383,7 @@ comm_write(int fd, char *buf, int size, CWCB * handler, void *handler_data, FREE commSetSelect(fd, COMM_SELECT_WRITE, commHandleWrite, state, 0); } -/* a wrapper around comm_write to allow for MemBuf to comm_written in a snap */ +/* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */ void comm_write_mbuf(int fd, MemBuf mb, CWCB * handler, void *handler_data) { diff --git a/src/defines.h b/src/defines.h index 4374ab82e4..d44ad06e24 100644 --- a/src/defines.h +++ b/src/defines.h @@ -222,6 +222,9 @@ /* handy to determine the #elements in a static array */ #define countof(arr) (sizeof(arr)/sizeof(*arr)) +/* to initialize static variables (see also MemBufNull) */ +#define MemBufNULL { NULL, 0, 0, 0, NULL } + /* * Max number of ICP messages to receive per call to icpHandleUdp */ diff --git a/src/disk.cc b/src/disk.cc index b27c12a457..c4010481e5 100644 --- a/src/disk.cc +++ b/src/disk.cc @@ -1,6 +1,6 @@ /* - * $Id: disk.cc,v 1.116 1998/05/22 23:44:03 wessels Exp $ + * $Id: disk.cc,v 1.117 1998/05/30 19:43:06 rousskov Exp $ * * DEBUG: section 6 Disk I/O Routines * AUTHOR: Harvest Derived @@ -466,7 +466,12 @@ file_write(int fd, } } - +/* a wrapper around file_write to allow for MemBuf to be file_written in a snap */ +void +file_write_mbuf(int fd, off_t off, MemBuf mb, DWCB * handler, void *handler_data) +{ + file_write(fd, off, mb.buf, mb.size, handler, handler_data, memBufFreeFunc(&mb)); +} /* Read from FD */ static void diff --git a/src/enums.h b/src/enums.h index e5f87eaf7d..587e0f73eb 100644 --- a/src/enums.h +++ b/src/enums.h @@ -516,6 +516,7 @@ typedef enum { MEM_DONTFREE, MEM_DISK_BUF, MEM_STMEM_BUF, + MEM_2K_BUF, MEM_4K_BUF, MEM_8K_BUF, MEM_ACL_IP_DATA, diff --git a/src/errorpage.cc b/src/errorpage.cc index 6d3ad288aa..1d7ec6c691 100644 --- a/src/errorpage.cc +++ b/src/errorpage.cc @@ -1,6 +1,6 @@ /* - * $Id: errorpage.cc,v 1.134 1998/05/27 22:51:52 rousskov Exp $ + * $Id: errorpage.cc,v 1.135 1998/05/30 19:43:07 rousskov Exp $ * * DEBUG: section 4 Error Generation * AUTHOR: Duane Wessels @@ -149,8 +149,7 @@ errorTryLoadText(const char *page_name, const char *dir) struct stat sb; char *text; - snprintf(path, MAXPATHLEN, "%s/%s", - dir, page_name); + snprintf(path, sizeof(path), "%s/%s", dir, page_name); fd = file_open(path, O_RDONLY, NULL, NULL, NULL); if (fd < 0 || fstat(fd, &sb) < 0) { debug(4, 0) ("errorTryLoadText: '%s': %s\n", path, xstrerror()); @@ -372,20 +371,22 @@ static const char * errorConvert(char token, ErrorState * err) { request_t *r = err->request; - static char buf[CVT_BUF_SZ]; - const char *p = buf; + static MemBuf mb = MemBufNULL; + const char *p = NULL; /* takes priority over mb if set */ + + memBufReset(&mb); switch (token) { case 'B': p = r ? ftpUrlWith2f(r) : "[no URL]"; break; case 'e': - snprintf(buf, CVT_BUF_SZ, "%d", err->xerrno); + memBufPrintf(&mb, "%d", err->xerrno); break; case 'E': if (err->xerrno) - snprintf(buf, CVT_BUF_SZ, "(%d) %s", err->xerrno, strerror(err->xerrno)); + memBufPrintf(&mb, "(%d) %s", err->xerrno, strerror(err->xerrno)); else - snprintf(buf, CVT_BUF_SZ, "[No Error]"); + memBufPrintf(&mb, "[No Error]"); break; case 'f': /* FTP REQUEST LINE */ @@ -403,26 +404,26 @@ errorConvert(char token, ErrorState * err) break; case 'g': /* FTP SERVER MESSAGE */ - p = wordlistCat(err->ftp_server_msg); + wordlistCat(err->ftp_server_msg, &mb); break; case 'h': - snprintf(buf, CVT_BUF_SZ, "%s", getMyHostname()); + memBufPrintf(&mb, "%s", getMyHostname()); break; case 'H': p = r ? r->host : "[unknown host]"; break; case 'i': - snprintf(buf, CVT_BUF_SZ, "%s", inet_ntoa(err->src_addr)); + memBufPrintf(&mb, "%s", inet_ntoa(err->src_addr)); break; case 'I': if (err->host) { - snprintf(buf, CVT_BUF_SZ, "%s", err->host); + memBufPrintf(&mb, "%s", err->host); } else p = "[unknown]"; break; case 'L': if (Config.errHtmlText) { - snprintf(buf, CVT_BUF_SZ, "%s", Config.errHtmlText); + memBufPrintf(&mb, "%s", Config.errHtmlText); } else p = "[not available]"; break; @@ -431,7 +432,7 @@ errorConvert(char token, ErrorState * err) break; case 'p': if (r) { - snprintf(buf, CVT_BUF_SZ, "%d", (int) r->port); + memBufPrintf(&mb, "%d", (int) r->port); } else { p = "[unknown port]"; } @@ -441,9 +442,7 @@ errorConvert(char token, ErrorState * err) break; case 'R': if (NULL != r) { - MemBuf mb; Packer p; - memBufDefInit(&mb); memBufPrintf(&mb, "%s %s HTTP/%3.1f\n", RequestMethodStr[r->method], strLen(r->urlpath) ? strBuf(r->urlpath) : "/", @@ -451,8 +450,6 @@ errorConvert(char token, ErrorState * err) packerToMemInit(&p, &mb); httpHeaderPackInto(&r->header, &p); packerClean(&p); - xstrncpy(buf, mb.buf, CVT_BUF_SZ); - memBufClean(&mb); } else if (err->request_hdrs) { p = err->request_hdrs; } else { @@ -466,11 +463,11 @@ errorConvert(char token, ErrorState * err) /* signature may contain %-escapes, recursion */ if (err->page_id != ERR_SQUID_SIGNATURE) { const int saved_id = err->page_id; - MemBuf mb; + MemBuf sign_mb; err->page_id = ERR_SQUID_SIGNATURE; - mb = errorBuildContent(err); - snprintf(buf, CVT_BUF_SZ, "%s", mb.buf); - memBufClean(&mb); + sign_mb = errorBuildContent(err); + memBufPrintf(&mb, "%s", sign_mb.buf); + memBufClean(&sign_mb); err->page_id = saved_id; } else { /* wow, somebody put %S into ERR_SIGNATURE, stop recursion */ @@ -478,18 +475,18 @@ errorConvert(char token, ErrorState * err) } break; case 't': - xstrncpy(buf, mkhttpdlogtime(&squid_curtime), 128); + memBufPrintf(&mb, "%s", mkhttpdlogtime(&squid_curtime)); break; case 'T': - snprintf(buf, CVT_BUF_SZ, "%s", mkrfc1123(squid_curtime)); + memBufPrintf(&mb, "%s", mkrfc1123(squid_curtime)); break; case 'U': p = r ? urlCanonicalClean(r) : err->url ? err->url : "[no URL]"; break; case 'w': - if (Config.adminEmail) { - snprintf(buf, CVT_BUF_SZ, "%s", Config.adminEmail); - } else + if (Config.adminEmail) + memBufPrintf(&mb, "%s", Config.adminEmail); + else p = "[unknown]"; break; case 'z': @@ -505,7 +502,9 @@ errorConvert(char token, ErrorState * err) p = "%UNKNOWN%"; break; } - assert(p != NULL); + if (!p) + p = mb.buf; /* do not use mb after this assignment! */ + assert(p); debug(4, 3) ("errorConvert: %%%c --> '%s'\n", token, p); return p; } diff --git a/src/globals.h b/src/globals.h index 9aa1a815b1..5153c82577 100644 --- a/src/globals.h +++ b/src/globals.h @@ -1,6 +1,6 @@ /* - * $Id: globals.h,v 1.57 1998/05/22 23:14:20 wessels Exp $ + * $Id: globals.h,v 1.58 1998/05/30 19:43:08 rousskov Exp $ */ extern FILE *debug_log; /* NULL */ @@ -97,6 +97,7 @@ extern int store_hash_buckets; /* 0 */ extern hash_table *store_table; /* NULL */ extern dlink_list store_list; extern const String StringNull; /* { 0, 0, NULL } */ +extern const MemBuf MemBufNull; /* MemBufNULL */ extern int hot_obj_count; /* 0 */ extern int _db_level; extern const int CacheDigestHashFuncCount; /* 4 */ diff --git a/src/gopher.cc b/src/gopher.cc index a7950c262f..f1e316d086 100644 --- a/src/gopher.cc +++ b/src/gopher.cc @@ -1,7 +1,7 @@ /* - * $Id: gopher.cc,v 1.125 1998/04/24 07:09:34 wessels Exp $ + * $Id: gopher.cc,v 1.126 1998/05/30 19:43:09 rousskov Exp $ * * DEBUG: section 10 Gopher * AUTHOR: Harvest Derived @@ -161,7 +161,7 @@ typedef struct gopher_ds { } GopherStateData; static PF gopherStateFree; -static void gopher_mime_content(char *buf, const char *name, const char *def); +static void gopher_mime_content(MemBuf *mb, const char *name, const char *def); static void gopherMimeCreate(GopherStateData *); static int gopher_url_parser(const char *url, char *host, @@ -199,13 +199,13 @@ gopherStateFree(int fdnotused, void *data) /* figure out content type from file extension */ static void -gopher_mime_content(char *buf, const char *name, const char *def_ctype) +gopher_mime_content(MemBuf *mb, const char *name, const char *def_ctype) { char *ctype = mimeGetContentType(name); char *cenc = mimeGetContentEncoding(name); if (cenc) - snprintf(buf + strlen(buf), MAX_MIME - strlen(buf), "Content-Encoding: %s\r\n", cenc); - snprintf(buf + strlen(buf), MAX_MIME - strlen(buf), "Content-Type: %s\r\n", + memBufPrintf(mb, "Content-Encoding: %s\r\n", cenc); + memBufPrintf(mb, "Content-Type: %s\r\n", ctype ? ctype : def_ctype); } @@ -215,9 +215,11 @@ gopher_mime_content(char *buf, const char *name, const char *def_ctype) static void gopherMimeCreate(GopherStateData * gopherState) { - LOCAL_ARRAY(char, tempMIME, MAX_MIME); + MemBuf mb; - snprintf(tempMIME, MAX_MIME, + memBufDefInit(&mb); + + memBufPrintf(&mb, "HTTP/1.0 200 OK Gatewaying\r\n" "Server: Squid/%s\r\n" "Date: %s\r\n" @@ -231,34 +233,35 @@ gopherMimeCreate(GopherStateData * gopherState) case GOPHER_HTML: case GOPHER_WWW: case GOPHER_CSO: - strcat(tempMIME, "Content-Type: text/html\r\n"); + memBufPrintf(&mb, "Content-Type: text/html\r\n"); break; case GOPHER_GIF: case GOPHER_IMAGE: case GOPHER_PLUS_IMAGE: - strcat(tempMIME, "Content-Type: image/gif\r\n"); + memBufPrintf(&mb, "Content-Type: image/gif\r\n"); break; case GOPHER_SOUND: case GOPHER_PLUS_SOUND: - strcat(tempMIME, "Content-Type: audio/basic\r\n"); + memBufPrintf(&mb, "Content-Type: audio/basic\r\n"); break; case GOPHER_PLUS_MOVIE: - strcat(tempMIME, "Content-Type: video/mpeg\r\n"); + memBufPrintf(&mb, "Content-Type: video/mpeg\r\n"); break; case GOPHER_MACBINHEX: case GOPHER_DOSBIN: case GOPHER_UUENCODED: case GOPHER_BIN: /* Rightnow We have no idea what it is. */ - gopher_mime_content(tempMIME, gopherState->request, def_gopher_bin); + gopher_mime_content(&mb, gopherState->request, def_gopher_bin); break; case GOPHER_FILE: default: - gopher_mime_content(tempMIME, gopherState->request, def_gopher_text); + gopher_mime_content(&mb, gopherState->request, def_gopher_text); break; } - strcat(tempMIME, "\r\n"); - storeAppend(gopherState->entry, tempMIME, strlen(tempMIME)); + memBufPrintf(&mb, "\r\n"); + storeAppend(gopherState->entry, mb.buf, mb.size); + memBufClean(&mb); } /* Parse a gopher url into components. By Anawat. */ @@ -330,15 +333,10 @@ gopherCachable(const char *url) static void gopherEndHTML(GopherStateData * gopherState) { - LOCAL_ARRAY(char, tmpbuf, TEMP_BUF_SIZE); - - if (!gopherState->data_in) { - snprintf(tmpbuf, TEMP_BUF_SIZE, + if (!gopherState->data_in) + storeAppendPrintf(gopherState->entry, "
This is a searchable Gopher index. Use the search\n"
"function of your browser to enter search terms.\n"
- "