* gpgkeys_hkp.c (main), gpgkeys_curl.c (main), curl-shim.h: Show
[gnupg.git] / keyserver / curl-shim.c
index 4828f52..7f9d431 100644 (file)
@@ -29,6 +29,7 @@
 #include <errno.h>
 #include "http.h"
 #include "util.h"
+#include "ksutil.h"
 #include "curl-shim.h"
 
 static CURLcode
@@ -44,6 +45,10 @@ handle_error(CURL *curl,CURLcode err,const char *str)
          strcpy(curl->errorbuffer,"okay");
          break;
 
+       case CURLE_UNSUPPORTED_PROTOCOL:
+         strcpy(curl->errorbuffer,"unsupported protocol");
+         break;
+
        case CURLE_COULDNT_CONNECT:
          strcpy(curl->errorbuffer,"couldn't connect");
          break;
@@ -142,10 +147,24 @@ curl_easy_perform(CURL *curl)
   int rc;
   CURLcode err=CURLE_OK;
   const char *errstr=NULL;
+  char *proxy=NULL;
+
+  /* Emulate the libcurl proxy behavior.  If the calling program set a
+     proxy, use it.  If it didn't set a proxy or set it to NULL, check
+     for one in the environment.  If the calling program explicitly
+     set a null-string proxy, don't set a proxy at all. */
+
+  if(curl->proxy)
+    {
+      if(*curl->proxy)
+       proxy=curl->proxy;
+    }
+  else
+    proxy=getenv(HTTP_PROXY_ENV);
 
   if(curl->flags.post)
     {
-      rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,curl->auth,0,curl->proxy);
+      rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,curl->auth,0,proxy);
       if(rc==0)
        {
          char content_len[50];
@@ -166,7 +185,7 @@ curl_easy_perform(CURL *curl)
     }
   else
     {
-      rc=http_open(&curl->hd,HTTP_REQ_GET,curl->url,curl->auth,0,curl->proxy);
+      rc=http_open(&curl->hd,HTTP_REQ_GET,curl->url,curl->auth,0,proxy);
       if(rc==0)
        {
          rc=http_wait_response(&curl->hd,&curl->status);
@@ -193,7 +212,7 @@ curl_easy_perform(CURL *curl)
                        }
                    }
 
-                 m_free(line);
+                 xfree(line);
                  http_close(&curl->hd);
                }
            }
@@ -202,16 +221,26 @@ curl_easy_perform(CURL *curl)
        }
     }
 
-  if(rc!=0)
+  switch(rc)
     {
-      if(rc==G10ERR_NETWORK)
-       errstr=strerror(errno);
-      else
-       errstr=g10_errstr(rc);
+    case 0:
+      break;
 
+    case G10ERR_INVALID_URI:
+      err=CURLE_UNSUPPORTED_PROTOCOL;
+      break;
+
+    case G10ERR_NETWORK:
+      errstr=strerror(errno);
       err=CURLE_COULDNT_CONNECT;
-    }
+      break;
 
+    default:
+      errstr=g10_errstr(rc);
+      err=CURLE_COULDNT_CONNECT;
+      break;
+    }
+      
   return handle_error(curl,err,errstr);
 }