5c2e3e44 |
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> |
5c2e3e44 |
9 | |
10 | #include "common.h" |
11 | #include "http.h" |
12 | |
13 | #define BUFFSIZE 1024 |
14 | |
15 | int is_http(char *url) |
16 | { |
17 | if (strlen(url) < 8) |
18 | return 0; |
19 | |
20 | if (!strncasecmp("http://",url,7)) |
21 | return 1; |
22 | |
23 | return 0; |
24 | } |
25 | |
26 | int open_http(char *url) |
27 | { |
28 | int fd; |
29 | struct sockaddr_in server; |
fbc24ab0 |
30 | static struct dvb_host *dvbhost = NULL; |
5c2e3e44 |
31 | char buffer[BUFFSIZE]; |
32 | int i; |
33 | |
34 | if(!is_http(url)) |
35 | return -1; |
36 | |
fbc24ab0 |
37 | if (!dvbhost) { |
38 | dvbhost = parse(&(url[7]), "80"); |
39 | dvbhost->socktype = SOCK_STREAM; |
40 | } |
5c2e3e44 |
41 | |
fbc24ab0 |
42 | if (resolve(dvbhost, &server) < 0) { |
5c2e3e44 |
43 | return -1; |
44 | } |
45 | |
46 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { |
47 | perror("socket"); |
48 | return -1; |
49 | } |
50 | |
5c2e3e44 |
51 | if (connect(fd, (struct sockaddr*) &server, sizeof(server)) < 0) { |
52 | perror("connect"); |
53 | return -1; |
54 | } |
55 | |
5c2e3e44 |
56 | snprintf(buffer, BUFFSIZE-1, "GET /%s HTTP/1.0\n\n",dvbhost->location); |
57 | buffer[BUFFSIZE-1] = 0; |
58 | send(fd, buffer, strlen(buffer), 0); |
59 | i = 0; |
60 | while(i < 2) { |
61 | if (recv(fd, buffer, 1, 0) < 1) { |
62 | perror("recv"); |
63 | exit(EXIT_FAILURE); |
64 | } |
65 | printf("%c",buffer[0]); |
66 | if (buffer[0] == 0x0a) |
67 | i++; |
68 | else |
69 | if (buffer[0] != 0x0d) |
70 | i = 0; |
71 | } |
72 | |
73 | return fd; |
74 | } |