-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathlcurlapi.c
218 lines (168 loc) · 4.86 KB
/
lcurlapi.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/******************************************************************************
* Author: Alexey Melnichuk <alexeymelnichuck@gmail.com>
*
* Copyright (C) 2018 Alexey Melnichuk <alexeymelnichuck@gmail.com>
*
* Licensed according to the included 'LICENSE' document
*
* This file is part of Lua-cURL library.
******************************************************************************/
#include "lcurlapi.h"
#include "lcurl.h"
#include "lcerror.h"
#include "lcutils.h"
#include <memory.h>
#define LCURL_URL_NAME LCURL_PREFIX" URL"
static const char *LCURL_URL = LCURL_URL_NAME;
#if LCURL_CURL_VER_GE(7,62,0)
#define lcurl_geturl(L) lcurl_geturl_at(L, 1)
int lcurl_url_create(lua_State *L, int error_mode){
lcurl_url_t *p;
p = lutil_newudatap(L, lcurl_url_t, LCURL_URL);
p->url = curl_url();
if(!p->url) return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_URL, CURLUE_OUT_OF_MEMORY);
p->err_mode = error_mode;
if (lua_gettop(L) > 1) {
const char *url = luaL_checkstring(L, 1);
unsigned int flags = 0;
CURLUcode code;
if (lua_gettop(L) > 2) {
flags = (unsigned int)lutil_optint64(L, 2, 0);
}
code = curl_url_set(p->url, CURLUPART_URL, url, flags);
if (code != CURLUE_OK) {
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_URL, code);
}
}
return 1;
}
lcurl_url_t *lcurl_geturl_at(lua_State *L, int i){
lcurl_url_t *p = (lcurl_url_t *)lutil_checkudatap (L, i, LCURL_URL);
luaL_argcheck (L, p != NULL, 1, LCURL_URL_NAME" object expected");
return p;
}
static int lcurl_url_cleanup(lua_State *L){
lcurl_url_t *p = lcurl_geturl(L);
if (p->url){
curl_url_cleanup(p->url);
p->url = NULL;
}
return 0;
}
static int lcurl_url_dup(lua_State *L) {
lcurl_url_t *r = lcurl_geturl(L);
lcurl_url_t *p = lutil_newudatap(L, lcurl_url_t, LCURL_URL);
p->url = curl_url_dup(r->url);
if (!p->url) return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_URL, CURLUE_OUT_OF_MEMORY);
p->err_mode = r->err_mode;
return 1;
}
static int lcurl_url_set(lua_State *L, CURLUPart what){
lcurl_url_t *p = lcurl_geturl(L);
CURLUcode code;
const char *part;
unsigned int flags = 0;
luaL_argcheck(L, lua_type(L, 2) == LUA_TSTRING || lutil_is_null(L, 2), 2, "string expected");
part = lua_tostring(L, 2);
flags = (unsigned int)lutil_optint64(L, 3, 0);
code = curl_url_set(p->url, what, part, flags);
if (code != CURLUE_OK) {
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_URL, code);
}
lua_settop(L, 1);
return 1;
}
static int lcurl_url_get(lua_State *L, CURLUPart what, CURLUcode empty) {
lcurl_url_t *p = lcurl_geturl(L);
CURLUcode code;
char *part = NULL;
unsigned int flags = 0;
flags = (unsigned int)lutil_optint64(L, 2, 0);
code = curl_url_get(p->url, what, &part, flags);
if (code != CURLUE_OK) {
if (part) {
curl_free(part);
part = NULL;
}
if (code != empty) {
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_URL, code);
}
}
if (part == NULL) {
lutil_push_null(L);
}
else {
lua_pushstring(L, part);
curl_free(part);
}
return 1;
}
static int lcurl_url_to_s(lua_State *L) {
lcurl_url_t *p = lcurl_geturl(L);
char *part = NULL;
CURLUcode code = curl_url_get(p->url, CURLUPART_URL, &part, 0);
if (code != CURLUE_OK) {
if (part) {
curl_free(part);
}
return lcurl_fail_ex(L, LCURL_ERROR_RAISE, LCURL_ERROR_URL, code);
}
if (part == NULL) {
lua_pushliteral(L, "");
}
else {
lua_pushstring(L, part);
curl_free(part);
}
return 1;
}
#define ENTRY_PART(N, S, E) static int lcurl_url_set_##N(lua_State *L){\
return lcurl_url_set(L, CURL##S);\
}
#define ENTRY_FLAG(S)
#include "lcopturl.h"
#undef ENTRY_PART
#undef ENTRY_FLAG
#define ENTRY_PART(N, S, E) static int lcurl_url_get_##N(lua_State *L){\
return lcurl_url_get(L, CURL##S, E);\
}
#define ENTRY_FLAG(S)
#include "lcopturl.h"
#undef ENTRY_PART
#undef ENTRY_FLAG
static const struct luaL_Reg lcurl_url_methods[] = {
#define ENTRY_PART(N, S, E) { "set_"#N, lcurl_url_set_##N },
#define ENTRY_FLAG(S)
#include "lcopturl.h"
#undef ENTRY_PART
#undef ENTRY_FLAG
#define ENTRY_PART(N, S, E) { "get_"#N, lcurl_url_get_##N },
#define ENTRY_FLAG(S)
#include "lcopturl.h"
#undef ENTRY_PART
#undef ENTRY_FLAG
{ "dup", lcurl_url_dup },
{ "cleanup", lcurl_url_cleanup },
{ "__gc", lcurl_url_cleanup },
{ "__tostring", lcurl_url_to_s },
{ NULL,NULL }
};
static const lcurl_const_t lcurl_url_opt[] = {
#define ENTRY_PART(N, S, E) { #S, CURL##S },
#define ENTRY_FLAG(S) { "U_"#S, CURLU_##S },
#include "lcopturl.h"
#undef ENTRY_PART
#undef ENTRY_FLAG
{NULL, 0}
};
#endif
void lcurl_url_initlib(lua_State *L, int nup){
#if LCURL_CURL_VER_GE(7,62,0)
if(!lutil_createmetap(L, LCURL_URL, lcurl_url_methods, nup))
lua_pop(L, nup);
lua_pop(L, 1);
lcurl_util_set_const(L, lcurl_url_opt);
#else
lua_pop(L, nup);
#endif
}