-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCURL4D.c
161 lines (121 loc) · 3.56 KB
/
CURL4D.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* --------------------------------------------------------------------------------
#
# 4DPlugin.c
# source generated by 4D Plugin Wizard
# Project : CURL
# author : jelle
# 06/09/09
#
# --------------------------------------------------------------------------------*/
#include "4DPluginAPI.h"
#include "CURL4D.h"
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
CURL *curlHandle = NULL;
struct MemoryStruct {
Byte *data;
size_t size;
};
typedef struct MemoryStruct MemoryBuffer;
void PluginMain( long selector, PA_PluginParameters params )
{
switch( selector )
{
case kInitPlugin :
InitPlugin();
break;
case kDeinitPlugin :
DeinitPlugin();
break;
// --- CURL
case eCMD_Get_URL_Blob :
Get_URL_Blob( params );
break;
case eCMD_Get_URL_Text :
Get_URL_Text( params );
break;
}
}
void InitPlugin()
{
curlHandle = curl_easy_init();
}
void DeinitPlugin()
{
curl_easy_cleanup(curlHandle);
curl_global_cleanup();
}
// ------------------------------------- CURL -------------------------------------
size_t writeToBuffer(void* data, size_t size, size_t nmemb, void* buffer){
size_t realsize = size * nmemb;
MemoryBuffer *mem = (MemoryBuffer *)buffer;
mem->data = realloc(mem->data, mem->size + realsize);
if (mem->data) {
memcpy(&(mem->data[mem->size]), data, realsize);
mem->size += realsize;
}
return realsize;
}
char* PA_UnistringToChar(PA_Unistring* src){
CFStringRef cfstr = CFStringCreateWithBytes(kCFAllocatorDefault, src->fString, (src->fLength)*2, kCFStringEncodingUTF16, false);
int size = CFStringGetLength(cfstr)+1;
char *dst = malloc(size);
CFStringGetCString(cfstr, dst, size, kCFStringEncodingUTF8);
return dst;
}
PA_Unichar* charToPA_Unichar(char* src){
CFStringRef cfstr = CFStringCreateWithCString(kCFAllocatorDefault, src, kCFStringEncodingUTF8);
PA_Unichar *dst = malloc(CFStringGetLength(cfstr));
CFIndex index;
CFStringGetBytes(cfstr, CFRangeMake(0, CFStringGetLength(cfstr)), kCFStringEncodingUTF16, 0, false, dst, CFStringGetLength(cfstr), &index);
return dst;
}
MemoryBuffer getURL(char* url){
CURLcode err = 0;
MemoryBuffer buffer;
buffer.data = NULL;
buffer.size = 0;
err = curl_easy_setopt(curlHandle, CURLOPT_URL, url);
if (err==0) {
err = curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, &writeToBuffer);
}
if (err==0) {
err = curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, (void*)&buffer);
}
if (err==0) {
err = curl_easy_perform(curlHandle);
}
return buffer;
}
void Get_URL_Blob( PA_PluginParameters params )
{
char* c_url;
PA_Unistring* URL;
PA_Handle returnValue;
MemoryBuffer buffer;
URL = PA_GetStringParameter( params, 1 );
c_url = PA_UnistringToChar(URL);
buffer = getURL(c_url);
returnValue = PA_NewHandle(buffer.size);
memcpy(*returnValue,buffer.data, buffer.size);
PA_ReturnBlobHandle( params, returnValue );
}
void Get_URL_Text( PA_PluginParameters params )
{
char* c_url;
char *returnString;
PA_Unichar* URL_uchars;
PA_Unistring* URL;
PA_Unichar* returnValue_uchars;
MemoryBuffer buffer;
URL = PA_GetStringParameter( params, 1 );
URL_uchars = PA_GetUnistring( URL );
c_url = PA_UnistringToChar(URL);
buffer = getURL(c_url);
returnString = malloc(buffer.size + 1);
bzero(returnString, buffer.size + 1);
memcpy(returnString, buffer.data, buffer.size);
returnValue_uchars = charToPA_Unichar(returnString);
PA_ReturnString( params, returnValue_uchars );
}