aboutsummaryrefslogtreecommitdiff
path: root/extension/extension.cpp
blob: fa82cf17600ce0ff3b40a307d962666f041338df (plain)
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
162
163
164
165
166
167
168
169
170
171
172
#include "extension.h"
#include "curlmanager.h"
#include "opensslmanager.h"
#include <curl/curl.h>

cURL_SM g_cURL_SM;

SMEXT_LINK(&g_cURL_SM);

extern sp_nativeinfo_t g_cURLNatives[];

HandleType_t g_cURLHandle = 0;
HandleType_t g_cURLFile = 0;
HandleType_t g_WebForm = 0;
HandleType_t g_cURLSlist = 0;

IdentityToken_t *myself_Identity = NULL;


bool cURL_SM::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
	shutdown = false;

	CURLcode code;

	code = curl_global_init(CURL_GLOBAL_ALL);

	if(code)
	{
		smutils->Format(error, maxlength, "%s", curl_easy_strerror(code));
		return false;
	}

	myself_Identity = myself->GetIdentity();

	bool valid = true;

	HandleError err_file, err_handle, err_webform, err_slist;
	g_cURLFile = handlesys->CreateType("cURLFile", this, 0, NULL, NULL, myself_Identity, &err_file);
	g_cURLHandle = handlesys->CreateType("cURLHandle", this, 0, NULL, NULL, myself_Identity, &err_handle);
	g_WebForm = handlesys->CreateType("cURLWebForm", this, 0, NULL, NULL, myself_Identity, &err_webform);
	g_cURLSlist = handlesys->CreateType("cURLSlist", this, 0, NULL, NULL, myself_Identity, &err_slist);
	
	if(g_cURLFile == 0)
	{
		handlesys->RemoveType(g_cURLFile, myself_Identity);
		g_cURLFile = 0;
		snprintf(error, maxlength, "Could not create CURL file type (err: %d)", err_file);
		valid = false;
	}

	if(g_cURLHandle == 0)
	{
		handlesys->RemoveType(g_cURLHandle, myself_Identity);
		g_cURLHandle = 0;
		snprintf(error, maxlength, "Could not create CURL handle type (err: %d)", err_handle);
		valid = false;
	}
	
	if(g_WebForm == 0)
	{
		handlesys->RemoveType(g_WebForm, myself_Identity);
		g_WebForm = 0;
		snprintf(error, maxlength, "Could not create CURL WebForm type (err: %d)", err_webform);
		valid = false;
	}

	if(g_cURLSlist == 0)
	{
		handlesys->RemoveType(g_cURLSlist, myself_Identity);
		g_cURLSlist = 0;
		snprintf(error, maxlength, "Could not create CURL Slist type (err: %d)", err_slist);
		valid = false;
	}

	if(!valid)
		return false;

	sharesys->AddNatives(myself, g_cURLNatives);

	g_cURLManager.SDK_OnLoad();

	g_OpensslManager.SDK_OnLoad();

	return true;
}

void cURL_SM::SDK_OnUnload()
{
	shutdown = true;

	g_cURLManager.SDK_OnUnload();

	g_OpensslManager.SDK_OnUnload();

	curl_global_cleanup();
}

void cURL_SM::SDK_OnAllLoaded()
{
}

bool cURL_SM::QueryRunning(char *error, size_t maxlength)
{
	return true;
}

void cURL_SM::OnHandleDestroy(HandleType_t type, void *object)
{
	if(type == g_cURLHandle)
	{
		g_cURLManager.RemovecURLHandle((cURLHandle *)object);
	} else if(type == g_cURLFile || type == g_WebForm || type == g_cURLSlist) {
		ICloseHelper *pointer = (ICloseHelper *)object;
		if(pointer->TryDelete())
		{
			g_cURLManager.RemoveCloseHelperHandle(pointer);
			pointer->Delete();
		}
	}
}

bool cURL_SM::IsShutdown()
{
	return shutdown;
}


bool ICloseHelper::TryDelete()
{
	if(_handle == NULL || !_handle->running)
	{
		return true;
	} else {
		_marked_delete = true;
		return false;
	}
}

void cURL_OpenFile::Delete()
{
	fclose(pFile);
	delete this;
}

void WebForm::Delete()
{
	curl_formfree(first);
	slist_record.clear();
	delete this;
}

void cURL_slist_pack::Delete()
{
	curl_slist_free_all(chunk); 
	delete this;
}

char *UTIL_ToLowerCase(const char *str)
{
	size_t len = strlen(str);
	char *buffer = new char[len + 1];
	for (size_t i = 0; i < len; i++)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
			buffer[i] = tolower(str[i]);
		else
			buffer[i] = str[i];
	}
	buffer[len] = '\0';
	return buffer;
}