]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
winhttp: Support basic authentication for URLs having credentials
authorMartin Willi <martin@revosec.ch>
Mon, 2 Jun 2014 09:55:18 +0000 (11:55 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 4 Jun 2014 14:34:16 +0000 (16:34 +0200)
src/libstrongswan/plugins/winhttp/winhttp_fetcher.c

index 9119e1d0a9548325269210c2ad8e5cf43f0b6a20..4a822bd9e44e8143199c3d0b472dae7e49a572b5 100644 (file)
@@ -177,6 +177,7 @@ static bool read_result(private_winhttp_fetcher_t *this, HINTERNET request,
  */
 static bool parse_uri(private_winhttp_fetcher_t *this, char *uri,
                                          LPWSTR host, int hostlen, LPWSTR path, int pathlen,
+                                         LPWSTR user, int userlen, LPWSTR pass, int passlen,
                                          DWORD *flags, INTERNET_PORT *port)
 {
        WCHAR wuri[512], extra[256];
@@ -186,6 +187,10 @@ static bool parse_uri(private_winhttp_fetcher_t *this, char *uri,
                .dwHostNameLength = hostlen,
                .lpszUrlPath = path,
                .dwUrlPathLength = pathlen,
+               .lpszUserName = user,
+               .dwUserNameLength = userlen,
+               .lpszPassword = pass,
+               .dwPasswordLength = passlen,
                .lpszExtraInfo = extra,
                .dwExtraInfoLength = countof(extra),
        };
@@ -215,6 +220,20 @@ static bool parse_uri(private_winhttp_fetcher_t *this, char *uri,
        return TRUE;
 }
 
+/**
+ * Set credentials for basic authentication, if given
+ */
+static bool set_credentials(private_winhttp_fetcher_t *this,
+                                                       HINTERNET *request, LPWSTR user, LPWSTR pass)
+{
+       if (!wcslen(user) && !wcslen(pass))
+       {       /* skip */
+               return TRUE;
+       }
+       return WinHttpSetCredentials(request, WINHTTP_AUTH_TARGET_SERVER,
+                                                                WINHTTP_AUTH_SCHEME_BASIC, user, pass, NULL);
+}
+
 METHOD(fetcher_t, fetch, status_t,
        private_winhttp_fetcher_t *this, char *uri, void *userdata)
 {
@@ -222,7 +241,7 @@ METHOD(fetcher_t, fetch, status_t,
        status_t status = FAILED;
        DWORD flags = 0;
        HINTERNET connection, request;
-       WCHAR host[256], path[512], *method;
+       WCHAR host[256], path[512], user[256], pass[256], *method;
 
        if (this->request.len)
        {
@@ -239,7 +258,7 @@ METHOD(fetcher_t, fetch, status_t,
        }
 
        if (parse_uri(this, uri, host, countof(host), path, countof(path),
-                                 &flags, &port))
+                                 user, countof(user), pass, countof(pass), &flags, &port))
        {
                connection = WinHttpConnect(this->session, host, port, 0);
                if (connection)
@@ -249,7 +268,8 @@ METHOD(fetcher_t, fetch, status_t,
                                                                                 WINHTTP_DEFAULT_ACCEPT_TYPES, flags);
                        if (request)
                        {
-                               if (send_request(this, request) &&
+                               if (set_credentials(this, request, user, pass) &&
+                                       send_request(this, request) &&
                                        read_result(this, request, userdata))
                                {
                                        status = SUCCESS;