[61330] trunk/base/src/pextlib1.0/curl.c

toby at macports.org toby at macports.org
Tue Dec 8 15:13:08 PST 2009


Revision: 61330
          http://trac.macports.org/changeset/61330
Author:   toby at macports.org
Date:     2009-12-08 15:13:05 -0800 (Tue, 08 Dec 2009)
Log Message:
-----------
Report a better error than "HTTP response said error" when a fetch fails.

Modified Paths:
--------------
    trunk/base/src/pextlib1.0/curl.c

Modified: trunk/base/src/pextlib1.0/curl.c
===================================================================
--- trunk/base/src/pextlib1.0/curl.c	2009-12-08 20:54:16 UTC (rev 61329)
+++ trunk/base/src/pextlib1.0/curl.c	2009-12-08 23:13:05 UTC (rev 61330)
@@ -37,6 +37,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <pthread.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -115,9 +116,10 @@
 	int theResult = TCL_OK;
 	CURL* theHandle = NULL;
 	FILE* theFile = NULL;
+	bool performFailed = false;
+	char theErrorString[CURL_ERROR_SIZE];
 
 	do {
-		long theResponseCode = 0;
 		int noprogress = 1;
 		int useepsv = 1;
 		int ignoresslcert = 0;
@@ -331,13 +333,19 @@
 			break;
 		}
 
+		theCurlCode = curl_easy_setopt(theHandle, CURLOPT_ERRORBUFFER, theErrorString);
+		if (theCurlCode != CURLE_OK) {
+			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			break;
+		}
+
 		/* actually fetch the resource */
 		theCurlCode = curl_easy_perform(theHandle);
 		if (theCurlCode != CURLE_OK) {
-			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
+			performFailed = true;
 			break;
 		}
-		
+
 		/* close the file */
 		(void) fclose( theFile );
 		theFile = NULL;
@@ -402,37 +410,23 @@
 				0);
 		}
 		
-		/* check everything went fine */
-		theCurlCode = curl_easy_getinfo(theHandle, CURLINFO_HTTP_CODE, &theResponseCode);
-		if (theCurlCode != CURLE_OK) {
-			theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
-			break;
-		}
-		
-		/* we need something between 200 (incl.) and 300 (excl.).*/
-		/* (actually, we sometimes get 0 from GNU FTP servers) */
-		if (((theResponseCode != 0)  && (theResponseCode < 200))
-			|| (theResponseCode >= 300)) {
-			char theErrorString[512];
-			(void) snprintf(theErrorString, sizeof(theErrorString),
-				"Download failed (code = %li)", theResponseCode);
-			Tcl_SetResult(interp, theErrorString, TCL_VOLATILE);
-			theResult = TCL_ERROR;
-			break;
-		}
-		
 		/* clean up */
 		curl_easy_cleanup( theHandle );
 		theHandle = NULL;
-    } while (0);
-    
-    if (theHandle != NULL) {
-    	curl_easy_cleanup( theHandle );
-    }
-    if (theFile != NULL) {
-    	fclose( theFile );
-    }
-    
+	} while (0);
+
+	if (performFailed) {
+		Tcl_SetResult(interp, theErrorString, TCL_VOLATILE);
+		theResult = TCL_ERROR;
+	}
+
+	if (theHandle != NULL) {
+		curl_easy_cleanup( theHandle );
+	}
+	if (theFile != NULL) {
+		fclose( theFile );
+	}
+
 	return theResult;
 }
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20091208/43f70430/attachment.html>


More information about the macports-changes mailing list