No more warnings for AMD64 (at least when cross-compiling). Thus tehre is a
[gnupg.git] / keyserver / curl-shim.c
index 33619df..406e26e 100644 (file)
@@ -1,7 +1,7 @@
 /* curl-shim.c - Implement a small subset of the curl API in terms of
  * the iobuf HTTP API
  *
- * Copyright (C) 2005 Free Software Foundation, Inc.
+ * Copyright (C) 2005, 2006 Free Software Foundation, Inc.
  *
  * This file is part of GnuPG.
  *
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
+
 #include "http.h"
 #include "util.h"
+#include "ksutil.h"
 #include "curl-shim.h"
 
 static CURLcode
@@ -44,6 +46,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;
@@ -83,13 +89,23 @@ curl_global_cleanup(void) {}
 CURL *
 curl_easy_init(void)
 {
-  return calloc(1,sizeof(CURL));
+  CURL *handle;
+
+  handle=calloc(1,sizeof(CURL));
+  if(handle)
+    handle->errors=stderr;
+
+  return handle;
 }
 
 void
 curl_easy_cleanup(CURL *curl)
 {
-  free(curl);
+  if (curl)
+    {
+      http_close (curl->hd, 0);
+      free(curl);
+    }
 }
 
 CURLcode
@@ -104,6 +120,9 @@ curl_easy_setopt(CURL *curl,CURLoption option,...)
     case CURLOPT_URL:
       curl->url=va_arg(ap,char *);
       break;
+    case CURLOPT_USERPWD:
+      curl->auth=va_arg(ap,char *);
+      break;
     case CURLOPT_WRITEFUNCTION:
       curl->writer=va_arg(ap,write_func);
       break;
@@ -125,6 +144,12 @@ curl_easy_setopt(CURL *curl,CURLoption option,...)
     case CURLOPT_FAILONERROR:
       curl->flags.failonerror=va_arg(ap,unsigned int);
       break;
+    case CURLOPT_VERBOSE:
+      curl->flags.verbose=va_arg(ap,unsigned int);
+      break;
+    case CURLOPT_STDERR:
+      curl->errors=va_arg(ap,FILE *);
+      break;
     default:
       /* We ignore the huge majority of curl options */
       break;
@@ -139,49 +164,71 @@ curl_easy_perform(CURL *curl)
   int rc;
   CURLcode err=CURLE_OK;
   const char *errstr=NULL;
+  char *proxy=NULL;
 
-  if(curl->flags.post)
-    {
-      rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,0,curl->proxy);
-      if(rc==0)
-       {
-         char content_len[50];
-         unsigned int post_len=strlen(curl->postfields);
+  /* 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 the http code doesn't use a proxy at
+     all. */
 
-         iobuf_writestr(curl->hd.fp_write,
-                        "Content-Type: application/x-www-form-urlencoded\r\n");
-         sprintf(content_len,"Content-Length: %u\r\n",post_len);
+  if(curl->proxy)
+    proxy=curl->proxy;
+  else
+    proxy=getenv(HTTP_PROXY_ENV);
 
-         iobuf_writestr(curl->hd.fp_write,content_len);
+  if(curl->flags.verbose)
+    fprintf(curl->errors,"* HTTP proxy is \"%s\"\n",proxy?proxy:"null");
 
-         http_start_data(&curl->hd);
-         iobuf_write(curl->hd.fp_write,curl->postfields,post_len);
-         rc=http_wait_response(&curl->hd,&curl->status);
-         if(rc==0 && curl->flags.failonerror && curl->status>=300)
-           err=CURLE_HTTP_RETURNED_ERROR;
+  if(curl->flags.post)
+    {
+      rc = http_open (&curl->hd, HTTP_REQ_POST, curl->url, curl->auth,
+                      0, proxy, NULL);
+      if (!rc)
+       {
+         unsigned int post_len = strlen(curl->postfields);
+
+         es_fprintf (http_get_write_ptr (curl->hd),
+                      "Content-Type: application/x-www-form-urlencoded\r\n"
+                      "Content-Length: %u\r\n", post_len);
+         http_start_data (curl->hd);
+         es_write (http_get_write_ptr (curl->hd),
+                    curl->postfields, post_len, NULL);
+
+         rc = http_wait_response (curl->hd);
+          curl->status = http_get_status_code (curl->hd);
+         if (!rc && curl->flags.failonerror && curl->status>=300)
+           err = CURLE_HTTP_RETURNED_ERROR;
+          http_close (curl->hd, 0);
+          curl->hd = NULL;
        }
     }
   else
     {
-      rc=http_open(&curl->hd,HTTP_REQ_GET,curl->url,0,curl->proxy);
-      if(rc==0)
+      rc = http_open (&curl->hd, HTTP_REQ_GET, curl->url, curl->auth,
+                      0, proxy, NULL);
+      if (!rc)
        {
-         rc=http_wait_response(&curl->hd,&curl->status);
-         if(rc==0)
+         rc = http_wait_response (curl->hd);
+          curl->status = http_get_status_code (curl->hd);
+         if (!rc)
            {
-             if(curl->flags.failonerror && curl->status>=300)
-               err=CURLE_HTTP_RETURNED_ERROR;
+             if (curl->flags.failonerror && curl->status>=300)
+               err = CURLE_HTTP_RETURNED_ERROR;
              else
                {
-                 unsigned int maxlen=1024,buflen,len;
-                 byte *line=NULL;
+                 size_t maxlen = 1024;
+                  size_t buflen;
+                  unsigned int len;
+                 char *line = NULL;
 
-                 while((len=iobuf_read_line(curl->hd.fp_read,
-                                            &line,&buflen,&maxlen)))
+                 while ((len = es_read_line (http_get_read_ptr (curl->hd),
+                                              &line, &buflen, &maxlen)))
                    {
-                     maxlen=1024;
                      size_t ret;
 
+                     maxlen=1024;
+
                      ret=(curl->writer)(line,len,1,curl->file);
                      if(ret!=len)
                        {
@@ -190,25 +237,34 @@ curl_easy_perform(CURL *curl)
                        }
                    }
 
-                 m_free(line);
-                 http_close(&curl->hd);
+                 es_free (line);
+                 http_close(curl->hd, 0);
+                  curl->hd = NULL;
                }
            }
          else
-           http_close(&curl->hd);
+            {
+              http_close (curl->hd, 0);
+              curl->hd = NULL;
+            }
        }
     }
 
-  if(rc!=0)
+  switch(gpg_err_code (rc))
     {
-      if(rc==G10ERR_NETWORK)
-       errstr=strerror(errno);
-      else
-       errstr=g10_errstr(rc);
+    case 0:
+      break;
+
+    case GPG_ERR_INV_URI:
+      err=CURLE_UNSUPPORTED_PROTOCOL;
+      break;
 
+    default:
+      errstr=gpg_strerror (rc);
       err=CURLE_COULDNT_CONNECT;
+      break;
     }
-
+      
   return handle_error(curl,err,errstr);
 }