struct conf_server *server);
static void http_destroy(IOCHAN i);
-// If this is set, we proxy normal HTTP requests
-static struct sockaddr_in *proxy_addr = 0;
-static char proxy_url[256] = "";
-static char myurl[256] = "";
static struct http_buf *http_buf_freelist = 0;
static struct http_channel *http_channel_freelist = 0;
static int http_weshouldproxy(struct http_request *rq)
{
- if (proxy_addr && !strstr(rq->path, "search.pz2"))
+ struct http_channel *c = rq->channel;
+ if (c->server->proxy_addr && !strstr(rq->path, "search.pz2"))
return 1;
return 0;
}
struct http_proxy *p = c->proxy;
struct http_header *hp;
struct http_buf *requestbuf;
- char server_via[128] = "";
char server_port[16] = "";
struct conf_server *ser = c->server;
&one, sizeof(one)) < 0)
abort();
enable_nonblock(sock);
- if (connect(sock, (struct sockaddr *) proxy_addr,
- sizeof(*proxy_addr)) < 0)
+ if (connect(sock, (struct sockaddr *) c->server->proxy_addr,
+ sizeof(*c->server->proxy_addr)) < 0)
{
if (!is_inprogress())
{
// Add new header about paraz2 version, host, remote client address, etc.
{
+ char server_via[128];
+
hp = rq->headers;
hp = http_header_append(c, hp,
"X-Pazpar2-Version", PACKAGE_VERSION);
sprintf(server_port, "%d", ser->port);
hp = http_header_append(c, hp,
"X-Pazpar2-Server-Port", server_port);
- sprintf(server_via, "1.1 %s:%s (%s/%s)",
- ser->host ? ser->host : "@",
- server_port, PACKAGE_NAME, PACKAGE_VERSION);
+ yaz_snprintf(server_via, sizeof(server_via),
+ "1.1 %s:%s (%s/%s)",
+ ser->host ? ser->host : "@",
+ server_port, PACKAGE_NAME, PACKAGE_VERSION);
hp = http_header_append(c, hp, "Via" , server_via);
hp = http_header_append(c, hp, "X-Forwarded-For", c->addr);
}
pp = strchr(addr, ':');
if (pp)
{
- int len = pp - addr;
- char hostname[128];
+ WRBUF w = wrbuf_alloc();
struct hostent *he;
- strncpy(hostname, addr, len);
- hostname[len] = '\0';
- if (!(he = gethostbyname(hostname))){
- yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname);
+ wrbuf_write(w, addr, pp - addr);
+ wrbuf_puts(w, "");
+
+ he = gethostbyname(wrbuf_cstr(w));
+ wrbuf_destroy(w);
+ if (!he)
+ {
+ yaz_log(YLOG_FATAL, "Unable to resolve '%s'", addr);
return 1;
}
-
memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
port = atoi(pp + 1);
}
}
}
-void http_set_proxyaddr(char *host, char *base_url)
+void http_set_proxyaddr(const char *host, struct conf_server *server)
{
- char *p;
+ const char *p;
short port;
struct hostent *he;
+ WRBUF w = wrbuf_alloc();
+
+ yaz_log(YLOG_LOG, "HTTP backend %s", host);
- strcpy(myurl, base_url);
- strcpy(proxy_url, host);
p = strchr(host, ':');
- yaz_log(YLOG_DEBUG, "Proxying for %s", host);
- yaz_log(YLOG_LOG, "HTTP backend %s", proxy_url);
- if (p) {
+ if (p)
+ {
port = atoi(p + 1);
- *p = '\0';
+ wrbuf_write(w, host, p - host);
+ wrbuf_puts(w, "");
}
else
+ {
port = 80;
- if (!(he = gethostbyname(host)))
+ wrbuf_puts(w, host);
+ }
+ if (!(he = gethostbyname(wrbuf_cstr(w))))
{
- fprintf(stderr, "Failed to lookup '%s'\n", host);
+ fprintf(stderr, "Failed to lookup '%s'\n", wrbuf_cstr(w));
exit(1);
}
- proxy_addr = xmalloc(sizeof(struct sockaddr_in));
- proxy_addr->sin_family = he->h_addrtype;
- memcpy(&proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
- proxy_addr->sin_port = htons(port);
+ wrbuf_destroy(w);
+
+ server->proxy_addr = xmalloc(sizeof(struct sockaddr_in));
+ server->proxy_addr->sin_family = he->h_addrtype;
+ memcpy(&server->proxy_addr->sin_addr.s_addr, he->h_addr_list[0], he->h_length);
+ server->proxy_addr->sin_port = htons(port);
}
static void http_fire_observers(struct http_channel *c)
struct conf_server *ser;
for (ser = conf->servers; ser; ser = ser->next)
{
- char hp[128];
- *hp = '\0';
+ WRBUF w = wrbuf_alloc();
+ int r;
if (listener_override)
{
- strcpy(hp, listener_override);
+ wrbuf_puts(w, listener_override);
listener_override = 0; /* only first server is overriden */
}
else
{
- strcpy(hp, ser->host ? ser->host : "");
+ if (ser->host)
+ wrbuf_puts(w, ser->host);
if (ser->port)
{
- if (*hp)
- strcat(hp, ":");
- sprintf(hp + strlen(hp), "%d", ser->port);
+ if (wrbuf_len(w))
+ wrbuf_puts(w, ":");
+ wrbuf_printf(w, "%d", ser->port);
}
}
- if (http_init(hp, ser))
+ r = http_init(wrbuf_cstr(w), ser);
+ wrbuf_destroy(w);
+ if (r)
return -1;
- *hp = '\0';
+ w = wrbuf_alloc();
if (proxy_override)
- strcpy(hp, proxy_override);
+ wrbuf_puts(w, proxy_override);
else if (ser->proxy_host || ser->proxy_port)
{
- strcpy(hp, ser->proxy_host ? ser->proxy_host : "");
+ if (ser->proxy_host)
+ wrbuf_puts(w, ser->proxy_host);
if (ser->proxy_port)
{
- if (*hp)
- strcat(hp, ":");
- sprintf(hp + strlen(hp), "%d", ser->proxy_port);
+ if (wrbuf_len(w))
+ wrbuf_puts(w, ":");
+ wrbuf_printf(w, "%d", ser->proxy_port);
}
}
- if (*hp)
- http_set_proxyaddr(hp, ser->myurl ? ser->myurl : "");
+ if (wrbuf_len(w))
+ http_set_proxyaddr(wrbuf_cstr(w), ser);
+ wrbuf_destroy(w);
}
return 0;
}
#include "pazpar2_config.h"
#include "settings.h"
-static char confdir[256] = ".";
+static WRBUF confdir = 0;
static char *parse_settings(NMEM nmem, xmlNode *node);
else
{
r = nmem_malloc(nmem,
- strlen(confdir) + strlen((const char *) src) + 2);
- sprintf(r, "%s/%s", confdir, src);
+ wrbuf_len(confdir) + strlen((const char *) src) + 2);
+ sprintf(r, "%s/%s", wrbuf_cstr(confdir), src);
}
}
else
server->proxy_host = 0;
server->proxy_port = 0;
server->myurl = 0;
+ server->proxy_addr = 0;
server->service = 0;
server->next = 0;
server->server_settings = 0;
}
else if (!strcmp((const char *) n->name, "service"))
{
- const char *service_id = (const char *)
+ char *service_id = (char *)
xmlGetProp(n, (xmlChar *) "id");
struct conf_service **sp = &server->service;
*sp = s;
}
}
+ xmlFree(service_id);
}
else
{
if (yaz_is_abspath(fname))
yaz_snprintf(path, sizeof(path), fname);
else
- yaz_snprintf(path, sizeof(path), "%s/%s", confdir, fname);
+ yaz_snprintf(path, sizeof(path), "%s/%s", wrbuf_cstr(confdir), fname);
return xsltParseStylesheetFile((xmlChar *) path);
}
yaz_log(YLOG_FATAL, "Failed to read %s", fname);
exit(1);
}
+ confdir = wrbuf_alloc();
if ((p = strrchr(fname,
#ifdef WIN32
'\\'
)))
{
int len = p - fname;
- if (len >= sizeof(confdir))
- len = sizeof(confdir)-1;
- strncpy(confdir, fname, len);
- confdir[len] = '\0';
+ wrbuf_write(confdir, fname, len);
}
+ wrbuf_puts(confdir, "");
config = parse_config(xmlDocGetRootElement(doc));
xmlFreeDoc(doc);