-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpClient-WebPage
More file actions
214 lines (183 loc) · 5.66 KB
/
httpClient-WebPage
File metadata and controls
214 lines (183 loc) · 5.66 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include"stdio.h"
#include"stdlib.h"
#include"sys/types.h"
#include"sys/socket.h"
#include"string.h"
#include"netinet/in.h"
#include"netdb.h"
#define BUF_SIZE 1024
int get_request(char * url, char * port);
int isValidIP(char * ip);
int parseHeader(char * header);
char * splitKeyValue(char * line, int index);
void openFile();
FILE * fileptr;
char keys[][25] = {"Date: ", "Hostname: ", "Location: ", "Content-Type: "};
char status[4] = {0, 0, 0, 0};
char contentFileType[100];
char path[1000];
int main(int argc, char**argv) {
struct sockaddr_in addr, cl_addr;
int sockfd, ret;
struct hostent * server;
char * url, * temp;
int portNumber;
char * fileName;
char status_ok[] = "OK";
char buffer[BUF_SIZE];
char http_not_found[] = "HTTP/1.0 404 Not Found";
char http_ok[] = "HTTP/1.0 200 OK";
char location[] = "Location: ";
char contentType[] = "Content-Type: ";
int sPos, ePos;
if (argc < 3) {
printf("usage: [URL] [port number]\n");
exit(1);
}
url = argv[1];
portNumber = atoi(argv[2]);
if ((temp = strstr(url, "http://")) != NULL) {
url = url + 7;
} else if ((temp = strstr(url, "https://")) != NULL) {
url = url + 8;
}
if (portNumber > 65536 || portNumber < 0) {
printf("Invalid Port Number!");
exit(1);
}
sockfd = get_request(url, argv[2]);
memset(&buffer, 0, sizeof(buffer));
ret = recv(sockfd, buffer, BUF_SIZE, 0);
if (ret < 0) {
printf("Error receiving HTTP status!\n");
} else {
printf("%s\n", buffer);
if ((temp = strstr(buffer, http_ok)) != NULL) {
send(sockfd, status_ok, strlen(status_ok), 0);
} else {
close(sockfd);
return 0;
}
}
memset(&buffer, 0, sizeof(buffer));
ret = recv(sockfd, buffer, BUF_SIZE, 0);
if (ret < 0) {
printf("Error receiving HTTP header!\n");
} else {
printf("%s\n", buffer);
if (parseHeader(buffer) == 0) {
send(sockfd, status_ok, strlen(status_ok), 0);
} else {
printf("Error in HTTP header!\n");
close(sockfd);
return 0;
}
}
fileptr = fopen(path, "w");
if (fileptr == NULL) {
printf("Error opening file!\n");
close(sockfd);
return 0;
}
memset(&buffer, 0, sizeof(buffer));
while (recv(sockfd, buffer, BUF_SIZE, 0) > 0) { //receives the file
if ((strstr(contentFileType, "text/html")) != NULL) {
fprintf(fileptr, "%s", buffer);
} else {
fwrite(&buffer, sizeof(buffer), 1, fileptr);
}
memset(&buffer, 0, sizeof(buffer));
}
fclose(fileptr);
close(sockfd);
openFile();
return 0;
}
int get_request(char * url, char * port) {
int sockfd, bindfd;
char * ptr, * host;
char getrequest[1024];
struct sockaddr_in addr;
if (isValidIP(url)) { //when an IP address is given
sprintf(getrequest, "GET / HTTP/1.0\nHOST: %s\n\n", url);
} else { //when a host name is given
if ((ptr = strstr(url, "/")) == NULL) {
sprintf(getrequest, "GET / HTTP/1.0\nHOST: %s\n\n", url);
} else {
strcpy(path, ptr);
host = strtok(url, "/");
sprintf(getrequest, "GET %s HTTP/1.0\nHOST: %s\n\n", path, url);
}
}
// creates a socket to the host
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Error creating socket!\n");
exit(1);
}
printf("Socket created...\n");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(url);
addr.sin_port = htons(atoi(port));
if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) < 0 ) {
printf("Connection Error!\n");
exit(1);
}
printf("Connection successful...\n\n\n");
ptr = strtok(path, "/");
strcpy(path, ptr);
write(sockfd, getrequest, strlen(getrequest));
return sockfd;
}
int isValidIP(char * ip) {
struct sockaddr_in addr;
int valid = inet_pton(AF_INET, ip, &(addr.sin_addr));
return valid != 0;
}
int parseHeader(char * header) {
char * line, * key, * value;
char temp[100];
int i = 0;
line = strtok(header, "\n");
while (line != NULL) {
strcpy(temp, line);
value = splitKeyValue(line, i);
if (i == 3) {
strcpy(contentFileType, value);
}
line = strtok(NULL, "\n");
i++;
}
for (i = 0; i < 4; i++) {
if (status[i] == 0) return 1;
}
return 0;
}
char * splitKeyValue(char * line, int index) {
char * temp;
if ((temp = strstr(line, keys[index])) != NULL) {
temp = temp + strlen(keys[index]);
status[index] = 1;
}
return temp;
}
void openFile() {
char * temp;
char command[100];
char fileName[1000];
strcpy(fileName, path);
if ((temp = strstr(contentFileType, "text/html")) != NULL) {
if ((temp = strstr(fileName, ".txt")) != NULL) {
sprintf(command, "gedit %s", fileName);
} else {
sprintf(command, "firefox %s", fileName);
}
system(command);
} else if ((temp = strstr(contentFileType, "application/pdf")) != NULL) {
sprintf(command, "acroread %s", fileName);
system(command);
} else {
printf("The filetype %s is not supported. Failed to open %s!\n", contentFileType, fileName);
}
}