65c12382c09e49341cbbe887e5772ef7f3502d6e
[record-dvb] / http.c
1 #include <strings.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <netdb.h>
10
11 #include "common.h"
12 #include "http.h"
13
14 #define BUFFSIZE 1024
15
16 int is_http(char *url)
17 {
18 if (strlen(url) < 8)
19 return 0;
20
21 if (!strncasecmp("http://",url,7))
22 return 1;
23
24 return 0;
25 }
26
27 int open_http(char *url)
28 {
29 int fd;
30 struct sockaddr_in server;
31 struct dvb_host *dvbhost;
32 struct addrinfo *s_addrinfo, addrhints;
33 char buffer[BUFFSIZE];
34 int i;
35
36 if(!is_http(url))
37 return -1;
38
39 dvbhost = parse(&(url[7]), "80");
40
41 bzero(&addrhints, sizeof(struct addrinfo));
42 addrhints.ai_family = PF_INET;
43 addrhints.ai_socktype = SOCK_STREAM;
44
45 if ((i = getaddrinfo(dvbhost->hostname, dvbhost->port, &addrhints, &s_addrinfo))) {
46 fprintf(stderr,"getaddrinfo: %s\n",gai_strerror(i));
47 return -1;
48 }
49
50 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
51 perror("socket");
52 return -1;
53 }
54
55 bzero(&server, sizeof(struct sockaddr_in));
56 server.sin_family = AF_INET;
57 server.sin_addr.s_addr = ((struct sockaddr_in*)(s_addrinfo->ai_addr))->sin_addr.s_addr;
58 server.sin_port = ((struct sockaddr_in*)(s_addrinfo->ai_addr))->sin_port;
59
60 if (connect(fd, (struct sockaddr*) &server, sizeof(server)) < 0) {
61 perror("connect");
62 return -1;
63 }
64
65 freeaddrinfo(s_addrinfo);
66
67
68 snprintf(buffer, BUFFSIZE-1, "GET /%s HTTP/1.0\n\n",dvbhost->location);
69 buffer[BUFFSIZE-1] = 0;
70 send(fd, buffer, strlen(buffer), 0);
71 i = 0;
72 while(i < 2) {
73 if (recv(fd, buffer, 1, 0) < 1) {
74 perror("recv");
75 exit(EXIT_FAILURE);
76 }
77 printf("%c",buffer[0]);
78 if (buffer[0] == 0x0a)
79 i++;
80 else
81 if (buffer[0] != 0x0d)
82 i = 0;
83 }
84
85 return fd;
86 }
Impressum, Datenschutz