-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
143 lines (134 loc) · 4.29 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#include <strings.h>
#include "parsing.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#define BUFLEN (1024*1024)
int main(int argc, char *argv[]) {
char writeBuf[BUFLEN] ;
char readBuf[BUFLEN];
char name[BUFLEN];
struct parg input;
analyzeparameter(argc, argv, &input);
struct sockaddr_in serv_addr;
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
error("ERROR opening socket");
exit(1);
}
struct hostent *server_addr = gethostbyname(input.hostname);
if (server_addr == NULL) {
error("ERROR, no such host");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server_addr->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server_addr->h_length);
serv_addr.sin_port = htons(input.portnum);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
error("ERROR connecting");
exit(1);
}
printf("connected\n");
bzero(writeBuf, BUFLEN);
// sprintf(writeBuf, "GET /\r\n");
sprintf(writeBuf, "GET /index.html HTTP/1.1\r\n"
// images/favicon.ico
"Host: %s\r\n"
"Accept: */*\r\n"
// "Cookie: 431425562842515b4dd25f3-660d-42d5-96ba-c7c9d738c6e4"
"Connection: Keep-Alive\r\n"
"\r\n", input.hostname);
send(sockfd, writeBuf, strlen(writeBuf), 0);
FILE* file = fopen("index.html", "a+");
bzero(readBuf, BUFLEN);
while (recv(sockfd, readBuf, BUFLEN, 0) > 0) {
fprintf(file, "%s", readBuf);
bzero(readBuf, BUFLEN);
}
// return 0;
file = fopen("index.html", "r");
while(!feof(file)) {
bzero(name, BUFLEN);
fgets(readBuf, BUFLEN, file);
char *p, *q;
if (strstr(readBuf, "src")) {
p = strstr(readBuf, "src");
p = strstr(p, "\"") + 1;
q = strstr(p, "\"");
bzero(name, BUFLEN);
int i = 0;
while (p != q) {
name[i] = *p;
p++;
i++;
}
printf("name: %s\n", name);
} else if (strstr(readBuf, "SRC")) {
p = strstr(readBuf, "SRC");
p = strstr(p, "\"") + 1;
q = strstr(p, "\"");
bzero(name, BUFLEN);
int i = 0;
while (p != q) {
name[i] = *p;
p++;
i++;
}
printf("name: %s\n", name);
} else if (strstr(readBuf, "href")) {
p = strstr(readBuf, "href");
p = strstr(p, "\"") + 1;
q = strstr(p, "\"");
bzero(name, BUFLEN);
int i = 0;
while (p != q) {
name[i] = *p;
p++;
i++;
}
printf("name: %s\n", name);
} else if (strstr(readBuf, "HREF")) {
p = strstr(readBuf, "HREF");
p = strstr(p, "\"") + 1;
q = strstr(p, "\"");
bzero(name, BUFLEN);
int i = 0;
while (p != q) {
name[i] = *p;
p++;
i++;
}
printf("name: %s\n", name);
}
if (name[0] && (!(name[0] == 'h' && name[1] == 't') || name[0]!= '#')) {
printf("name: %s\n", name);
bzero(writeBuf, BUFLEN);
// sprintf(writeBuf, "GET /\r\n");
sprintf(writeBuf, "GET /%s HTTP/1.1\r\n"
"Host: %s\r\n"
"Accept: */*\r\n"
//"Cookie: 431425562842515b4dd25f3-660d-42d5-96ba-c7c9d738c6e4"
"Connection: Keep-Alive\r\n"
"\r\n", name, input.hostname);
send(sockfd, writeBuf, strlen(writeBuf), 0);
FILE *file = fopen(name, "a+");
bzero(readBuf, BUFLEN);
while (recv(sockfd, readBuf, BUFLEN, 0) > 0) {
fprintf(file, "%s", readBuf);
bzero(readBuf, BUFLEN);
}
}
}
return 0;
}