Skip to content

Commit 3757bc5

Browse files
author
Alexey Botchkov
committed
MDEV-8431 Feedback plugin needs an option for http proxy.
'feedback_http_proxy' system variable added to specify the proxy server as host:port. Not a dynamic one.
1 parent 16ad1fc commit 3757bc5

File tree

4 files changed

+101
-13
lines changed

4 files changed

+101
-13
lines changed

plugin/feedback/feedback.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace feedback {
2525
char server_uid_buf[SERVER_UID_SIZE+1]; ///< server uid will be written here
2626

2727
/* backing store for system variables */
28-
static char *server_uid= server_uid_buf, *url;
28+
static char *server_uid= server_uid_buf, *url, *http_proxy;
2929
char *user_info;
3030
ulong send_timeout, send_retry_wait;
3131

@@ -272,7 +272,13 @@ static int init(void *p)
272272
if (*e == 0 || *e == ' ')
273273
{
274274
if (e > s && (urls[slot]= Url::create(s, e - s)))
275+
{
276+
if (urls[slot]->set_proxy(http_proxy,
277+
http_proxy ? strlen(http_proxy) : 0))
278+
sql_print_error("feedback plugin: invalid proxy '%s'",
279+
http_proxy ? http_proxy : "");
275280
slot++;
281+
}
276282
else
277283
{
278284
if (e > s)
@@ -350,13 +356,18 @@ static MYSQL_SYSVAR_ULONG(send_timeout, send_timeout, PLUGIN_VAR_RQCMDARG,
350356
static MYSQL_SYSVAR_ULONG(send_retry_wait, send_retry_wait, PLUGIN_VAR_RQCMDARG,
351357
"Wait this many seconds before retrying a failed send.",
352358
NULL, NULL, 60, 1, 60*60*24, 1);
359+
static MYSQL_SYSVAR_STR(http_proxy, http_proxy,
360+
PLUGIN_VAR_READONLY | PLUGIN_VAR_RQCMDARG,
361+
"Proxy server host:port.", NULL, NULL,0);
362+
353363

354364
static struct st_mysql_sys_var* settings[] = {
355365
MYSQL_SYSVAR(server_uid),
356366
MYSQL_SYSVAR(user_info),
357367
MYSQL_SYSVAR(url),
358368
MYSQL_SYSVAR(send_timeout),
359369
MYSQL_SYSVAR(send_retry_wait),
370+
MYSQL_SYSVAR(http_proxy),
360371
NULL
361372
};
362373

plugin/feedback/feedback.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ class Url {
5252
const char *url() { return full_url.str; }
5353
size_t url_length() { return full_url.length; }
5454
virtual int send(const char* data, size_t data_length) = 0;
55+
virtual int set_proxy(const char *proxy, size_t proxy_len)
56+
{
57+
return 0;
58+
}
5559

5660
static Url* create(const char *url, size_t url_length);
61+
static int parse_proxy_server(const char *proxy_server, size_t proxy_length,
62+
LEX_STRING *host, LEX_STRING *port);
5763
};
5864

5965
extern Url **urls;

plugin/feedback/url_base.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,50 @@ Url* Url::create(const char *url, size_t url_length)
4848
return self;
4949
}
5050

51+
52+
int Url::parse_proxy_server(const char *proxy_server, size_t proxy_length,
53+
LEX_STRING *host, LEX_STRING *port)
54+
{
55+
const char *s;
56+
57+
host->length= 0;
58+
if (proxy_server == NULL)
59+
return 0;
60+
61+
for (; proxy_length > 0 && isspace(*proxy_server);
62+
proxy_server++, proxy_length--) /* no-op */;
63+
64+
if (proxy_length == 0)
65+
return 0;
66+
67+
for (s=proxy_server; *s && *s != ':'; s++) /* no-op */;
68+
69+
host->str= const_cast<char*>(proxy_server);
70+
if ((host->length= s-proxy_server) == 0)
71+
return 0;
72+
73+
port->length= 0;
74+
75+
if (*s == ':')
76+
{
77+
s++;
78+
port->str= const_cast<char*>(s);
79+
while (*s >= '0' && *s <= '9')
80+
{
81+
s++;
82+
port->length++;
83+
}
84+
}
85+
86+
if (port->length == 0)
87+
{
88+
port->str= const_cast<char*>("80");
89+
port->length= 2;
90+
}
91+
92+
host->str= my_strndup(host->str, host->length, MYF(MY_WME));
93+
port->str= my_strndup(port->str, port->length, MYF(MY_WME));
94+
return 0;
95+
}
96+
5197
} // namespace feedback

plugin/feedback/url_http.cc

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,39 @@ class Url_http: public Url {
3838
protected:
3939
const LEX_STRING host, port, path;
4040
bool ssl;
41+
LEX_STRING proxy_host, proxy_port;
42+
43+
int use_proxy()
44+
{
45+
return proxy_host.length;
46+
}
4147

4248
Url_http(LEX_STRING &url_arg, LEX_STRING &host_arg,
4349
LEX_STRING &port_arg, LEX_STRING &path_arg, bool ssl_arg) :
4450
Url(url_arg), host(host_arg), port(port_arg), path(path_arg), ssl(ssl_arg)
45-
{}
51+
{
52+
proxy_host.length= 0;
53+
}
4654
~Url_http()
4755
{
4856
my_free(host.str);
4957
my_free(port.str);
5058
my_free(path.str);
59+
set_proxy(0,0);
5160
}
5261

5362
public:
5463
int send(const char* data, size_t data_length);
64+
int set_proxy(const char *proxy, size_t proxy_len)
65+
{
66+
if (use_proxy())
67+
{
68+
my_free(proxy_host.str);
69+
my_free(proxy_port.str);
70+
}
71+
72+
return parse_proxy_server(proxy, proxy_len, &proxy_host, &proxy_port);
73+
}
5574

5675
friend Url* http_create(const char *url, size_t url_length);
5776
};
@@ -150,7 +169,9 @@ int Url_http::send(const char* data, size_t data_length)
150169
uint len= 0;
151170

152171
addrinfo *addrs, *addr, filter= {0, AF_UNSPEC, SOCK_STREAM, 6, 0, 0, 0, 0};
153-
int res= getaddrinfo(host.str, port.str, &filter, &addrs);
172+
int res= use_proxy() ?
173+
getaddrinfo(proxy_host.str, proxy_port.str, &filter, &addrs) :
174+
getaddrinfo(host.str, port.str, &filter, &addrs);
154175

155176
if (res)
156177
{
@@ -228,16 +249,20 @@ int Url_http::send(const char* data, size_t data_length)
228249
};
229250

230251
len= my_snprintf(buf, sizeof(buf),
231-
"POST %s HTTP/1.0\r\n"
232-
"User-Agent: MariaDB User Feedback Plugin\r\n"
233-
"Host: %s:%s\r\n"
234-
"Accept: */*\r\n"
235-
"Content-Length: %u\r\n"
236-
"Content-Type: multipart/form-data; boundary=%s\r\n"
237-
"\r\n",
238-
path.str, host.str, port.str,
239-
(uint)(2*boundary.length + header.length + data_length + 4),
240-
boundary.str + 2);
252+
use_proxy() ? "POST http://%s:%s/" : "POST ",
253+
host.str, port.str);
254+
255+
len+= my_snprintf(buf+len, sizeof(buf)-len,
256+
"%s HTTP/1.0\r\n"
257+
"User-Agent: MariaDB User Feedback Plugin\r\n"
258+
"Host: %s:%s\r\n"
259+
"Accept: */*\r\n"
260+
"Content-Length: %u\r\n"
261+
"Content-Type: multipart/form-data; boundary=%s\r\n"
262+
"\r\n",
263+
path.str, host.str, port.str,
264+
(uint)(2*boundary.length + header.length + data_length + 4),
265+
boundary.str + 2);
241266

242267
vio_timeout(vio, FOR_READING, send_timeout);
243268
vio_timeout(vio, FOR_WRITING, send_timeout);

0 commit comments

Comments
 (0)