Cookie handling; deal with relative URI in Location YAZ-719
[yaz-moved-to-github.git] / src / cookie.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file cookie.c
7  * \brief HTTP cookie utility
8  */
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
28 yaz_cookies_t yaz_cookies_create(void)
29 {
30     yaz_cookies_t yc = xmalloc(sizeof(*yc));
31     yc->list = 0;
32     return yc;
33 }
34
35 void yaz_cookies_destroy(yaz_cookies_t yc)
36 {
37     if (yc)
38     {
39         struct cookie *c = yc->list;
40         while (c)
41         {
42             struct cookie *c1 = c->next;
43             xfree(c->name);
44             xfree(c->value);
45             xfree(c->path);
46             xfree(c->domain);
47             xfree(c);
48             c = c1;
49         }
50         xfree(yc);
51     }
52 }
53
54 void yaz_cookies_response(yaz_cookies_t yc, Z_HTTP_Response *res)
55 {
56     struct Z_HTTP_Header *h;
57     for (h = res->headers; h; h = h->next)
58     {
59         if (!strcmp(h->name, "Set-Cookie"))
60         {
61             const char *cp;
62             const char *cp1;
63             size_t len;
64             struct cookie *c;
65             cp = strchr(h->value, '=');
66             if (!cp)
67                 continue;
68             len = cp - h->value;
69             for (c = yc->list; c; c = c->next)
70                 if (!strncmp(h->value, c->name, len) && c->name[len] == '\0')
71                     break;
72             if (!c)
73             {
74                 c = xmalloc(sizeof(*c));
75                 c->name = xstrndup(h->value, len);
76                 c->value = 0;
77                 c->path = 0;
78                 c->domain = 0;
79                 c->next = yc->list;
80                 yc->list = c;
81             }
82             cp++; /* skip = */
83             cp1 = strchr(cp, ';');
84             if (!cp1)
85                 cp1 = cp + strlen(cp);
86             xfree(c->value);
87             c->value = xstrndup(cp, cp1 - cp);
88         }
89     }
90 }
91
92 void yaz_cookies_request(yaz_cookies_t yc, ODR odr, Z_HTTP_Request *req)
93 {
94     struct cookie *c;
95     size_t sz = 0;
96
97     for (c = yc->list; c; c = c->next)
98     {
99         if (c->name && c->value)
100             sz += strlen(c->name) + strlen(c->value) + 3;
101     }
102     if (sz)
103     {
104         char *buf = odr_malloc(odr, sz + 1);
105
106         *buf = '\0';
107         for (c = yc->list; c; c = c->next)
108         {
109             if (*buf)
110                 strcat(buf, "; ");
111             strcat(buf, c->name);
112             strcat(buf, "=");
113             strcat(buf, c->value);
114         }
115         z_HTTP_header_add(odr, &req->headers, "Cookie", buf);
116     }
117 }
118
119 /*
120  * Local variables:
121  * c-basic-offset: 4
122  * c-file-style: "Stroustrup"
123  * indent-tabs-mode: nil
124  * End:
125  * vim: shiftwidth=4 tabstop=8 expandtab
126  */
127