1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-02-08 17:43:04 +01:00

* curl-shim.h, curl-shim.c (handle_error, curl_easy_setopt,

curl_easy_perform): Add POST functionality to the curl shim.
This commit is contained in:
David Shaw 2005-04-17 01:39:24 +00:00
parent 352db25580
commit f50e99ed7b
3 changed files with 112 additions and 24 deletions

View File

@ -1,5 +1,8 @@
2005-04-16 David Shaw <dshaw@jabberwocky.com> 2005-04-16 David Shaw <dshaw@jabberwocky.com>
* curl-shim.h, curl-shim.c (handle_error, curl_easy_setopt,
curl_easy_perform): Add POST functionality to the curl shim.
* curl-shim.h, curl-shim.c (curl_escape, curl_free): Emulate * curl-shim.h, curl-shim.c (curl_escape, curl_free): Emulate
curl_escape and curl_free. curl_escape and curl_free.

View File

@ -48,6 +48,10 @@ static CURLcode handle_error(CURL *curl,CURLcode err,const char *str)
strcpy(curl->errorbuffer,"write error"); strcpy(curl->errorbuffer,"write error");
break; break;
case CURLE_HTTP_RETURNED_ERROR:
sprintf(curl->errorbuffer,"url returned error %u",curl->status);
break;
default: default:
strcpy(curl->errorbuffer,"generic error"); strcpy(curl->errorbuffer,"generic error");
break; break;
@ -103,6 +107,15 @@ CURLcode curl_easy_setopt(CURL *curl,CURLoption option,...)
case CURLOPT_PROXY: case CURLOPT_PROXY:
curl->proxy=va_arg(ap,char *); curl->proxy=va_arg(ap,char *);
break; break;
case CURLOPT_POST:
curl->flags.post=va_arg(ap,unsigned int);
break;
case CURLOPT_POSTFIELDS:
curl->postfields=va_arg(ap,char *);
break;
case CURLOPT_FAILONERROR:
curl->flags.failonerror=va_arg(ap,unsigned int);
break;
default: default:
/* We ignore the huge majority of curl options */ /* We ignore the huge majority of curl options */
break; break;
@ -117,7 +130,9 @@ CURLcode curl_easy_perform(CURL *curl)
CURLcode err=CURLE_OK; CURLcode err=CURLE_OK;
const char *errstr=NULL; const char *errstr=NULL;
rc=http_open_document(&curl->hd,curl->url,0,curl->proxy); if(curl->flags.post)
{
rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,0,curl->proxy);
if(rc!=0) if(rc!=0)
{ {
if(rc==G10ERR_NETWORK) if(rc==G10ERR_NETWORK)
@ -127,12 +142,69 @@ CURLcode curl_easy_perform(CURL *curl)
err=CURLE_COULDNT_CONNECT; err=CURLE_COULDNT_CONNECT;
} }
else
{
char content_len[50];
unsigned int post_len=strlen(curl->postfields);
iobuf_writestr(curl->hd.fp_write,
"Content-Type: application/x-www-form-urlencoded\r\n");
sprintf(content_len,"Content-Length: %u\r\n",post_len);
iobuf_writestr(curl->hd.fp_write,content_len);
http_start_data(&curl->hd);
iobuf_write(curl->hd.fp_write,curl->postfields,post_len);
rc=http_wait_response(&curl->hd,&curl->status);
if(rc!=0)
{
if(rc==G10ERR_NETWORK)
errstr=strerror(errno);
else
errstr=g10_errstr(rc);
err=CURLE_COULDNT_CONNECT;
}
if(curl->flags.failonerror && curl->status>=300)
err=CURLE_HTTP_RETURNED_ERROR;
}
}
else
{
rc=http_open(&curl->hd,HTTP_REQ_GET,curl->url,0,curl->proxy);
if(rc!=0)
{
if(rc==G10ERR_NETWORK)
errstr=strerror(errno);
else
errstr=g10_errstr(rc);
err=CURLE_COULDNT_CONNECT;
}
else
{
rc=http_wait_response(&curl->hd,&curl->status);
if(rc)
{
http_close(&curl->hd);
if(rc==G10ERR_NETWORK)
errstr=strerror(errno);
else
errstr=g10_errstr(rc);
err=CURLE_COULDNT_CONNECT;
}
else if(curl->flags.failonerror && curl->status>=300)
err=CURLE_HTTP_RETURNED_ERROR;
else else
{ {
unsigned int maxlen=1024,buflen,len; unsigned int maxlen=1024,buflen,len;
byte *line=NULL; byte *line=NULL;
while((len=iobuf_read_line(curl->hd.fp_read,&line,&buflen,&maxlen))) while((len=iobuf_read_line(curl->hd.fp_read,
&line,&buflen,&maxlen)))
{ {
maxlen=1024; maxlen=1024;
size_t ret; size_t ret;
@ -148,6 +220,8 @@ CURLcode curl_easy_perform(CURL *curl)
m_free(line); m_free(line);
http_close(&curl->hd); http_close(&curl->hd);
} }
}
}
return handle_error(curl,err,errstr); return handle_error(curl,err,errstr);
} }

View File

@ -26,9 +26,10 @@
typedef enum typedef enum
{ {
CURLE_OK=0, CURLE_OK=0,
CURLE_FTP_COULDNT_RETR_FILE, CURLE_COULDNT_CONNECT=7,
CURLE_COULDNT_CONNECT, CURLE_FTP_COULDNT_RETR_FILE=19,
CURLE_WRITE_ERROR CURLE_HTTP_RETURNED_ERROR=22,
CURLE_WRITE_ERROR=23
} CURLcode; } CURLcode;
typedef enum typedef enum
@ -43,7 +44,10 @@ typedef enum
CURLOPT_VERBOSE, CURLOPT_VERBOSE,
CURLOPT_SSL_VERIFYPEER, CURLOPT_SSL_VERIFYPEER,
CURLOPT_PROXY, CURLOPT_PROXY,
CURLOPT_CAINFO CURLOPT_CAINFO,
CURLOPT_POST,
CURLOPT_POSTFIELDS,
CURLOPT_FAILONERROR
} CURLoption; } CURLoption;
typedef size_t (*write_func)(char *buffer,size_t size, typedef size_t (*write_func)(char *buffer,size_t size,
@ -56,6 +60,13 @@ typedef struct
char *proxy; char *proxy;
write_func writer; write_func writer;
void *file; void *file;
char *postfields;
unsigned int status;
struct
{
unsigned int post:1;
unsigned int failonerror:1;
} flags;
struct http_context hd; struct http_context hd;
} CURL; } CURL;