]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
factor out internal_get_url function
authorDaniel Lenski <dlenski@gmail.com>
Sun, 7 Feb 2021 22:20:06 +0000 (14:20 -0800)
committerDaniel Lenski <dlenski@gmail.com>
Mon, 29 Mar 2021 02:27:01 +0000 (19:27 -0700)
Returns 'https://hostname[:port][/urlpath]' in a newly malloc'ed string.

Signed-off-by: Daniel Lenski <dlenski@gmail.com>
auth-juniper.c
auth.c
http.c
openconnect-internal.h

index 17c0b707e561bac28b7ac472e7e500f4e06ad7ea..64d2d82eb64b0aace21c5896e7184df43c9c9e59 100644 (file)
@@ -453,7 +453,7 @@ int oncp_obtain_cookie(struct openconnect_info *vpninfo)
        while (1) {
                char *form_buf = NULL;
                int role_select = 0;
-               struct oc_text_buf *url;
+               char *url;
 
                if (resp_buf && resp_buf->pos)
                        ret = do_https_request(vpninfo, "POST",
@@ -466,30 +466,22 @@ int oncp_obtain_cookie(struct openconnect_info *vpninfo)
                if (ret < 0)
                        break;
 
-               url = buf_alloc();
-               buf_append(url, "https://%s", vpninfo->hostname);
-               if (vpninfo->port != 443)
-                       buf_append(url, ":%d", vpninfo->port);
-               buf_append(url, "/");
-               if (vpninfo->urlpath)
-                       buf_append(url, "%s", vpninfo->urlpath);
-
-               if (buf_error(url)) {
+               if (!check_cookie_success(vpninfo)) {
                        free(form_buf);
-                       ret = buf_free(url);
+                       ret = 0;
                        break;
                }
 
-               if (!check_cookie_success(vpninfo)) {
-                       buf_free(url);
+               url = internal_get_url(vpninfo);
+               if (!url) {
                        free(form_buf);
-                       ret = 0;
+                       ret = -ENOMEM;
                        break;
                }
 
-               doc = htmlReadMemory(form_buf, ret, url->data, NULL,
+               doc = htmlReadMemory(form_buf, ret, url, NULL,
                                     HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING|HTML_PARSE_NONET);
-               buf_free(url);
+               free(url);
                free(form_buf);
                if (!doc) {
                        vpn_progress(vpninfo, PRG_ERR,
diff --git a/auth.c b/auth.c
index 643cfc61863fd828678ab4c984510f2548d8975f..4a63a01799582f74428fc1077d6b2a5b9ecb16ae 100644 (file)
--- a/auth.c
+++ b/auth.c
@@ -812,23 +812,16 @@ static int xmlpost_initial_req(struct openconnect_info *vpninfo,
 {
        xmlNodePtr root, node;
        xmlDocPtr doc = xmlpost_new_query(vpninfo, "init", &root);
-       struct oc_text_buf *url_buf;
+       char *url;
 
        if (!doc)
                return -ENOMEM;
 
-       url_buf = buf_alloc();
-       buf_append(url_buf, "https://%s", vpninfo->hostname);
-       if (vpninfo->port != 443)
-               buf_append(url_buf, ":%d", vpninfo->port);
-       /* Do we *need* to omit the trailing / here when no path? */
-       if (vpninfo->urlpath)
-               buf_append(url_buf, "/%s", vpninfo->urlpath);
-
-       if (buf_error(url_buf))
+       url = internal_get_url(vpninfo);
+       if (!url)
                goto bad;
 
-       node = xmlNewTextChild(root, NULL, XCAST("group-access"), XCAST(url_buf->data));
+       node = xmlNewTextChild(root, NULL, XCAST("group-access"), XCAST(url));
        if (!node)
                goto bad;
        if (cert_fail) {
@@ -841,11 +834,10 @@ static int xmlpost_initial_req(struct openconnect_info *vpninfo,
                if (!node)
                        goto bad;
        }
-       buf_free(url_buf);
+       free(url);
        return xmlpost_complete(doc, request_body);
 
 bad:
-       buf_free(url_buf);
        xmlpost_complete(doc, NULL);
        return -ENOMEM;
 }
diff --git a/http.c b/http.c
index 1fa301428ccaa5787e745eb5f580a758ff203d15..7598862e2fcc72a5dabfa35441b3b9c2040e08ad 100644 (file)
--- a/http.c
+++ b/http.c
@@ -811,6 +811,15 @@ int internal_parse_url(const char *url, char **res_proto, char **res_host,
        return 0;
 }
 
+char *internal_get_url(struct openconnect_info *vpninfo)
+{
+       char *url;
+       if (asprintf(&url, "https://%s%s%s", vpninfo->hostname, vpninfo->urlpath ? "/" : "", vpninfo->urlpath) < 0)
+               return NULL;
+       else
+               return url;
+}
+
 void openconnect_clear_cookies(struct openconnect_info *vpninfo)
 {
        struct oc_vpn_option *opt, *next;
index b3e084f8c8d3ac6feb247a146c43b52870b651a5..033190b332c61126363f507192b253f6b30bf945 100644 (file)
@@ -1157,6 +1157,7 @@ char *openconnect_create_useragent(const char *base);
 int process_proxy(struct openconnect_info *vpninfo, int ssl_sock);
 int internal_parse_url(const char *url, char **res_proto, char **res_host,
                       int *res_port, char **res_path, int default_port);
+char *internal_get_url(struct openconnect_info *vpninfo);
 int do_https_request(struct openconnect_info *vpninfo, const char *method,
                     const char *request_body_type, struct oc_text_buf *request_body,
                     char **form_buf, int fetch_redirect);