Updated to latest m4.
[pazpar2-moved-to-github.git] / src / http.c
index b1d7b43..2ad82ce 100644 (file)
@@ -17,6 +17,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 */
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdio.h>
 #ifdef WIN32
 #include <winsock.h>
@@ -28,9 +32,6 @@ typedef int socklen_t;
 #endif
 
 #include <sys/types.h>
-#if HAVE_SYS_UIO_H
-#include <sys/uio.h>
-#endif
 
 #include <yaz/snprintf.h>
 #if HAVE_UNISTD_H
@@ -49,10 +50,6 @@ typedef int socklen_t;
 #include <assert.h>
 #include <string.h>
 
-#if HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #if HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
@@ -233,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++);
@@ -591,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);
         }
@@ -704,10 +703,10 @@ struct http_header * http_header_append(struct http_channel *ch,
 static int is_inprogress(void)
 {
 #ifdef WIN32
-    if (WSAGetLastError() != WSAEWOULDBLOCK)
+    if (WSAGetLastError() == WSAEWOULDBLOCK)
         return 1;
 #else
-    if (errno != EINPROGRESS)
+    if (errno == EINPROGRESS)
         return 1;
 #endif
     return 0;
@@ -890,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
@@ -1124,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;
@@ -1150,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);
@@ -1165,27 +1168,43 @@ 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)