소스 검색

Send auth headers with requests

Gaël Métais 10 년 전
부모
커밋
31b66ee6fa
3개의 변경된 파일23개의 추가작업 그리고 5개의 파일을 삭제
  1. 1 0
      front/src/css/index.css
  2. 2 2
      front/src/views/index.html
  3. 20 3
      lib/tools/weightChecker/weightChecker.js

+ 1 - 0
front/src/css/index.css

@@ -128,6 +128,7 @@
 .advanced > div > div.label {
   width: 25%;
   white-space: nowrap;
+  vertical-align: middle;
 }
 .advanced .subTable {
   display: table;

+ 2 - 2
front/src/views/index.html

@@ -42,7 +42,7 @@
                 </div>
                 <div><input type="text" name="cookie" ng-model="settings.cookie" /></div>
             </div>
-            <!--<div>
+            <div>
                 <div class="label">
                     Authent
                     <span class="settingsTooltip">
@@ -60,7 +60,7 @@
                         <div><input type="text" class="authField" name="authPass" ng-model="settings.authPass" /></div>
                     </div>
                 </div>
-            </div>-->
+            </div>
             <!--<div>
                 <div class="label">Blocked domains</div>
                 <div>

+ 20 - 3
lib/tools/weightChecker/weightChecker.js

@@ -24,7 +24,7 @@ var WeightChecker = function() {
 
 
     // This function will re-download every asset and check if it could be optimized
-    function recheckAllFiles(data) {
+    function recheckAllFiles(data, httpAuth) {
         var startTime = Date.now();
         debug('Redownload started');
         var deferred = Q.defer();
@@ -33,11 +33,19 @@ var WeightChecker = function() {
         delete data.toolsResults.phantomas.metrics.requestsList;
         delete data.toolsResults.phantomas.offenders.requestsList;
 
+        var httpAuth = null;
+        if (data.params.options.authUser && data.params.options.authPass) {
+            httpAuth = {
+                username: data.params.options.authUser,
+                password: data.params.options.authPass
+            };
+        }
+
         // Transform every request into a download function with a callback when done
         var redownloadList = requestsList.map(function(entry) {
             return function(callback) {
                 
-                redownloadEntry(entry)
+                redownloadEntry(entry, httpAuth)
 
                 .then(imageOptimizer.optimizeImage)
 
@@ -313,7 +321,7 @@ var WeightChecker = function() {
     }
 
 
-    function redownloadEntry(entry) {
+    function redownloadEntry(entry, httpAuth) {
         var deferred = Q.defer();
         
         function downloadError(message) {
@@ -367,6 +375,15 @@ var WeightChecker = function() {
             timeout: REQUEST_TIMEOUT
         };
 
+        // Basic auth
+        if (httpAuth) {
+            requestOptions.auth = {
+                user: httpAuth.username,
+                pass: httpAuth.password,
+                sendImmediately: false // Tries a first time without auth, wait for a 401 error before resending
+            }
+        }
+
         download(requestOptions, entry.contentType, function(error, result) {
             if (error) {
                 if (error.code === 'ETIMEDOUT') {