Marked all unused args on non-W32 platforms.
[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, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28
29 #include "http.h"
30 #include "util.h"
31 #include "ksutil.h"
32 #include "curl-shim.h"
33
34 static CURLcode
35 handle_error(CURL *curl,CURLcode err,const char *str)
36 {
37   if(curl->errorbuffer)
38     {
39       /* Make sure you never exceed CURL_ERROR_SIZE, currently set to
40          256 in curl-shim.h */
41       switch(err)
42         {
43         case CURLE_OK:
44           strcpy(curl->errorbuffer,"okay");
45           break;
46
47         case CURLE_UNSUPPORTED_PROTOCOL:
48           strcpy(curl->errorbuffer,"unsupported protocol");
49           break;
50
51         case CURLE_COULDNT_CONNECT:
52           strcpy(curl->errorbuffer,"couldn't connect");
53           break;
54
55         case CURLE_WRITE_ERROR:
56           strcpy(curl->errorbuffer,"write error");
57           break;
58
59         case CURLE_HTTP_RETURNED_ERROR:
60           sprintf(curl->errorbuffer,"url returned error %u",curl->status);
61           break;
62
63         default:
64           strcpy(curl->errorbuffer,"generic error");
65           break;
66         }
67
68       if(str && (strlen(curl->errorbuffer)+2+strlen(str)+1)<=CURL_ERROR_SIZE)
69         {
70           strcat(curl->errorbuffer,": ");
71           strcat(curl->errorbuffer,str);
72         }
73     }
74
75   return err;
76 }
77
78 CURLcode
79 curl_global_init(long flags)
80 {
81   (void)flags;
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 #ifdef HAVE_W32_SYSTEM
94   w32_init_sockets ();
95 #endif
96
97   handle=calloc(1,sizeof(CURL));
98   if(handle)
99     handle->errors=stderr;
100
101   return handle;
102 }
103
104 void
105 curl_easy_cleanup(CURL *curl)
106 {
107   if (curl)
108     {
109       http_close (curl->hd, 0);
110       free(curl);
111     }
112 }
113
114 CURLcode
115 curl_easy_setopt(CURL *curl,CURLoption option,...)
116 {
117   va_list ap;
118
119   va_start(ap,option);
120
121   switch(option)
122     {
123     case CURLOPT_URL:
124       curl->url=va_arg(ap,char *);
125       break;
126     case CURLOPT_USERPWD:
127       curl->auth=va_arg(ap,char *);
128       break;
129     case CURLOPT_WRITEFUNCTION:
130       curl->writer=va_arg(ap,write_func);
131       break;
132     case CURLOPT_FILE:
133       curl->file=va_arg(ap,void *);
134       break;
135     case CURLOPT_ERRORBUFFER:
136       curl->errorbuffer=va_arg(ap,char *);
137       break;
138     case CURLOPT_PROXY:
139       curl->proxy=va_arg(ap,char *);
140       break;
141     case CURLOPT_POST:
142       curl->flags.post=va_arg(ap,long)?1:0;
143       break;
144     case CURLOPT_POSTFIELDS:
145       curl->postfields=va_arg(ap,char *);
146       break;
147     case CURLOPT_FAILONERROR:
148       curl->flags.failonerror=va_arg(ap,long)?1:0;
149       break;
150     case CURLOPT_VERBOSE:
151       curl->flags.verbose=va_arg(ap,long)?1:0;
152       break;
153     case CURLOPT_STDERR:
154       curl->errors=va_arg(ap,FILE *);
155       break;
156     default:
157       /* We ignore the huge majority of curl options */
158       break;
159     }
160
161   return handle_error(curl,CURLE_OK,NULL);
162 }
163
164 CURLcode
165 curl_easy_perform(CURL *curl)
166 {
167   int rc;
168   CURLcode err=CURLE_OK;
169   const char *errstr=NULL;
170   char *proxy=NULL;
171
172   /* Emulate the libcurl proxy behavior.  If the calling program set a
173      proxy, use it.  If it didn't set a proxy or set it to NULL, check
174      for one in the environment.  If the calling program explicitly
175      set a null-string proxy the http code doesn't use a proxy at
176      all. */
177
178   if(curl->proxy)
179     proxy=curl->proxy;
180   else
181     proxy=getenv(HTTP_PROXY_ENV);
182
183   if(curl->flags.verbose)
184     {
185       fprintf(curl->errors,"* HTTP proxy is \"%s\"\n",proxy?proxy:"null");
186       fprintf(curl->errors,"* HTTP URL is \"%s\"\n",curl->url);
187       fprintf(curl->errors,"* HTTP auth is \"%s\"\n",
188               curl->auth?curl->auth:"null");
189       fprintf(curl->errors,"* HTTP method is %s\n",
190               curl->flags.post?"POST":"GET");
191     }
192
193   if(curl->flags.post)
194     {
195       rc = http_open (&curl->hd, HTTP_REQ_POST, curl->url, curl->auth,
196                       0, proxy, NULL);
197       if (!rc)
198         {
199           unsigned int post_len = strlen(curl->postfields);
200
201           es_fprintf (http_get_write_ptr (curl->hd),
202                       "Content-Type: application/x-www-form-urlencoded\r\n"
203                       "Content-Length: %u\r\n", post_len);
204           http_start_data (curl->hd);
205           es_write (http_get_write_ptr (curl->hd),
206                     curl->postfields, post_len, NULL);
207
208           rc = http_wait_response (curl->hd);
209           curl->status = http_get_status_code (curl->hd);
210           if (!rc && curl->flags.failonerror && curl->status>=300)
211             err = CURLE_HTTP_RETURNED_ERROR;
212           http_close (curl->hd, 0);
213           curl->hd = NULL;
214         }
215     }
216   else
217     {
218       rc = http_open (&curl->hd, HTTP_REQ_GET, curl->url, curl->auth,
219                       0, proxy, NULL);
220       if (!rc)
221         {
222           rc = http_wait_response (curl->hd);
223           curl->status = http_get_status_code (curl->hd);
224           if (!rc)
225             {
226               if (curl->flags.failonerror && curl->status>=300)
227                 err = CURLE_HTTP_RETURNED_ERROR;
228               else
229                 {
230                   size_t maxlen = 1024;
231                   size_t buflen;
232                   unsigned int len;
233                   char *line = NULL;
234
235                   while ((len = es_read_line (http_get_read_ptr (curl->hd),
236                                               &line, &buflen, &maxlen)))
237                     {
238                       size_t ret;
239
240                       maxlen=1024;
241
242                       ret=(curl->writer)(line,len,1,curl->file);
243                       if(ret!=len)
244                         {
245                           err=CURLE_WRITE_ERROR;
246                           break;
247                         }
248                     }
249
250                   es_free (line);
251                   http_close(curl->hd, 0);
252                   curl->hd = NULL;
253                 }
254             }
255           else
256             {
257               http_close (curl->hd, 0);
258               curl->hd = NULL;
259             }
260         }
261     }
262
263   switch(gpg_err_code (rc))
264     {
265     case 0:
266       break;
267
268     case GPG_ERR_INV_URI:
269       err=CURLE_UNSUPPORTED_PROTOCOL;
270       break;
271
272     default:
273       errstr=gpg_strerror (rc);
274       err=CURLE_COULDNT_CONNECT;
275       break;
276     }
277       
278   return handle_error(curl,err,errstr);
279 }
280
281 /* This is not the same exact set that is allowed according to
282    RFC-2396, but it is what the real curl uses. */
283 #define VALID_URI_CHARS "abcdefghijklmnopqrstuvwxyz" \
284                         "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
285                         "0123456789"
286
287 char *
288 curl_escape(char *str,int length)
289 {
290   int len,max,idx,enc_idx=0;
291   char *enc;
292
293   if(length)
294     len=length;
295   else
296     len=strlen(str);
297
298   enc=malloc(len+1);
299   if(!enc)
300     return enc;
301
302   max=len;
303
304   for(idx=0;idx<len;idx++)
305     {
306       if(enc_idx+3>max)
307         {
308           char *tmp;
309
310           max+=100;
311
312           tmp=realloc(enc,max+1);
313           if(!tmp)
314             {
315               free(enc);
316               return NULL;
317             }
318
319           enc=tmp;
320         }
321
322       if(strchr(VALID_URI_CHARS,str[idx]))
323         enc[enc_idx++]=str[idx];
324       else
325         {
326           char numbuf[5];
327           sprintf(numbuf,"%%%02X",str[idx]);
328           strcpy(&enc[enc_idx],numbuf);
329           enc_idx+=3;
330         }
331     }
332
333   enc[enc_idx]='\0';
334
335   return enc;
336 }
337
338 curl_version_info_data *
339 curl_version_info(int type)
340 {
341   static curl_version_info_data data;
342   static const char *protocols[]={"http",NULL};
343
344   (void)type;
345
346   data.protocols=protocols;
347
348   return &data;
349 }