]> cvs.zerfleddert.de Git - record-dvb/blobdiff - http.c
better reconnect-logic
[record-dvb] / http.c
diff --git a/http.c b/http.c
index a4f4c9178406d80e3256d11acfd1f8610573aa97..ab798d9956f64cd55ee030d05fe45a3cd101cd29 100644 (file)
--- a/http.c
+++ b/http.c
@@ -6,7 +6,6 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#include <netdb.h>
 
 #include "common.h"
 #include "http.h"
 
 #include "common.h"
 #include "http.h"
@@ -28,22 +27,19 @@ int open_http(char *url)
 {
        int fd;
        struct sockaddr_in server;
 {
        int fd;
        struct sockaddr_in server;
-       struct dvb_host *dvbhost;
-       struct addrinfo *s_addrinfo, addrhints;
-       char buffer[BUFFSIZE];
-       int i;
+       static struct dvb_host *dvbhost = NULL;
+       char c, buffer[BUFFSIZE], *pos;
+       int lines = 0;
 
        if(!is_http(url))
                return -1;
 
 
        if(!is_http(url))
                return -1;
 
-       dvbhost = parse(&(url[7]), "80");
-
-       bzero(&addrhints, sizeof(struct addrinfo));
-       addrhints.ai_family = PF_INET;
-       addrhints.ai_socktype = SOCK_STREAM;
+       if (!dvbhost) {
+               dvbhost = parse(&(url[7]), "80");
+               dvbhost->socktype = SOCK_STREAM;
+       }
 
 
-       if ((i=getaddrinfo(dvbhost->hostname, dvbhost->port, &addrhints, &s_addrinfo))) {
-               fprintf(stderr,"getaddrinfo: %s\n",gai_strerror(i));
+       if (resolve(dvbhost, &server) < 0) {
                return -1;
        }
 
                return -1;
        }
 
@@ -52,34 +48,68 @@ int open_http(char *url)
                return -1;
        }
 
                return -1;
        }
 
-       bzero(&server, sizeof(struct sockaddr_in));
-       server.sin_family = AF_INET;
-       server.sin_addr.s_addr = ((struct sockaddr_in*)(s_addrinfo->ai_addr))->sin_addr.s_addr;
-       server.sin_port = ((struct sockaddr_in*)(s_addrinfo->ai_addr))->sin_port;
-
        if (connect(fd, (struct sockaddr*) &server, sizeof(server)) < 0) {
                perror("connect");
                return -1;
        }
 
        if (connect(fd, (struct sockaddr*) &server, sizeof(server)) < 0) {
                perror("connect");
                return -1;
        }
 
-       freeaddrinfo(s_addrinfo);
-
-
        snprintf(buffer, BUFFSIZE-1, "GET /%s HTTP/1.0\n\n",dvbhost->location);
        buffer[BUFFSIZE-1] = 0;
        snprintf(buffer, BUFFSIZE-1, "GET /%s HTTP/1.0\n\n",dvbhost->location);
        buffer[BUFFSIZE-1] = 0;
-       send(fd, buffer, strlen(buffer), 0);
-       i = 0;
-       while(i < 2) {
-               if (recv(fd, buffer, 1, 0) < 1) {
+       if (send(fd, buffer, strlen(buffer), 0) < 0) {
+               perror("send");
+               return -1;
+       }
+
+       pos = buffer;
+       while (1) {
+               if (recv(fd, &c, 1, 0) < 1) {
                        perror("recv");
                        exit(EXIT_FAILURE);
                }
                        perror("recv");
                        exit(EXIT_FAILURE);
                }
-               printf("%c",buffer[0]);
-               if (buffer[0] == 0x0a)
-                       i++;
-               else
-                       if (buffer[0] != 0x0d)
-                               i = 0;
+
+               if (pos-buffer >= BUFFSIZE)
+                       pos = buffer;
+
+               *(pos++) = c;
+
+               if (c == 0x0a) {
+                       *(--pos) = 0;
+                       if (pos-buffer > 0 && *(--pos) == 0x0d)
+                               *pos = 0;
+
+                       if (pos-buffer == 0)
+                               break;
+
+#ifdef DEBUG
+                       printf("%d. %s (%d)\n", lines, buffer, pos-buffer);
+#endif
+                       if (lines == 0) {
+                               if (strncasecmp("HTTP/", buffer, 5)) {
+                                       fprintf(stderr, "Wrong answer from server: %s\n", buffer);
+                                       return -1;
+                               }
+
+                               pos = buffer;
+                               while (*pos != 0) {
+                                       if (*(pos++) == ' ') {
+                                               if(strncmp("200", pos, 3)) {
+                                                       fprintf(stderr, "Wrong result-code: %s\n", buffer);
+                                                       return -1;
+                                               } else {
+                                                       break;
+                                               }
+                                       }
+
+                                       if (*(++pos) == 0) {
+                                               fprintf(stderr, "Wrong answer from server: %s\n", buffer);
+                                               return -1;
+                                       }
+                               }
+                       }
+
+                       pos = buffer;
+                       lines++;
+               }
        }
 
        return fd;
        }
 
        return fd;
Impressum, Datenschutz