From 235995289021a11b33312c91a55ccfbca605348b Mon Sep 17 00:00:00 2001 From: Marc Cromme Date: Wed, 28 Mar 2007 12:05:18 +0000 Subject: [PATCH] getting the paraz2 server host and port info from the HTTP request, and mirroring back to the web client in the headers pazpar2-server-host pazpar2-server-port Notice these change according to the connection the client did made, so if a paraz2 is hidden behind firewalling and port-forwarding, the server host and port of the shielding firewall is reported back, and not the shielded host and port numbers. --- src/eventl.c | 23 +++++++++------ src/eventl.h | 17 +++++++++-- src/http.c | 79 +++++++++++++++++++++++++++++++++++++--------------- src/http_command.c | 4 +-- src/pazpar2.c | 4 +-- 5 files changed, 90 insertions(+), 37 deletions(-) diff --git a/src/eventl.c b/src/eventl.c index 89ffb12..cc8a6b9 100644 --- a/src/eventl.c +++ b/src/eventl.c @@ -6,7 +6,7 @@ */ /* - * $Id: eventl.c,v 1.2 2007-01-08 12:43:41 adam Exp $ + * $Id: eventl.c,v 1.3 2007-03-28 12:05:18 marc Exp $ * Based on revision YAZ' server/eventl.c 1.29. */ @@ -17,11 +17,7 @@ #include #endif -#ifdef WIN32 -#include -#else -#include -#endif + #include #include #include @@ -30,10 +26,13 @@ #include #include #include -#include "eventl.h" #include -IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags) +#include "eventl.h" + + +IOCHAN iochan_create(int fd, struct sockaddr_in *addr_in, + IOC_CALLBACK cb, int flags) { IOCHAN new_iochan; @@ -41,6 +40,14 @@ IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags) return 0; new_iochan->destroyed = 0; new_iochan->fd = fd; + + if(addr_in){ + new_iochan->addr_in.sin_family = addr_in->sin_family; + new_iochan->addr_in.sin_port = addr_in->sin_port; + new_iochan->addr_in.sin_addr = addr_in->sin_addr; + strncpy(new_iochan->addr_str, inet_ntoa(addr_in->sin_addr), 64); + } + new_iochan->flags = flags; new_iochan->fun = cb; new_iochan->force_event = 0; diff --git a/src/eventl.h b/src/eventl.h index 199d944..aee2106 100644 --- a/src/eventl.h +++ b/src/eventl.h @@ -3,8 +3,8 @@ * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Log: eventl.h,v $ - * Revision 1.1 2006-12-20 20:47:16 quinn + * $Id: eventl.h,v 1.2 2007-03-28 12:05:18 marc Exp $ + * Revision 1.1 2006/12/20 20:47:16 quinn * Reorganized source tree * * Revision 1.3 2006/12/12 02:36:24 quinn @@ -61,6 +61,14 @@ #include +#ifdef WIN32 +#include +#else +#include +#include +#endif + + struct iochan; typedef void (*IOC_CALLBACK)(struct iochan *i, int event); @@ -68,6 +76,8 @@ typedef void (*IOC_CALLBACK)(struct iochan *i, int event); typedef struct iochan { int fd; + struct sockaddr_in addr_in; + char addr_str[64]; int flags; #define EVENT_INPUT 0x01 #define EVENT_OUTPUT 0x02 @@ -101,7 +111,8 @@ typedef struct iochan #define iochan_settimeout(i, t) ((i)->max_idle = (t), (i)->last_event = time(0)) #define iochan_activity(i) ((i)->last_event = time(0)) -IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags); +IOCHAN iochan_create(int fd, struct sockaddr_in * addr_in, + IOC_CALLBACK cb, int flags); int event_loop(IOCHAN *iochans); #endif diff --git a/src/http.c b/src/http.c index 533df30..deb500c 100644 --- a/src/http.c +++ b/src/http.c @@ -1,5 +1,5 @@ /* - * $Id: http.c,v 1.13 2007-03-27 13:41:23 marc Exp $ + * $Id: http.c,v 1.14 2007-03-28 12:05:18 marc Exp $ */ #include @@ -14,6 +14,7 @@ #include #include #include +#include #if HAVE_CONFIG_H #include @@ -37,8 +38,10 @@ static struct http_channel *http_create(void); static void http_destroy(IOCHAN i); extern IOCHAN channel_list; +//extern struct parameters global_parameters; -static struct sockaddr_in *proxy_addr = 0; // If this is set, we proxy normal HTTP requests +// 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; @@ -516,17 +519,27 @@ struct http_header * http_header_append(struct http_channel *ch, const char *name, const char *value) { - struct http_header *hpnew = nmem_malloc(ch->nmem, sizeof *hpnew); + struct http_header *hpnew = 0; + + if (!hp | !ch) + return 0; while (hp && hp->next) hp = hp->next; - hpnew->name = nmem_strdup(ch->nmem, name); - hpnew->value = nmem_strdup(ch->nmem, value); - hpnew->next = 0; - hp->next = hpnew; - hp = hp->next; - return hpnew; + if(name && strlen(name)&& value && strlen(value)){ + hpnew = nmem_malloc(ch->nmem, sizeof *hpnew); + hpnew->name = nmem_strdup(ch->nmem, name); + hpnew->value = nmem_strdup(ch->nmem, value); + + hpnew->next = 0; + hp->next = hpnew; + hp = hp->next; + + return hpnew; + } + + return hp; } @@ -537,7 +550,8 @@ static int http_proxy(struct http_request *rq) struct http_proxy *p = c->proxy; struct http_header *hp; struct http_buf *requestbuf; - //struct conf_server *ser = global_parameters.server; + char server_host[64] = ""; + char server_port[16] = ""; if (!p) // This is a new connection. Create a proxy channel @@ -576,13 +590,13 @@ static int http_proxy(struct http_request *rq) p->first_response = 1; c->proxy = p; // We will add EVENT_OUTPUT below - p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT); + p->iochan = iochan_create(sock, 0, proxy_io, EVENT_INPUT); iochan_setdata(p->iochan, p); p->iochan->next = channel_list; channel_list = p->iochan; } - // Modify Host: header + // Modify Host: header, but getting the host and port info first for (hp = rq->headers; hp; hp = hp->next) if (!strcmp(hp->name, "Host")) break; @@ -591,16 +605,37 @@ static int http_proxy(struct http_request *rq) yaz_log(YLOG_WARN, "Failed to find Host header in proxy"); return -1; } + + { + char * colon = 0; + + if((colon = strchr(hp->value, ':'))){ + int collen = colon - hp->value; + strncpy(server_host, hp->value, (collen < 64) ? collen : 64 ); + strncpy(server_port, colon + 1, 16); + } else { + strncpy(server_host, hp->value, 64); + strncpy(server_port, hp->value, 16); + } + } + hp->value = nmem_strdup(c->nmem, proxy_url); - // Add new header about paraz2 version, host, remote client address, etc. - - hp = rq->headers; - hp = http_header_append(c, hp, PACKAGE_NAME "-version", PACKAGE_VERSION); - //hp = http_header_append(c, hp, PACKAGE_NAME "-server-host", ser->host); - //hp = http_header_append(c, hp, PACKAGE_NAME "-server-port", ser->port); - //hp = http_header_append(c, hp, PACKAGE_NAME "-remote-host", "blabla"); - //hp = http_header_append(c, hp, PACKAGE_NAME "-remote-port", "blabla"); + // Add new header about paraz2 version, host, remote client address, etc. + { + + hp = rq->headers; + hp = http_header_append(c, hp, + PACKAGE_NAME "-version", PACKAGE_VERSION); + hp = http_header_append(c, hp, + PACKAGE_NAME "-server-host", server_host); + //sprintf(server_port, "%d", ser->port); + hp = http_header_append(c, hp, + PACKAGE_NAME "-server-port", server_port); + hp = http_header_append(c, hp, + PACKAGE_NAME "-remote-addr", + c->iochan->addr_str); + } requestbuf = http_serialize_request(rq); http_buf_enqueue(&p->oqueue, requestbuf); @@ -925,7 +960,7 @@ static void http_accept(IOCHAN i, int event) yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2"); yaz_log(YLOG_DEBUG, "New command connection"); - c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT); + c = iochan_create(s, &addr, http_io, EVENT_INPUT | EVENT_EXCEPT); ch = http_create(); ch->iochan = c; @@ -988,7 +1023,7 @@ void http_init(const char *addr) if (listen(l, SOMAXCONN) < 0) yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen"); - c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT); + c = iochan_create(l, &myaddr, http_accept, EVENT_INPUT | EVENT_EXCEPT); c->next = channel_list; channel_list = c; } diff --git a/src/http_command.c b/src/http_command.c index 223903d..4d6c355 100644 --- a/src/http_command.c +++ b/src/http_command.c @@ -1,5 +1,5 @@ /* - * $Id: http_command.c,v 1.28 2007-03-20 07:27:51 adam Exp $ + * $Id: http_command.c,v 1.29 2007-03-28 12:05:18 marc Exp $ */ #include @@ -53,7 +53,7 @@ struct http_session *http_session_create() r->timestamp = 0; r->next = session_list; session_list = r; - r->timeout_iochan = iochan_create(-1, session_timeout, 0); + r->timeout_iochan = iochan_create(-1, 0, session_timeout, 0); iochan_setdata(r->timeout_iochan, r); iochan_settimeout(r->timeout_iochan, global_parameters.session_timeout); r->timeout_iochan->next = channel_list; diff --git a/src/pazpar2.c b/src/pazpar2.c index 1a8fdfa..31cf431 100644 --- a/src/pazpar2.c +++ b/src/pazpar2.c @@ -1,4 +1,4 @@ -/* $Id: pazpar2.c,v 1.55 2007-03-28 04:33:41 quinn Exp $ */ +/* $Id: pazpar2.c,v 1.56 2007-03-28 12:05:18 marc Exp $ */ #include #include @@ -1020,7 +1020,7 @@ static struct connection *connection_create(struct client *cl) cl->connection = new; new->link = link; - new->iochan = iochan_create(cs_fileno(link), handler, 0); + new->iochan = iochan_create(cs_fileno(link), 0, handler, 0); iochan_setdata(new->iochan, new); new->iochan->next = channel_list; channel_list = new->iochan; -- 1.7.10.4