You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by "Roy T. Fielding" <fi...@liege.ICS.UCI.EDU> on 1996/09/24 02:37:37 UTC

Re: patch to 1.2 for tom tromey's HTTP compliance patches

> This patch merges the three patches Tom posted, and has them applied to
> the current 1.2 cvs snapshot.  It compiles fine, and I tested each
> condition Tom claims to have fixed.  
> 
> 
> Index: http_protocol.c
> ===================================================================
> RCS file: /export/home/cvs/apache/src/http_protocol.c,v
> retrieving revision 1.46
> diff -C3 -r1.46 http_protocol.c
> *** http_protocol.c	1996/09/17 14:53:54	1.46
> --- http_protocol.c	1996/09/23 03:15:10
> ***************
> *** 327,332 ****
> --- 327,333 ----
>       char *if_unmodified = table_get (r->headers_in, "If-Unmodified-Since");
>       char *if_nonematch = table_get (r->headers_in, "If-None-Match");
>       char *if_match = table_get (r->headers_in, "If-Match");
> +     time_t server_time = time (NULL);
>   
>       /* Invalid, future time... just ignore it */
>       if (mtime > r->request_time) return OK;
> ***************
> *** 369,375 ****
>       if ((r->status < 200) || (r->status >= 300))
>           return OK;
>   
> !     if (if_modified_since && later_than(gmtime(&mtime), if_modified_since))
>           return USE_LOCAL_COPY;
>       else if (if_unmodified && !later_than(gmtime(&mtime), if_unmodified))
>           return PRECONDITION_FAILED;
> --- 370,378 ----
>       if ((r->status < 200) || (r->status >= 300))
>           return OK;
>   
> !     if (if_modified_since && !r->header_only &&
> ! 	later_than(gmtime(&mtime), if_modified_since) &&
> ! 	!later_than(gmtime(&server_time), if_modified_since))
>           return USE_LOCAL_COPY;
>       else if (if_unmodified && !later_than(gmtime(&mtime), if_unmodified))
>           return PRECONDITION_FAILED;

Nope, -1 on this part -- later_than(x, if_modified_since) does an
alpha->date conversion, which should only be done once.  I think this
code (and later_than()) is a good candidate for rewriting.

What we need is a

   time_t parse_date(char *HTTP_date)

function that returns (time_t)0 if the date is invalid.  Then we
can just do numeric comparisons on the returned value. 

Unfortunately, I am on a trial jury Tuesday and Wednesday.  Here is
something I wrote 2.5 years ago on the same issue, just in case somebody
wants to try and make it work.  Note that some of this doesn't apply
any more, and strptime() is not portable [which is why RobM used sscanf],
but this is the general drift.

......Roy

================================================================
To: Rob McCool <ro...@ncsa.uiuc.edu>
Subject: 1.21b If-modified-since Solved
Date: Sun, 24 Apr 1994 05:13:37 -0700
From: "Roy T. Fielding" <fi...@simplon.ics.uci.edu>

Ignore all the prior stuff.

I finally got it to work!  Here is what I did:

First, I tried to stay with the existing function, but for some reason
I could not get a hold of the conceptual model of what we were trying to
do with the later_than name.  So, I changed its functionality to get_gmtime
as follows:

in httpd.c replace later_than def (line 560) with:
--------------
time_t get_gmtime(char *gdstr);
--------------

in http_mime.c, replace set_last_modified() with:
--------------
void set_last_modified(time_t t, FILE *out) {
    struct tm *tms;
    char ts[MAX_STRING_LEN];

    tms = gmtime(&t);
    strftime(ts,MAX_STRING_LEN,HTTP_TIME_FORMAT,tms);
    strcpy(last_modified,ts);

    if(!ims[0])
        return;

    if(t <= get_gmtime(ims))
        die(USE_LOCAL_COPY,NULL,out);
}
--------------
Note the <= --- that's why the later_than was causing me so much trouble.

