~ubuntu-branches/ubuntu/hardy/gnupg/hardy-updates

« back to all changes in this revision

Viewing changes to keyserver/curl-shim.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-12-16 16:57:39 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20051216165739-v0m2d1you6hd8jho
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *
18
18
 * You should have received a copy of the GNU General Public License
19
19
 * along with this program; if not, write to the Free Software
20
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 
21
 * USA.
21
22
 */
22
23
 
23
24
#include <config.h>
28
29
#include <errno.h>
29
30
#include "http.h"
30
31
#include "util.h"
 
32
#include "ksutil.h"
31
33
#include "curl-shim.h"
32
34
 
33
 
static CURLcode handle_error(CURL *curl,CURLcode err,const char *str)
 
35
static CURLcode
 
36
handle_error(CURL *curl,CURLcode err,const char *str)
34
37
{
35
38
  if(curl->errorbuffer)
36
39
    {
 
40
      /* Make sure you never exceed CURL_ERROR_SIZE, currently set to
 
41
         256 in curl-shim.h */
37
42
      switch(err)
38
43
        {
39
44
        case CURLE_OK:
40
45
          strcpy(curl->errorbuffer,"okay");
41
46
          break;
42
47
 
 
48
        case CURLE_UNSUPPORTED_PROTOCOL:
 
49
          strcpy(curl->errorbuffer,"unsupported protocol");
 
50
          break;
 
51
 
43
52
        case CURLE_COULDNT_CONNECT:
44
53
          strcpy(curl->errorbuffer,"couldn't connect");
45
54
          break;
48
57
          strcpy(curl->errorbuffer,"write error");
49
58
          break;
50
59
 
 
60
        case CURLE_HTTP_RETURNED_ERROR:
 
61
          sprintf(curl->errorbuffer,"url returned error %u",curl->status);
 
62
          break;
 
63
 
51
64
        default:
52
65
          strcpy(curl->errorbuffer,"generic error");
53
66
          break;
63
76
  return err;
64
77
}
65
78
 
66
 
CURLcode curl_global_init(long flags)
 
79
CURLcode
 
80
curl_global_init(long flags)
67
81
{
68
82
  return CURLE_OK;
69
83
}
70
84
 
71
 
void curl_global_cleanup(void) {}
 
85
void
 
86
curl_global_cleanup(void) {}
72
87
 
73
 
CURL *curl_easy_init(void)
 
88
CURL *
 
89
curl_easy_init(void)
74
90
{
75
91
  return calloc(1,sizeof(CURL));
76
92
}
77
93
 
78
 
void curl_easy_cleanup(CURL *curl)
 
94
void
 
95
curl_easy_cleanup(CURL *curl)
79
96
{
80
97
  free(curl);
81
98
}
82
99
 
83
 
CURLcode curl_easy_setopt(CURL *curl,CURLoption option,...)
 
100
CURLcode
 
101
curl_easy_setopt(CURL *curl,CURLoption option,...)
84
102
{
85
103
  va_list ap;
86
104
 
91
109
    case CURLOPT_URL:
92
110
      curl->url=va_arg(ap,char *);
93
111
      break;
 
112
    case CURLOPT_USERPWD:
 
113
      curl->auth=va_arg(ap,char *);
 
114
      break;
94
115
    case CURLOPT_WRITEFUNCTION:
95
116
      curl->writer=va_arg(ap,write_func);
96
117
      break;
103
124
    case CURLOPT_PROXY:
104
125
      curl->proxy=va_arg(ap,char *);
105
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;
106
136
    default:
107
137
      /* We ignore the huge majority of curl options */
108
138
      break;
111
141
  return handle_error(curl,CURLE_OK,NULL);
112
142
}
113
143
 
114
 
CURLcode curl_easy_perform(CURL *curl)
 
144
CURLcode
 
145
curl_easy_perform(CURL *curl)
115
146
{
116
147
  int rc;
117
148
  CURLcode err=CURLE_OK;
118
149
  const char *errstr=NULL;
119
 
 
120
 
  rc=http_open_document(&curl->hd,curl->url,0,curl->proxy);
121
 
  if(rc!=0)
122
 
    {
123
 
      if(rc==G10ERR_NETWORK)
124
 
        errstr=strerror(errno);
 
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
                  m_free(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];
125
290
      else
126
 
        errstr=g10_errstr(rc);
127
 
 
128
 
      err=CURLE_COULDNT_CONNECT;
129
 
    }
130
 
  else
131
 
    {
132
 
      unsigned int maxlen=1024,buflen,len;
133
 
      byte *line=NULL;
134
 
 
135
 
      while((len=iobuf_read_line(curl->hd.fp_read,&line,&buflen,&maxlen)))
136
291
        {
137
 
          maxlen=1024;
138
 
          size_t ret;
139
 
 
140
 
          ret=(curl->writer)(line,len,1,curl->file);
141
 
          if(ret!=len)
142
 
            {
143
 
              err=CURLE_WRITE_ERROR;
144
 
              break;
145
 
            }
 
292
          char numbuf[5];
 
293
          sprintf(numbuf,"%%%02X",str[idx]);
 
294
          strcpy(&enc[enc_idx],numbuf);
 
295
          enc_idx+=3;
146
296
        }
147
 
 
148
 
      m_free(line);
149
 
      http_close(&curl->hd);
150
297
    }
151
298
 
152
 
  return handle_error(curl,err,errstr);
 
299
  enc[enc_idx]='\0';
 
300
 
 
301
  return enc;
 
302
}
 
303
 
 
304
void
 
305
curl_free(char *ptr)
 
306
{
 
307
  free(ptr);
153
308
}