[GB.NET.CURL]

* NEW: Support for the https protocol in the HTTP client.


git-svn-id: svn://localhost/gambas/trunk@3868 867c0c6c-44f3-4631-809d-bfa615b0a4ec
This commit is contained in:
Benoît Minisini 2011-06-02 16:09:04 +00:00
parent b921eb888f
commit 9d69842038
6 changed files with 77 additions and 48 deletions

View file

@ -417,33 +417,56 @@ END_PROPERTY
URL to work with
*****************************************************************/
BEGIN_PROPERTY(CCURL_URL)
BEGIN_PROPERTY ( CCURL_URL )
char *tmp=NULL;
char *url, *tmp;
char *protocol;
if (READ_PROPERTY)
{
GB.ReturnNewZeroString(THIS_URL);
GB.ReturnString(THIS_URL);
return;
}
if (THIS_STATUS > 0)
{
GB.Error ("URL property is read-only while working");
GB.Error ("URL is read-only while working");
return;
}
if (THIS_URL)
{
tmp=THIS_URL;
GB.Free(POINTER(&tmp));
}
GB.Alloc(POINTER(&tmp),(strlen(GB.ToZeroString(PROP(GB_STRING)))+1)*sizeof(char));
strcpy(tmp,GB.ToZeroString(PROP(GB_STRING)));
Adv_correct_url(&tmp,THIS_PROTOCOL);
THIS_URL=tmp;
url = GB.NewString(PSTRING(), PLENGTH());
if (GB.Is(THIS, GB.FindClass("FtpClient")))
{
protocol = CURL_get_protocol(url, "ftp://");
if (strcmp(protocol, "ftp://"))
goto UNKNOWN_PROTOCOL;
}
else if (GB.Is(THIS, GB.FindClass("HttpClient")))
{
protocol = CURL_get_protocol(url, "http://");
if (strcmp(protocol, "http://") && strcmp(protocol, "https://"))
goto UNKNOWN_PROTOCOL;
}
else
goto UNKNOWN_PROTOCOL;
if (strncmp(url, protocol, strlen(protocol)))
{
tmp = GB.NewZeroString(protocol);
GB.AddString(&tmp, url, GB.StringLength(url));
GB.FreeString(&url);
url = tmp;
}
GB.FreeString(&THIS_URL);
THIS_URL = url;
return;
UNKNOWN_PROTOCOL:
GB.Error("Unknown protocol");
END_PROPERTY
BEGIN_METHOD_VOID(CCURL_new)
@ -457,15 +480,16 @@ BEGIN_METHOD_VOID(CCURL_new)
GB.Alloc(POINTER(&data),sizeof(curlData));
((void**)THIS->stream._free)[0]=(void*)data;*/
THIS->stream.desc=NULL;
THIS_CURL=NULL;
THIS_URL=NULL;
THIS_FILE=NULL;
THIS_PROTOCOL=NULL;
THIS_STATUS=0;
Adv_user_NEW (&THIS->user);
THIS->stream.desc = NULL;
THIS_CURL = NULL;
THIS_URL = NULL;
THIS_FILE = NULL;
THIS_STATUS = 0;
Adv_user_NEW(&THIS->user);
Adv_proxy_NEW(&THIS->proxy.proxy);
THIS->proxy.parent_status=(int*)&THIS_STATUS;
THIS->proxy.parent_status = (int*)&THIS_STATUS;
END_METHOD
@ -475,9 +499,8 @@ BEGIN_METHOD_VOID(CCURL_free)
fprintf(stderr, "CCURL_free: %p\n", THIS);
#endif
char *tmp=THIS_URL;
GB.FreeString(&THIS_URL);
if (tmp) GB.Free(POINTER(&tmp));
if (THIS_FILE) fclose(THIS_FILE);
if (THIS_CURL)
{
@ -488,8 +511,6 @@ BEGIN_METHOD_VOID(CCURL_free)
}
Adv_user_CLEAR (&THIS->user);
Adv_proxy_CLEAR(&THIS->proxy.proxy);
tmp=THIS_PROTOCOL;
GB.Free(POINTER(&tmp));
END_METHOD
@ -550,7 +571,6 @@ GB_DESC CCurlDesc[] =
GB_PROPERTY("URL", "s",CCURL_URL),
GB_PROPERTY("User","s",CCURL_sUser),
GB_PROPERTY("Password","s",CCURL_Password),
//GB_PROPERTY("Tag", "v", CCURL_tag),
GB_PROPERTY("Async","b",CCURL_Async),
GB_PROPERTY("Timeout","i",CCURL_TimeOut),
GB_PROPERTY_SELF("Proxy",".CurlProxy"),