finally, in util.c replace later_than() with:
--------------
time_t get_gmtime(char *gdstr) {
    char *ip;
    struct tm gtime;

    /* Whatever format we're looking at, it will start with weekday. */
    /* Skip to first space. */
    if(!(ip = strchr(gdstr,' ')))
        return 0;
    else
        while(*ip && isspace(*ip))
            ++ip;

    if(!(*ip))
        return 0;

    if(isalpha(*ip)) {
        /* ctime  (Apr 24 02:49:37 GMT 1994) */
        strptime(ip,"%b %d %T GMT %Y",&gtime);
    }
    else if(ip[2] == '-') {
        /* RFC 850 (24-Apr-94 02:49:37 GMT) */
        strptime(ip,"%d-%b-%y %T GMT",&gtime);
    }
    else {
        /* RFC 822 (24 Apr 1994 02:49:37 GMT) */
        strptime(ip,"%d %b %Y %T GMT",&gtime);
    }
    if (!(gtime.tm_mday && gtime.tm_year))      /* Check for failed strptime */
        return 0;

    gtime.tm_isdst  = 0;                        /* Force it to be GMT */
    gtime.tm_gmtoff = 0;

    return timegm(&gtime);
}
--------------

Naturally, this is dependent on the portability of strptime() and
struct tm including tm_isdst and tm_gmtoff.  I have tested it thoroughly
on  http://simplon.ics.uci.edu/  and it works great (it even behaves
robustly if the date passed is garbage).

Whatcha think?

........Roy


Re: patch to 1.2 for tom tromey's HTTP compliance patches

Posted by Brian Behlendorf <br...@organic.com>.
Okay, another try.  Here's a patch which implements *two* of the three
compliance patches Tom submitted - the third was vetoed by Roy.  I'll commit
this tomorrow unless I hear dissent.  It's not major, it's just something that
needs checking.

	Brian

taz [148] cvs diff -C3 http_protocol.c httpd.h util.c 
Index: http_protocol.c
===================================================================
RCS file: /export/home/cvs/apache/src/http_protocol.c,v
retrieving revision 1.48
diff -C3 -r1.48 http_protocol.c
*** http_protocol.c     1996/09/28 02:01:02     1.48
--- http_protocol.c     1996/09/28 02:21:15
***************
*** 329,335 ****
      char *if_match = table_get (r->headers_in, "If-Match");
  
      /* Invalid, future time... just ignore it */
!     if (mtime > r->request_time) return OK;
  
      ts = gm_timestr_822(r->pool, (mtime > r->request_time) ? r->request_time : mtime);
      table_set (r->headers_out, "Last-Modified", ts);
--- 329,335 ----
      char *if_match = table_get (r->headers_in, "If-Match");
  
      /* Invalid, future time... just ignore it */
!     if (mtime >= r->request_time) return OK;
  
      ts = gm_timestr_822(r->pool, (mtime > r->request_time) ? r->request_time : mtime);
      table_set (r->headers_out, "Last-Modified", ts);
***************
*** 369,375 ****
      if ((r->status < 200) || (r->status >= 300))
          return OK;
  
!     if (if_modified_since && later_than(gmtime(&mtime), if_modified_since))
          return USE_LOCAL_COPY;
      else if (if_unmodified && !later_than(gmtime(&mtime), if_unmodified))
          return PRECONDITION_FAILED;
--- 369,376 ----
      if ((r->status < 200) || (r->status >= 300))
          return OK;
  
!     if (if_modified_since && !r->header_only &&
!       later_than(gmtime(&mtime), if_modified_since))
          return USE_LOCAL_COPY;
      else if (if_unmodified && !later_than(gmtime(&mtime), if_unmodified))
          return PRECONDITION_FAILED;
***************
*** 508,515 ****
          return 0;
  
      r->the_request = pstrdup (r->pool, l);
!     r->method = getword(r->pool, &ll,' ');
!     uri = getword(r->pool, &ll,' ');
      uri = check_fulluri(r, uri);
      parse_uri (r, uri);
      
--- 509,516 ----
          return 0;
  
      r->the_request = pstrdup (r->pool, l);
