31
33
#include "curl-shim.h"
33
static CURLcode handle_error(CURL *curl,CURLcode err,const char *str)
36
handle_error(CURL *curl,CURLcode err,const char *str)
35
38
if(curl->errorbuffer)
40
/* Make sure you never exceed CURL_ERROR_SIZE, currently set to
40
45
strcpy(curl->errorbuffer,"okay");
48
case CURLE_UNSUPPORTED_PROTOCOL:
49
strcpy(curl->errorbuffer,"unsupported protocol");
43
52
case CURLE_COULDNT_CONNECT:
44
53
strcpy(curl->errorbuffer,"couldn't connect");
66
CURLcode curl_global_init(long flags)
80
curl_global_init(long flags)
71
void curl_global_cleanup(void) {}
86
curl_global_cleanup(void) {}
73
CURL *curl_easy_init(void)
75
91
return calloc(1,sizeof(CURL));
78
void curl_easy_cleanup(CURL *curl)
95
curl_easy_cleanup(CURL *curl)
83
CURLcode curl_easy_setopt(CURL *curl,CURLoption option,...)
101
curl_easy_setopt(CURL *curl,CURLoption option,...)
111
141
return handle_error(curl,CURLE_OK,NULL);
114
CURLcode curl_easy_perform(CURL *curl)
145
curl_easy_perform(CURL *curl)
117
148
CURLcode err=CURLE_OK;
118
149
const char *errstr=NULL;
120
rc=http_open_document(&curl->hd,curl->url,0,curl->proxy);
123
if(rc==G10ERR_NETWORK)
124
errstr=strerror(errno);
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. */
163
proxy=getenv(HTTP_PROXY_ENV);
167
rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,curl->auth,0,proxy);
170
char content_len[50];
171
unsigned int post_len=strlen(curl->postfields);
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);
177
iobuf_writestr(curl->hd.fp_write,content_len);
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;
188
rc=http_open(&curl->hd,HTTP_REQ_GET,curl->url,curl->auth,0,proxy);
191
rc=http_wait_response(&curl->hd,&curl->status);
194
if(curl->flags.failonerror && curl->status>=300)
195
err=CURLE_HTTP_RETURNED_ERROR;
198
unsigned int maxlen=1024,buflen,len;
201
while((len=iobuf_read_line(curl->hd.fp_read,
202
&line,&buflen,&maxlen)))
207
ret=(curl->writer)(line,len,1,curl->file);
210
err=CURLE_WRITE_ERROR;
216
http_close(&curl->hd);
220
http_close(&curl->hd);
229
case G10ERR_INVALID_URI:
230
err=CURLE_UNSUPPORTED_PROTOCOL;
234
errstr=strerror(errno);
235
err=CURLE_COULDNT_CONNECT;
239
errstr=g10_errstr(rc);
240
err=CURLE_COULDNT_CONNECT;
244
return handle_error(curl,err,errstr);
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" \
254
curl_escape(char *str,int length)
256
int len,max,idx,enc_idx=0;
270
for(idx=0;idx<len;idx++)
278
tmp=realloc(enc,max+1);
288
if(strchr(VALID_URI_CHARS,str[idx]))
289
enc[enc_idx++]=str[idx];
126
errstr=g10_errstr(rc);
128
err=CURLE_COULDNT_CONNECT;
132
unsigned int maxlen=1024,buflen,len;
135
while((len=iobuf_read_line(curl->hd.fp_read,&line,&buflen,&maxlen)))
140
ret=(curl->writer)(line,len,1,curl->file);
143
err=CURLE_WRITE_ERROR;
293
sprintf(numbuf,"%%%02X",str[idx]);
294
strcpy(&enc[enc_idx],numbuf);
149
http_close(&curl->hd);
152
return handle_error(curl,err,errstr);