* curl-shim.c (curl_easy_perform): Minor cleanup of proxy code.
[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, 2006 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   CURL *handle;
92
93   handle=calloc(1,sizeof(CURL));
94   if(handle)
95     handle->errors=stderr;
96
97   return handle;
98 }
99
100 void
101 curl_easy_cleanup(CURL *curl)
102 {
103   free(curl);
104 }
105
106 CURLcode
107 curl_easy_setopt(CURL *curl,CURLoption option,...)
108 {
109   va_list ap;
110
111   va_start(ap,option);
112
113   switch(option)
114     {
115     case CURLOPT_URL:
116       curl->url=va_arg(ap,char *);
117       break;
118     case CURLOPT_USERPWD:
119       curl->auth=va_arg(ap,char *);
120       break;
121     case CURLOPT_WRITEFUNCTION:
122       curl->writer=va_arg(ap,write_func);
123       break;
124     case CURLOPT_FILE:
125       curl->file=va_arg(ap,void *);
126       break;
127     case CURLOPT_ERRORBUFFER:
128       curl->errorbuffer=va_arg(ap,char *);
129       break;
130     case CURLOPT_PROXY:
131       curl->proxy=va_arg(ap,char *);
132       break;
133     case CURLOPT_POST:
134       curl->flags.post=va_arg(ap,unsigned int);
135       break;
136     case CURLOPT_POSTFIELDS:
137       curl->postfields=va_arg(ap,char *);
138       break;
139     case CURLOPT_FAILONERROR:
140       curl->flags.failonerror=va_arg(ap,unsigned int);
141       break;
142     case CURLOPT_VERBOSE:
143       curl->flags.verbose=va_arg(ap,unsigned int);
144       break;
145     case CURLOPT_STDERR:
146       curl->errors=va_arg(ap,FILE *);
147       break;
148     default:
149       /* We ignore the huge majority of curl options */
150       break;
151     }
152
153   return handle_error(curl,CURLE_OK,NULL);
154 }
155
156 CURLcode
157 curl_easy_perform(CURL *curl)
158 {
159   int rc;
160   CURLcode err=CURLE_OK;
161   const char *errstr=NULL;
162   char *proxy=NULL;
163
164   /* Emulate the libcurl proxy behavior.  If the calling program set a
165      proxy, use it.  If it didn't set a proxy or set it to NULL, check
166      for one in the environment.  If the calling program explicitly
167      set a null-string proxy the http code doesn't use a proxy at
168      all. */
169
170   if(curl->proxy)
171     proxy=curl->proxy;
172   else
173     proxy=getenv(HTTP_PROXY_ENV);
174
175   if(curl->flags.verbose)
176     fprintf(curl->errors,"* HTTP proxy is \"%s\"\n",proxy?proxy:"null");
177
178   if(curl->flags.post)
179     {
180       rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,curl->auth,0,proxy);
181       if(rc==0)
182         {
183           char content_len[50];
184           unsigned int post_len=strlen(curl->postfields);
185
186           iobuf_writestr(curl->hd.fp_write,
187                          "Content-Type: application/x-www-form-urlencoded\r\n");
188           sprintf(content_len,"Content-Length: %u\r\n",post_len);
189
190           iobuf_writestr(curl->hd.fp_write,content_len);
191
192           http_start_data(&curl->hd);
193           iobuf_write(curl->hd.fp_write,curl->postfields,post_len);
194           rc=http_wait_response(&curl->hd,&curl->status);
195           if(rc==0 && curl->flags.failonerror && curl->status>=300)
196             err=CURLE_HTTP_RETURNED_ERROR;
197         }
198     }
199   else
200     {
201       rc=http_open(&curl->hd,HTTP_REQ_GET,curl->url,curl->auth,0,proxy);
202       if(rc==0)
203         {
204           rc=http_wait_response(&curl->hd,&curl->status);
205           if(rc==0)
206             {
207               if(curl->flags.failonerror && curl->status>=300)
208                 err=CURLE_HTTP_RETURNED_ERROR;
209               else
210                 {
211                   unsigned int maxlen=1024,buflen,len;
212                   byte *line=NULL;
213
214                   while((len=iobuf_read_line(curl->hd.fp_read,
215                                              &line,&buflen,&maxlen)))
216                     {
217                       size_t ret;
218
219                       maxlen=1024;
220
221                       ret=(curl->writer)(line,len,1,curl->file);
222                       if(ret!=len)
223                         {
224                           err=CURLE_WRITE_ERROR;
225                           break;
226                         }
227                     }
228
229                   xfree(line);
230                   http_close(&curl->hd);
231                 }
232             }
233           else
234             http_close(&curl->hd);
235         }
236     }
237
238   switch(rc)
239     {
240     case 0:
241       break;
242
243     case G10ERR_INVALID_URI:
244       err=CURLE_UNSUPPORTED_PROTOCOL;
245       break;
246
247     case G10ERR_NETWORK:
248       errstr=strerror(errno);
249       err=CURLE_COULDNT_CONNECT;
250       break;
251
252     default:
253       errstr=g10_errstr(rc);
254       err=CURLE_COULDNT_CONNECT;
255       break;
256     }
257       
258   return handle_error(curl,err,errstr);
259 }
260
261 /* This is not the same exact set that is allowed according to
262    RFC-2396, but it is what the real curl uses. */
263 #define VALID_URI_CHARS "abcdefghijklmnopqrstuvwxyz" \
264                         "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
265                         "0123456789"
266
267 char *
268 curl_escape(char *str,int length)
269 {
270   int len,max,idx,enc_idx=0;
271   char *enc;
272
273   if(length)
274     len=length;
275   else
276     len=strlen(str);
277
278   enc=malloc(len+1);
279   if(!enc)
280     return enc;
281
282   max=len;
283
284   for(idx=0;idx<len;idx++)
285     {
286       if(enc_idx+3>max)
287         {
288           char *tmp;
289
290           max+=100;
291
292           tmp=realloc(enc,max+1);
293           if(!tmp)
294             {
295               free(enc);
296               return NULL;
297             }
298
299           enc=tmp;
300         }
301
302       if(strchr(VALID_URI_CHARS,str[idx]))
303         enc[enc_idx++]=str[idx];
304       else
305         {
306           char numbuf[5];
307           sprintf(numbuf,"%%%02X",(unsigned char)str[idx]);
308           strcpy(&enc[enc_idx],numbuf);
309           enc_idx+=3;
310         }
311     }
312
313   enc[enc_idx]='\0';
314
315   return enc;
316 }
317
318 void
319 curl_free(char *ptr)
320 {
321   free(ptr);
322 }