interpret server response
[record-dvb] / http.c
diff --git a/http.c b/http.c
index ca756bd5a56cfa57897d047db6d4d934b2e28dec..eee73454e763b0b5d5770954cfeeb7a146e8cad3 100644 (file)
--- a/http.c
+++ b/http.c
@@ -28,8 +28,8 @@ int open_http(char *url)
        int fd;
        struct sockaddr_in server;
        static struct dvb_host *dvbhost = NULL;
-       char buffer[BUFFSIZE];
-       int i;
+       char c, buffer[BUFFSIZE], *pos;
+       int lines = 0;
 
        if(!is_http(url))
                return -1;
@@ -55,19 +55,58 @@ int open_http(char *url)
 
        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);
                }
-               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 == ' ') {
+                                               pos++;
+                                               if(strncmp("200", pos, 3)) {
+                                                       fprintf(stderr, "Wrong result-code: %s\n", buffer);
+                                                       return -1;
+                                               } else {
+                                                       break;
+                                               }
+                                       }
+                                       pos++;
+                               }
+                       }
+
+                       pos = buffer;
+                       lines++;
+               }
        }
 
        return fd;
Impressum, Datenschutz