~ubuntu-branches/ubuntu/edgy/curl/edgy

« back to all changes in this revision

Viewing changes to lib/cookie.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2006-06-29 15:04:24 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20060629150424-pn00qumt9sml8p4m
Tags: 7.15.4-1ubuntu1
Synchronize to Debian. Only change left: Removal of stunnel and
libdb4.2-dev build dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: cookie.c,v 1.73 2005/08/17 09:11:27 bagder Exp $
 
21
 * $Id: cookie.c,v 1.74 2006-05-24 22:46:39 bagder Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
/***
733
733
struct Cookie *Curl_cookie_getlist(struct CookieInfo *c,
734
734
                                   char *host, char *path, bool secure)
735
735
{
736
 
   struct Cookie *newco;
737
 
   struct Cookie *co;
738
 
   time_t now = time(NULL);
739
 
   struct Cookie *mainco=NULL;
740
 
 
741
 
   if(!c || !c->cookies)
742
 
      return NULL; /* no cookie struct or no cookies in the struct */
743
 
 
744
 
   co = c->cookies;
745
 
 
746
 
   while(co) {
747
 
     /* only process this cookie if it is not expired or had no expire
748
 
        date AND that if the cookie requires we're secure we must only
749
 
        continue if we are! */
750
 
     if( (co->expires<=0 || (co->expires> now)) &&
751
 
         (co->secure?secure:TRUE) ) {
752
 
 
753
 
       /* now check if the domain is correct */
754
 
       if(!co->domain ||
755
 
          (co->tailmatch && tailmatch(co->domain, host)) ||
756
 
          (!co->tailmatch && strequal(host, co->domain)) ) {
757
 
         /* the right part of the host matches the domain stuff in the
758
 
            cookie data */
759
 
 
760
 
         /* now check the left part of the path with the cookies path
761
 
            requirement */
762
 
         if(!co->path ||
763
 
            checkprefix(co->path, path) ) {
764
 
 
765
 
           /* and now, we know this is a match and we should create an
766
 
              entry for the return-linked-list */
767
 
 
768
 
           newco = (struct Cookie *)malloc(sizeof(struct Cookie));
769
 
           if(newco) {
770
 
             /* first, copy the whole source cookie: */
771
 
             memcpy(newco, co, sizeof(struct Cookie));
772
 
 
773
 
             /* then modify our next */
774
 
             newco->next = mainco;
775
 
 
776
 
             /* point the main to us */
777
 
             mainco = newco;
778
 
           }
779
 
           else {
780
 
              /* failure, clear up the allocated chain and return NULL */
781
 
             while(mainco) {
782
 
               co = mainco->next;
783
 
               free(mainco);
784
 
               mainco = co;
785
 
             }
786
 
 
787
 
             return NULL;
788
 
           }
789
 
         }
790
 
       }
791
 
     }
792
 
     co = co->next;
793
 
   }
794
 
 
795
 
   return mainco; /* return the new list */
796
 
}
797
 
 
 
736
  struct Cookie *newco;
 
737
  struct Cookie *co;
 
738
  time_t now = time(NULL);
 
739
  struct Cookie *mainco=NULL;
 
740
 
 
741
  if(!c || !c->cookies)
 
742
    return NULL; /* no cookie struct or no cookies in the struct */
 
743
 
 
744
  co = c->cookies;
 
745
 
 
746
  while(co) {
 
747
    /* only process this cookie if it is not expired or had no expire
 
748
       date AND that if the cookie requires we're secure we must only
 
749
       continue if we are! */
 
750
    if( (co->expires<=0 || (co->expires> now)) &&
 
751
        (co->secure?secure:TRUE) ) {
 
752
 
 
753
      /* now check if the domain is correct */
 
754
      if(!co->domain ||
 
755
         (co->tailmatch && tailmatch(co->domain, host)) ||
 
756
         (!co->tailmatch && strequal(host, co->domain)) ) {
 
757
        /* the right part of the host matches the domain stuff in the
 
758
           cookie data */
 
759
 
 
760
        /* now check the left part of the path with the cookies path
 
761
           requirement */
 
762
        if(!co->path ||
 
763
           checkprefix(co->path, path) ) {
 
764
 
 
765
          /* and now, we know this is a match and we should create an
 
766
             entry for the return-linked-list */
 
767
 
 
768
          newco = (struct Cookie *)malloc(sizeof(struct Cookie));
 
769
          if(newco) {
 
770
            /* first, copy the whole source cookie: */
 
771
            memcpy(newco, co, sizeof(struct Cookie));
 
772
 
 
773
            /* then modify our next */
 
774
            newco->next = mainco;
 
775
 
 
776
            /* point the main to us */
 
777
            mainco = newco;
 
778
          }
 
779
          else {
 
780
            /* failure, clear up the allocated chain and return NULL */
 
781
            while(mainco) {
 
782
              co = mainco->next;
 
783
              free(mainco);
 
784
              mainco = co;
 
785
            }
 
786
 
 
787
            return NULL;
 
788
          }
 
789
        }
 
790
      }
 
791
    }
 
792
    co = co->next;
 
793
  }
 
794
 
 
795
  return mainco; /* return the new list */
 
796
}
 
