This commit is contained in:
Valentín Blanco 2017-12-26 23:32:56 +00:00 committed by GitHub
commit 74571fcd57
4 changed files with 48 additions and 16 deletions

View File

@ -254,8 +254,8 @@ struct {
BEGIN "(no" WS "upstream)" WS STR END, handle_upstream_no, NULL BEGIN "(no" WS "upstream)" WS STR END, handle_upstream_no, NULL
}, },
{ {
BEGIN "(upstream)" WS "(" IP "|" ALNUM ")" ":" INT "(" WS STR BEGIN "(upstream)" WS "(" STR "@" ")?" "(" IP "|" ALNUM ")" ":"
")?" END, handle_upstream, NULL INT "(" WS STR ")?" END, handle_upstream, NULL
}, },
#endif #endif
/* loglevel */ /* loglevel */
@ -1071,22 +1071,30 @@ static HANDLE_FUNC (handle_upstream)
char *ip; char *ip;
int port; int port;
char *domain; char *domain;
char *basic_auth = NULL; /* optional, Base64 basic auth */
ip = get_string_arg (line, &match[2]); if (match[3].rm_so != -1) {
/* Basic auth is set for upstream proxy. */
basic_auth = get_string_arg (line, &match[3]);
}
ip = get_string_arg (line, &match[4]);
if (!ip) if (!ip)
return -1; return -1;
port = (int) get_long_arg (line, &match[7]); port = (int) get_long_arg (line, &match[9]);
if (match[10].rm_so != -1) { if (match[12].rm_so != -1) {
domain = get_string_arg (line, &match[10]); domain = get_string_arg (line, &match[12]);
if (domain) { if (domain) {
upstream_add (ip, port, domain, &conf->upstream_list); upstream_add (ip, port, basic_auth, domain,
&conf->upstream_list);
safefree (domain); safefree (domain);
} }
} else { } else {
upstream_add (ip, port, NULL, &conf->upstream_list); upstream_add (ip, port, basic_auth, NULL, &conf->upstream_list);
} }
safefree (basic_auth);
safefree (ip); safefree (ip);
return 0; return 0;
@ -1100,7 +1108,7 @@ static HANDLE_FUNC (handle_upstream_no)
if (!domain) if (!domain)
return -1; return -1;
upstream_add (NULL, 0, domain, &conf->upstream_list); upstream_add (NULL, 0, NULL, domain, &conf->upstream_list);
safefree (domain); safefree (domain);
return 0; return 0;

View File

@ -267,6 +267,17 @@ establish_http_connection (struct conn_s *connptr, struct request_s *request)
"Connection: close\r\n", "Connection: close\r\n",
request->method, request->path, request->method, request->path,
request->host, portbuff); request->host, portbuff);
} else if ((connptr->upstream_proxy) &&
(connptr->upstream_proxy->basic_auth)) {
/* Basic auth is set for upstream proxy. */
return write_message (connptr->server_fd,
"%s %s HTTP/1.0\r\n"
"Host: %s%s\r\n"
"Connection: close\r\n"
"Proxy-Authorization: Basic %s\r\n",
request->method, request->path,
request->host, portbuff,
connptr->upstream_proxy->basic_auth);
} else { } else {
return write_message (connptr->server_fd, return write_message (connptr->server_fd,
"%s %s HTTP/1.0\r\n" "%s %s HTTP/1.0\r\n"

View File

@ -32,7 +32,9 @@
/** /**
* Construct an upstream struct from input data. * Construct an upstream struct from input data.
*/ */
static struct upstream *upstream_build (const char *host, int port, const char *domain) static struct upstream *upstream_build (const char *host, int port,
const char *basic_auth,
const char *domain)
{ {
char *ptr; char *ptr;
struct upstream *up; struct upstream *up;
@ -44,7 +46,7 @@ static struct upstream *upstream_build (const char *host, int port, const char *
return NULL; return NULL;
} }
up->host = up->domain = NULL; up->host = up->domain = up->basic_auth = NULL;
up->ip = up->mask = 0; up->ip = up->mask = 0;
if (domain == NULL) { if (domain == NULL) {
@ -57,6 +59,9 @@ static struct upstream *upstream_build (const char *host, int port, const char *
up->host = safestrdup (host); up->host = safestrdup (host);
up->port = port; up->port = port;
if (basic_auth != NULL)
up->basic_auth = safestrdup (basic_auth);
log_message (LOG_INFO, "Added upstream %s:%d for [default]", log_message (LOG_INFO, "Added upstream %s:%d for [default]",
host, port); host, port);
} else if (host == NULL) { } else if (host == NULL) {
@ -101,6 +106,9 @@ static struct upstream *upstream_build (const char *host, int port, const char *
up->port = port; up->port = port;
up->domain = safestrdup (domain); up->domain = safestrdup (domain);
if (basic_auth != NULL)
up->basic_auth = safestrdup (basic_auth);
log_message (LOG_INFO, "Added upstream %s:%d for %s", log_message (LOG_INFO, "Added upstream %s:%d for %s",
host, port, domain); host, port, domain);
} }
@ -109,6 +117,7 @@ static struct upstream *upstream_build (const char *host, int port, const char *
fail: fail:
safefree (up->host); safefree (up->host);
safefree (up->basic_auth);
safefree (up->domain); safefree (up->domain);
safefree (up); safefree (up);
@ -116,14 +125,14 @@ fail:
} }
/* /*
* Add an entry to the upstream list * Add an entry to the upstream list.
*/ */
void upstream_add (const char *host, int port, const char *domain, void upstream_add (const char *host, int port, const char *basic_auth,
struct upstream **upstream_list) const char *domain, struct upstream **upstream_list)
{ {
struct upstream *up; struct upstream *up;
up = upstream_build (host, port, domain); up = upstream_build (host, port, basic_auth, domain);
if (up == NULL) { if (up == NULL) {
return; return;
} }
@ -155,6 +164,7 @@ void upstream_add (const char *host, int port, const char *domain,
upstream_cleanup: upstream_cleanup:
safefree (up->host); safefree (up->host);
safefree (up->basic_auth);
safefree (up->domain); safefree (up->domain);
safefree (up); safefree (up);
@ -216,6 +226,7 @@ void free_upstream_list (struct upstream *up)
struct upstream *tmp = up; struct upstream *tmp = up;
up = up->next; up = up->next;
safefree (tmp->domain); safefree (tmp->domain);
safefree (tmp->basic_auth);
safefree (tmp->host); safefree (tmp->host);
safefree (tmp); safefree (tmp);
} }

View File

@ -36,11 +36,13 @@ struct upstream {
char *domain; /* optional */ char *domain; /* optional */
char *host; char *host;
int port; int port;
char *basic_auth; /* optional, Base64 basic auth */
in_addr_t ip, mask; in_addr_t ip, mask;
}; };
#ifdef UPSTREAM_SUPPORT #ifdef UPSTREAM_SUPPORT
extern void upstream_add (const char *host, int port, const char *domain, extern void upstream_add (const char *host, int port,
const char *basic_auth, const char *domain,
struct upstream **upstream_list); struct upstream **upstream_list);
extern struct upstream *upstream_get (char *host, struct upstream *up); extern struct upstream *upstream_get (char *host, struct upstream *up);
extern void free_upstream_list (struct upstream *up); extern void free_upstream_list (struct upstream *up);