* gpgkeys_hkp.c (main), gpgkeys_curl.c (main), curl-shim.h: Show
[gnupg.git] / keyserver / curl-shim.c
index 623d685..7f9d431 100644 (file)
@@ -17,7 +17,8 @@
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
  */
 
 #include <config.h>
 #include <errno.h>
 #include "http.h"
 #include "util.h"
+#include "ksutil.h"
 #include "curl-shim.h"
 
-static CURLcode handle_error(CURL *curl,CURLcode err,const char *str)
+static CURLcode
+handle_error(CURL *curl,CURLcode err,const char *str)
 {
   if(curl->errorbuffer)
     {
+      /* Make sure you never exceed CURL_ERROR_SIZE, currently set to
+        256 in curl-shim.h */
       switch(err)
        {
        case CURLE_OK:
          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;
@@ -48,6 +57,10 @@ static CURLcode handle_error(CURL *curl,CURLcode err,const char *str)
          strcpy(curl->errorbuffer,"write error");
          break;
 
+       case CURLE_HTTP_RETURNED_ERROR:
+         sprintf(curl->errorbuffer,"url returned error %u",curl->status);
+         break;
+
        default:
          strcpy(curl->errorbuffer,"generic error");
          break;
@@ -63,24 +76,29 @@ static CURLcode handle_error(CURL *curl,CURLcode err,const char *str)
   return err;
 }
 
-CURLcode curl_global_init(long flags)
+CURLcode
+curl_global_init(long flags)
 {
   return CURLE_OK;
 }
 
-void curl_global_cleanup(void) {}
+void
+curl_global_cleanup(void) {}
 
-CURL *curl_easy_init(void)
+CURL *
+curl_easy_init(void)
 {
   return calloc(1,sizeof(CURL));
 }
 
-void curl_easy_cleanup(CURL *curl)
+void
+curl_easy_cleanup(CURL *curl)
 {
   free(curl);
 }
 
-CURLcode curl_easy_setopt(CURL *curl,CURLoption option,...)
+CURLcode
+curl_easy_setopt(CURL *curl,CURLoption option,...)
 {
   va_list ap;
 
@@ -91,6 +109,9 @@ CURLcode 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;
@@ -103,6 +124,15 @@ CURLcode curl_easy_setopt(CURL *curl,CURLoption option,...)
     case CURLOPT_PROXY:
       curl->proxy=va_arg(ap,char *);
       break;
+    case CURLOPT_POST:
+      curl->flags.post=va_arg(ap,unsigned int);
+      break;
+    case CURLOPT_POSTFIELDS:
+      curl->postfields=va_arg(ap,char *);
+      break;
+    case CURLOPT_FAILONERROR:
+      curl->flags.failonerror=va_arg(ap,unsigned int);
+      break;
     default:
       /* We ignore the huge majority of curl options */
       break;
@@ -111,43 +141,168 @@ CURLcode curl_easy_setopt(CURL *curl,CURLoption option,...)
   return handle_error(curl,CURLE_OK,NULL);
 }
 
-CURLcode curl_easy_perform(CURL *curl)
+CURLcode
+curl_easy_perform(CURL *curl)
 {
   int rc;
   CURLcode err=CURLE_OK;
   const char *errstr=NULL;
+  char *proxy=NULL;
 
-  rc=http_open_document(&curl->hd,curl->url,0,curl->proxy);
-  if(rc!=0)
+  /* 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(rc==G10ERR_NETWORK)
-       errstr=strerror(errno);
-      else
-       errstr=g10_errstr(rc);
+      if(*curl->proxy)
+       proxy=curl->proxy;
+    }
+  else
+    proxy=getenv(HTTP_PROXY_ENV);
 
-      err=CURLE_COULDNT_CONNECT;
+  if(curl->flags.post)
+    {
+      rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,curl->auth,0,proxy);
+      if(rc==0)
+       {
+         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;
+       }
     }
   else
     {
-      size_t maxlen=1024,buflen,len;
-      byte *line=NULL;
+      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);
+         if(rc==0)
+           {
+             if(curl->flags.failonerror && curl->status>=300)
+               err=CURLE_HTTP_RETURNED_ERROR;
+             else
+               {
+                 unsigned int maxlen=1024,buflen,len;
+                 byte *line=NULL;
+
+                 while((len=iobuf_read_line(curl->hd.fp_read,
+                                            &line,&buflen,&maxlen)))
+                   {
+                     maxlen=1024;
+                     size_t ret;
+
+                     ret=(curl->writer)(line,len,1,curl->file);
+                     if(ret!=len)
+                       {
+                         err=CURLE_WRITE_ERROR;
+                         break;
+                       }
+                   }
+
+                 xfree(line);
+                 http_close(&curl->hd);
+               }
+           }
+         else
+           http_close(&curl->hd);
+       }
+    }
+
+  switch(rc)
+    {
+    case 0:
+      break;
 
-      while((len=iobuf_read_line(curl->hd.fp_read,&line,&buflen,&maxlen)))
+    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);
+}
+
+/* This is not the same exact set that is allowed according to
+   RFC-2396, but it is what the real curl uses. */
+#define VALID_URI_CHARS "abcdefghijklmnopqrstuvwxyz" \
+                        "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
+                        "0123456789"
+
+char *
+curl_escape(char *str,int length)
+{
+  int len,max,idx,enc_idx=0;
+  char *enc;
+
+  if(length)
+    len=length;
+  else
+    len=strlen(str);
+
+  enc=malloc(len+1);
+  if(!enc)
+    return enc;
+
+  max=len;
+
+  for(idx=0;idx<len;idx++)
+    {
+      if(enc_idx+3>max)
        {
-         maxlen=1024;
-         size_t ret;
+         char *tmp;
+
+         max+=100;
 
-         ret=(curl->writer)(line,len,1,curl->file);
-         if(ret!=len)
+         tmp=realloc(enc,max+1);
+         if(!tmp)
            {
-             err=CURLE_WRITE_ERROR;
-             break;
+             free(enc);
+             return NULL;
            }
+
+         enc=tmp;
        }
 
-      m_free(line);
-      http_close(&curl->hd);
+      if(strchr(VALID_URI_CHARS,str[idx]))
+       enc[enc_idx++]=str[idx];
+      else
+       {
+         char numbuf[5];
+         sprintf(numbuf,"%%%02X",str[idx]);
+         strcpy(&enc[enc_idx],numbuf);
+         enc_idx+=3;
+       }
     }
 
-  return handle_error(curl,err,errstr);
+  enc[enc_idx]='\0';
+
+  return enc;
+}
+
+void
+curl_free(char *ptr)
+{
+  free(ptr);
 }