X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fhttp.c;h=9bee65d14841c5206b756620186015669b818e60;hb=142c8c510a7b23c633272c656e45277e4ec35d30;hp=f4d8d697574daf18abfbe4dbc7a8312bfd9a44d2;hpb=f89a08d579f270d78b6e48a04ec63cef23539c88;p=pazpar2-moved-to-github.git diff --git a/src/http.c b/src/http.c index f4d8d69..9bee65d 100644 --- a/src/http.c +++ b/src/http.c @@ -17,28 +17,46 @@ 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 @@ -81,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; @@ -212,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++); @@ -332,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; @@ -385,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; { @@ -570,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); } @@ -679,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) { @@ -696,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(); @@ -709,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; @@ -810,7 +853,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); @@ -846,6 +889,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 @@ -860,7 +905,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"); @@ -914,15 +959,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; } @@ -949,7 +998,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"); @@ -957,7 +1006,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; @@ -990,7 +1039,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); @@ -1002,7 +1055,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); } @@ -1048,7 +1105,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; @@ -1057,10 +1113,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); @@ -1072,8 +1125,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; @@ -1081,7 +1136,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); @@ -1098,7 +1153,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); @@ -1113,33 +1168,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);