!     r->method = getword_white(r->pool, &ll);
!     uri = getword_white(r->pool, &ll);
      uri = check_fulluri(r, uri);
      parse_uri (r, uri);
      
Index: httpd.h
===================================================================
RCS file: /export/home/cvs/apache/src/httpd.h,v
retrieving revision 1.50
diff -C3 -r1.50 httpd.h
*** httpd.h     1996/09/24 12:44:58     1.50
--- httpd.h     1996/09/26 04:05:21
***************
*** 556,561 ****
--- 556,562 ----
  /* String handling */     
       
  char *getword(pool *p, char **line, char stop);
+ char *getword_white(pool *p, char **line);
  char *getword_nulls (pool *p, char **line, char stop);
  char *getword_conf (pool *p, char **line);      
  
Index: util.c
===================================================================
RCS file: /export/home/cvs/apache/src/util.c,v
retrieving revision 1.20
diff -C3 -r1.20 util.c
*** util.c      1996/09/26 04:16:06     1.20
--- util.c      1996/09/26 04:13:29
***************
*** 50,56 ****
   *
   */
  
! /* $Id: util.c,v 1.20 1996/09/26 04:16:06 brian Exp $ */
  
  /*
   * str.c: string utility things
--- 50,56 ----
   *
   */
  
! /* $Id: util.c,v 1.19 1996/09/23 20:38:53 jim Exp $ */
  
  /*
   * str.c: string utility things
***************
*** 395,400 ****
--- 395,429 ----
      
      return res;
  }
+ 
+ char *getword_white(pool* atrans, char **line) {
+     int pos = -1, x;
+     char *res;
+ 
+     for(x=0;(*line)[x];x++) {
+         if (isspace((*line)[x])) {
+           pos=x;
+           break;
+       }
+    }
+ 
+     if (pos == -1) {
+         res = pstrdup (atrans, *line);
+       *line += strlen (*line);
+       return res;
+     }
+ 
+     res = palloc(atrans, pos + 1);
+     strncpy (res, *line, pos);
+     res[pos] = '\0';
+ 
+     while (isspace((*line)[pos])) ++pos;
+ 
+     *line += pos;
+ 
+     return res;
+ }
+ 
  char *getword_nulls(pool* atrans, char **line, char stop) {
      int pos = ind(*line, stop);
      char *res;


Re: patch to 1.2 for tom tromey's HTTP compliance patches

Posted by Tom Tromey <tr...@creche.cygnus.com>.
Roy> Nope, -1 on this part -- later_than(x, if_modified_since) does an
Roy> alpha-> date conversion, which should only be done once.  I think
Roy> this code (and later_than()) is a good candidate for rewriting.

Roy> What we need is a
Roy>    time_t parse_date(char *HTTP_date)
Roy> function that returns (time_t)0 if the date is invalid.  Then we
Roy> can just do numeric comparisons on the returned value.

I doubt this will gain very much in terms of speed or clarity.
However, it might gain in error-checking capabilities.

One choice is to use the "getdate.y" that is floating around (there's
a modified version in Tcl these days, for instance).  It can handle a
large number of date formats (in fact, maybe it is overkill).  Another
choice would be to find a free strptime and use it on machines that
don't have it natively.

The HTTP standard is actually fairly imprecise about what constitutes
a valid date.  For instance, if the weekday is wrong (but everything
else is ok), is that invalid?  (I think so -- but that currently isn't
verified by Apache.)  Ditto stuff like "Feb 31", incorrect times, etc.
getdate.y checks some of this (but not all of it, eg the weekday).

Also, the HTTP standard doesn't put any boundaries on dates.  So my
test suite tries some dates that I happen to know are outside the
range of a (32 bit) time_t.  I was moderately suprised that Apache
actually passed these tests.  I realize that this isn't important
except for pedagogical reasons.  Still... sometime soon I'm going to
write a back end for my test suite so that I can run it against a
netscape server.  It would be nice if Apache performed better, even if
only pedagogically :-)

Tom
-- 
tromey@cygnus.com                 Member, League for Programming Freedom