View file

@ -43,7 +43,6 @@ extern GB_STREAM_DESC CurlStream;
#define THIS_CURL THIS->curl
#define THIS_URL THIS->url
#define THIS_FILE THIS->file
#define THIS_PROTOCOL THIS->protocol
typedef
struct {
@ -56,11 +55,10 @@ typedef
struct {
GB_BASE ob;
GB_STREAM stream;
int status;
CURL* curl;
char* url;
FILE* file;
char* protocol;
int status;
CURL *curl;
char *url;
FILE *file;
CPROXY proxy;
Adv_user user;
bool async;

View file

@ -267,16 +267,14 @@ END_METHOD
BEGIN_METHOD_VOID(FtpClient_new)
char *tmp=NULL;
char *tmp;
GB.Alloc((void**)POINTER(&tmp),sizeof(char)*(1+strlen("ftp://127.0.0.1:21")));
THIS_URL=tmp;
strcpy(tmp,"ftp://127.0.0.1:21");
tmp=NULL;
GB.Alloc((void**)POINTER(&tmp),7);
strcpy(tmp,"ftp://");
THIS_PROTOCOL=tmp;
Adv_user_SETAUTH (&THIS->user,CURLAUTH_BASIC);
Adv_user_SETAUTH (&THIS->user, CURLAUTH_BASIC);
THIS->user.user = GB.NewZeroString("anonymous");
END_METHOD

View file

@ -508,13 +508,6 @@ BEGIN_METHOD_VOID(HttpClient_new)
THIS_URL=tmp;
THIS_HTTP->sUserAgent = GB.NewZeroString("Gambas Http/1.0");
tmp=NULL;
GB.Alloc((void**)POINTER(&tmp),8);
strcpy(tmp,"http://");
THIS_PROTOCOL=tmp;
//GB.Array.New(&THIS->headers, GB_T_STRING, 0);
END_METHOD
@ -571,7 +564,7 @@ GB_DESC CHttpClientDesc[] =
GB_METHOD("Stop", NULL, HttpClient_Stop, NULL),
GB_METHOD("Get", NULL, HttpClient_Get, "[(Headers)String[];(TargetFile)s]"),
GB_METHOD("Post", NULL, HttpClient_Post, "(ContentType)s(Data)s[(Headers)String[];(TargetFile)s]"),
GB_METHOD("Put", NULL, HttpClient_Post, "(ContentType)s(Data)s[(Headers)String[];(TargetFile)s]"),
GB_METHOD("Put", NULL, HttpClient_Put, "(ContentType)s(Data)s[(Headers)String[];(TargetFile)s]"),
GB_PROPERTY("Auth", "i", HttpClient_Auth),
GB_PROPERTY("CookiesFile", "s",HttpClient_CookiesFile),

View file

@ -19,6 +19,7 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
***************************************************************************/
/*****************************
NOTE THAT :
libcurl <= 7.10.7 lacks CURLE_LDAP_INVALID_URL and CURLE_FILESIZE_EXCEEDED constants
@ -30,6 +31,7 @@
#include <curl/curl.h>
#include "gbcurl.h"
static char *_protocols[] = { "ftp://", "http://", "https://", NULL };
#ifndef CURLAUTH_NONE
void Adv_WarningAuth(void)
@ -52,6 +54,23 @@ void Adv_WarningProxyAuth(void)
}
char *CURL_get_protocol(char *url, char *default_protocol)
{
char **p;
char *pos;
for (p = _protocols; *p; p++)
{
if (!strncmp(url, *p, strlen(*p)))
return *p;
}
pos = strstr(url, "://");
if (pos)
return "?";
return default_protocol;
}
void Adv_correct_url(char **buf,char *protocol)
{

View file

@ -46,6 +46,7 @@ typedef struct
int auth;
} Adv_user;
char *CURL_get_protocol(char *url, char *default_protocol);
void Adv_correct_url(char **buf,char *protocol);
void Adv_add_info (Adv_ARRAY *Array, int *narray, void *Obj);