fix use of libmagic
[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 _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include <microhttpd.h>
29 #include <json.h>
30
31 #if defined(USE_MAGIC_MIME_TYPE)
32 #include <magic.h>
33 #endif
34
35 #include "afb-method.h"
36 #include "afb-req-itf.h"
37 #include "afb-hreq.h"
38 #include "session.h"
39 #include "verbose.h"
40
41 #define SIZE_RESPONSE_BUFFER   8000
42
43 static char empty_string[] = "";
44
45 static const char uuid_header[] = "x-afb-uuid";
46 static const char uuid_arg[] = "uuid";
47 static const char uuid_cookie[] = "uuid";
48
49 static const char token_header[] = "x-afb-token";
50 static const char token_arg[] = "token";
51 static const char token_cookie[] = "token";
52
53 static char *cookie_name = NULL;
54 static char *cookie_setter = NULL;
55
56 struct hreq_data {
57         struct hreq_data *next;
58         char *key;
59         size_t length;
60         char *value;
61         char *path;
62 };
63
64 static struct json_object *req_json(struct afb_hreq *hreq);
65 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name);
66 static void req_fail(struct afb_hreq *hreq, const char *status, const char *info);
67 static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info);
68 static const char *req_raw(struct afb_hreq *hreq, size_t *size);
69 static void req_send(struct afb_hreq *hreq, char *buffer, size_t size);
70 static int req_session_create(struct afb_hreq *hreq);
71 static int req_session_check(struct afb_hreq *hreq, int refresh);
72 static void req_session_close(struct afb_hreq *hreq);
73
74 static const struct afb_req_itf afb_hreq_itf = {
75         .json = (void*)req_json,
76         .get = (void*)req_get,
77         .success = (void*)req_success,
78         .fail = (void*)req_fail,
79         .raw = (void*)req_raw,
80         .send = (void*)req_send,
81         .session_create = (void*)req_session_create,
82         .session_check = (void*)req_session_check,
83         .session_close = (void*)req_session_close
84 };
85
86 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
87 {
88         struct hreq_data *data = hreq->data;
89         while (data != NULL) {
90                 if (!strcasecmp(data->key, key))
91                         return data;
92                 data = data->next;
93         }
94         if (create) {
95                 data = calloc(1, sizeof *data);
96                 if (data != NULL) {
97                         data->key = strdup(key);
98                         if (data->key == NULL) {
99                                 free(data);
100                                 data = NULL;
101                         } else {
102                                 data->next = hreq->data;
103                                 hreq->data = data;
104                         }
105                 }
106         }
107         return data;
108 }
109
110 /* a valid subpath is a relative path not looking deeper than root using .. */
111 static int validsubpath(const char *subpath)
112 {
113         int l = 0, i = 0;
114
115         while (subpath[i]) {
116                 switch (subpath[i++]) {
117                 case '.':
118                         if (!subpath[i])
119                                 break;
120                         if (subpath[i] == '/') {
121                                 i++;
122                                 break;
123                         }
124                         if (subpath[i++] == '.') {
125                                 if (!subpath[i]) {
126                                         if (--l < 0)
127                                                 return 0;
128                                         break;
129                                 }
130                                 if (subpath[i++] == '/') {
131                                         if (--l < 0)
132                                                 return 0;
133                                         break;
134                                 }
135                         }
136                 default:
137                         while (subpath[i] && subpath[i] != '/')
138                                 i++;
139                         l++;
140                 case '/':
141                         break;
142                 }
143         }
144         return 1;
145 }
146
147 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
148 {
149         char *cookie;
150         const char *k, *v;
151         k = va_arg(args, const char *);
152         while (k != NULL) {
153                 v = va_arg(args, const char *);
154                 MHD_add_response_header(response, k, v);
155                 k = va_arg(args, const char *);
156         }
157         if (hreq->context != NULL && asprintf(&cookie, cookie_setter, hreq->context->uuid)) {
158                 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
159                 free(cookie);
160         }
161         MHD_queue_response(hreq->connection, status, response);
162         MHD_destroy_response(response);
163 }
164
165 void afb_hreq_reply(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, ...)
166 {
167         va_list args;
168         va_start(args, response);
169         afb_hreq_reply_v(hreq, status, response, args);
170         va_end(args);
171 }
172
173 void afb_hreq_reply_empty(struct afb_hreq *hreq, unsigned status, ...)
174 {
175         va_list args;
176         va_start(args, status);
177         afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT), args);
178         va_end(args);
179 }
180
181 void afb_hreq_reply_static(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
182 {
183         va_list args;
184         va_start(args, buffer);
185         afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_PERSISTENT), args);
186         va_end(args);
187 }
188
189 void afb_hreq_reply_copy(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
190 {
191         va_list args;
192         va_start(args, buffer);
193         afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_COPY), args);
194         va_end(args);
195 }
196
197 void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
198 {
199         va_list args;
200         va_start(args, buffer);
201         afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_FREE), args);
202         va_end(args);
203 }
204
205 #if defined(USE_MAGIC_MIME_TYPE)
206
207 #if !defined(MAGIC_DB)
208 #define MAGIC_DB "/usr/share/misc/magic.mgc"
209 #endif
210
211 static magic_t lazy_libmagic()
212 {
213         static int done = 0;
214         static magic_t result = NULL;
215
216         if (!done) {
217                 done = 1;
218                 /* MAGIC_MIME tells magic to return a mime of the file,
219                          but you can specify different things */
220                 if (verbosity)
221                         fprintf(stderr, "Loading mimetype default magic database\n");
222
223                 result = magic_open(MAGIC_MIME_TYPE);
224                 if (result == NULL) {
225                         fprintf(stderr,"ERROR: unable to initialize magic library\n");
226                 }
227                 /* Warning: should not use NULL for DB
228                                 [libmagic bug wont pass efence check] */
229                 else if (magic_load(result, MAGIC_DB) != 0) {
230                         fprintf(stderr,"cannot load magic database - %s\n",
231                                         magic_error(result));
232                         magic_close(result);
233                         result = NULL;
234                 }
235         }
236
237         return result;
238 }
239
240 static const char *magic_mimetype_fd(int fd)
241 {
242         magic_t lib = lazy_libmagic();
243         return lib ? magic_descriptor(lib, fd) : NULL;
244 }
245
246 #endif
247
248 static const char *mimetype_fd_name(int fd, const char *filename)
249 {
250         const char *result = NULL;
251
252 #if defined(INFER_EXTENSION)
253         const char *extension = strrchr(filename, '.');
254         if (extension) {
255                 static const char *const known[][2] = {
256                         { ".js",   "text/javascript" },
257                         { ".html", "text/html" },
258                         { ".css",  "text/css" },
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