--------------------- PatchSet 10273 Date: 2007/12/18 12:10:29 Author: adri Branch: s27_adri Tag: (none) Log: String-ify parts of the mime.c code; eliminate some strBuf()'s in ftp.code with the new mime.c changes and strSubStr(). Some temporary Strings are used here to map C strings to Strings. Members: src/HttpHdrRange.c:1.8.58.2->1.8.58.3 src/ftp.c:1.44.4.2.4.7->1.44.4.2.4.8 src/gopher.c:1.26.12.3->1.26.12.4 src/internal.c:1.15.12.4->1.15.12.5 src/mime.c:1.19.12.2->1.19.12.3 src/peer_digest.c:1.21.10.2.4.4->1.21.10.2.4.5 src/protos.h:1.146.2.4.4.24->1.146.2.4.4.25 Index: squid/src/HttpHdrRange.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/HttpHdrRange.c,v retrieving revision 1.8.58.2 retrieving revision 1.8.58.3 diff -u -r1.8.58.2 -r1.8.58.3 --- squid/src/HttpHdrRange.c 17 Dec 2007 08:17:48 -0000 1.8.58.2 +++ squid/src/HttpHdrRange.c 18 Dec 2007 12:10:29 -0000 1.8.58.3 @@ -1,6 +1,6 @@ /* - * $Id: HttpHdrRange.c,v 1.8.58.2 2007/12/17 08:17:48 adri Exp $ + * $Id: HttpHdrRange.c,v 1.8.58.3 2007/12/18 12:10:29 adri Exp $ * * DEBUG: section 64 HTTP Range Header * AUTHOR: Alex Rousskov @@ -242,7 +242,7 @@ if (strNCaseCmp(*str, "bytes=", 6)) return 0; /* skip "bytes="; hack! */ - pos = strBuf(*str) + 5; + pos = 5; /* iterate through comma separated list */ while (strListGetItem(str, ',', &item, &ilen, &pos)) { HttpHdrRangeSpec *spec = httpHdrRangeSpecParseCreate(item, ilen); Index: squid/src/ftp.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/ftp.c,v retrieving revision 1.44.4.2.4.7 retrieving revision 1.44.4.2.4.8 diff -u -r1.44.4.2.4.7 -r1.44.4.2.4.8 --- squid/src/ftp.c 17 Dec 2007 08:17:49 -0000 1.44.4.2.4.7 +++ squid/src/ftp.c 18 Dec 2007 12:10:29 -0000 1.44.4.2.4.8 @@ -1,6 +1,6 @@ /* - * $Id: ftp.c,v 1.44.4.2.4.7 2007/12/17 08:17:49 adri Exp $ + * $Id: ftp.c,v 1.44.4.2.4.8 2007/12/18 12:10:29 adri Exp $ * * DEBUG: section 9 File Transfer Protocol (FTP) * AUTHOR: Harvest Derived @@ -394,6 +394,9 @@ i += strCSpn(ftpState->title_url, i, "/"); if (i > j) { char *url = strCDup(ftpState->title_url); + debug(1, 1) ("i: %d, j: %d\n", i, j); + debug(1, 1) ("title_url: %.*s\n", strLen2(ftpState->title_url), strBuf2(ftpState->title_url)); + debug(1, 1) ("url (%d bytes): %s\n", (int) strlen(url), url); url[i] = '\0'; storeAppendPrintf(e, "", html_quote(url + k, strlen(url + k))); rfc1738_unescape(url + j); @@ -669,6 +672,7 @@ LOCAL_ARRAY(char, link, 2048 + 40); LOCAL_ARRAY(char, html, 8192); LOCAL_ARRAY(char, prefix, 2048); + String s = StringNull; size_t width = Config.Ftp.list_width; ftpListParts *parts; *icon = *href = *text = *size = *chdir = *view = *download = *link = *html = '\0'; @@ -683,9 +687,11 @@ /* Handle builtin */ if (strcmp(line, "") == 0) { /* {icon} {text} {link} */ + stringInitStatic(&s, "internal-dirup"); snprintf(icon, 2048, "\"%-6s\"", - mimeGetIconURL("internal-dirup"), + mimeGetIconURL(s), "[DIRUP]"); + stringClean(&s); if (!ftpState->flags.no_dotdot && !ftpState->flags.root_dir) { /* Normal directory */ if (!ftpState->flags.dir_slash) @@ -708,10 +714,9 @@ if (ftpState->flags.dir_slash) { url = xstrdup("./"); } else { - const char *title = strBuf(ftpState->title_url); - int k = 6 + strcspn(&title[6], "/"); + int k = 6 + strCSpn(ftpState->title_url, 6, "/"); char *t; - url = xstrdup(title + k); + url = strCDupOffset(ftpState->title_url, k); t = url + strlen(url) - 2; while (t > url && *t != '/') *t-- = '\0'; @@ -752,15 +757,19 @@ xstrncpy(text, parts->showname, 2048); switch (parts->type) { case 'd': + stringInitStatic(&s, "internal-dir"); snprintf(icon, 2048, "\"%-6s\"", - mimeGetIconURL("internal-dir"), + mimeGetIconURL(s), "[DIR]"); strcat(href, "/"); /* margin is allocated above */ + stringClean(&s); break; case 'l': + stringInitStatic(&s, "internal-link"); snprintf(icon, 2048, "\"%-6s\"", - mimeGetIconURL("internal-link"), + mimeGetIconURL(s), "[LINK]"); + stringClean(&s); /* sometimes there is an 'l' flag, but no "->" link */ if (parts->link) { char *e = rfc1738_escape(parts->link); @@ -772,22 +781,28 @@ } break; case '\0': + stringInit(&s, parts->name); snprintf(icon, 2048, "\"%-6s\"", - mimeGetIconURL(parts->name), + mimeGetIconURL(s), "[UNKNOWN]"); + stringClean(&s); + stringInitStatic(&s, "internal-dir"); snprintf(chdir, 2048, " ", rfc1738_escape_part(parts->name), - mimeGetIconURL("internal-dir")); + mimeGetIconURL(s)); + stringClean(&s); break; case '-': default: { squid_off_t sz = parts->size; char units = ' '; + stringInit(&s, parts->name); snprintf(icon, 2048, "\"%-6s\"", - mimeGetIconURL(parts->name), + mimeGetIconURL(s), "[FILE]"); + stringClean(&s); if (sz > 10 * 1024) { sz = (sz + 1023) >> 10; units = 'K'; @@ -805,16 +820,23 @@ break; } if (parts->type != 'd') { - if (mimeGetViewOption(parts->name)) { + String nameStr; + stringInit(&nameStr, parts->name); + if (mimeGetViewOption(nameStr)) { + stringInitStatic(&s, "internal-view"); snprintf(view, 2048, " ", - prefix, href, mimeGetIconURL("internal-view")); + prefix, href, mimeGetIconURL(s)); + stringClean(&s); } - if (mimeGetDownloadOption(parts->name)) { + if (mimeGetDownloadOption(nameStr)) { + stringInitStatic(&s, "internal-download"); snprintf(download, 2048, " ", - prefix, href, mimeGetIconURL("internal-download")); + prefix, href, mimeGetIconURL(s)); + stringClean(&s); } + stringClean(&nameStr); } /* {icon} {text} . . . {date}{size}{chdir}{view}{download}{link}\n */ if (parts->type != '\0') { @@ -1484,7 +1506,7 @@ static void ftpSendType(FtpStateData * ftpState) { - const char *filename; + String filename = StringNull; char mode; int s; /* @@ -1502,11 +1524,12 @@ mode = 'A'; } else { s = strRChr(ftpState->request->urlpath, '/'); - /* XXX replace this as soon as possible with a String type! */ - filename = strBuf(ftpState->request->urlpath); if (s != -1) - filename += s; + filename = strSubStr(ftpState->request->urlpath, s, -1); + else + filename = stringDup(&ftpState->request->urlpath); mode = mimeGetTransferMode(filename); + stringClean(&filename); } break; } @@ -2544,8 +2567,9 @@ { const char *mime_type = NULL; const char *mime_enc = NULL; + /* A weak-reference String */ String urlpath = ftpState->request->urlpath; - const char *filename = NULL; + String filename = StringNull; int s; StoreEntry *e = ftpState->entry; http_reply *reply = e->mem_obj->reply; @@ -2557,11 +2581,11 @@ EBIT_CLR(e->flags, ENTRY_FWD_HDR_WAIT); storeBuffer(e); /* released when done processing current data payload */ s = strRChr(urlpath, '/'); - filename = strBuf(urlpath); - /* XXX replace this as soon as possible with a String type! */ - if (s > -1) { - filename += s; - } + if (s > -1) + filename = strSubStr(urlpath, s, -1); + else + filename = stringDup(&urlpath); + if (ftpState->flags.isdir) { mime_type = "text/html"; } else { @@ -2579,6 +2603,7 @@ break; } } + stringClean(&filename); httpReplyReset(reply); /* set standard stuff */ if (ftpState->restarted_offset) { Index: squid/src/gopher.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/gopher.c,v retrieving revision 1.26.12.3 retrieving revision 1.26.12.4 diff -u -r1.26.12.3 -r1.26.12.4 --- squid/src/gopher.c 15 Dec 2007 05:17:57 -0000 1.26.12.3 +++ squid/src/gopher.c 18 Dec 2007 12:10:30 -0000 1.26.12.4 @@ -1,6 +1,6 @@ /* - * $Id: gopher.c,v 1.26.12.3 2007/12/15 05:17:57 adri Exp $ + * $Id: gopher.c,v 1.26.12.4 2007/12/18 12:10:30 adri Exp $ * * DEBUG: section 10 Gopher * AUTHOR: Harvest Derived @@ -126,6 +126,7 @@ HttpReply *reply = e->mem_obj->reply; const char *mime_type = NULL; const char *mime_enc = NULL; + String req; switch (gopherState->type_id) { case GOPHER_DIRECTORY: @@ -156,8 +157,10 @@ case GOPHER_FILE: default: /* Rightnow We have no idea what it is. */ - mime_type = mimeGetContentType(gopherState->request); - mime_enc = mimeGetContentEncoding(gopherState->request); + stringInit(&req, gopherState->request); + mime_type = mimeGetContentType(req); + mime_enc = mimeGetContentEncoding(req); + stringClean(&req); break; } @@ -282,6 +285,7 @@ const char *icon_url = NULL; char gtype; StoreEntry *entry = NULL; + String iconStr; memset(tmpbuf, '\0', TEMP_BUF_SIZE); memset(line, '\0', TEMP_BUF_SIZE); @@ -431,43 +435,61 @@ switch (gtype) { case GOPHER_DIRECTORY: - icon_url = mimeGetIconURL("internal-menu"); + stringInitStatic(&iconStr, "internal-menu"); + icon_url = mimeGetIconURL(iconStr); + stringClean(&iconStr); break; case GOPHER_HTML: case GOPHER_FILE: - icon_url = mimeGetIconURL("internal-text"); + stringInitStatic(&iconStr, "internal-text"); + icon_url = mimeGetIconURL(iconStr); + stringClean(&iconStr); break; case GOPHER_INDEX: case GOPHER_CSO: - icon_url = mimeGetIconURL("internal-index"); + stringInitStatic(&iconStr, "internal-index"); + icon_url = mimeGetIconURL(iconStr); + stringClean(&iconStr); break; case GOPHER_IMAGE: case GOPHER_GIF: case GOPHER_PLUS_IMAGE: - icon_url = mimeGetIconURL("internal-image"); + stringInitStatic(&iconStr, "internal-image"); + icon_url = mimeGetIconURL(iconStr); + stringClean(&iconStr); break; case GOPHER_SOUND: case GOPHER_PLUS_SOUND: - icon_url = mimeGetIconURL("internal-sound"); + stringInitStatic(&iconStr, "internal-sound"); + icon_url = mimeGetIconURL(iconStr); + stringClean(&iconStr); break; case GOPHER_PLUS_MOVIE: - icon_url = mimeGetIconURL("internal-movie"); + stringInitStatic(&iconStr, "internal-movie"); + icon_url = mimeGetIconURL(iconStr); + stringClean(&iconStr); break; case GOPHER_TELNET: case GOPHER_3270: - icon_url = mimeGetIconURL("internal-telnet"); + stringInitStatic(&iconStr, "internal-telnet"); + icon_url = mimeGetIconURL(iconStr); + stringClean(&iconStr); break; case GOPHER_BIN: case GOPHER_MACBINHEX: case GOPHER_DOSBIN: case GOPHER_UUENCODED: - icon_url = mimeGetIconURL("internal-binary"); + stringInitStatic(&iconStr, "internal-binary"); + icon_url = mimeGetIconURL(iconStr); + stringClean(&iconStr); break; case GOPHER_INFO: icon_url = NULL; break; default: - icon_url = mimeGetIconURL("internal-unknown"); + stringInitStatic(&iconStr, "internal-unknown"); + icon_url = mimeGetIconURL(iconStr); + stringClean(&iconStr); break; } Index: squid/src/internal.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/internal.c,v retrieving revision 1.15.12.4 retrieving revision 1.15.12.5 diff -u -r1.15.12.4 -r1.15.12.5 --- squid/src/internal.c 16 Dec 2007 02:32:26 -0000 1.15.12.4 +++ squid/src/internal.c 18 Dec 2007 12:10:30 -0000 1.15.12.5 @@ -1,6 +1,6 @@ /* - * $Id: internal.c,v 1.15.12.4 2007/12/16 02:32:26 adri Exp $ + * $Id: internal.c,v 1.15.12.5 2007/12/18 12:10:30 adri Exp $ * * DEBUG: section 76 Internal Squid Object handling * AUTHOR: Duane, Alex, Henrik @@ -81,11 +81,12 @@ * makes internal url with a given host and port (remote internal url) */ char * -internalRemoteUri(const char *host, u_short port, const char *dir, const char *name) +internalRemoteUri(const char *host, u_short port, const char *dir, String name) { static MemBuf mb = MemBufNULL; static char lc_host[SQUIDHOSTNAMELEN]; - assert(host && name); + assert(host); + assert(strIsNotNull(name)); /* convert host name to lower case */ xstrncpy(lc_host, host, SQUIDHOSTNAMELEN); Tolower(lc_host); @@ -104,7 +105,7 @@ memBufPrintf(&mb, ":%d", port); if (dir) memBufPrintf(&mb, "%s", dir); - memBufPrintf(&mb, "%s", name); + memBufPrintf(&mb, "%.*s", strLen2(name), strBuf2(name)); /* return a pointer to a local static buffer */ return mb.buf; } @@ -113,7 +114,7 @@ * makes internal url with local host and port */ char * -internalLocalUri(const char *dir, const char *name) +internalLocalUri(const char *dir, String name) { return internalRemoteUri(internalHostname(), getMyPort(), dir, name); Index: squid/src/mime.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/mime.c,v retrieving revision 1.19.12.2 retrieving revision 1.19.12.3 diff -u -r1.19.12.2 -r1.19.12.3 --- squid/src/mime.c 15 Dec 2007 11:09:24 -0000 1.19.12.2 +++ squid/src/mime.c 18 Dec 2007 12:10:30 -0000 1.19.12.3 @@ -1,6 +1,6 @@ /* - * $Id: mime.c,v 1.19.12.2 2007/12/15 11:09:24 adri Exp $ + * $Id: mime.c,v 1.19.12.3 2007/12/18 12:10:30 adri Exp $ * * DEBUG: section 25 MIME Parsing * AUTHOR: Harvest Derived @@ -40,7 +40,7 @@ typedef struct _mime_entry { char *pattern; regex_t compiled_pattern; - char *icon; + String iconStr; char *content_type; char *content_encoding; char transfer_mode; @@ -51,7 +51,7 @@ static mimeEntry *MimeTable = NULL; static mimeEntry **MimeTableTail = &MimeTable; -static void mimeLoadIconFile(const char *icon); +static void mimeLoadIconFile(String s); /* returns a pointer to a field-value of the first matching field-name */ char * @@ -172,11 +172,11 @@ } static mimeEntry * -mimeGetEntry(const char *fn, int skip_encodings) +mimeGetEntry(String s, int skip_encodings) { mimeEntry *m; char *t; - char *name = xstrdup(fn); + char *name = strCDup(s); try_again: for (m = MimeTable; m; m = m->next) { if (regexec(&m->compiled_pattern, name, 0, 0, 0) == 0) @@ -203,27 +203,28 @@ return m; } -char * -mimeGetIcon(const char *fn) +String +mimeGetIcon(String s) { - mimeEntry *m = mimeGetEntry(fn, 1); + mimeEntry *m = mimeGetEntry(s, 1); if (m == NULL) - return NULL; - if (!strcmp(m->icon, dash_str)) - return NULL; - return m->icon; + return StringNull; + if (!strCmp(m->iconStr, dash_str)) + return StringNull; + return m->iconStr; } const char * -mimeGetIconURL(const char *fn) +mimeGetIconURL(String s) { static MemBuf mb = MemBufNULL; - char *icon = mimeGetIcon(fn); - if (icon == NULL) + /* A weak string reference */ + String icon = mimeGetIcon(s); + if (strIsNull(s)) return null_string; if (Config.icons.use_short_names) { memBufReset(&mb); - memBufPrintf(&mb, "/squid-internal-static/icons/%s", icon); + memBufPrintf(&mb, "/squid-internal-static/icons/%.*s", strLen2(icon), strBuf2(icon)); return mb.buf; } else { return internalLocalUri("/squid-internal-static/icons/", icon); @@ -231,9 +232,9 @@ } char * -mimeGetContentType(const char *fn) +mimeGetContentType(String s) { - mimeEntry *m = mimeGetEntry(fn, 1); + mimeEntry *m = mimeGetEntry(s, 1); if (m == NULL) return NULL; if (!strcmp(m->content_type, dash_str)) @@ -242,9 +243,9 @@ } char * -mimeGetContentEncoding(const char *fn) +mimeGetContentEncoding(String s) { - mimeEntry *m = mimeGetEntry(fn, 0); + mimeEntry *m = mimeGetEntry(s, 0); if (m == NULL) return NULL; if (!strcmp(m->content_encoding, dash_str)) @@ -253,23 +254,23 @@ } char -mimeGetTransferMode(const char *fn) +mimeGetTransferMode(String s) { - mimeEntry *m = mimeGetEntry(fn, 0); + mimeEntry *m = mimeGetEntry(s, 0); return m ? m->transfer_mode : 'I'; } int -mimeGetDownloadOption(const char *fn) +mimeGetDownloadOption(String s) { - mimeEntry *m = mimeGetEntry(fn, 1); + mimeEntry *m = mimeGetEntry(s, 1); return m ? m->download_option : 0; } int -mimeGetViewOption(const char *fn) +mimeGetViewOption(String s) { - mimeEntry *m = mimeGetEntry(fn, 0); + mimeEntry *m = mimeGetEntry(s, 0); return m ? m->view_option : 0; } @@ -353,7 +354,7 @@ m = xcalloc(1, sizeof(mimeEntry)); m->pattern = xstrdup(pattern); m->content_type = xstrdup(type); - m->icon = xstrdup(icon); + stringInit(&m->iconStr, icon); m->content_encoding = xstrdup(encoding); m->compiled_pattern = re; if (!strcasecmp(mode, "ascii")) @@ -373,7 +374,7 @@ * Create Icon StoreEntry's */ for (m = MimeTable; m != NULL; m = m->next) - mimeLoadIconFile(m->icon); + mimeLoadIconFile(m->iconStr); debug(25, 1) ("Loaded Icons.\n"); } @@ -385,7 +386,7 @@ MimeTable = m->next; safe_free(m->pattern); safe_free(m->content_type); - safe_free(m->icon); + stringClean(&m->iconStr); safe_free(m->content_encoding); regfree(&m->compiled_pattern); safe_free(m); @@ -394,7 +395,7 @@ } static void -mimeLoadIconFile(const char *icon) +mimeLoadIconFile(String iconStr) { int fd; int n; @@ -404,16 +405,16 @@ LOCAL_ARRAY(char, path, MAXPATHLEN); LOCAL_ARRAY(char, url, MAX_URL); char *buf; - const char *type = mimeGetContentType(icon); + const char *type = mimeGetContentType(iconStr); HttpReply *reply; request_t *r; if (type == NULL) fatal("Unknown icon format while reading mime.conf\n"); - buf = internalStoreUri("/squid-internal-static/icons/", icon, strlen(icon)); + buf = internalStoreUri("/squid-internal-static/icons/", strBuf2(iconStr), strLen2(iconStr)); xstrncpy(url, buf, MAX_URL); if (storeGetPublic(url, METHOD_GET)) return; - snprintf(path, MAXPATHLEN, "%s/%s", Config.icons.directory, icon); + snprintf(path, MAXPATHLEN, "%s/%.*s", Config.icons.directory, strLen2(iconStr), strBuf2(iconStr)); fd = file_open(path, O_RDONLY | O_BINARY); if (fd < 0) { debug(25, 0) ("mimeLoadIconFile: %s: %s\n", path, xstrerror()); Index: squid/src/peer_digest.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/peer_digest.c,v retrieving revision 1.21.10.2.4.4 retrieving revision 1.21.10.2.4.5 diff -u -r1.21.10.2.4.4 -r1.21.10.2.4.5 --- squid/src/peer_digest.c 16 Dec 2007 02:28:29 -0000 1.21.10.2.4.4 +++ squid/src/peer_digest.c 18 Dec 2007 12:10:31 -0000 1.21.10.2.4.5 @@ -1,6 +1,6 @@ /* - * $Id: peer_digest.c,v 1.21.10.2.4.4 2007/12/16 02:28:29 adri Exp $ + * $Id: peer_digest.c,v 1.21.10.2.4.5 2007/12/18 12:10:31 adri Exp $ * * DEBUG: section 72 Peer Digest Routines * AUTHOR: Alex Rousskov @@ -281,9 +281,13 @@ /* compute future request components */ if (p->digest_url) url = xstrdup(p->digest_url); - else + else { + String tmp; + stringInit(&tmp, StoreDigestFileName); url = internalRemoteUri(p->host, p->http_port, - "/squid-internal-periodic/", StoreDigestFileName); + "/squid-internal-periodic/", tmp); + stringClean(&tmp); + } req = urlParse(METHOD_GET, url, strlen(url)); assert(req); Index: squid/src/protos.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/protos.h,v retrieving revision 1.146.2.4.4.24 retrieving revision 1.146.2.4.4.25 diff -u -r1.146.2.4.4.24 -r1.146.2.4.4.25 --- squid/src/protos.h 17 Dec 2007 08:17:50 -0000 1.146.2.4.4.24 +++ squid/src/protos.h 18 Dec 2007 12:10:31 -0000 1.146.2.4.4.25 @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.146.2.4.4.24 2007/12/17 08:17:50 adri Exp $ + * $Id: protos.h,v 1.146.2.4.4.25 2007/12/18 12:10:31 adri Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -629,13 +629,13 @@ extern void mimeInit(char *filename); extern void mimeFreeMemory(void); -extern char *mimeGetContentEncoding(const char *fn); -extern char *mimeGetContentType(const char *fn); -extern char *mimeGetIcon(const char *fn); -extern const char *mimeGetIconURL(const char *fn); -extern char mimeGetTransferMode(const char *fn); -extern int mimeGetDownloadOption(const char *fn); -extern int mimeGetViewOption(const char *fn); +extern char *mimeGetContentEncoding(String s); +extern char *mimeGetContentType(String s); +extern String mimeGetIcon(String s); +extern const char *mimeGetIconURL(String s); +extern char mimeGetTransferMode(String s); +extern int mimeGetDownloadOption(String s); +extern int mimeGetViewOption(String s); extern int mcastSetTtl(int, int); extern IPH mcastJoinGroups; @@ -1224,9 +1224,9 @@ extern void internalStart(request_t *, StoreEntry *); extern int internalCheck(const char *urlpath, int len); extern int internalStaticCheck(String urlpath); -extern char *internalLocalUri(const char *dir, const char *name); +extern char *internalLocalUri(const char *dir, String name); extern char *internalStoreUri(const char *dir, const char *name, int len); -extern char *internalRemoteUri(const char *, u_short, const char *, const char *); +extern char *internalRemoteUri(const char *, u_short, const char *, String name); extern const char *internalHostname(void); extern int internalHostnameIs(const char *);