2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2006 - 2015 Reyk Floeter <reyk@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
49 #define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
52 # define G_TOUT(t) ((t).tv_sec)
54 # define G_TOUT(t) (t)
57 /* provided by OpenBSD' base libevent but not in any header? */
58 extern void bufferevent_read_pressure_cb(struct evbuffer *, size_t,
61 void proxy_init(struct privsep *, struct privsep_proc *, void *);
62 int proxy_launch(struct galileo *);
63 void proxy_inflight_dec(const char *);
64 int proxy_dispatch_parent(int, struct privsep_proc *, struct imsg *);
65 int proxy_resurl(struct client *, const char *, char *, size_t);
66 void proxy_translate_gemtext(struct client *);
67 void proxy_resolved(struct asr_result *, void *);
68 void proxy_connect(int, short, void *);
69 int proxy_start_reply(struct client *, int, const char *);
70 void proxy_read(struct bufferevent *, void *);
71 void proxy_write(struct bufferevent *, void *);
72 void proxy_error(struct bufferevent *, short, void *);
73 int proxy_bufferevent_add(struct event *, int);
74 void proxy_tls_writecb(int, short, void *);
75 void proxy_tls_readcb(int, short, void *);
77 static struct privsep_proc procs[] = {
78 { "parent", PROC_PARENT, proxy_dispatch_parent },
81 volatile int proxy_clients;
82 volatile int proxy_inflight;
83 uint32_t proxy_fcg_id;
86 proxy(struct privsep *ps, struct privsep_proc *p)
88 proc_run(ps, p, procs, nitems(procs), proxy_init, NULL);
92 proxy_init(struct privsep *ps, struct privsep_proc *p, void *arg)
94 if (config_init(ps->ps_env) == -1)
95 fatal("failed to initialize configuration");
97 /* We use a custom shutdown callback */
98 /* p->p_shutdown = proxy_shutdown */
100 if (pledge("stdio recvfd unix inet dns", NULL) == -1)
105 proxy_launch(struct galileo *env)
107 event_add(&env->sc_evsock, NULL);
112 proxy_purge(struct proxy *pr)
118 proxy_inflight_dec(const char *why)
121 log_debug("%s: inflight decremented, now %d, %s",
122 __func__, proxy_inflight, why);
126 proxy_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
128 struct privsep *ps = p->p_ps;
129 struct galileo *env = ps->ps_env;
131 switch (imsg->hdr.type) {
133 if (config_getproxy(env, imsg) == -1)
134 fatal("config_getproxy");
139 env->sc_sock_fd = config_getsock(env, imsg);
140 if (env->sc_sock_fd == -1)
141 fatal("config_getsock");
143 event_set(&env->sc_evsock, env->sc_sock_fd,
144 EV_READ | EV_PERSIST, fcgi_accept, env);
145 evtimer_set(&env->sc_evpause, fcgi_accept, env);
148 config_getcfg(env, imsg);
154 config_getreset(env, imsg);
164 portok(const char **url, struct client *clt)
166 const char *u = *url;
167 const char *port = clt->clt_pc->proxy_port;
172 if (*u == '\0' || *u == '/' || *u == '?' || *u == '#')
177 if (strncmp(u, port, len) != 0)
180 if (*u != '\0' && *u != '/' && *u != '?' && *u != '#')
187 proxy_resurl(struct client *clt, const char *url, char *buf, size_t len)
192 if (clt->clt_server_name == NULL)
195 l = strlen(clt->clt_server_name);
198 log_warn("%s: zero-sized buffer!", __func__);
202 /* look if it's an absolute URI */
203 if (strncmp(url, "//", 2) == 0) {
205 if (strncmp(tmp, clt->clt_server_name, l) != 0)
209 if (!portok(&tmp, clt))
212 } else if (strncmp(url, "gemini://", 9) == 0) {
214 if (strncmp(tmp, clt->clt_server_name, l) != 0)
218 if (!portok(&tmp, clt))
223 while (isalpha((unsigned char)*tmp) || *tmp == '+')
225 if (strncmp(tmp, "://", 3) == 0)
229 /* maybe it's an absolute path? */
230 if (*url == '\0' || *url == '/') {
231 if (strlcpy(buf, clt->clt_script_name, len) >= len)
233 if (strlcat(buf, url + 1, len) >= len)
238 /* otherwise, leave it as it is */
240 if (strlcpy(buf, url, len) >= len)
246 gemtext_translate_line(struct client *clt, char *line)
251 /* preformatted line / closing */
252 if (clt->clt_translate & TR_PRE) {
253 if (!strncmp(line, "```", 3)) {
254 clt->clt_translate &= ~TR_PRE;
255 return (tp_pre_close(clt->clt_tp));
258 if (tp_htmlescape(clt->clt_tp, line) == -1)
260 return (clt_putc(clt, '\n'));
264 if (!strncmp(line, "* ", 2)) {
265 if (clt->clt_translate & TR_NAV) {
266 if (clt_puts(clt, "</ul></nav>") == -1)
268 clt->clt_translate &= ~TR_NAV;
271 if (!(clt->clt_translate & TR_LIST)) {
272 if (clt_puts(clt, "<ul>") == -1)
274 clt->clt_translate |= TR_LIST;
277 if (clt_puts(clt, "<li>") == -1 ||
278 tp_htmlescape(clt->clt_tp, line + 2) == -1 ||
279 clt_puts(clt, "</li>") == -1)
284 if (clt->clt_translate & TR_LIST) {
285 if (clt_puts(clt, "</ul>") == -1)
287 clt->clt_translate &= ~TR_LIST;
291 if (!strncmp(line, "=>", 2)) {
295 line += strspn(line, " \t");
297 label = line + strcspn(line, " \t");
302 label += strspn(label, " \t");
307 if (proxy_resurl(clt, line, buf, sizeof(buf)) == 0)
310 url = line; /* leave the URL as it is */
312 if (fnmatch("*.jpg", url, 0) == 0 ||
313 fnmatch("*.jpeg", url, 0) == 0 ||
314 fnmatch("*.gif", url, 0) == 0 ||
315 fnmatch("*.png", url, 0) == 0 ||
316 fnmatch("*.svg", url, 0) == 0 ||
317 fnmatch("*.webp", url, 0) == 0) {
318 if (clt->clt_translate & TR_NAV) {
319 if (clt_puts(clt, "</ul></nav>") == -1)
321 clt->clt_translate &= ~TR_NAV;
324 if (tp_figure(clt->clt_tp, url, label) == -1)
330 if (!(clt->clt_translate & TR_NAV)) {
331 if (clt_puts(clt, "<nav><ul>") == -1)
333 clt->clt_translate |= TR_NAV;
336 if (clt_puts(clt, "<li><a href='") == -1)
339 if (tp_urlescape(clt->clt_tp, url) == -1 ||
340 clt_puts(clt, "'>") == -1 ||
341 tp_htmlescape(clt->clt_tp, label) == -1 ||
342 clt_puts(clt, "</a></li>") == -1)
348 if (clt->clt_translate & TR_NAV) {
349 if (clt_puts(clt, "</ul></nav>") == -1)
351 clt->clt_translate &= ~TR_NAV;
355 if (!strncmp(line, "```", 3)) {
357 line += strspn(line, " \t");
359 clt->clt_translate |= TR_PRE;
360 return (tp_pre_open(clt->clt_tp, line));
365 if (clt_puts(clt, "<blockquote>") == -1 ||
366 tp_htmlescape(clt->clt_tp, line + 1) == -1 ||
367 clt_puts(clt, "</blockquote>") == -1)
373 if (!strncmp(line, "###", 3)) {
374 if (clt_puts(clt, "<h3>") == -1 ||
375 tp_htmlescape(clt->clt_tp, line + 3) == -1 ||
376 clt_puts(clt, "</h3>") == -1)
380 if (!strncmp(line, "##", 2)) {
381 if (clt_puts(clt, "<h2>") == -1 ||
382 tp_htmlescape(clt->clt_tp, line + 2) == -1 ||
383 clt_puts(clt, "</h2>") == -1)
387 if (!strncmp(line, "#", 1)) {
388 if (clt_puts(clt, "<h1>") == -1 ||
389 tp_htmlescape(clt->clt_tp, line + 1) == -1 ||
390 clt_puts(clt, "</h1>") == -1)
395 /* Not following strictly the gemini specification... */
400 if (clt_puts(clt, "<p>") == -1 ||
401 tp_htmlescape(clt->clt_tp, line) == -1 ||
402 clt_puts(clt, "</p>") == -1)
409 proxy_translate_gemtext(struct client *clt)
411 struct bufferevent *bev = clt->clt_bev;
412 struct evbuffer *src = EVBUFFER_INPUT(bev);
418 line = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF);
422 r = gemtext_translate_line(clt, line);
429 struct proxy_config *
430 proxy_match(struct galileo *env, const char *name)
437 TAILQ_FOREACH(pr, &env->sc_proxies, pr_entry) {
438 if (!strcmp(name, pr->pr_conf.host))
439 return (&pr->pr_conf);
446 proxy_start_request(struct galileo *env, struct client *clt)
448 struct addrinfo hints;
449 struct asr_query *query;
453 if (clt->clt_path_info == NULL) {
454 log_warnx("PATH_INFO not defined!");
455 if (proxy_start_reply(clt, 501, "text/html") == -1)
457 if (tp_error(clt->clt_tp, -1, "internal server error") == -1)
459 return (fcgi_end_request(clt, 1));
462 if ((clt->clt_pc = proxy_match(env, clt->clt_server_name)) == NULL) {
463 if (proxy_start_reply(clt, 501, "text/html") == -1)
465 if (tp_error(clt->clt_tp, -1, "unknown server") == -1)
467 return (fcgi_end_request(clt, 1));
470 if (clt->clt_bodylen != 0 && clt->clt_body == NULL) {
471 if (proxy_start_reply(clt, 400, "text/html") == -1)
473 if (tp_error(clt->clt_tp, -1, "bad request") == -1)
475 return (fcgi_end_request(clt, 1));
479 r = asprintf(&url, "%s%s?%s", clt->clt_script_name,
480 clt->clt_path_info + 1, clt->clt_body);
482 return (fcgi_end_request(clt, 1));
484 if (proxy_start_reply(clt, 302, url) == -1 ||
485 fcgi_end_request(clt, 1) == -1) {
493 memset(&hints, 0, sizeof(hints));
494 hints.ai_family = AF_UNSPEC;
495 hints.ai_socktype = SOCK_STREAM;
497 query = getaddrinfo_async(clt->clt_pc->proxy_addr,
498 clt->clt_pc->proxy_port, &hints, NULL);
500 log_warn("getaddrinfo_async");
501 return (fcgi_abort_request(clt));
504 clt->clt_evasr = event_asr_run(query, proxy_resolved, clt);
505 if (clt->clt_evasr == NULL) {
506 log_warn("event_asr_run");
508 return (fcgi_abort_request(clt));
515 proxy_resolved(struct asr_result *res, void *d)
517 struct client *clt = d;
518 struct proxy_config *pc = clt->clt_pc;
520 clt->clt_evasr = NULL;
522 if (res->ar_gai_errno != 0) {
523 log_warnx("failed to resolve %s:%s: %s",
524 pc->proxy_addr, pc->proxy_port,
525 gai_strerror(res->ar_gai_errno));
526 if (proxy_start_reply(clt, 501, "text/html") == -1)
528 if (tp_error(clt->clt_tp, -1, "Can't resolve host") == -1)
530 fcgi_end_request(clt, 1);
534 clt->clt_addrinfo = res->ar_addrinfo;
535 clt->clt_p = clt->clt_addrinfo;
536 proxy_connect(-1, 0, clt);
540 proxy_connect(int fd, short ev, void *d)
542 struct client *clt = d;
543 struct evbuffer *out;
545 struct tls_config *conf;
546 struct timeval conntv = {5, 0};
548 socklen_t len = sizeof(err);
551 if (clt->clt_p == NULL)
554 if (clt->clt_fd != -1) {
555 if (getsockopt(clt->clt_fd, SOL_SOCKET, SO_ERROR, &err, &len)
566 clt->clt_fd = socket(p->ai_family, p->ai_socktype | SOCK_NONBLOCK,
568 if (clt->clt_fd == -1) {
569 clt->clt_p = clt->clt_p->ai_next;
573 if (connect(clt->clt_fd, p->ai_addr, p->ai_addrlen) == 0)
576 clt->clt_evconn_live = 1;
577 event_set(&clt->clt_evconn, clt->clt_fd, EV_WRITE, proxy_connect, clt);
578 event_add(&clt->clt_evconn, &conntv);
582 clt->clt_evconn_live = 0;
583 freeaddrinfo(clt->clt_addrinfo);
584 clt->clt_addrinfo = clt->clt_p = NULL;
586 clt->clt_bev = bufferevent_new(clt->clt_fd, proxy_read, proxy_write,
588 if (clt->clt_bev == NULL) {
589 log_warn("bufferevent_new");
593 if (!clt->clt_pc->no_tls) {
594 /* initialize TLS for Gemini */
595 if ((conf = tls_config_new()) == NULL) {
596 log_warn("tls_config_new failed");
600 tls_config_insecure_noverifycert(conf);
602 if ((clt->clt_ctx = tls_client()) == NULL) {
603 log_warnx("tls_client failed");
604 tls_config_free(conf);
608 if (tls_configure(clt->clt_ctx, conf) == -1) {
609 log_warnx("tls_configure failed");
610 tls_config_free(conf);
614 tls_config_free(conf);
616 if (tls_connect_socket(clt->clt_ctx, clt->clt_fd,
617 clt->clt_pc->proxy_name) == -1) {
618 log_warnx("tls_connect_socket failed");
622 event_set(&clt->clt_bev->ev_read, clt->clt_fd, EV_READ,
623 proxy_tls_readcb, clt->clt_bev);
624 event_set(&clt->clt_bev->ev_write, clt->clt_fd, EV_WRITE,
625 proxy_tls_writecb, clt->clt_bev);
628 evbuffer_unfreeze(clt->clt_bev->input, 0);
629 evbuffer_unfreeze(clt->clt_bev->output, 1);
633 /* bufferevent_settimeout(); */
634 bufferevent_enable(clt->clt_bev, EV_READ|EV_WRITE);
636 /* TODO: compute the URL */
637 out = EVBUFFER_OUTPUT(clt->clt_bev);
638 if (evbuffer_add_printf(out, "gemini://%s/%s",
639 clt->clt_pc->proxy_name, clt->clt_path_info) == -1) {
640 log_warn("bufferevent_printf failed");
644 if (clt->clt_query &&
645 evbuffer_add_printf(out, "?%s", clt->clt_query) == -1) {
646 log_warn("bufferevent_printf failed");
650 if (evbuffer_add(out, "\r\n", 2) == -1) {
651 log_warn("bufferevent_add failed");
658 log_warn("failed to connect to %s:%s",
659 clt->clt_pc->proxy_addr, clt->clt_pc->proxy_port);
660 if (proxy_start_reply(clt, 501, "text/html") == -1)
662 if (tp_error(clt->clt_tp, -1, "Can't connect") == -1)
664 fcgi_end_request(clt, 1);
668 parse_mime(struct client *clt, char *mime, char *lang, size_t len)
672 if (strncmp(mime, "text/gemini", 11) != 0)
675 clt->clt_translate = TR_ENABLED;
677 if ((mime = strchr(mime, ';')) == NULL)
681 while ((t = strsep(&mime, ";")) != NULL) {
682 if (!strncmp(t, "charset=", 8)) {
684 if (!strncasecmp(t, "utf8", 4) ||
685 !strncasecmp(t, "utf-8", 5) ||
686 !strncasecmp(t, "ascii", 5)) {
687 log_debug("unknown charset %s", t);
693 if (!strncmp(t, "lang=", 5)) {
695 if ((semi = strchr(t, ';')) != NULL)
698 if (strlcpy(lang, t, len) >= len) {
699 log_debug("lang too long: %s", t);
713 proxy_start_reply(struct client *clt, int status, const char *ctype)
717 csp = "Content-Security-Policy: default-src 'self'; "
718 "script-src 'none'; object-src 'none';\r\n";
721 clt_printf(clt, "Status: %d\r\n", status) == -1)
724 if (clt_puts(clt, csp) == -1)
728 /* use "ctype" as redirect target */
729 if (clt_printf(clt, "Location: %s\r\n", ctype) == -1)
731 if (clt_puts(clt, "\r\n") == -1)
737 if (!strcmp(ctype, "text/html"))
738 ctype = "text/html;charset=utf-8";
739 if (clt_printf(clt, "Content-Type: %s\r\n", ctype)
744 if (clt_puts(clt, "\r\n") == -1)
751 proxy_read(struct bufferevent *bev, void *d)
753 struct client *clt = d;
754 struct evbuffer *src = EVBUFFER_INPUT(bev);
762 if (clt->clt_headersdone) {
763 if (clt->clt_translate)
764 proxy_translate_gemtext(clt);
766 clt_write_bufferevent(clt, bev);
770 hdr = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
772 if (EVBUFFER_LENGTH(src) >= 1026)
773 proxy_error(bev, EV_READ, clt);
778 !isdigit((unsigned char)hdr[0]) ||
779 !isdigit((unsigned char)hdr[1]) ||
781 log_warnx("invalid ");
782 proxy_error(bev, EV_READ, clt);
786 code = (hdr[0] - '0') * 10 + (hdr[1] - '0');
790 if (proxy_start_reply(clt, 200, "text/html") == -1)
792 if (tp_inputpage(clt->clt_tp, &hdr[3]) == -1)
794 fcgi_end_request(clt, 0);
800 if (proxy_resurl(clt, &hdr[3], buf, sizeof(buf)) == 0) {
801 if (proxy_start_reply(clt, 302, buf) == -1)
803 fcgi_end_request(clt, 0);
808 if (proxy_start_reply(clt, 501, "text/html") == -1)
810 if (tp_error(clt->clt_tp, code, &hdr[3]) == -1)
812 fcgi_end_request(clt, 1);
816 mime = hdr + 2 + strspn(hdr + 2, " \t");
817 if (parse_mime(clt, mime, lang, sizeof(lang)) == -1) {
818 if (proxy_start_reply(clt, 501, "text/html") == -1)
820 if (tp_error(clt->clt_tp, -1, "Bad response") == -1)
822 fcgi_end_request(clt, 1);
826 if (clt->clt_translate)
827 ctype = "text/html;charset=utf-8";
831 if (clt_printf(clt, "Content-Type: %s\r\n\r\n", ctype) == -1)
834 clt->clt_headersdone = 1;
836 if (clt->clt_translate &&
837 tp_head(clt->clt_tp, lang, NULL) == -1)
841 * Trigger the read again so we proceed with the response
853 proxy_write(struct bufferevent *bev, void *d)
859 proxy_error(struct bufferevent *bev, short err, void *d)
861 struct client *clt = d;
862 int status = !(err & EVBUFFER_EOF);
864 log_debug("proxy error, shutting down the connection (err: %x)",
867 if (!clt->clt_headersdone) {
868 if (proxy_start_reply(clt, 501, "text/html") == -1)
870 if (tp_error(clt->clt_tp, -1, "Proxy error") == -1)
872 } else if (status == 0) {
873 if (clt->clt_translate & TR_PRE) {
874 if (tp_pre_close(clt->clt_tp))
876 clt->clt_translate &= ~TR_PRE;
879 if (clt->clt_translate & TR_LIST) {
880 if (clt_puts(clt, "</ul>") == -1)
882 clt->clt_translate &= ~TR_LIST;
885 if (clt->clt_translate & TR_NAV) {
886 if (clt_puts(clt, "</ul></nav>") == -1)
888 clt->clt_translate &= ~TR_NAV;
891 if (clt->clt_translate &&
892 tp_foot(clt->clt_tp) == -1)
896 fcgi_end_request(clt, status);
900 proxy_tls_readcb(int fd, short event, void *arg)
902 struct bufferevent *bufev = arg;
903 struct client *clt = bufev->cbarg;
904 char rbuf[IBUF_READ_SIZE];
905 int what = EVBUFFER_READ;
906 int howmuch = IBUF_READ_SIZE;
910 if (event == EV_TIMEOUT) {
911 what |= EVBUFFER_TIMEOUT;
915 if (bufev->wm_read.high != 0)
916 howmuch = MINIMUM(sizeof(rbuf), bufev->wm_read.high);
918 ret = tls_read(clt->clt_ctx, rbuf, howmuch);
919 if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) {
921 } else if (ret == -1) {
922 what |= EVBUFFER_ERROR;
928 what |= EVBUFFER_EOF;
932 if (evbuffer_add(bufev->input, rbuf, len) == -1) {
933 what |= EVBUFFER_ERROR;
937 proxy_bufferevent_add(&bufev->ev_read, G_TOUT(bufev->timeout_read));
939 len = EVBUFFER_LENGTH(bufev->input);
940 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
942 if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
943 struct evbuffer *buf = bufev->input;
944 event_del(&bufev->ev_read);
945 evbuffer_setcb(buf, bufferevent_read_pressure_cb, bufev);
949 if (bufev->readcb != NULL)
950 (*bufev->readcb)(bufev, bufev->cbarg);
954 proxy_bufferevent_add(&bufev->ev_read, G_TOUT(bufev->timeout_read));
958 (*bufev->errorcb)(bufev, what, bufev->cbarg);
962 proxy_bufferevent_add(struct event *ev, int timeout)
964 struct timeval tv, *ptv = NULL;
972 return (event_add(ev, ptv));
976 proxy_tls_writecb(int fd, short event, void *arg)
978 struct bufferevent *bufev = arg;
979 struct client *clt = bufev->cbarg;
981 short what = EVBUFFER_WRITE;
984 if (event == EV_TIMEOUT) {
985 what |= EVBUFFER_TIMEOUT;
989 if (EVBUFFER_LENGTH(bufev->output)) {
990 ret = tls_write(clt->clt_ctx,
991 EVBUFFER_DATA(bufev->output),
992 EVBUFFER_LENGTH(bufev->output));
993 if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) {
995 } else if (ret == -1) {
996 what |= EVBUFFER_ERROR;
1000 evbuffer_drain(bufev->output, len);
1003 if (EVBUFFER_LENGTH(bufev->output) != 0)
1004 proxy_bufferevent_add(&bufev->ev_write,
1005 G_TOUT(bufev->timeout_write));
1007 if (bufev->writecb != NULL &&
1008 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
1009 (*bufev->writecb)(bufev, bufev->cbarg);
1013 proxy_bufferevent_add(&bufev->ev_write, G_TOUT(bufev->timeout_write));
1017 (*bufev->errorcb)(bufev, what, bufev->cbarg);
1021 proxy_client_free(struct client *clt)
1024 event_asr_abort(clt->clt_evasr);
1026 if (clt->clt_addrinfo)
1027 freeaddrinfo(clt->clt_addrinfo);
1029 if (clt->clt_evconn_live)
1030 event_del(&clt->clt_evconn);
1032 if (clt->clt_fd != -1)
1036 tls_free(clt->clt_ctx);
1039 bufferevent_free(clt->clt_bev);
1041 template_free(clt->clt_tp);
1043 free(clt->clt_body);
1044 free(clt->clt_server_name);
1045 free(clt->clt_script_name);
1046 free(clt->clt_path_info);
1047 free(clt->clt_query);