diff --git a/advagg.admin.inc b/advagg.admin.inc
index f9cb00f..c913d21 100644
--- a/advagg.admin.inc
+++ b/advagg.admin.inc
@@ -152,18 +152,10 @@ function advagg_admin_info_form() {
     '#title'         => t('Asynchronous debug info'),
   );
 
-  global $base_path;
-
   list($css_path, $js_path) = advagg_get_root_files_dir();
-    $ip = variable_get('advagg_server_addr', FALSE);
-    if ($ip == -1) {
-      $ip = $_SERVER['HTTP_HOST'];
-    }
-    elseif (empty($ip)) {
-      $ip = empty($_SERVER['SERVER_ADDR']) ? '127.0.0.1' : $_SERVER['SERVER_ADDR'];
-    }
+
   $filepath = $css_path . '/css_missing' . mt_rand() . time() . '_0.css';
-  $url = 'http://' . $ip . $base_path . $filepath;
+  $url = _advagg_build_url($filepath);
   $headers = array(
     'Host' => $_SERVER['HTTP_HOST'],
   );
diff --git a/advagg.install b/advagg.install
index 4df24ff..b56d4f1 100644
--- a/advagg.install
+++ b/advagg.install
@@ -285,15 +285,7 @@ function advagg_check_missing_handler() {
 
   // Setup request
   list($css_path, $js_path) = advagg_get_root_files_dir();
-    $ip = variable_get('advagg_server_addr', FALSE);
-    if ($ip == -1) {
-      $ip = $_SERVER['HTTP_HOST'];
-    }
-    elseif (empty($ip)) {
-      $ip = empty($_SERVER['SERVER_ADDR']) ? '127.0.0.1' : $_SERVER['SERVER_ADDR'];
-    }
-  $filepath = '/missing' . mt_rand() . time();
-  $url = 'http://' . $ip . $base_path . $css_path . $filepath . '.css';
+  $url = _advagg_build_url($css_path . $filepath . '.css');
   $headers = array(
     'Host' => $_SERVER['HTTP_HOST'],
   );
@@ -529,14 +521,7 @@ function advagg_install_test_async_stream() {
   $advagg_async_socket_connect = variable_get('advagg_async_socket_connect', ADVAGG_ASYNC_SOCKET_CONNECT);
 
   // Build test request.
-  $ip = variable_get('advagg_server_addr', FALSE);
-  if ($ip == -1) {
-    $ip = $_SERVER['HTTP_HOST'];
-  }
-  elseif (empty($ip)) {
-    $ip = empty($_SERVER['SERVER_ADDR']) ? '127.0.0.1' : $_SERVER['SERVER_ADDR'];
-  }
-  $url = 'http://' . $ip . $base_path;
+  $url = _advagg_build_url();
   $headers = array(
     'Host' => $_SERVER['HTTP_HOST'],
   );
diff --git a/advagg.module b/advagg.module
index afeed59..8a3377f 100644
--- a/advagg.module
+++ b/advagg.module
@@ -2336,14 +2336,7 @@ function advagg_css_js_file_builder($type, $files, $counter = FALSE, $force = FA
       // Generate on request?
       if (variable_get('advagg_async_generation', ADVAGG_ASYNC_GENERATION) && !$force) {
         // Build request.
-        $ip = variable_get('advagg_server_addr', FALSE);
-        if ($ip == -1) {
-          $ip = $_SERVER['HTTP_HOST'];
-        }
-        elseif (empty($ip)) {
-          $ip = empty($_SERVER['SERVER_ADDR']) ? '127.0.0.1' : $_SERVER['SERVER_ADDR'];
-        }
-        $url = 'http://' . $ip . $base_path . $filepath . '?generator=1';
+        $url = _advagg_build_url($filepath . '?generator=1');
         $headers = array(
           'Host' => $_SERVER['HTTP_HOST'],
         );
@@ -3451,3 +3444,39 @@ function advagg_file_download($file, $type = '') {
     return $return;
   }
 }
+
+/**
+ * Helper function to build an URL for asynchronous requests.
+ *
+ * @param $filepath
+ *   Path to a URI excluding everything to the left and including the base path.
+ */
+function _advagg_build_url($filepath = '') {
+  global $base_path;
+
+  // Server auth.
+  $auth = '';
+  if (isset($_SERVER['AUTH_TYPE']) && $_SERVER['AUTH_TYPE'] == 'Basic') {
+    $auth = $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'] .'@';
+  }
+
+  // Host.
+  $ip = variable_get('advagg_server_addr', FALSE);
+  if ($ip == -1) {
+    $ip = $_SERVER['HTTP_HOST'];
+  }
+  elseif (empty($ip)) {
+    $ip = empty($_SERVER['SERVER_ADDR']) ? '127.0.0.1' : $_SERVER['SERVER_ADDR'];
+  }
+
+  // Port.
+  $port = '';
+  if (   isset($_SERVER['SERVER_PORT'])
+      && is_numeric($_SERVER['SERVER_PORT'])
+      && ($_SERVER['SERVER_PORT'] != 80 || $_SERVER['SERVER_PORT'] != 443)
+        ) {
+    $port = ':' . $_SERVER['SERVER_PORT'];
+  }
+
+  return 'http://' . $auth . $ip . $port . $base_path . $filepath;
+}
diff --git a/advagg_js_compress/advagg_js_compress.module b/advagg_js_compress/advagg_js_compress.module
index 3dd6290..14b4ca2 100644
--- a/advagg_js_compress/advagg_js_compress.module
+++ b/advagg_js_compress/advagg_js_compress.module
@@ -344,14 +344,7 @@ function advagg_js_compress_test_compression($files_to_test) {
     $query['values'] = $info;
     $query['key'] = $key;
     $query_string = http_build_query($query, '', '&');
-    $ip = variable_get('advagg_server_addr', FALSE);
-    if ($ip == -1) {
-      $ip = $_SERVER['HTTP_HOST'];
-    }
-    elseif (empty($ip)) {
-      $ip = empty($_SERVER['SERVER_ADDR']) ? '127.0.0.1' : $_SERVER['SERVER_ADDR'];
-    }
-    $url = 'http://' . $ip . $base_path . 'advagg/js_compress_test_file';
+    $url = _advagg_build_url('advagg/js_compress_test_file');
     $headers = array(
       'Host' => $_SERVER['HTTP_HOST'],
       'Content-Type' => 'application/x-www-form-urlencoded',
