cURL / Mailing Lists / curl-users / Single Mail

curl-users

strnequal -> checkprefix patch

From: <kromJx_at_crosswinds.net>
Date: Mon, 28 Oct 2002 14:45:51 EST

Daniel Stenberg:
> > #define strnequal2(s1, s2) strnequal((s1),(s2),strlen(s1))
> >
> > portable enough (and worth the hassle to change it) ?
>
> I think so, if you use a better name! ;-) 'checkprefix' or something would
> more clearly explain what it does.

Ok, here comes the (trivial) patch (base version is vanilla 7.10.1):

strnequal() is alse used in src/main.c, but this file doesn't include
lib/strnequal.h so I didn't change it there.

I also encountered a number of cases where the *second* argument of
strnequal is the constant. I didn't know whether it was safe to change
the order of the arguments and use checkprefix() instead, so I left
them alone.

diff --minimal --unidirectional-new-file --recursive -u -r curl-7.10.1/lib/cookie.c curl-7.10.1-checkprefix/lib/cookie.c
--- curl-7.10.1/lib/cookie.c 2002-09-04 13:19:35.000000000 +0000
+++ curl-7.10.1-checkprefix/lib/cookie.c 2002-10-28 19:19:31.000000000 +0000
@@ -519,7 +519,7 @@
     char *lineptr;
     bool headerline;
     while(fgets(line, MAX_COOKIE_LINE, fp)) {
- if(strnequal("Set-Cookie:", line, 11)) {
+ if(checkprefix("Set-Cookie:", line)) {
         /* This is a cookie line, get it! */
         lineptr=&line[11];
         headerline=TRUE;
diff --minimal --unidirectional-new-file --recursive -u -r curl-7.10.1/lib/formdata.c curl-7.10.1-checkprefix/lib/formdata.c
--- curl-7.10.1/lib/formdata.c 2002-09-04 13:19:36.000000000 +0000
+++ curl-7.10.1-checkprefix/lib/formdata.c 2002-10-28 19:19:35.000000000 +0000
@@ -1218,7 +1218,7 @@
        */
       
       if(file->contenttype &&
- !strnequal("text/", file->contenttype, 5)) {
+ !checkprefix("text/", file->contenttype)) {
         /* this is not a text content, mention our binary encoding */
         size += AddFormData(&form, "\r\nContent-Transfer-Encoding: binary", 0);
       }
diff --minimal --unidirectional-new-file --recursive -u -r curl-7.10.1/lib/strequal.h curl-7.10.1-checkprefix/lib/strequal.h
--- curl-7.10.1/lib/strequal.h 2002-09-04 13:19:42.000000000 +0000
+++ curl-7.10.1-checkprefix/lib/strequal.h 2002-10-28 19:14:19.000000000 +0000
@@ -31,6 +31,8 @@
 
 #define strequal(a,b) curl_strequal(a,b)
 #define strnequal(a,b,c) curl_strnequal(a,b,c)
+/* a shorter version of the above, used when the first argument is a constant */
+#define checkprefix(a,b) strnequal(a,b,strlen(a))
 
 #ifndef HAVE_STRLCAT
 #define strlcat(x,y,z) Curl_strlcat(x,y,z)
diff --minimal --unidirectional-new-file --recursive -u -r curl-7.10.1/lib/transfer.c curl-7.10.1-checkprefix/lib/transfer.c
--- curl-7.10.1/lib/transfer.c 2002-10-11 12:57:08.000000000 +0000
+++ curl-7.10.1-checkprefix/lib/transfer.c 2002-10-28 19:19:26.000000000 +0000
@@ -511,13 +511,13 @@
           }
 
           /* check for Content-Length: header lines to get size */
- if (strnequal("Content-Length:", k->p, 15) &&
+ if (checkprefix("Content-Length:", k->p) &&
               sscanf (k->p+15, " %ld", &k->contentlength)) {
             conn->size = k->contentlength;
             Curl_pgrsSetDownloadSize(data, k->contentlength);
             }
           /* check for Content-Type: header lines to get the mime-type */
- else if (strnequal("Content-Type:", k->p, 13)) {
+ else if (checkprefix("Content-Type:", k->p)) {
             char *start;
             char *end;
             int len;
@@ -587,7 +587,7 @@
             /* init our chunky engine */
             Curl_httpchunk_init(conn);
           }
- else if (strnequal("Content-Encoding:", k->p, 17) &&
+ else if (checkprefix("Content-Encoding:", k->p) &&
                    data->set.encoding) {
             /*
              * Process Content-Encoding. Look for the values: identity, gzip,
@@ -604,18 +604,18 @@
                 start++);
 
             /* Record the content-encoding for later use. 08/27/02 jhrg */
- if (strnequal("identity", start, 8))
+ if (checkprefix("identity", start))
               k->content_encoding = IDENTITY;
- else if (strnequal("deflate", start, 7))
+ else if (checkprefix("deflate", start))
               k->content_encoding = DEFLATE;
- else if (strnequal("gzip", start, 4)
- || strnequal("x-gzip", start, 6))
+ else if (checkprefix("gzip", start)
+ || checkprefix("x-gzip", start))
               k->content_encoding = GZIP;
- else if (strnequal("compress", start, 8)
- || strnequal("x-compress", start, 10))
+ else if (checkprefix("compress", start)
+ || checkprefix("x-compress", start))
               k->content_encoding = COMPRESS;
           }
- else if (strnequal("Content-Range:", k->p, 14)) {
+ else if (checkprefix("Content-Range:", k->p)) {
             if (sscanf (k->p+14, " bytes %d-", &k->offset) ||
                 sscanf (k->p+14, " bytes: %d-", &k->offset)) {
               /* This second format was added August 1st 2000 by Igor
@@ -628,11 +628,10 @@
             }
           }
           else if(data->cookies &&
- strnequal("Set-Cookie:", k->p, 11)) {
+ checkprefix("Set-Cookie:", k->p)) {
             Curl_cookie_add(data->cookies, TRUE, k->p+11, conn->name);
           }
- else if(strnequal("Last-Modified:", k->p,
- strlen("Last-Modified:")) &&
+ else if(checkprefix("Last-Modified:", k->p) &&
                   (data->set.timecondition || data->set.get_filetime) ) {
             time_t secs=time(NULL);
             k->timeofdoc = curl_getdate(k->p+strlen("Last-Modified:"),
@@ -642,7 +641,7 @@
           }
           else if ((k->httpcode >= 300 && k->httpcode < 400) &&
                    (data->set.http_follow_location) &&
- strnequal("Location:", k->p, 9)) {
+ checkprefix("Location:", k->p)) {
             /* this is the URL that the server advices us to get instead */
             char *ptr;
             char *start=k->p;

___________________________________
Build high quality traffic with the Web's Premier traffic building system. 2 to 1 ratio! http://www.itrafficstar.com/?ref=6

-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Received on 2002-10-28