X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fhttp.c;h=62233b2cbf1abca0d9ebd5b07331d569ba0e2e13;hb=6267b857abe8e71547f34e00b6813e086dace360;hp=dfc4f38b8507989e597b7de6c9cb4049a5d95dab;hpb=3695af4856ba7393a6ae4e793afa8d3263db9416;p=pazpar2-moved-to-github.git diff --git a/src/http.c b/src/http.c index dfc4f38..62233b2 100644 --- a/src/http.c +++ b/src/http.c @@ -1,7 +1,5 @@ -/* $Id: http.c,v 1.40 2007-09-26 09:09:15 adam Exp $ - Copyright (c) 2006-2007, Index Data. - -This file is part of Pazpar2. +/* This file is part of Pazpar2. + Copyright (C) 2006-2009 Index Data Pazpar2 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -14,39 +12,56 @@ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License -along with Pazpar2; see the file LICENSE. If not, write to the -Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -02111-1307, USA. - */ +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +#if HAVE_CONFIG_H +#include +#endif #include +#ifdef WIN32 +#include +typedef int socklen_t; +#endif + +#if HAVE_SYS_SOCKET_H #include +#endif + #include -#include + #include +#if HAVE_UNISTD_H #include +#endif + #include -#include +#include #include #include +#if HAVE_NETDB_H #include +#endif + #include #include #include -#if HAVE_CONFIG_H -#include +#if HAVE_NETINET_IN_H +#include #endif -#include +#if HAVE_ARPA_INET_H #include -#include +#endif #include #include #include -#include "cconfig.h" #include "util.h" #include "eventl.h" #include "pazpar2.h" @@ -68,7 +83,8 @@ static struct http_channel *http_channel_freelist = 0; struct http_channel_observer_s { void *data; - void (*destroy)(void *data, struct http_channel *chan); + void *data2; + http_channel_destroy_t destroy; struct http_channel_observer_s *next; struct http_channel *chan; }; @@ -83,7 +99,7 @@ static const char *http_lookup_header(struct http_header *header, return 0; } -static struct http_buf *http_buf_create() +static struct http_buf *http_buf_create(void) { struct http_buf *r; @@ -117,17 +133,6 @@ static void http_buf_destroy_queue(struct http_buf *b) } } -#ifdef GAGA -// Calculate length of chain -static int http_buf_len(struct http_buf *b) -{ - int sum = 0; - for (; b; b = b->next) - sum += b->len; - return sum; -} -#endif - static struct http_buf *http_buf_bybuf(char *b, int len) { struct http_buf *res = 0; @@ -225,12 +230,13 @@ static void urldecode(char *i, char *o) *(o++) = ' '; i++; } - else if (*i == '%') + else if (*i == '%' && i[1] && i[2]) { + int v; i++; - sscanf(i, "%2hhx", o); + sscanf(i, "%2x", &v); + *o++ = v; i += 2; - o++; } else *(o++) = *(i++); @@ -292,6 +298,7 @@ struct http_response *http_create_response(struct http_channel *c) r->channel = c; r->headers = 0; r->payload = 0; + r->content_type = "text/xml"; return r; } @@ -344,7 +351,7 @@ static int package_check(const char *buf, int sz) while (*cp == ' ') cp++; content_len = 0; - while (*cp && isdigit(*cp)) + while (*cp && isdigit(*(const unsigned char *)cp)) content_len = content_len*10 + (*cp++ - '0'); if (content_len < 0) /* prevent negative offsets */ content_len = 0; @@ -397,7 +404,7 @@ struct http_response *http_parse_response_buf(struct http_channel *c, const char return 0; *(value++) = '\0'; h->name = nmem_strdup(c->nmem, p); - while (isspace(*value)) + while (isspace(*(const unsigned char *) value)) value++; if (value >= p2) // Empty header; { @@ -582,7 +589,8 @@ struct http_request *http_parse_request(struct http_channel *c, r->content_len = start + len - buf; r->content_buf = buf; - if (!strcmp(content_type, "application/x-www-form-urlencoded")) + if (!yaz_strcmp_del("application/x-www-form-urlencoded", + content_type, "; ")) { http_parse_arguments(r, c->nmem, r->content_buf); } @@ -603,8 +611,8 @@ static struct http_buf *http_serialize_response(struct http_channel *c, { wrbuf_printf(c->wrbuf, "Content-Length: %d\r\n", r->payload ? (int) strlen(r->payload) : 0); - wrbuf_printf(c->wrbuf, "Content-Type: text/xml\r\n"); - if (1) + wrbuf_printf(c->wrbuf, "Content-Type: %s\r\n", r->content_type); + if (!strcmp(r->content_type, "text/xml")) { xmlDoc *doc = xmlParseMemory(r->payload, strlen(r->payload)); if (doc) @@ -691,7 +699,33 @@ struct http_header * http_header_append(struct http_channel *ch, return hp; } - + +static int is_inprogress(void) +{ +#ifdef WIN32 + if (WSAGetLastError() == WSAEWOULDBLOCK) + return 1; +#else + if (errno == EINPROGRESS) + return 1; +#endif + return 0; +} + +static void enable_nonblock(int sock) +{ + int flags; +#ifdef WIN32 + flags = (flags & CS_FLAGS_BLOCKING) ? 0 : 1; + if (ioctlsocket(sock, FIONBIO, &flags) < 0) + yaz_log(YLOG_FATAL|YLOG_ERRNO, "ioctlsocket"); +#else + if ((flags = fcntl(sock, F_GETFL, 0)) < 0) + yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl"); + if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) + yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2"); +#endif +} static int http_proxy(struct http_request *rq) { @@ -708,7 +742,6 @@ static int http_proxy(struct http_request *rq) int sock; struct protoent *pe; int one = 1; - int flags; if (!(pe = getprotobyname("tcp"))) { abort(); @@ -721,18 +754,16 @@ static int http_proxy(struct http_request *rq) if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*) &one, sizeof(one)) < 0) abort(); - if ((flags = fcntl(sock, F_GETFL, 0)) < 0) - yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl"); - if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) - yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2"); + enable_nonblock(sock); if (connect(sock, (struct sockaddr *) proxy_addr, sizeof(*proxy_addr)) < 0) - if (errno != EINPROGRESS) + { + if (!is_inprogress()) { yaz_log(YLOG_WARN|YLOG_ERRNO, "Proxy connect"); return -1; } - + } p = xmalloc(sizeof(struct http_proxy)); p->oqueue = 0; p->channel = c; @@ -763,7 +794,8 @@ static int http_proxy(struct http_request *rq) hp = http_header_append(c, hp, "X-Pazpar2-Server-Port", server_port); sprintf(server_via, "1.1 %s:%s (%s/%s)", - ser->host, server_port, PACKAGE_NAME, PACKAGE_VERSION); + 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); } @@ -822,7 +854,7 @@ static void http_io(IOCHAN i, int event) case EVENT_INPUT: htbuf = http_buf_create(); - res = read(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1); + res = recv(iochan_getfd(i), htbuf->buf, HTTP_BUF_SIZE -1, 0); if (res == -1 && errno == EAGAIN) { http_buf_destroy(htbuf); @@ -858,6 +890,8 @@ static void http_io(IOCHAN i, int event) hc->request->path, *hc->request->search ? "?" : "", hc->request->search); + if (hc->request->content_buf) + yaz_log(YLOG_LOG, "%s", hc->request->content_buf); if (http_weshouldproxy(hc->request)) http_proxy(hc->request); else @@ -872,7 +906,7 @@ static void http_io(IOCHAN i, int event) if (hc->oqueue) { struct http_buf *wb = hc->oqueue; - res = write(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len); + res = send(iochan_getfd(hc->iochan), wb->buf + wb->offset, wb->len, 0); if (res <= 0) { yaz_log(YLOG_WARN|YLOG_ERRNO, "write"); @@ -913,25 +947,6 @@ static void http_io(IOCHAN i, int event) } } -#ifdef GAGA -// If this hostname contains our proxy host as a prefix, replace with myurl -static char *sub_hostname(struct http_channel *c, char *buf) -{ - char tmp[1024]; - if (strlen(buf) > 1023) - return buf; - if (strncmp(buf, "http://", 7)) - return buf; - if (!strncmp(buf + 7, proxy_url, strlen(proxy_url))) - { - strcpy(tmp, myurl); - strcat(tmp, buf + strlen(proxy_url) + 7); - return nmem_strdup(c->nmem, tmp); - } - return buf; -} -#endif - // Handles I/O on a client connection to a backend web server (proxy mode) static void proxy_io(IOCHAN pi, int event) { @@ -945,15 +960,19 @@ static void proxy_io(IOCHAN pi, int event) case EVENT_INPUT: htbuf = http_buf_create(); - res = read(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1); - if (res == 0 || (res < 0 && errno != EINPROGRESS)) + res = recv(iochan_getfd(pi), htbuf->buf, HTTP_BUF_SIZE -1, 0); + if (res == 0 || (res < 0 && !is_inprogress())) { if (hc->oqueue) { yaz_log(YLOG_WARN, "Proxy read came up short"); // Close channel and alert client HTTP channel that we're gone http_buf_destroy(htbuf); +#ifdef WIN32 + closesocket(iochan_getfd(pi)); +#else close(iochan_getfd(pi)); +#endif iochan_destroy(pi); pc->iochan = 0; } @@ -968,33 +987,6 @@ static void proxy_io(IOCHAN pi, int event) htbuf->buf[res] = '\0'; htbuf->offset = 0; htbuf->len = res; -#ifdef GAGA - if (pc->first_response) // Check if this is a redirect - { - int len; - if ((len = package_check(htbuf->buf))) - { - struct http_response *res = http_parse_response_buf(hc, htbuf->buf, len); - if (res) - { - const char *location = http_lookup_header( - res->header, "Location"); - if (location) - { - // We found a location header. Rewrite it. - struct http_buf *buf; - h->value = sub_hostname(hc, location); - buf = http_serialize_response(hc, res); - yaz_log(YLOG_LOG, "Proxy rewrite"); - http_buf_enqueue(&hc->oqueue, buf); - htbuf->offset = len; - break; - } - } - } - pc->first_response = 0; - } -#endif // Write any remaining payload if (htbuf->len - htbuf->offset > 0) http_buf_enqueue(&hc->oqueue, htbuf); @@ -1007,7 +999,7 @@ static void proxy_io(IOCHAN pi, int event) iochan_clearflag(pi, EVENT_OUTPUT); return; } - res = write(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len); + res = send(iochan_getfd(pi), htbuf->buf + htbuf->offset, htbuf->len, 0); if (res <= 0) { yaz_log(YLOG_WARN|YLOG_ERRNO, "write"); @@ -1015,7 +1007,7 @@ static void proxy_io(IOCHAN pi, int event) return; } if (res == htbuf->len) - { + { struct http_buf *np = htbuf->next; http_buf_destroy(htbuf); pc->oqueue = np; @@ -1048,7 +1040,11 @@ static void http_destroy(IOCHAN i) { if (s->proxy->iochan) { +#ifdef WIN32 + closesocket(iochan_getfd(s->proxy->iochan)); +#else close(iochan_getfd(s->proxy->iochan)); +#endif iochan_destroy(s->proxy->iochan); } http_buf_destroy_queue(s->proxy->oqueue); @@ -1060,7 +1056,11 @@ static void http_destroy(IOCHAN i) http_destroy_observers(s); s->next = http_channel_freelist; http_channel_freelist = s; +#ifdef WIN32 + closesocket(iochan_getfd(i)); +#else close(iochan_getfd(i)); +#endif iochan_destroy(i); } @@ -1106,7 +1106,6 @@ static void http_accept(IOCHAN i, int event) socklen_t len; int s; IOCHAN c; - int flags; struct http_channel *ch; len = sizeof addr; @@ -1115,10 +1114,7 @@ static void http_accept(IOCHAN i, int event) yaz_log(YLOG_WARN|YLOG_ERRNO, "accept"); return; } - if ((flags = fcntl(s, F_GETFL, 0)) < 0) - yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl"); - if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) - yaz_log(YLOG_FATAL|YLOG_ERRNO, "fcntl2"); + enable_nonblock(s); yaz_log(YLOG_DEBUG, "New command connection"); c = iochan_create(s, http_io, EVENT_INPUT | EVENT_EXCEPT); @@ -1130,8 +1126,10 @@ static void http_accept(IOCHAN i, int event) pazpar2_add_channel(c); } +static int listener_socket = 0; + /* Create a http-channel listener, syntax [host:]port */ -void http_init(const char *addr) +int http_init(const char *addr) { IOCHAN c; int l; @@ -1139,7 +1137,7 @@ void http_init(const char *addr) struct sockaddr_in myaddr; int one = 1; const char *pp; - int port; + short port; yaz_log(YLOG_LOG, "HTTP listener %s", addr); @@ -1156,7 +1154,7 @@ void http_init(const char *addr) hostname[len] = '\0'; if (!(he = gethostbyname(hostname))){ yaz_log(YLOG_FATAL, "Unable to resolve '%s'", hostname); - exit(1); + return 1; } memcpy(&myaddr.sin_addr.s_addr, he->h_addr_list[0], he->h_length); @@ -1171,33 +1169,49 @@ void http_init(const char *addr) myaddr.sin_port = htons(port); if (!(p = getprotobyname("tcp"))) { - abort(); + return 1; } if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0) yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket"); if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*) &one, sizeof(one)) < 0) - abort(); + return 1; if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0) { yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind"); - exit(1); + return 1; } if (listen(l, SOMAXCONN) < 0) { yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen"); - exit(1); + return 1; } + listener_socket = l; + c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT); pazpar2_add_channel(c); + return 0; +} + +void http_close_server(void) +{ + /* break the event_loop (select) by closing down the HTTP listener sock */ + if (listener_socket) + { +#ifdef WIN32 + closesocket(listener_socket); +#else + close(listener_socket); +#endif + } } void http_set_proxyaddr(char *host, char *base_url) { char *p; - int port; + short port; struct hostent *he; strcpy(myurl, base_url); @@ -1227,7 +1241,7 @@ static void http_fire_observers(struct http_channel *c) http_channel_observer_t p = c->observers; while (p) { - p->destroy(p->data, c); + p->destroy(p->data, c, p->data2); p = p->next; } } @@ -1248,6 +1262,7 @@ http_channel_observer_t http_add_observer(struct http_channel *c, void *data, http_channel_observer_t obs = xmalloc(sizeof(*obs)); obs->chan = c; obs->data = data; + obs->data2 = 0; obs->destroy= des; obs->next = c->observers; c->observers = obs; @@ -1271,10 +1286,18 @@ struct http_channel *http_channel_observer_chan(http_channel_observer_t obs) return obs->chan; } +void http_observer_set_data2(http_channel_observer_t obs, void *data2) +{ + obs->data2 = data2; +} + + /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab */ +