You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by ia...@apache.org on 2002/06/22 05:36:57 UTC

cvs commit: httpd-2.0/modules/mappers mod_rewrite.c mod_rewrite.h

ianh        2002/06/21 20:36:57

  Modified:    .        CHANGES
               docs/manual/mod mod_rewrite.html.en mod_rewrite.xml
               modules/mappers mod_rewrite.c mod_rewrite.h
  Log:
  new configuration option for mod_rewrite to set cookies.
  configuration is like the following
  
  RewriteRule (.*) - [CO=<cookiename>:$1:<domain>:<expiry in minutes>]
  
  Submitted by:	Brian Degenhardt <bm...@mp3.com>
  Reviewed by:	Ian Holsman
  
  Revision  Changes    Path
  1.844     +3 -0      httpd-2.0/CHANGES
  
  Index: CHANGES
  ===================================================================
  RCS file: /home/cvs/httpd-2.0/CHANGES,v
  retrieving revision 1.843
  retrieving revision 1.844
  diff -u -r1.843 -r1.844
  --- CHANGES	21 Jun 2002 11:00:43 -0000	1.843
  +++ CHANGES	22 Jun 2002 03:36:56 -0000	1.844
  @@ -1,6 +1,9 @@
   
   Changes with Apache 2.0.40
   
  +  *) mod_rewrite can now set cookies  (RewriteRule (.*) - [CO=name:$1:.domain])
  +     [Brian Degenhardt <bm...@mp3.com>, Ian Holsman]
  +
     *) Fix perchild to work with apachectl by adding -k support to perchild.
        PR 10074  [Jeff Trawick]
   
  
  
  
  1.3       +9 -0      httpd-2.0/docs/manual/mod/mod_rewrite.html.en
  
  Index: mod_rewrite.html.en
  ===================================================================
  RCS file: /home/cvs/httpd-2.0/docs/manual/mod/mod_rewrite.html.en,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- mod_rewrite.html.en	28 May 2002 07:35:00 -0000	1.2
  +++ mod_rewrite.html.en	22 Jun 2002 03:36:57 -0000	1.3
  @@ -1467,6 +1467,15 @@
           it in a following RewriteCond pattern via
           <code>%{ENV:VAR}</code>. Use this to strip but remember
           information from URLs.</li>
  +
  +        <li>
  +        '<strong><code>cookie|CO=</code></strong><em>NAME</em>:<em>VAL</em>:<em>domain</em>[:<em>lifetime</em>]'
  +        (set <strong>co</strong>cookie)<br>
  +        This sets a cookie on the client's browser.  The cookie's name
  +        is specified by <em>NAME</em> and the value is
  +        <em>VAL</em>. The <em>domain</em> field is the domain of the
  +        cookie, such as '.apache.org' and the optional <em>lifetime</em>
  +        is the lifetime of the cookie in minutes.</li>
         </ul>
   
   <blockquote><table><tr><td bgcolor="#e0e5f5"><p align="center"><strong>Note</strong></p> Never forget that <em>Pattern</em> is
  
  
  
  1.4       +10 -0     httpd-2.0/docs/manual/mod/mod_rewrite.xml
  
  Index: mod_rewrite.xml
  ===================================================================
  RCS file: /home/cvs/httpd-2.0/docs/manual/mod/mod_rewrite.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- mod_rewrite.xml	23 May 2002 14:50:11 -0000	1.3
  +++ mod_rewrite.xml	22 Jun 2002 03:36:57 -0000	1.4
  @@ -1605,6 +1605,16 @@
           it in a following RewriteCond pattern via
           <code>%{ENV:VAR}</code>. Use this to strip but remember
           information from URLs.</li>
  +
  +        <li>
  +        '<strong><code>cookie|CO=</code></strong><em>NAME</em>:<em>VAL</em>:<em>domain</em>[:<em>lifetime</em>]'
  +        (set <strong>co</strong>cookie)<br>
  +        This sets a cookie on the client's browser.  The cookie's name
  +        is specified by <em>NAME</em> and the value is
  +        <em>VAL</em>. The <em>domain</em> field is the domain of the
  +        cookie, such as '.apache.org' and the optional <em>lifetime</em>
  +        is the lifetime of the cookie in minutes.</li>
  + 
         </ul>
   
   <note><title>Note</title> Never forget that <em>Pattern</em> is
  
  
  
  1.123     +78 -2     httpd-2.0/modules/mappers/mod_rewrite.c
  
  Index: mod_rewrite.c
  ===================================================================
  RCS file: /home/cvs/httpd-2.0/modules/mappers/mod_rewrite.c,v
  retrieving revision 1.122
  retrieving revision 1.123
  diff -u -r1.122 -r1.123
  --- mod_rewrite.c	11 Jun 2002 14:43:04 -0000	1.122
  +++ mod_rewrite.c	22 Jun 2002 03:36:57 -0000	1.123
  @@ -707,6 +707,7 @@
       newrule->forced_responsecode = HTTP_MOVED_TEMPORARILY;
       newrule->flags  = RULEFLAG_NONE;
       newrule->env[0] = NULL;
  +    newrule->cookie[0] = NULL;
       newrule->skip   = 0;
       if (a3 != NULL) {
           if ((err = cmd_rewriterule_parseflagfield(cmd->pool, newrule,
  @@ -875,6 +876,17 @@
               return "RewriteRule: too many environment flags 'E'";
           }
       }
  +    else  if ( strcasecmp(key, "cookie") == 0 || strcasecmp(key, "CO") == 0) {
  +        for (i = 0; (cfg->cookie[i] != NULL) && (i < MAX_COOKIE_FLAGS); i++)
  +            ;
  +        if (i < MAX_COOKIE_FLAGS) {
  +            cfg->cookie[i] = apr_pstrdup(p, val);
  +            cfg->cookie[i+1] = NULL;
  +        }
  +        else {
  +            return "RewriteRule: too many cookie flags 'CO'";
  +        }
  +    }
       else if (   strcasecmp(key, "nosubreq") == 0
                || strcasecmp(key, "NS") == 0      ) {
           cfg->flags |= RULEFLAG_IGNOREONSUBREQ;
  @@ -1960,11 +1972,13 @@
       /*
        *  If this is a pure matching rule (`RewriteRule <pat> -')
        *  we stop processing and return immediately. The only thing
  -     *  we have not to forget are the environment variables
  -     *  (`RewriteRule <pat> - [E=...]')
  +     *  we have not to forget are the environment variables and
  +     *  cookies:
  +     *  (`RewriteRule <pat> - [E=...,CO=...]')
        */
       if (strcmp(output, "-") == 0) {
           do_expand_env(r, p->env, briRR, briRC);
  +        do_expand_cookie(r, p->cookie, briRR, briRC);
           if (p->forced_mimetype != NULL) {
               if (perdir == NULL) {
                   /* In the per-server context we can force the MIME-type
  @@ -2014,6 +2028,12 @@
       do_expand_env(r, p->env, briRR, briRC);
   
       /*
  +     *  Also set cookies for any cookie strings
  +     *  (`RewriteRule .. .. [CO=<string>]').
  +     */
  +    do_expand_cookie(r, p->cookie, briRR, briRC);
  +
  +    /*
        *  Now replace API's knowledge of the current URI:
        *  Replace r->filename with the new URI string and split out
        *  an on-the-fly generated QUERY_STRING part into r->args
  @@ -2460,6 +2480,18 @@
       }
   }
   
  +static void do_expand_cookie( request_rec *r, char *cookie[],
  +                              backrefinfo *briRR, backrefinfo *briRC)
  +{
  +    int i;
  +    char buf[MAX_STRING_LEN];
  +
  +    for (i = 0; cookie[i] != NULL; i++) {
  +        do_expand(r, cookie[i], buf, sizeof(buf), briRR, briRC);
  +        add_cookie(r, buf);
  +    }
  +}
  +
   
   /*
   **
  @@ -4094,6 +4126,50 @@
           apr_cpystrn(val, cp+1, sizeof(val));
           apr_table_set(r->subprocess_env, var, val);
           rewritelog(r, 5, "setting env variable '%s' to '%s'", var, val);
  +    }
  +}
  +
  +static void add_cookie(request_rec *r, char *s)
  +{
  +    char *var;
  +    char *val;
  +    char *domain;
  +    char *expires;
  +
  +    char *tok_cntx;
  +    char *cookie;
  +
  +    if (s) {
  +        var = apr_strtok(s, ":", &tok_cntx);
  +        val = apr_strtok(NULL, ":", &tok_cntx);
  +        domain = apr_strtok(NULL, ":", &tok_cntx);
  +        /** the line below won't hit the token ever **/
  +        expires = apr_strtok(NULL, ":", &tok_cntx); 
  +
  +        if (var && val && domain) {
  +            /* FIX: use cached time similar to how logging does it */
  +            cookie = apr_pstrcat( r->pool, 
  +                                  var,
  +                                  "=",
  +                                  val,
  +                                  "; path=/; domain=",
  +                                  domain,
  +                                  (expires)? "; expires=" : NULL,
  +                                  (expires)? ap_ht_time(r->pool, 
  +                                                        r->request_time + 
  +                                                        (60 * atol(expires)),
  +                                                         "%a, %d-%b-%Y %T GMT", 1)
  +                                           : NULL, 
  +                                  NULL);
  +
  +            
  +            /* 
  +             * XXX: should we add it to err_headers_out as well ?
  +             * if we do we need to be careful that only ONE gets sent out
  +             */
  +            apr_table_add(r->headers_out, "Set-Cookie", cookie);
  +            rewritelog(r, 5, "setting cookie '%s' to '%s'", var, val);
  +        }
       }
   }
   
  
  
  
  1.35      +9 -0      httpd-2.0/modules/mappers/mod_rewrite.h
  
  Index: mod_rewrite.h
  ===================================================================
  RCS file: /home/cvs/httpd-2.0/modules/mappers/mod_rewrite.h,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- mod_rewrite.h	30 May 2002 00:02:59 -0000	1.34
  +++ mod_rewrite.h	22 Jun 2002 03:36:57 -0000	1.35
  @@ -231,9 +231,14 @@
   #endif
   
   #define MAX_ENV_FLAGS 15
  +#define MAX_COOKIE_FLAGS 15
  +/*** max cookie size in rfc 2109 ***/
  +#define MAX_COOKIE_LEN 4096
   
   #define MAX_NMATCH    10
   
  +
  +
   /*
   **
   **  our private data structures we handle with
  @@ -272,6 +277,7 @@
       char    *forced_mimetype;      /* forced MIME type of substitution */
       int      forced_responsecode;  /* forced HTTP redirect response status */
       char    *env[MAX_ENV_FLAGS+1]; /* added environment variables */
  +    char    *cookie[MAX_COOKIE_FLAGS+1]; /* added cookies */
       int      skip;                 /* number of next rules to skip */
   } rewriterule_entry;
   
  @@ -414,6 +420,8 @@
   		      backrefinfo *briRR, backrefinfo *briRC);
   static void do_expand_env(request_rec *r, char *env[],
   			  backrefinfo *briRR, backrefinfo *briRC);
  +static void do_expand_cookie(request_rec *r, char *cookie[],
  +			  backrefinfo *briRR, backrefinfo *briRC);
   
       /* URI transformation function */
   static void  splitout_queryargs(request_rec *r, int qsappend);
  @@ -481,6 +489,7 @@
   static int    parseargline(char *str, char **a1, char **a2, char **a3);
   static int    prefix_stat(const char *path, apr_finfo_t *sb);
   static void   add_env_variable(request_rec *r, char *s);
  +static void   add_cookie(request_rec *r, char *s);
   static int    subreq_ok(request_rec *r);
   
       /* Lexicographic Comparison */