797
 
 
798
/*****************************************************************************
 
799
 *
 
800
 * Curl_cookie_clearall()
 
801
 *
 
802
 * Clear all existing cookies and reset the counter.
 
803
 *
 
804
 ****************************************************************************/
 
805
void Curl_cookie_clearall(struct CookieInfo *cookies)
 
806
{
 
807
  Curl_cookie_freelist(cookies->cookies);
 
808
  cookies->cookies = NULL;
 
809
  cookies->numcookies = 0;
 
810
}
798
811
 
799
812
/*****************************************************************************
800
813
 *
806
819
 
807
820
void Curl_cookie_freelist(struct Cookie *co)
808
821
{
809
 
   struct Cookie *next;
810
 
   if(co) {
811
 
      while(co) {
812
 
         next = co->next;
813
 
         free(co); /* we only free the struct since the "members" are all
 
822
  struct Cookie *next;
 
823
  if(co) {
 
824
    while(co) {
 
825
      next = co->next;
 
826
      free(co); /* we only free the struct since the "members" are all
814
827
                      just copied! */
815
 
         co = next;
816
 
      }
817
 
   }
818
 
}
 
828
      co = next;
 
829
    }
 
830
  }
 
831
}
 
832
 
 
833
 
 
834
/*****************************************************************************
 
835
 *
 
836
 * Curl_cookie_clearsess()
 
837
 *
 
838
 * Free all session cookies in the cookies list.
 
839
 *
 
840
 ****************************************************************************/
 
841
void Curl_cookie_clearsess(struct CookieInfo *cookies)
 
842
{
 
843
  struct Cookie *first, *curr, *next, *prev = NULL;
 
844
 
 
845
  if(!cookies->cookies)
 
846
    return;
 
847
 
 
848
  first = curr = prev = cookies->cookies;
 
849
 
 
850
  for(; curr; curr = next) {
 
851
    next = curr->next;
 
852
    if(!curr->expires) {
 
853
      if(first == curr)
 
854
        first = next;
 
855
 
 
856
      if(prev == curr)
 
857
        prev = next;
 
858
      else
 
859
        prev->next = next;
 
860
 
 
861
      free(curr);
 
862
      cookies->numcookies--;
 
863
    }
 
864
    else
 
865
      prev = curr;
 
866
  }
 
867
 
 
868
  cookies->cookies = first;
 
869
}
 
870
 
819
871
 
820
872
/*****************************************************************************
821
873
 *
826
878
 ****************************************************************************/
827
879
void Curl_cookie_cleanup(struct CookieInfo *c)
828
880
{
829
 
   struct Cookie *co;
830
 
   struct Cookie *next;
831
 
   if(c) {
832
 
      if(c->filename)
833
 
         free(c->filename);
834
 
      co = c->cookies;
 
881
  struct Cookie *co;
 
882
  struct Cookie *next;
 
883
  if(c) {
 
884
    if(c->filename)
 
885
      free(c->filename);
 
886
    co = c->cookies;
835
887
 
836
 
      while(co) {
837
 
         next = co->next;
838
 
         freecookie(co);
839
 
         co = next;
840
 
      }
841
 
      free(c); /* free the base struct as well */
842
 
   }
 
888
    while(co) {
 
889
      next = co->next;
 
890
      freecookie(co);
 
891
      co = next;
 
892
    }
 
893
    free(c); /* free the base struct as well */
 
894
  }
843
895
}
844
896
 
845
897
/* get_netscape_format()
850
902
*/
851
903
static char *get_netscape_format(const struct Cookie *co)
852
904
{
853
 
   return aprintf(
854
 
     "%s%s\t" /* domain */
855
 
     "%s\t"   /* tailmatch */
856
 
     "%s\t"   /* path */
857
 
     "%s\t"   /* secure */
858
 
     "%" FORMAT_OFF_T "\t"   /* expires */
859
 
     "%s\t"   /* name */
860
 
     "%s",    /* value */
861
 
     /* Make sure all domains are prefixed with a dot if they allow
862
 
        tailmatching. This is Mozilla-style. */
863
 
     (co->tailmatch && co->domain && co->domain[0] != '.')? ".":"",
864
 
     co->domain?co->domain:"unknown",
865
 
     co->tailmatch?"TRUE":"FALSE",
866
 
     co->path?co->path:"/",
867
 
     co->secure?"TRUE":"FALSE",
868
 
     co->expires,
869
 
     co->name,
870
 
     co->value?co->value:"");
 
905
  return aprintf(
 
906
    "%s%s\t" /* domain */
 
907
    "%s\t"   /* tailmatch */
 
908
    "%s\t"   /* path */
 
909
    "%s\t"   /* secure */
 
910
    "%" FORMAT_OFF_T "\t"   /* expires */
 
911
    "%s\t"   /* name */
 
912
    "%s",    /* value */
 
913
    /* Make sure all domains are prefixed with a dot if they allow
 
914
       tailmatching. This is Mozilla-style. */
 
915
    (co->tailmatch && co->domain && co->domain[0] != '.')? ".":"",
 
916
    co->domain?co->domain:"unknown",
 
917
    co->tailmatch?"TRUE":"FALSE",
 
918
    co->path?co->path:"/",
 
919
    co->secure?"TRUE":"FALSE",
 
920
    co->expires,
 
921
    co->name,
 
922
    co->value?co->value:"");
871
923
}
872
924
 
873
925
/*