HttpClient.Download() is a new static method that download an HTTP url and return the result as a string.
[GB.NET.CURL] * NEW: HttpClient.Download() is a new static method that download an HTTP url and return the result as a string.
This commit is contained in:
parent
fbbc82b012
commit
d9996b4354
8 changed files with 72 additions and 46 deletions
|
@ -569,9 +569,6 @@ END_PROPERTY
|
|||
|
||||
BEGIN_PROPERTY(Curl_URL)
|
||||
|
||||
char *url, *tmp;
|
||||
char *protocol;
|
||||
|
||||
if (READ_PROPERTY)
|
||||
{
|
||||
GB.ReturnString(THIS_URL);
|
||||
|
@ -581,41 +578,7 @@ BEGIN_PROPERTY(Curl_URL)
|
|||
if (CURL_check_active(THIS))
|
||||
return;
|
||||
|
||||
if (PLENGTH() == 0)
|
||||
goto UNKNOWN_PROTOCOL;
|
||||
|
||||
url = GB.NewString(PSTRING(), PLENGTH());
|
||||
|
||||
if (GB.Is(THIS, GB.FindClass("FtpClient")))
|
||||
{
|
||||
protocol = CURL_get_protocol(url, "ftp://");
|
||||
if (strcmp(protocol, "ftp://") && strcmp(protocol, "ftps://"))
|
||||
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);
|
||||
tmp = 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");
|
||||
CURL_set_url(THIS, PSTRING(), PLENGTH());
|
||||
|
||||
END_PROPERTY
|
||||
|
||||
|
|
|
@ -293,7 +293,7 @@ static void http_get(void *_object, GB_ARRAY custom_headers, char *target, CURLo
|
|||
}
|
||||
}
|
||||
|
||||
THIS->method=0;
|
||||
THIS->method = 0;
|
||||
|
||||
http_initialize_curl_handle(_object, custom_headers);
|
||||
|
||||
|
@ -645,7 +645,22 @@ BEGIN_PROPERTY(HttpClient_TargetFile)
|
|||
END_PROPERTY
|
||||
|
||||
|
||||
GB_DESC CHttpClientDesc[] =
|
||||
BEGIN_METHOD(HttpClient_Download, GB_STRING url; GB_OBJECT headers)
|
||||
|
||||
_object = GB.New(GB.FindClass("HttpClient"), NULL, NULL);
|
||||
|
||||
GB.Ref(THIS);
|
||||
THIS->async = FALSE;
|
||||
if (CURL_set_url(THIS, STRING(url), LENGTH(url)))
|
||||
return;
|
||||
|
||||
http_get(THIS, VARGOPT(headers, NULL), NULL, CURLOPT_HTTPGET);
|
||||
GB.ReturnString(THIS->data);
|
||||
GB.Unref(POINTER(&_object));
|
||||
|
||||
END_METHOD
|
||||
|
||||
GB_DESC HttpClientDesc[] =
|
||||
{
|
||||
GB_DECLARE("HttpClient", sizeof(CHTTPCLIENT)),
|
||||
|
||||
|
@ -674,6 +689,8 @@ GB_DESC CHttpClientDesc[] =
|
|||
|
||||
GB_METHOD("CopyFrom", NULL, HttpClient_CopyFrom, "(HttpClient)Source"),
|
||||
|
||||
GB_STATIC_METHOD("Download", "s", HttpClient_Download, "(URL)s[(Headers)String[];]"),
|
||||
|
||||
GB_CONSTANT("_IsControl", "b", TRUE),
|
||||
GB_CONSTANT("_IsVirtual", "b", TRUE),
|
||||
GB_CONSTANT("_Group", "s", "Network"),
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
#ifndef __CHTTPCLIENT_C
|
||||
|
||||
|
||||
extern GB_DESC CHttpClientDesc[];
|
||||
extern GB_DESC HttpClientDesc[];
|
||||
extern GB_STREAM_DESC HttpStream;
|
||||
|
||||
#else
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# Gambas Project File 3.0
|
||||
# Compiled with Gambas 3.8.90
|
||||
# Compiled with Gambas 3.10.90
|
||||
Title=gb.net.curl
|
||||
Startup=MMain
|
||||
Version=3.8.90
|
||||
Version=3.10.90
|
||||
VersionFile=1
|
||||
Component=gb.net
|
||||
Component=gb.net.curl
|
||||
|
|
|
@ -2,4 +2,6 @@
|
|||
|
||||
Public Sub Main()
|
||||
|
||||
Print HttpClient.Download("https://www.w3schools.com/cssref/")
|
||||
|
||||
End
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
#include <string.h>
|
||||
#include <curl/curl.h>
|
||||
#include "gbcurl.h"
|
||||
#include "CCurl.h"
|
||||
|
||||
static char *_protocols[] = { "ftp://", "http://", "https://", NULL };
|
||||
|
||||
|
@ -60,7 +61,7 @@ static void warning_proxy_auth(void)
|
|||
}
|
||||
|
||||
|
||||
char *CURL_get_protocol(char *url, char *default_protocol)
|
||||
static char *CURL_get_protocol(char *url, char *default_protocol)
|
||||
{
|
||||
char **p;
|
||||
char *pos;
|
||||
|
@ -78,6 +79,48 @@ char *CURL_get_protocol(char *url, char *default_protocol)
|
|||
return default_protocol;
|
||||
}
|
||||
|
||||
bool CURL_set_url(void *_object, const char *src, int len)
|
||||
{
|
||||
char *url, *tmp, *protocol;
|
||||
|
||||
if (len == 0)
|
||||
goto UNKNOWN_PROTOCOL;
|
||||
|
||||
url = GB.NewString(src, len);
|
||||
|
||||
if (GB.Is(THIS, GB.FindClass("FtpClient")))
|
||||
{
|
||||
protocol = CURL_get_protocol(url, "ftp://");
|
||||
if (strcmp(protocol, "ftp://") && strcmp(protocol, "ftps://"))
|
||||
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);
|
||||
tmp = GB.AddString(tmp, url, GB.StringLength(url));
|
||||
GB.FreeString(&url);
|
||||
url = tmp;
|
||||
}
|
||||
|
||||
GB.FreeString(&THIS_URL);
|
||||
THIS_URL = url;
|
||||
return FALSE;
|
||||
|
||||
UNKNOWN_PROTOCOL:
|
||||
|
||||
GB.Error("Unknown protocol");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
#if 0
|
||||
void Adv_correct_url(char **buf,char *protocol)
|
||||
{
|
||||
|
|
|
@ -49,7 +49,8 @@ typedef
|
|||
}
|
||||
CURL_USER;
|
||||
|
||||
char *CURL_get_protocol(char *url, char *default_protocol);
|
||||
//char *CURL_get_protocol(char *url, char *default_protocol);
|
||||
bool CURL_set_url(void *_object, const char *src, int len);
|
||||
|
||||
bool CURL_check_userpwd(CURL_USER *user);
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ GB_DESC *GB_CLASSES[] EXPORT =
|
|||
CProxyDesc,
|
||||
CurlSSLDesc,
|
||||
CurlDesc,
|
||||
CHttpClientDesc,
|
||||
HttpClientDesc,
|
||||
CFtpClientDesc,
|
||||
NULL
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue