2005-02-11 19:05:13 +01:00
|
|
|
/* curl-shim.c - Implement a small subset of the curl API in terms of
|
|
|
|
* the iobuf HTTP API
|
|
|
|
*
|
2007-01-16 19:12:43 +01:00
|
|
|
* Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
|
2005-02-11 19:05:13 +01:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-10-23 12:48:09 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2005-02-11 19:05:13 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-10-23 12:48:09 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2005-02-11 19:05:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "http.h"
|
|
|
|
#include "util.h"
|
2005-06-24 01:42:35 +02:00
|
|
|
#include "ksutil.h"
|
2005-02-11 19:05:13 +01:00
|
|
|
#include "curl-shim.h"
|
|
|
|
|
2005-04-17 04:18:32 +02:00
|
|
|
static CURLcode
|
|
|
|
handle_error(CURL *curl,CURLcode err,const char *str)
|
2005-02-11 19:05:13 +01:00
|
|
|
{
|
|
|
|
if(curl->errorbuffer)
|
|
|
|
{
|
2005-04-17 04:18:32 +02:00
|
|
|
/* Make sure you never exceed CURL_ERROR_SIZE, currently set to
|
|
|
|
256 in curl-shim.h */
|
2005-02-11 19:05:13 +01:00
|
|
|
switch(err)
|
|
|
|
{
|
|
|
|
case CURLE_OK:
|
|
|
|
strcpy(curl->errorbuffer,"okay");
|
|
|
|
break;
|
|
|
|
|
2005-07-20 23:48:28 +02:00
|
|
|
case CURLE_UNSUPPORTED_PROTOCOL:
|
|
|
|
strcpy(curl->errorbuffer,"unsupported protocol");
|
|
|
|
break;
|
|
|
|
|
2005-02-11 19:05:13 +01:00
|
|
|
case CURLE_COULDNT_CONNECT:
|
|
|
|
strcpy(curl->errorbuffer,"couldn't connect");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURLE_WRITE_ERROR:
|
|
|
|
strcpy(curl->errorbuffer,"write error");
|
|
|
|
break;
|
|
|
|
|
2005-04-17 03:39:24 +02:00
|
|
|
case CURLE_HTTP_RETURNED_ERROR:
|
|
|
|
sprintf(curl->errorbuffer,"url returned error %u",curl->status);
|
|
|
|
break;
|
|
|
|
|
2005-02-11 19:05:13 +01:00
|
|
|
default:
|
|
|
|
strcpy(curl->errorbuffer,"generic error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(str && (strlen(curl->errorbuffer)+2+strlen(str)+1)<=CURL_ERROR_SIZE)
|
|
|
|
{
|
|
|
|
strcat(curl->errorbuffer,": ");
|
|
|
|
strcat(curl->errorbuffer,str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2005-04-17 04:18:32 +02:00
|
|
|
CURLcode
|
|
|
|
curl_global_init(long flags)
|
2005-02-11 19:05:13 +01:00
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2005-04-17 04:18:32 +02:00
|
|
|
void
|
|
|
|
curl_global_cleanup(void) {}
|
2005-02-11 19:05:13 +01:00
|
|
|
|
2005-04-17 04:18:32 +02:00
|
|
|
CURL *
|
|
|
|
curl_easy_init(void)
|
2005-02-11 19:05:13 +01:00
|
|
|
{
|
2006-02-21 17:16:09 +01:00
|
|
|
CURL *handle;
|
|
|
|
|
|
|
|
handle=calloc(1,sizeof(CURL));
|
|
|
|
if(handle)
|
2006-02-22 03:11:35 +01:00
|
|
|
handle->errors=stderr;
|
2006-02-21 17:16:09 +01:00
|
|
|
|
|
|
|
return handle;
|
2005-02-11 19:05:13 +01:00
|
|
|
}
|
|
|
|
|
2005-04-17 04:18:32 +02:00
|
|
|
void
|
|
|
|
curl_easy_cleanup(CURL *curl)
|
2005-02-11 19:05:13 +01:00
|
|
|
{
|
|
|
|
free(curl);
|
|
|
|
}
|
|
|
|
|
2005-04-17 04:18:32 +02:00
|
|
|
CURLcode
|
|
|
|
curl_easy_setopt(CURL *curl,CURLoption option,...)
|
2005-02-11 19:05:13 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap,option);
|
|
|
|
|
|
|
|
switch(option)
|
|
|
|
{
|
|
|
|
case CURLOPT_URL:
|
|
|
|
curl->url=va_arg(ap,char *);
|
|
|
|
break;
|
2005-06-23 06:44:20 +02:00
|
|
|
case CURLOPT_USERPWD:
|
|
|
|
curl->auth=va_arg(ap,char *);
|
|
|
|
break;
|
2005-02-11 19:05:13 +01:00
|
|
|
case CURLOPT_WRITEFUNCTION:
|
|
|
|
curl->writer=va_arg(ap,write_func);
|
|
|
|
break;
|
|
|
|
case CURLOPT_FILE:
|
|
|
|
curl->file=va_arg(ap,void *);
|
|
|
|
break;
|
|
|
|
case CURLOPT_ERRORBUFFER:
|
|
|
|
curl->errorbuffer=va_arg(ap,char *);
|
|
|
|
break;
|
|
|
|
case CURLOPT_PROXY:
|
|
|
|
curl->proxy=va_arg(ap,char *);
|
|
|
|
break;
|
2005-04-17 03:39:24 +02:00
|
|
|
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;
|
2006-02-21 17:16:09 +01:00
|
|
|
case CURLOPT_VERBOSE:
|
|
|
|
curl->flags.verbose=va_arg(ap,unsigned int);
|
|
|
|
break;
|
|
|
|
case CURLOPT_STDERR:
|
2006-02-22 03:11:35 +01:00
|
|
|
curl->errors=va_arg(ap,FILE *);
|
2006-02-21 17:16:09 +01:00
|
|
|
break;
|
2005-02-11 19:05:13 +01:00
|
|
|
default:
|
|
|
|
/* We ignore the huge majority of curl options */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handle_error(curl,CURLE_OK,NULL);
|
|
|
|
}
|
|
|
|
|
2005-04-17 04:18:32 +02:00
|
|
|
CURLcode
|
|
|
|
curl_easy_perform(CURL *curl)
|
2005-02-11 19:05:13 +01:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
CURLcode err=CURLE_OK;
|
|
|
|
const char *errstr=NULL;
|
2005-06-24 01:42:35 +02:00
|
|
|
char *proxy=NULL;
|
|
|
|
|
|
|
|
/* Emulate the libcurl proxy behavior. If the calling program set a
|
|
|
|
proxy, use it. If it didn't set a proxy or set it to NULL, check
|
|
|
|
for one in the environment. If the calling program explicitly
|
2006-07-21 04:19:43 +02:00
|
|
|
set a null-string proxy the http code doesn't use a proxy at
|
|
|
|
all. */
|
2005-06-24 01:42:35 +02:00
|
|
|
|
|
|
|
if(curl->proxy)
|
2006-07-21 04:19:43 +02:00
|
|
|
proxy=curl->proxy;
|
2005-06-24 01:42:35 +02:00
|
|
|
else
|
|
|
|
proxy=getenv(HTTP_PROXY_ENV);
|
2005-02-11 19:05:13 +01:00
|
|
|
|
2006-02-21 17:16:09 +01:00
|
|
|
if(curl->flags.verbose)
|
2007-02-11 04:56:59 +01:00
|
|
|
{
|
|
|
|
fprintf(curl->errors,"* HTTP proxy is \"%s\"\n",proxy?proxy:"null");
|
|
|
|
fprintf(curl->errors,"* HTTP URL is \"%s\"\n",curl->url);
|
|
|
|
fprintf(curl->errors,"* HTTP auth is \"%s\"\n",
|
|
|
|
curl->auth?curl->auth:"null");
|
|
|
|
fprintf(curl->errors,"* HTTP method is %s\n",
|
|
|
|
curl->flags.post?"POST":"GET");
|
|
|
|
}
|
2006-02-21 17:16:09 +01:00
|
|
|
|
2005-04-17 03:39:24 +02:00
|
|
|
if(curl->flags.post)
|
2005-02-11 19:05:13 +01:00
|
|
|
{
|
2005-06-24 01:42:35 +02:00
|
|
|
rc=http_open(&curl->hd,HTTP_REQ_POST,curl->url,curl->auth,0,proxy);
|
2005-04-17 04:18:32 +02:00
|
|
|
if(rc==0)
|
2005-04-17 03:39:24 +02:00
|
|
|
{
|
|
|
|
char content_len[50];
|
|
|
|
unsigned int post_len=strlen(curl->postfields);
|
2005-02-11 19:05:13 +01:00
|
|
|
|
2005-04-17 03:39:24 +02:00
|
|
|
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);
|
2005-04-17 04:18:32 +02:00
|
|
|
if(rc==0 && curl->flags.failonerror && curl->status>=300)
|
2005-04-17 03:39:24 +02:00
|
|
|
err=CURLE_HTTP_RETURNED_ERROR;
|
|
|
|
}
|
2005-02-11 19:05:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-06-24 01:42:35 +02:00
|
|
|
rc=http_open(&curl->hd,HTTP_REQ_GET,curl->url,curl->auth,0,proxy);
|
2005-04-17 04:18:32 +02:00
|
|
|
if(rc==0)
|
2005-02-11 19:05:13 +01:00
|
|
|
{
|
2005-04-17 03:39:24 +02:00
|
|
|
rc=http_wait_response(&curl->hd,&curl->status);
|
2005-04-17 04:18:32 +02:00
|
|
|
if(rc==0)
|
2005-04-17 03:39:24 +02:00
|
|
|
{
|
2005-04-17 04:18:32 +02:00
|
|
|
if(curl->flags.failonerror && curl->status>=300)
|
|
|
|
err=CURLE_HTTP_RETURNED_ERROR;
|
2005-04-17 03:39:24 +02:00
|
|
|
else
|
|
|
|
{
|
2005-04-17 04:18:32 +02:00
|
|
|
unsigned int maxlen=1024,buflen,len;
|
|
|
|
byte *line=NULL;
|
2005-04-17 03:39:24 +02:00
|
|
|
|
2005-04-17 04:18:32 +02:00
|
|
|
while((len=iobuf_read_line(curl->hd.fp_read,
|
|
|
|
&line,&buflen,&maxlen)))
|
2005-04-17 03:39:24 +02:00
|
|
|
{
|
2005-04-17 04:18:32 +02:00
|
|
|
size_t ret;
|
|
|
|
|
2005-12-06 19:49:34 +01:00
|
|
|
maxlen=1024;
|
|
|
|
|
2005-04-17 04:18:32 +02:00
|
|
|
ret=(curl->writer)(line,len,1,curl->file);
|
|
|
|
if(ret!=len)
|
|
|
|
{
|
|
|
|
err=CURLE_WRITE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2005-04-17 03:39:24 +02:00
|
|
|
}
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(line);
|
2005-04-17 04:18:32 +02:00
|
|
|
http_close(&curl->hd);
|
|
|
|
}
|
2005-02-11 19:05:13 +01:00
|
|
|
}
|
2005-04-17 04:18:32 +02:00
|
|
|
else
|
|
|
|
http_close(&curl->hd);
|
2005-02-11 19:05:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-20 23:48:28 +02:00
|
|
|
switch(rc)
|
2005-04-17 04:18:32 +02:00
|
|
|
{
|
2005-07-20 23:48:28 +02:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G10ERR_INVALID_URI:
|
|
|
|
err=CURLE_UNSUPPORTED_PROTOCOL;
|
|
|
|
break;
|
2005-04-17 04:18:32 +02:00
|
|
|
|
2005-07-20 23:48:28 +02:00
|
|
|
case G10ERR_NETWORK:
|
|
|
|
errstr=strerror(errno);
|
2005-04-17 04:18:32 +02:00
|
|
|
err=CURLE_COULDNT_CONNECT;
|
2005-07-20 23:48:28 +02:00
|
|
|
break;
|
2005-04-17 04:18:32 +02:00
|
|
|
|
2005-07-20 23:48:28 +02:00
|
|
|
default:
|
|
|
|
errstr=g10_errstr(rc);
|
|
|
|
err=CURLE_COULDNT_CONNECT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-02-11 19:05:13 +01:00
|
|
|
return handle_error(curl,err,errstr);
|
|
|
|
}
|
2005-04-17 00:21:28 +02:00
|
|
|
|
|
|
|
/* This is not the same exact set that is allowed according to
|
|
|
|
RFC-2396, but it is what the real curl uses. */
|
|
|
|
#define VALID_URI_CHARS "abcdefghijklmnopqrstuvwxyz" \
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
|
|
|
"0123456789"
|
|
|
|
|
2005-04-17 04:18:32 +02:00
|
|
|
char *
|
2007-01-16 19:12:43 +01:00
|
|
|
curl_easy_escape(CURL *curl,char *str,int length)
|
2005-04-17 00:21:28 +02:00
|
|
|
{
|
|
|
|
int len,max,idx,enc_idx=0;
|
|
|
|
char *enc;
|
|
|
|
|
|
|
|
if(length)
|
|
|
|
len=length;
|
|
|
|
else
|
|
|
|
len=strlen(str);
|
|
|
|
|
|
|
|
enc=malloc(len+1);
|
|
|
|
if(!enc)
|
|
|
|
return enc;
|
|
|
|
|
|
|
|
max=len;
|
|
|
|
|
|
|
|
for(idx=0;idx<len;idx++)
|
|
|
|
{
|
|
|
|
if(enc_idx+3>max)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
max+=100;
|
|
|
|
|
|
|
|
tmp=realloc(enc,max+1);
|
|
|
|
if(!tmp)
|
|
|
|
{
|
|
|
|
free(enc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
enc=tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strchr(VALID_URI_CHARS,str[idx]))
|
|
|
|
enc[enc_idx++]=str[idx];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char numbuf[5];
|
2006-07-12 23:16:57 +02:00
|
|
|
sprintf(numbuf,"%%%02X",(unsigned char)str[idx]);
|
2005-04-17 00:21:28 +02:00
|
|
|
strcpy(&enc[enc_idx],numbuf);
|
|
|
|
enc_idx+=3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enc[enc_idx]='\0';
|
|
|
|
|
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
2007-03-13 18:53:57 +01:00
|
|
|
curl_version_info_data *
|
|
|
|
curl_version_info(int type)
|
2005-04-17 00:21:28 +02:00
|
|
|
{
|
2007-03-13 18:53:57 +01:00
|
|
|
static curl_version_info_data data;
|
|
|
|
static const char *protocols[]={"http",NULL};
|
|
|
|
|
|
|
|
data.protocols=protocols;
|
|
|
|
|
|
|
|
return &data;
|
2005-04-17 00:21:28 +02:00
|
|
|
}
|