* ksutil.h, ksutil.c (parse_ks_options): New keyserver-option
[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                       maxlen=1024;
205                       size_t ret;
206
207                       ret=(curl->writer)(line,len,1,curl->file);
208                       if(ret!=len)
209                         {
210                           err=CURLE_WRITE_ERROR;
211                           break;
212                         }
213                     }
214
215                   xfree(line);
216                   http_close(&curl->hd);
217                 }
218             }
219           else
220             http_close(&curl->hd);
221         }
222     }
223
224   switch(rc)
225     {
226     case 0:
227       break;
228
229     case G10ERR_INVALID_URI:
230       err=CURLE_UNSUPPORTED_PROTOCOL;
231       break;
232
233     case G10ERR_NETWORK:
234       errstr=strerror(errno);
235       err=CURLE_COULDNT_CONNECT;
236       break;
237
238     default:
239       errstr=g10_errstr(rc);
240       err=CURLE_COULDNT_CONNECT;
241       break;
242     }
243       
244   return handle_error(curl,err,errstr);
245 }
246
247 /* This is not the same exact set that is allowed according to
248    RFC-2396, but it is what the real curl uses. */
249 #define VALID_URI_CHARS "abcdefghijklmnopqrstuvwxyz" \
250                         "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
251                         "0123456789"
252
253 char *
254 curl_escape(char *str,int length)
255 {
256   int len,max,idx,enc_idx=0;
257   char *enc;
258
259   if(length)
260     len=length;
261   else
262     len=strlen(str);
263
264   enc=malloc(len+1);
265   if(!enc)
266     return enc;
267
268   max=len;
269
270   for(idx=0;idx<len;idx++)
271     {
272       if(enc_idx+3>max)
273         {
274           char *tmp;
275
276           max+=100;
277
278           tmp=realloc(enc,max+1);
279           if(!tmp)
280             {
281               free(enc);
282               return NULL;
283             }
284
285           enc=tmp;
286         }
287
288       if(strchr(VALID_URI_CHARS,str[idx]))
289         enc[enc_idx++]=str[idx];
290       else
291         {
292           char numbuf[5];
293           sprintf(numbuf,"%%%02X",str[idx]);
294           strcpy(&enc[enc_idx],numbuf);
295           enc_idx+=3;
296         }
297     }
298
299   enc[enc_idx]='\0';
300
301   return enc;
302 }
303
304 void
305 curl_free(char *ptr)
306 {
307   free(ptr);
308 }