Extend get_org_info (snippets) to return original string YAZ-836
[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     yaz_cookies_reset(yc);
38     xfree(yc);
39 }
40
41 void yaz_cookies_reset(yaz_cookies_t yc)
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
60 void yaz_cookies_response(yaz_cookies_t yc, Z_HTTP_Response *res)
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
98 void yaz_cookies_request(yaz_cookies_t yc, ODR odr, Z_HTTP_Request *req)
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