+#include <strings.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+
+#include "common.h"
+#include "http.h"
+
+#define BUFFSIZE 1024
+
+int is_http(char *url)
+{
+ if (strlen(url) < 8)
+ return 0;
+
+ if (!strncasecmp("http://",url,7))
+ return 1;
+
+ return 0;
+}
+
+int open_http(char *url)
+{
+ int fd;
+ struct sockaddr_in server;
+ struct dvb_host *dvbhost;
+ struct addrinfo *s_addrinfo, addrhints;
+ char buffer[BUFFSIZE];
+ int i;
+
+ 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 ((i=getaddrinfo(dvbhost->hostname, dvbhost->port, &addrhints, &s_addrinfo))) {
+ fprintf(stderr,"getaddrinfo: %s\n",gai_strerror(i));
+ return -1;
+ }
+
+ if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
+ perror("socket");
+ 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;
+ }
+
+ freeaddrinfo(s_addrinfo);
+
+
+ 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) {
+ perror("recv");
+ exit(EXIT_FAILURE);
+ }
+ printf("%c",buffer[0]);
+ if (buffer[0] == 0x0a)
+ i++;
+ else
+ if (buffer[0] != 0x0d)
+ i = 0;
+ }
+
+ return fd;
+}