restoring cookie of session
[src/app-framework-binder.git] / src / afb-hreq.c
1 /*
2  * Copyright 2016 IoT.bzh
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define USE_MAGIC_MIME_TYPE
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28
29 #include <microhttpd.h>
30 #include <json.h>
31
32 #if defined(USE_MAGIC_MIME_TYPE)
33 #include <magic.h>
34 #endif
35
36 #include "afb-method.h"
37 #include "afb-req-itf.h"
38 #include "afb-hreq.h"
39 #include "session.h"
40 #include "verbose.h"
41
42 #define SIZE_RESPONSE_BUFFER   8000
43
44 static char empty_string[] = "";
45
46 static const char uuid_header[] = "x-afb-uuid";
47 static const char uuid_arg[] = "uuid";
48 static const char uuid_cookie[] = "uuid";
49
50 static const char token_header[] = "x-afb-token";
51 static const char token_arg[] = "token";
52 static const char token_cookie[] = "token";
53
54 static char *cookie_name = NULL;
55 static char *cookie_setter = NULL;
56
57 struct hreq_data {
58         struct hreq_data *next;
59         char *key;
60         size_t length;
61         char *value;
62         char *path;
63 };
64
65 static struct json_object *req_json(struct afb_hreq *hreq);
66 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name);
67 static void req_fail(struct afb_hreq *hreq, const char *status, const char *info);
68 static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info);
69 static const char *req_raw(struct afb_hreq *hreq, size_t *size);
70 static void req_send(struct afb_hreq *hreq, char *buffer, size_t size);
71 static int req_session_create(struct afb_hreq *hreq);
72 static int req_session_check(struct afb_hreq *hreq, int refresh);
73 static void req_session_close(struct afb_hreq *hreq);
74
75 static const struct afb_req_itf afb_hreq_itf = {
76         .json = (void*)req_json,
77         .get = (void*)req_get,
78         .success = (void*)req_success,
79         .fail = (void*)req_fail,
80         .raw = (void*)req_raw,
81         .send = (void*)req_send,
82         .session_create = (void*)req_session_create,
83         .session_check = (void*)req_session_check,
84         .session_close = (void*)req_session_close
85 };
86
87 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
88 {
89         struct hreq_data *data = hreq->data;
90         while (data != NULL) {
91                 if (!strcasecmp(data->key, key))
92                         return data;
93                 data = data->next;
94         }
95         if (create) {
96                 data = calloc(1, sizeof *data);
97                 if (data != NULL) {
98                         data->key = strdup(key);
99                         if (data->key == NULL) {
100                                 free(data);
101                                 data = NULL;
102                         } else {
103                                 data->next = hreq->data;
104                                 hreq->data = data;
105                         }
106                 }
107         }
108         return data;
109 }
110
111 /* a valid subpath is a relative path not looking deeper than root using .. */
112 static int validsubpath(const char *subpath)
113 {
114         int l = 0, i = 0;
115
116         while (subpath[i]) {
117                 switch (subpath[i++]) {
118                 case '.':
119                         if (!subpath[i])
120                                 break;
121                         if (subpath[i] == '/') {
122                                 i++;
123                                 break;
124                         }
125                         if (subpath[i++] == '.') {
126                                 if (!subpath[i]) {
127                                         if (--l < 0)
128                                                 return 0;
129                                         break;
130                                 }
131                                 if (subpath[i++] == '/') {
132                                         if (--l < 0)
133                                                 return 0;
134                                         break;
135                                 }
136                         }
137                 default:
138                         while (subpath[i] && subpath[i] != '/')
139                                 i++;
140                         l++;
141                 case '/':
142                         break;
143                 }
144         }
145         return 1;
146 }
147
148 #if defined(USE_MAGIC_MIME_TYPE)
149
150 #if !defined(MAGIC_DB)
151 #define MAGIC_DB "/usr/share/misc/magic.mgc"
152 #endif
153
154 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
155 {
156         char *cookie;
157         const char *k, *v;
158         k = va_arg(args, const char *);
159         while (k != NULL) {
160                 v = va_arg(args, const char *);
161                 MHD_add_response_header(response, k, v);
162                 k = va_arg(args, const char *);
163         }
164         if (hreq->context != NULL && asprintf(&cookie, cookie_setter, hreq->context->uuid)) {
165                 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
166                 free(cookie);
167         }
168         MHD_queue_response(hreq->connection, status, response);
169         MHD_destroy_response(response);
170 }
171
172 void afb_hreq_reply(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, ...)
173 {
174         va_list args;
175         va_start(args, response);
176         afb_hreq_reply_v(hreq, status, response, args);
177         va_end(args);
178 }
179
180 void afb_hreq_reply_empty(struct afb_hreq *hreq, unsigned status, ...)
181 {
182         va_list args;
183         va_start(args, status);
184         afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT), args);
185         va_end(args);
186 }
187
188 void afb_hreq_reply_static(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
189 {
190         va_list args;
191         va_start(args, buffer);
192         afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_PERSISTENT), args);
193         va_end(args);
194 }
195
196 void afb_hreq_reply_copy(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
197 {
198         va_list args;
199         va_start(args, buffer);
200         afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_COPY), args);
201         va_end(args);
202 }
203
204 void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
205 {
206         va_list args;
207         va_start(args, buffer);
208         afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_FREE), args);
209         va_end(args);
210 }
211
212 static magic_t lazy_libmagic()
213 {
214         static int done = 0;
215         static magic_t result = NULL;
216
217         if (!done) {
218                 done = 1;
219                 /* MAGIC_MIME tells magic to return a mime of the file,
220                          but you can specify different things */
221                 if (verbosity)
222                         printf("Loading mimetype default magic database\n");
223
224                 result = magic_open(MAGIC_MIME_TYPE);
225                 if (result == NULL) {
226                         fprintf(stderr,"ERROR: unable to initialize magic library\n");
227                 }
228                 /* Warning: should not use NULL for DB
229                                 [libmagic bug wont pass efence check] */
230                 else if (magic_load(result, MAGIC_DB) != 0) {
231                         fprintf(stderr,"cannot load magic database - %s\n",
232                                         magic_error(result));
233                         magic_close(result);
234                         result = NULL;
235                 }
236         }
237
238         return result;
239 }
240
241 static const char *magic_mimetype_fd(int fd)
242 {
243         magic_t lib = lazy_libmagic();
244         return lib ? magic_descriptor(lib, fd) : NULL;
245 }
246
247 #endif
248
249 static const char *mimetype_fd_name(int fd, const char *filename)
250 {
251         const char *result = NULL;
252
253 #if defined(INFER_EXTENSION)
254         const char *extension = strrchr(filename, '.');
255         if (extension) {
256                 static const char *const known[][2] = {
257                         { ".js", "text/javascript" },
258                         { ".html", "text/html" },
259                         { NULL, NULL }
260                 };
261                 int i = 0;
262                 while (known[i][0]) {
263                         if (!strcasecmp(extension, known[i][0])) {
264                                 result = known[i][1];
265                                 break;
266                         }
267                         i++;
268                 }
269         }
270 #endif
271 #if defined(USE_MAGIC_MIME_TYPE)
272         if (result == NULL)
273                 result = magic_mimetype_fd(fd);
274 #endif
275         return result;
276 }
277
278 void afb_hreq_free(struct afb_hreq *hreq)
279 {
280         struct hreq_data *data;
281         if (hreq != NULL) {
282                 if (hreq->postform != NULL)
283                         MHD_destroy_post_processor(hreq->postform);
284                 for (data = hreq->data; data; data = hreq->data) {
285                         hreq->data = data->next;
286                         if (data->path) {
287                                 unlink(data->path);
288                                 free(data->path);
289                         }
290                         free(data->key);
291                         free(data->value);
292                         free(data);
293                 }
294                 ctxClientPut(hreq->context);
295                 json_object_put(hreq->json);
296                 free(hreq);
297         }
298 }
299
300 /*
301  * Removes the 'prefix' of 'length' from the tail of 'hreq'
302  * if and only if the prefix exists and is terminated by a leading
303  * slash
304  */
305 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
306 {
307         /* check the prefix ? */
308         if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
309             || strncasecmp(prefix, hreq->tail, length))
310                 return 0;
311
312         /* removes successives / */
313         while (length < hreq->lentail && hreq->tail[length + 1] == '/')
314                 length++;
315
316         /* update the tail */
317         hreq->lentail -= length;
318         hreq->tail += length;
319         return 1;
320 }
321
322 int afb_hreq_valid_tail(struct afb_hreq *hreq)
323 {
324         return validsubpath(hreq->tail);
325 }
326
327 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
328 {
329         afb_hreq_reply_empty(hreq, status, NULL);
330 }
331
332 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
333 {
334         int rc;
335         int fd;
336         unsigned int status;
337         struct stat st;
338         char etag[1 + 2 * sizeof(int)];
339         const char *inm;
340         struct MHD_Response *response;
341         const char *mimetype;
342
343         /* Opens the file or directory */
344         if (filename[0]) {
345                 fd = openat(dirfd, filename, O_RDONLY);
346                 if (fd < 0) {
347                         if (errno == ENOENT)
348                                 return 0;
349                         afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
350                         return 1;
351                 }
352         } else {
353                 fd = dup(dirfd);
354                 if (fd < 0) {
355                         afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
356                         return 1;
357                 }
358         }
359
360         /* Retrieves file's status */
361         if (fstat(fd, &st) != 0) {
362                 close(fd);
363                 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
364                 return 1;
365         }
366
367         /* serve directory */
368         if (S_ISDIR(st.st_mode)) {
369                 static const char *indexes[] = { "index.html", NULL };
370                 int i = 0;
371                 rc = 0;
372                 while (indexes[i] != NULL) {
373                         if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
374                                 if (hreq->url[hreq->lenurl - 1] != '/') {
375                                         /* the redirect is needed for reliability of relative path */
376                                         char *tourl = alloca(hreq->lenurl + 2);
377                                         memcpy(tourl, hreq->url, hreq->lenurl);
378                                         tourl[hreq->lenurl] = '/';
379                                         tourl[hreq->lenurl + 1] = 0;
380                                         rc = afb_hreq_redirect_to(hreq, tourl);
381                                 } else {
382                                         rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
383                                 }
384                                 break;
385                         }
386                         i++;
387                 }
388                 close(fd);
389                 return rc;
390         }
391
392         /* Don't serve special files */
393         if (!S_ISREG(st.st_mode)) {
394                 close(fd);
395                 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
396                 return 1;
397         }
398
399         /* Check the method */
400         if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
401                 close(fd);
402                 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
403                 return 1;
404         }
405
406         /* computes the etag */
407         sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
408
409         /* checks the etag */
410         inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
411         if (inm && 0 == strcmp(inm, etag)) {
412                 /* etag ok, return NOT MODIFIED */
413                 close(fd);
414                 if (verbosity)
415                         fprintf(stderr, "Not Modified: [%s]\n", filename);
416                 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
417                 status = MHD_HTTP_NOT_MODIFIED;
418         } else {
419                 /* check the size */
420                 if (st.st_size != (off_t) (size_t) st.st_size) {
421                         close(fd);
422                         afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
423                         return 1;
424                 }
425
426                 /* create the response */
427                 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
428                 status = MHD_HTTP_OK;
429
430                 /* set the type */
431                 mimetype = mimetype_fd_name(fd, filename);
432                 if (mimetype != NULL)
433                         MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
434         }
435
436         /* fills the value and send */
437         afb_hreq_reply(hreq, status, response,
438                         MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
439                         MHD_HTTP_HEADER_ETAG, etag,
440                         NULL);
441         return 1;
442 }
443
444 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
445 {
446         int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
447         if (rc == 0)
448                 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
449         return 1;
450 }
451
452 int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url)
453 {
454         afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
455                         MHD_HTTP_HEADER_LOCATION, url, NULL);
456         if (verbosity)
457                 fprintf(stderr, "redirect from [%s] to [%s]\n", hreq->url, url);
458         return 1;
459 }
460
461 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
462 {
463         return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
464 }
465
466 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
467 {
468         struct hreq_data *data = get_data(hreq, name, 0);
469         return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
470 }
471
472 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
473 {
474         return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
475 }
476
477 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
478 {
479         void *p;
480         struct hreq_data *hdat = get_data(hreq, key, 1);
481         if (hdat->path != NULL) {
482                 return 0;
483         }
484         p = realloc(hdat->value, hdat->length + size + 1);
485         if (p == NULL) {
486                 return 0;
487         }
488         hdat->value = p;
489         memcpy(&hdat->value[hdat->length], data, size);
490         hdat->length += size;
491         hdat->value[hdat->length] = 0;
492         return 1;
493 }
494
495 static int opentempfile(char **path)
496 {
497         int fd;
498         char *fname;
499
500         fname = strdup("XXXXXX"); /* TODO improve the path */
501         if (fname == NULL)
502                 return -1;
503
504         fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
505         if (fd < 0)
506                 free(fname);
507         else
508                 *path = fname;
509         return fd;
510 }
511
512 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
513 {
514         int fd;
515         ssize_t sz;
516         struct hreq_data *hdat = get_data(hreq, key, 1);
517
518         if (hdat->value == NULL) {
519                 hdat->value = strdup(file);
520                 if (hdat->value == NULL)
521                         return 0;
522                 fd = opentempfile(&hdat->path);
523         } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
524                 return 0;
525         } else {
526                 fd = open(hdat->path, O_WRONLY|O_APPEND);
527         }
528         if (fd < 0)
529                 return 0;
530         while (size) {
531                 sz = write(fd, data, size);
532                 if (sz >= 0) {
533                         hdat->length += (size_t)sz;
534                         size -= (size_t)sz;
535                         data += sz;
536                 } else if (errno != EINTR)
537                         break;
538         }
539         close(fd);
540         return !size;
541 }
542
543 struct afb_req afb_hreq_to_req(struct afb_hreq *hreq)
544 {
545         return (struct afb_req){ .itf = &afb_hreq_itf, .data = hreq };
546 }
547
548 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
549 {
550         struct hreq_data *hdat = get_data(hreq, name, 0);
551         if (hdat)
552                 return (struct afb_arg){
553                         .name = hdat->key,
554                         .value = hdat->value,
555                         .path = hdat->path
556                 };
557                 
558         return (struct afb_arg){
559                 .name = name,
560                 .value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name),
561                 .path = NULL
562         };
563 }
564
565 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
566 {
567         json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
568         return 1;
569 }
570
571 static struct json_object *req_json(struct afb_hreq *hreq)
572 {
573         struct hreq_data *hdat;
574         struct json_object *obj, *val;
575
576         obj = hreq->json;
577         if (obj == NULL) {
578                 hreq->json = obj = json_object_new_object();
579                 if (obj == NULL) {
580                 } else {
581                         MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
582                         for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
583                                 if (hdat->path == NULL)
584                                         val = hdat->value ? json_object_new_string(hdat->value) : NULL;
585                                 else {
586                                         val = json_object_new_object();
587                                         if (val == NULL) {
588                                         } else {
589                                                 json_object_object_add(val, "file", json_object_new_string(hdat->value));
590                                                 json_object_object_add(val, "path", json_object_new_string(hdat->path));
591                                         }
592                                 }
593                                 json_object_object_add(obj, hdat->key, val);
594                         }
595                 }
596         }
597         return obj;
598 }
599
600 static const char *req_raw(struct afb_hreq *hreq, size_t *size)
601 {
602         const char *result = json_object_get_string(req_json(hreq));
603         *size = result ? strlen(result) : 0;
604         return result;
605 }
606
607 static void req_send(struct afb_hreq *hreq, char *buffer, size_t size)
608 {
609         afb_hreq_reply_free(hreq, MHD_HTTP_OK, size, buffer, NULL);
610 }
611
612 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
613 {
614         ssize_t len = stpncpy(buf, json_object_to_json_string(obj)+pos, max) - buf;
615         return len ? : -1;
616 }
617
618 static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *status, const char *info, json_object *resp)
619 {
620         json_object *root, *request;
621         struct MHD_Response *response;
622
623         root = json_object_new_object();
624         json_object_object_add(root, "jtype", json_object_new_string("afb-reply"));
625         request = json_object_new_object();
626         json_object_object_add(root, "request", request);
627         json_object_object_add(request, "status", json_object_new_string(status));
628         if (info)
629                 json_object_object_add(request, "info", json_object_new_string(info));
630         if (resp)
631                 json_object_object_add(root, "response", resp);
632         if (hreq->context) {
633                 json_object_object_add(request, uuid_arg, json_object_new_string(hreq->context->uuid));
634                 json_object_object_add(request, token_arg, json_object_new_string(hreq->context->token));
635         }
636
637         response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, SIZE_RESPONSE_BUFFER, (void*)send_json_cb, root, (void*)json_object_put);
638         afb_hreq_reply(hreq, retcode, response, NULL);
639 }
640
641 static void req_fail(struct afb_hreq *hreq, const char *status, const char *info)
642 {
643         req_reply(hreq, MHD_HTTP_OK, status, info, NULL);
644 }
645
646 static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info)
647 {
648         req_reply(hreq, MHD_HTTP_OK, "success", info, obj);
649 }
650
651 struct AFB_clientCtx *afb_hreq_context(struct afb_hreq *hreq)
652 {
653         const char *uuid;
654
655         if (hreq->context == NULL) {
656                 uuid = afb_hreq_get_header(hreq, uuid_header);
657                 if (uuid == NULL)
658                         uuid = afb_hreq_get_argument(hreq, uuid_arg);
659                 if (uuid == NULL)
660                         uuid = afb_hreq_get_cookie(hreq, cookie_name);
661                 hreq->context = ctxClientGetForUuid(uuid);
662         }
663         return hreq->context;
664 }
665
666 static int req_session_create(struct afb_hreq *hreq)
667 {
668         struct AFB_clientCtx *context = afb_hreq_context(hreq);
669         if (context == NULL)
670                 return 0;
671         if (context->created)
672                 return 0;
673         return req_session_check(hreq, 1);
674 }
675
676 static int req_session_check(struct afb_hreq *hreq, int refresh)
677 {
678         const char *token;
679
680         struct AFB_clientCtx *context = afb_hreq_context(hreq);
681
682         if (context == NULL)
683                 return 0;
684
685         token = afb_hreq_get_header(hreq, token_header);
686         if (token == NULL)
687                 token = afb_hreq_get_argument(hreq, token_arg);
688         if (token == NULL)
689                 token = afb_hreq_get_cookie(hreq, token_cookie);
690         if (token == NULL)
691                 return 0;
692
693         if (!ctxTokenCheck (context, token))
694                 return 0;
695
696         if (refresh) {
697                 ctxTokenNew (context);
698         }
699
700         return 1;
701 }
702
703 static void req_session_close(struct afb_hreq *hreq)
704 {
705         struct AFB_clientCtx *context = afb_hreq_context(hreq);
706         if (context != NULL)
707                 ctxClientClose(context);
708 }
709
710 int afb_hreq_init_cookie(int port, const char *path, int maxage)
711 {
712         int rc;
713
714         free(cookie_name);
715         free(cookie_setter);
716         cookie_name = NULL;
717         cookie_setter = NULL;
718
719         path = path ? : "/";
720         rc = asprintf(&cookie_name, "x-afb-uuid-%d", port);
721         if (rc < 0)
722                 return 0;
723         rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
724                         cookie_name, path, maxage);
725         if (rc < 0)
726                 return 0;
727         return 1;
728 }
729
730