1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
7 * \brief HTTP cookie utility
13 #include <yaz/cookie.h>
24 struct yaz_cookies_s {
28 yaz_cookies_t yaz_cookies_create(void)
30 yaz_cookies_t yc = xmalloc(sizeof(*yc));
35 void yaz_cookies_destroy(yaz_cookies_t yc)
37 yaz_cookies_reset(yc);
41 void yaz_cookies_reset(yaz_cookies_t yc)
45 struct cookie *c = yc->list;
48 struct cookie *c1 = c->next;
60 void yaz_cookies_response(yaz_cookies_t yc, Z_HTTP_Response *res)
62 struct Z_HTTP_Header *h;
63 for (h = res->headers; h; h = h->next)
65 if (!strcmp(h->name, "Set-Cookie"))
71 cp = strchr(h->value, '=');
75 for (c = yc->list; c; c = c->next)
76 if (!strncmp(h->value, c->name, len) && c->name[len] == '\0')
80 c = xmalloc(sizeof(*c));
81 c->name = xstrndup(h->value, len);
89 cp1 = strchr(cp, ';');
91 cp1 = cp + strlen(cp);
93 c->value = xstrndup(cp, cp1 - cp);
98 void yaz_cookies_request(yaz_cookies_t yc, ODR odr, Z_HTTP_Request *req)
103 for (c = yc->list; c; c = c->next)
105 if (c->name && c->value)
106 sz += strlen(c->name) + strlen(c->value) + 3;
110 char *buf = odr_malloc(odr, sz + 1);
113 for (c = yc->list; c; c = c->next)
117 strcat(buf, c->name);
119 strcat(buf, c->value);
121 z_HTTP_header_add(odr, &req->headers, "Cookie", buf);
128 * c-file-style: "Stroustrup"
129 * indent-tabs-mode: nil
131 * vim: shiftwidth=4 tabstop=8 expandtab