aboutsummaryrefslogtreecommitdiffstats
path: root/keyserver/curl-shim.c
diff options
context:
space:
mode:
authorDavid Shaw <[email protected]>2005-04-17 01:39:24 +0000
committerDavid Shaw <[email protected]>2005-04-17 01:39:24 +0000
commitf50e99ed7b9f55e95daa863e53c1a70da3110c1a (patch)
tree474e27dfa5759b604e81d4f39f2d58847d33c32e /keyserver/curl-shim.c
parent* configure.ac: Remove --disable-old-hkp - use new HKP handler for (diff)
downloadgnupg-f50e99ed7b9f55e95daa863e53c1a70da3110c1a.tar.gz
gnupg-f50e99ed7b9f55e95daa863e53c1a70da3110c1a.zip
* curl-shim.h, curl-shim.c (handle_error, curl_easy_setopt,
curl_easy_perform): Add POST functionality to the curl shim.
Diffstat (limited to 'keyserver/curl-shim.c')
-rw-r--r--keyserver/curl-shim.c110
1 files changed, 92 insertions, 18 deletions
diff --git a/keyserver/curl-shim.c b/keyserver/curl-shim.c
index f9ef61aa0..8e0641f27 100644
--- a/keyserver/curl-shim.c
+++ b/keyserver/curl-shim.c
@@ -48,6 +48,10 @@ static CURLcode handle_error(CURL *curl,CURLcode err,const char *str)
strcpy(curl->errorbuffer,"write error");
break;
+ case CURLE_HTTP_RETURNED_ERROR:
+ sprintf(curl->errorbuffer,"url returned error %u",curl->status);
+ break;
+
default:
strcpy(curl->errorbuffer,"generic error");
break;
@@ -103,6 +107,15 @@ CURLcode curl_easy_setopt(CURL *curl,CURLoption option,...)
case CURLOPT_PROXY:
curl->proxy=va_arg(ap,char *);
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:
/* We ignore the huge majority of curl options */
break;
@@ -117,36 +130,97 @@ CURLcode curl_easy_perform(CURL *curl)
CURLcode err=CURLE_OK;
const char *errstr=NULL;
- rc=http_open_document(&curl->hd,curl->url,0,curl->proxy);
- if(rc!=0)
+ if(curl->flags.post)
{
- if(rc==G10ERR_NETWORK)
- errstr=strerror(errno);
+ rc=http_open(&curl->hd,HTTP_REQ_POST,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
- errstr=g10_errstr(rc);
+ {
+ char content_len[50];
+ unsigned int post_len=strlen(curl->postfields);
- err=CURLE_COULDNT_CONNECT;
+ 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
{
- unsigned int maxlen=1024,buflen,len;
- byte *line=NULL;
+ 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);
- while((len=iobuf_read_line(curl->hd.fp_read,&line,&buflen,&maxlen)))
+ err=CURLE_COULDNT_CONNECT;
+ }
+ else
{
- maxlen=1024;
- size_t ret;
+ 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);
- ret=(curl->writer)(line,len,1,curl->file);
- if(ret!=len)
+ err=CURLE_COULDNT_CONNECT;
+ }
+ else if(curl->flags.failonerror && curl->status>=300)
+ err=CURLE_HTTP_RETURNED_ERROR;
+ else
{
- err=CURLE_WRITE_ERROR;
- break;
+ unsigned int maxlen=1024,buflen,len;
+ byte *line=NULL;
+
+ while((len=iobuf_read_line(curl->hd.fp_read,
+ &line,&buflen,&maxlen)))
+ {
+ maxlen=1024;
+ size_t ret;
+
+ ret=(curl->writer)(line,len,1,curl->file);
+ if(ret!=len)
+ {
+ err=CURLE_WRITE_ERROR;
+ break;
+ }
+ }
+
+ m_free(line);
+ http_close(&curl->hd);
}
}
-
- m_free(line);
- http_close(&curl->hd);
}
return handle_error(curl,err,errstr);