Post release updates
[gnupg.git] / keyserver / curl-shim.c
index c71c655..500d9f5 100644 (file)
@@ -1,13 +1,13 @@
 /* curl-shim.c - Implement a small subset of the curl API in terms of
  * the iobuf HTTP API
  *
- * Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+ * Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
  *
  * This file is part of GnuPG.
  *
  * GnuPG is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * GnuPG is distributed in the hope that it will be useful,
@@ -16,9 +16,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
- * USA.
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <config.h>
@@ -27,8 +25,9 @@
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
-#include "http.h"
+
 #include "util.h"
+#include "http.h"
 #include "ksutil.h"
 #include "curl-shim.h"
 
@@ -79,6 +78,7 @@ handle_error(CURL *curl,CURLcode err,const char *str)
 CURLcode
 curl_global_init(long flags)
 {
+  (void)flags;
   return CURLE_OK;
 }
 
@@ -90,6 +90,10 @@ curl_easy_init(void)
 {
   CURL *handle;
 
+#ifdef HAVE_W32_SYSTEM
+  w32_init_sockets ();
+#endif
+
   handle=calloc(1,sizeof(CURL));
   if(handle)
     handle->errors=stderr;
@@ -100,7 +104,11 @@ curl_easy_init(void)
 void
 curl_easy_cleanup(CURL *curl)
 {
-  free(curl);
+  if (curl)
+    {
+      http_close (curl->hd, 0);
+      free(curl);
+    }
 }
 
 CURLcode
@@ -131,20 +139,26 @@ curl_easy_setopt(CURL *curl,CURLoption option,...)
       curl->proxy=va_arg(ap,char *);
       break;
     case CURLOPT_POST:
-      curl->flags.post=va_arg(ap,unsigned int);
+      curl->flags.post=va_arg(ap,long)?1:0;
       break;
     case CURLOPT_POSTFIELDS:
       curl->postfields=va_arg(ap,char *);
       break;
+    case CURLOPT_SRVTAG_GPG_HACK:
+      curl->srvtag=va_arg(ap,char *);
+      break;
     case CURLOPT_FAILONERROR:
-      curl->flags.failonerror=va_arg(ap,unsigned int);
+      curl->flags.failonerror=va_arg(ap,long)?1:0;
       break;
     case CURLOPT_VERBOSE:
-      curl->flags.verbose=va_arg(ap,unsigned int);
+      curl->flags.verbose=va_arg(ap,long)?1:0;
       break;
     case CURLOPT_STDERR:
       curl->errors=va_arg(ap,FILE *);
       break;
+    case CURLOPT_HTTPHEADER:
+      curl->headers=va_arg(ap,struct curl_slist *);
+      break;
     default:
       /* We ignore the huge majority of curl options */
       break;
@@ -173,46 +187,61 @@ curl_easy_perform(CURL *curl)
     proxy=getenv(HTTP_PROXY_ENV);
 
   if(curl->flags.verbose)
-    fprintf(curl->errors,"* HTTP proxy is \"%s\"\n",proxy?proxy:"null");
+    {
+      fprintf(curl->errors,"* HTTP proxy is \"%s\"\n",proxy?proxy:"null");
+      fprintf(curl->errors,"* HTTP URL is \"%s\"\n",curl->url);
+      fprintf(curl->errors,"* HTTP auth is \"%s\"\n",
+             curl->auth?curl->auth:"null");
+      fprintf(curl->errors,"* HTTP method is %s\n",
+             curl->flags.post?"POST":"GET");
+    }
 
   if(curl->flags.post)
     {
-      rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,curl->auth,0,proxy);
-      if(rc==0)
+      rc = http_open (&curl->hd, HTTP_REQ_POST, curl->url, curl->auth,
+                      0, proxy, NULL, curl->srvtag,
+                     curl->headers?curl->headers->list:NULL);
+      if (!rc)
        {
-         char content_len[50];
-         unsigned int post_len=strlen(curl->postfields);
-
-         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);
-
-         iobuf_writestr(curl->hd.fp_write,content_len);
-
-         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;
+         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,curl->auth,0,proxy);
-      if(rc==0)
+      rc = http_open (&curl->hd, HTTP_REQ_GET, curl->url, curl->auth,
+                      0, proxy, NULL, curl->srvtag,
+                     curl->headers?curl->headers->list: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)))
                    {
                      size_t ret;
 
@@ -226,31 +255,30 @@ curl_easy_perform(CURL *curl)
                        }
                    }
 
-                 xfree(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;
+            }
        }
     }
 
-  switch(rc)
+  switch(gpg_err_code (rc))
     {
     case 0:
       break;
 
-    case G10ERR_INVALID_URI:
+    case GPG_ERR_INV_URI:
       err=CURLE_UNSUPPORTED_PROTOCOL;
       break;
 
-    case G10ERR_NETWORK:
-      errstr=strerror(errno);
-      err=CURLE_COULDNT_CONNECT;
-      break;
-
     default:
-      errstr=g10_errstr(rc);
+      errstr=gpg_strerror (rc);
       err=CURLE_COULDNT_CONNECT;
       break;
     }
@@ -315,8 +343,40 @@ curl_escape(char *str,int length)
   return enc;
 }
 
+curl_version_info_data *
+curl_version_info(int type)
+{
+  static curl_version_info_data data;
+  static const char *protocols[]={"http",NULL};
+
+  (void)type;
+
+  data.protocols=protocols;
+
+  return &data;
+}
+
+struct curl_slist *
+curl_slist_append(struct curl_slist *list,const char *string)
+{
+  if(!list)
+    {
+      list=calloc(1,sizeof(*list));
+      if(!list)
+       return NULL;
+    }
+
+  add_to_strlist(&list->list,string);
+
+  return list;
+}
+
 void
-curl_free(char *ptr)
+curl_slist_free_all(struct curl_slist *list)
 {
-  free(ptr);
+  if(list)
+    {
+      free_strlist(list->list);
+      free(list);
+    }
 }