YAZ  5.23.1
cookie.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12 
13 #include <yaz/cookie.h>
14 #include <yaz/log.h>
15 
16 struct cookie {
17  char *name;
18  char *value;
19  char *path;
20  char *domain;
21  struct cookie *next;
22 };
23 
24 struct yaz_cookies_s {
25  struct cookie *list;
26 };
27 
29 {
30  yaz_cookies_t yc = xmalloc(sizeof(*yc));
31  yc->list = 0;
32  return yc;
33 }
34 
36 {
38  xfree(yc);
39 }
40 
42 {
43  if (yc)
44  {
45  struct cookie *c = yc->list;
46  while (c)
47  {
48  struct cookie *c1 = c->next;
49  xfree(c->name);
50  xfree(c->value);
51  xfree(c->path);
52  xfree(c->domain);
53  xfree(c);
54  c = c1;
55  }
56  yc->list = 0;
57  }
58 }
59 
61 {
62  struct Z_HTTP_Header *h;
63  for (h = res->headers; h; h = h->next)
64  {
65  if (!strcmp(h->name, "Set-Cookie"))
66  {
67  const char *cp;
68  const char *cp1;
69  size_t len;
70  struct cookie *c;
71  cp = strchr(h->value, '=');
72  if (!cp)
73  continue;
74  len = cp - h->value;
75  for (c = yc->list; c; c = c->next)
76  if (!strncmp(h->value, c->name, len) && c->name[len] == '\0')
77  break;
78  if (!c)
79  {
80  c = xmalloc(sizeof(*c));
81  c->name = xstrndup(h->value, len);
82  c->value = 0;
83  c->path = 0;
84  c->domain = 0;
85  c->next = yc->list;
86  yc->list = c;
87  }
88  cp++; /* skip = */
89  cp1 = strchr(cp, ';');
90  if (!cp1)
91  cp1 = cp + strlen(cp);
92  xfree(c->value);
93  c->value = xstrndup(cp, cp1 - cp);
94  }
95  }
96 }
97 
99 {
100  struct cookie *c;
101  size_t sz = 0;
102 
103  for (c = yc->list; c; c = c->next)
104  {
105  if (c->name && c->value)
106  sz += strlen(c->name) + strlen(c->value) + 3;
107  }
108  if (sz)
109  {
110  char *buf = odr_malloc(odr, sz + 1);
111 
112  *buf = '\0';
113  for (c = yc->list; c; c = c->next)
114  {
115  if (*buf)
116  strcat(buf, "; ");
117  strcat(buf, c->name);
118  strcat(buf, "=");
119  strcat(buf, c->value);
120  }
121  z_HTTP_header_add(odr, &req->headers, "Cookie", buf);
122  }
123 }
124 
125 /*
126  * Local variables:
127  * c-basic-offset: 4
128  * c-file-style: "Stroustrup"
129  * indent-tabs-mode: nil
130  * End:
131  * vim: shiftwidth=4 tabstop=8 expandtab
132  */
133 
char * value
Definition: zgdu.h:44
Z_HTTP_Header * headers
Definition: zgdu.h:60
struct cookie * next
Definition: cookie.c:21
char * domain
Definition: cookie.c:20
#define xstrndup(s, n)
utility macro which calls xstrndup_f
Definition: xmalloc.h:57
char * value
Definition: cookie.c:18
void * odr_malloc(ODR o, size_t size)
Definition: odr_mem.c:31
struct cookie * list
Definition: cookie.c:25
#define xfree(x)
utility macro which calls xfree_f
Definition: xmalloc.h:53
Z_HTTP_Header * next
Definition: zgdu.h:45
void z_HTTP_header_add(ODR o, Z_HTTP_Header **hp, const char *n, const char *v)
Definition: http.c:189
Definition: odr.h:124
#define xmalloc(x)
utility macro which calls malloc_f
Definition: xmalloc.h:49
char * name
Definition: cookie.c:17
Definition: cookie.c:16
char * name
Definition: zgdu.h:43
Z_HTTP_Header * headers
Definition: zgdu.h:52
Logging utility.
char * path
Definition: cookie.c:19