--------------------- PatchSet 10193 Date: 2007/12/04 21:56:04 Author: adri Branch: s27_adri Tag: (none) Log: A few changes in preparation for ref-counted copy-on-modification Strings. * replace the str* functions which return -pointers- with ones which return offsets (strChr, strRChr) * replace a str* function which takes a pointer with one which takes an offset (strSet) * Add a couple of functions - strGetPos (to get a character at offset X) and strNCaseCmpOffset (strncasecmp(String s + offset, const char *, int len) * modify the ftp and urn code to use the new functions. What they're doing is already evil so I don't feel that bad by replacing it with something just as evil.. * .. but leave a comment (and add ADRIAN_TODO) which outlines what should be revisited once the String API is a little more fruitful. I've tested FTP; I haven't tested URN. Members: ADRIAN_TODO:1.1->1.1.2.1 libbuf/String.h:1.1.2.1->1.1.2.2 src/ftp.c:1.44.4.2->1.44.4.2.4.1 src/urn.c:1.24.6.2->1.24.6.2.4.1 --- /dev/null Wed Dec 5 01:19:39 2007 +++ squid/ADRIAN_TODO Wed Dec 5 01:19:39 2007 @@ -0,0 +1,25 @@ +String-related stuff: + +Specifically: + +* urn.c: str* routine uses - should convert the 'host' to be a String rather + than a const char * thats strdup'ed; especially if its not being modified.. + +* ftp.c: the str* routines which then create a const char * filename pointer should + be changed to be a String and not take the const char *. + +* There's an XXX in one of the above which inserts a '\0' but doesn't call + strCut(); fix that! + +Then: + +* Insert refcounted backend stuff whilst preserving the same semantics (NUL-terminated + strings) + +* Test test test! + +Then! + +* Begin looking all over the code and begin using String and stringDup() where + appropriate instead of xstrdup() ! + Index: squid/libbuf/String.h =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/String.h,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid/libbuf/String.h 3 Dec 2007 07:59:25 -0000 1.1.2.1 +++ squid/libbuf/String.h 4 Dec 2007 21:56:05 -0000 1.1.2.2 @@ -14,17 +14,49 @@ /* String */ #define strLen(s) ((/* const */ int)(s).len) #define strBuf(s) ((const char*)(s).nbuf) -#define strChr(s,ch) ((const char*)strchr(strBuf(s), (ch))) -#define strRChr(s,ch) ((const char*)strrchr(strBuf(s), (ch))) #define strStr(s,str) ((const char*)strstr(strBuf(s), (str))) #define strCmp(s,str) strcmp(strBuf(s), (str)) #define strNCmp(s,str,n) strncmp(strBuf(s), (str), (n)) #define strCaseCmp(s,str) strcasecmp(strBuf(s), (str)) #define strNCaseCmp(s,str,n) strncasecmp(strBuf(s), (str), (n)) -#define strSet(s,ptr,ch) (s).nbuf[ptr-(s).nbuf] = (ch) + +/* XXX this should eventually take the String length + offset under consideration! */ +#define strNCaseCmpOffset(s, o, str, n) strncasecmp(strBuf(s) + (o), (str), (n)) + +/* XXX this should also take into account the length! */ +#define strGetPos(s, o) *(strBuf(s) + o) + +static inline int +strChr(String s, char ch) +{ + const char *t2; + t2 = strchr(strBuf(s), ch); + if (t2 == NULL) + return -1; + return t2 - strBuf(s); +} + +static inline int +strRChr(String s, char ch) +{ + const char *t2; + t2 = strrchr(strBuf(s), ch); + if (t2 == NULL) + return -1; + return t2 - strBuf(s); + +} + +static inline void +strSet(String s, int o, char ch) +{ + assert(o < s.len); + s.nbuf[o] = ch; +} + #define strCut(s,pos) (((s).len = pos) , ((s).nbuf[pos] = '\0')) -#define strCutPtr(s,ptr) (((s).len = (ptr)-(s).nbuf) , ((s).nbuf[(s).len] = '\0')) #define strCat(s,str) stringAppend(&(s), (str), strlen(str)) + extern void stringInit(String * s, const char *str); extern void stringLimitInit(String * s, const char *str, int len); extern String stringDup(const String * s); Index: squid/src/ftp.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/ftp.c,v retrieving revision 1.44.4.2 retrieving revision 1.44.4.2.4.1 diff -u -r1.44.4.2 -r1.44.4.2.4.1 --- squid/src/ftp.c 26 Sep 2007 07:33:50 -0000 1.44.4.2 +++ squid/src/ftp.c 4 Dec 2007 21:56:05 -0000 1.44.4.2.4.1 @@ -1,6 +1,6 @@ /* - * $Id: ftp.c,v 1.44.4.2 2007/09/26 07:33:50 adri Exp $ + * $Id: ftp.c,v 1.44.4.2.4.1 2007/12/04 21:56:05 adri Exp $ * * DEBUG: section 9 File Transfer Protocol (FTP) * AUTHOR: Harvest Derived @@ -1030,11 +1030,12 @@ { request_t *request = ftpState->request; int l; - const char *t; - if ((t = strRChr(request->urlpath, ';')) != NULL) { - if (strncasecmp(t + 1, "type=", 5) == 0) { - ftpState->typecode = (char) xtoupper(*(t + 6)); - strCutPtr(request->urlpath, t); + int s; + + if ((s = strRChr(request->urlpath, ';')) != -1) { + if (strNCaseCmpOffset(request->urlpath, s + 1, "type=", 5) == 0) { + ftpState->typecode = (char) strGetPos(request->urlpath, s + 6); + strCut(request->urlpath, s); } } l = strLen(request->urlpath); @@ -1483,9 +1484,9 @@ static void ftpSendType(FtpStateData * ftpState) { - const char *t; const char *filename; char mode; + int s; /* * Ref section 3.2.2 of RFC 1738 */ @@ -1500,8 +1501,11 @@ if (ftpState->flags.isdir) { mode = 'A'; } else { - t = strRChr(ftpState->request->urlpath, '/'); - filename = t ? t + 1 : strBuf(ftpState->request->urlpath); + 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; mode = mimeGetTransferMode(filename); } break; @@ -2541,7 +2545,7 @@ const char *mime_enc = NULL; String urlpath = ftpState->request->urlpath; const char *filename = NULL; - const char *t = NULL; + int s; StoreEntry *e = ftpState->entry; http_reply *reply = e->mem_obj->reply; @@ -2551,7 +2555,12 @@ assert(e->mem_obj->inmem_hi == 0); EBIT_CLR(e->flags, ENTRY_FWD_HDR_WAIT); storeBuffer(e); /* released when done processing current data payload */ - filename = (t = strRChr(urlpath, '/')) ? t + 1 : strBuf(urlpath); + s = strRChr(urlpath, '/'); + filename = strBuf(urlpath); + /* XXX replace this as soon as possible with a String type! */ + if (s > -1) { + filename += s; + } if (ftpState->flags.isdir) { mime_type = "text/html"; } else { Index: squid/src/urn.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/urn.c,v retrieving revision 1.24.6.2 retrieving revision 1.24.6.2.4.1 diff -u -r1.24.6.2 -r1.24.6.2.4.1 --- squid/src/urn.c 29 Sep 2007 10:38:26 -0000 1.24.6.2 +++ squid/src/urn.c 4 Dec 2007 21:56:06 -0000 1.24.6.2.4.1 @@ -1,6 +1,6 @@ /* - * $Id: urn.c,v 1.24.6.2 2007/09/29 10:38:26 adri Exp $ + * $Id: urn.c,v 1.24.6.2.4.1 2007/12/04 21:56:06 adri Exp $ * * DEBUG: section 52 URN Parsing * AUTHOR: Kostas Anagnostakis @@ -101,7 +101,7 @@ { LOCAL_ARRAY(char, urlres, 4096); request_t *urlres_r = NULL; - const char *t; + int s; char *host; UrnState *urnState; StoreEntry *urlres_e; @@ -118,10 +118,11 @@ stringReset(&r->urlpath, new_path); xfree(new_path); } - if ((t = strChr(r->urlpath, ':')) != NULL) { - strSet(r->urlpath, t, '\0'); + if ((s = strChr(r->urlpath, ':')) != -1) { + /* XXX this doesn't update the length; perhaps strCut() is better? */ + strSet(r->urlpath, s, '\0'); host = xstrdup(strBuf(r->urlpath)); - strSet(r->urlpath, t, ':'); + strSet(r->urlpath, s, ':'); } else { host = xstrdup(strBuf(r->urlpath)); }