* ksutil.c (curl_err_to_gpg_err): Add CURLE_OK and CURLE_COULDNT_CONNECT.
[gnupg.git] / keyserver / curl-shim.c
1 /* curl-shim.c - Implement a small subset of the curl API in terms of
2  * the iobuf HTTP API
3  *
4  * Copyright (C) 2005 Free Software Foundation, Inc.
5  *
6  * This file is part of GnuPG.
7  *
8  * GnuPG is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuPG is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21  * USA.
22  */
23
24 #include <config.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include "http.h"
31 #include "util.h"
32 #include "ksutil.h"
33 #include "curl-shim.h"
34
35 static CURLcode
36 handle_error(CURL *curl,CURLcode err,const char *str)
37 {
38   if(curl->errorbuffer)
39     {
40       /* Make sure you never exceed CURL_ERROR_SIZE, currently set to
41          256 in curl-shim.h */
42       switch(err)
43         {
44         case CURLE_OK:
45           strcpy(curl->errorbuffer,"okay");
46           break;
47
48         case CURLE_UNSUPPORTED_PROTOCOL:
49           strcpy(curl->errorbuffer,"unsupported protocol");
50           break;
51
52         case CURLE_COULDNT_CONNECT:
53           strcpy(curl->errorbuffer,"couldn't connect");
54           break;
55
56         case CURLE_WRITE_ERROR:
57           strcpy(curl->errorbuffer,"write error");
58           break;
59
60         case CURLE_HTTP_RETURNED_ERROR:
61           sprintf(curl->errorbuffer,"url returned error %u",curl->status);
62           break;
63
64         default:
65           strcpy(curl->errorbuffer,"generic error");
66           break;
67         }
68
69       if(str && (strlen(curl->errorbuffer)+2+strlen(str)+1)<=CURL_ERROR_SIZE)
70         {
71           strcat(curl->errorbuffer,": ");
72           strcat(curl->errorbuffer,str);
73         }
74     }
75
76   return err;
77 }
78
79 CURLcode
80 curl_global_init(long flags)
81 {
82   return CURLE_OK;
83 }
84
85 void
86 curl_global_cleanup(void) {}
87
88 CURL *
89 curl_easy_init(void)
90 {
91   return calloc(1,sizeof(CURL));
92 }
93
94 void
95 curl_easy_cleanup(CURL *curl)
96 {
97   free(curl);
98 }
99
100 CURLcode
101 curl_easy_setopt(CURL *curl,CURLoption option,...)
102 {
103   va_list ap;
104
105   va_start(ap,option);
106
107   switch(option)
108     {
109     case CURLOPT_URL:
110       curl->url=va_arg(ap,char *);
111       break;
112     case CURLOPT_USERPWD:
113       curl->auth=va_arg(ap,char *);
114       break;
115     case CURLOPT_WRITEFUNCTION:
116       curl->writer=va_arg(ap,write_func);
117       break;
118     case CURLOPT_FILE:
119       curl->file=va_arg(ap,void *);
120       break;
121     case CURLOPT_ERRORBUFFER:
122       curl->errorbuffer=va_arg(ap,char *);
123       break;
124     case CURLOPT_PROXY:
125       curl->proxy=va_arg(ap,char *);
126       break;
127     case CURLOPT_POST:
128       curl->flags.post=va_arg(ap,unsigned int);
129       break;
130     case CURLOPT_POSTFIELDS:
131       curl->postfields=va_arg(ap,char *);
132       break;
133     case CURLOPT_FAILONERROR:
134       curl->flags.failonerror=va_arg(ap,unsigned int);
135       break;
136     default:
137       /* We ignore the huge majority of curl options */
138       break;
139     }
140
141   return handle_error(curl,CURLE_OK,NULL);
142 }
143
144 CURLcode
145 curl_easy_perform(CURL *curl)
146 {
147   int rc;
148   CURLcode err=CURLE_OK;
149   const char *errstr=NULL;
150   char *proxy=NULL;
151
152   /* Emulate the libcurl proxy behavior.  If the calling program set a
153      proxy, use it.  If it didn't set a proxy or set it to NULL, check
154      for one in the environment.  If the calling program explicitly
155      set a null-string proxy, don't set a proxy at all. */
156
157   if(curl->proxy)
158     {
159       if(*curl->proxy)
160         proxy=curl->proxy;
161     }
162   else
163     proxy=getenv(HTTP_PROXY_ENV);
164
165   if(curl->flags.post)
166     {
167       rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,curl->auth,0,proxy);
168       if(rc==0)
169         {
170           char content_len[50];
171           unsigned int post_len=strlen(curl->postfields);
172
173           iobuf_writestr(curl->hd.fp_write,
174                          "Content-Type: application/x-www-form-urlencoded\r\n");
175           sprintf(content_len,"Content-Length: %u\r\n",post_len);
176
177           iobuf_writestr(curl->hd.fp_write,content_len);
178
179           http_start_data(&curl->hd);
180           iobuf_write(curl->hd.fp_write,curl->postfields,post_len);
181           rc=http_wait_response(&curl->hd,&curl->status);
182           if(rc==0 && curl->flags.failonerror && curl->status>=300)
183             err=CURLE_HTTP_RETURNED_ERROR;
184         }
185     }
186   else
187     {
188       rc=http_open(&curl->hd,HTTP_REQ_GET,curl->url,curl->auth,0,proxy);
189       if(rc==0)
190         {
191           rc=http_wait_response(&curl->hd,&curl->status);
192           if(rc==0)
193             {
194               if(curl->flags.failonerror && curl->status>=300)
195                 err=CURLE_HTTP_RETURNED_ERROR;
196               else
197                 {
198                   unsigned int maxlen=1024,buflen,len;
199                   byte *line=NULL;
200
201                   while((len=iobuf_read_line(curl->hd.fp_read,
202                                              &line,&buflen,&maxlen)))
203                     {
204                       size_t ret;
205
206                       maxlen=1024;
207
208                       ret=(curl->writer)(line,len,1,curl->file);
209                       if(ret!=len)
210                         {
211                           err=CURLE_WRITE_ERROR;
212                           break;
213                         }
214                     }
215
216                   xfree(line);
217                   http_close(&curl->hd);
218                 }
219             }
220           else
221             http_close(&curl->hd);
222         }
223     }
224
225   switch(rc)
226     {
227     case 0:
228       break;
229
230     case G10ERR_INVALID_URI:
231       err=CURLE_UNSUPPORTED_PROTOCOL;
232       break;
233
234     case G10ERR_NETWORK:
235       errstr=strerror(errno);
236       err=CURLE_COULDNT_CONNECT;
237       break;
238
239     default:
240       errstr=g10_errstr(rc);
241       err=CURLE_COULDNT_CONNECT;
242       break;
243     }
244       
245   return handle_error(curl,err,errstr);
246 }
247
248 /* This is not the same exact set that is allowed according to
249    RFC-2396, but it is what the real curl uses. */
250 #define VALID_URI_CHARS "abcdefghijklmnopqrstuvwxyz" \
251                         "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
252                         "0123456789"
253
254 char *
255 curl_escape(char *str,int length)
256 {
257   int len,max,idx,enc_idx=0;
258   char *enc;
259
260   if(length)
261     len=length;
262   else
263     len=strlen(str);
264
265   enc=malloc(len+1);
266   if(!enc)
267     return enc;
268
269   max=len;
270
271   for(idx=0;idx<len;idx++)
272     {
273       if(enc_idx+3>max)
274         {
275           char *tmp;
276
277           max+=100;
278
279           tmp=realloc(enc,max+1);
280           if(!tmp)
281             {
282               free(enc);
283               return NULL;
284             }
285
286           enc=tmp;
287         }
288
289       if(strchr(VALID_URI_CHARS,str[idx]))
290         enc[enc_idx++]=str[idx];
291       else
292         {
293           char numbuf[5];
294           sprintf(numbuf,"%%%02X",str[idx]);
295           strcpy(&enc[enc_idx],numbuf);
296           enc_idx+=3;
297         }
298     }
299
300   enc[enc_idx]='\0';
301
302   return enc;
303 }
304
305 void
306 curl_free(char *ptr)
307 {
308   free(ptr);
309 }