-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_file_client.c
168 lines (135 loc) · 4.25 KB
/
http_file_client.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
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int main(int argc, char *argv[]) {
// file url
char *f_url = argv[1];
// declarations
int i, j, https, status, content_length, client_socket;
size_t f_url_length;
char domain[BUFSIZ], path[BUFSIZ], filename[BUFSIZ], buffer[BUFSIZ], ip_address[128], request_buffer[BUFSIZ], response_buffer[BUFSIZ], *ptr;
char *request = "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n";
ssize_t bytes_sent, bytes_received, bytes_written;
// initializations
f_url_length = strlen(f_url);
https = 0;
bytes_written = 0;
// extract filename
for (i = (int) f_url_length - 1; i >= 0; i--) {
if (f_url[i] == '/') {
strcpy(filename, f_url + i + 1);
break;
}
}
// https check
if (f_url[4] == 's') {
https = 1;
}
// extract domain
for (j = 7 + https; j < f_url_length; j++) {
if (f_url[j] == '/') {
strncpy(domain, f_url + 7 + https, j - 7 - https);
break;
}
}
// extract path
strcpy(path, f_url + j + 1);
printf("INFO: \nHTTPS: %d, \nDomain: %s, \nFile Path: %s, \nFile Name: %s\n", https, domain, path, filename);
struct hostent *he;
struct in_addr **address_list;
he = gethostbyname(domain);
if (he == NULL) {
perror("ERROR");
exit(1);
}
address_list = (struct in_addr **) he->h_addr_list;
// extract ip_address address
for (i = 0; address_list[i] != NULL; i++) {
strcpy(ip_address, inet_ntoa(*address_list[i]));
break;
}
// create socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("ERROR");
exit(1);
}
puts("INFO: Socket created successfully.");
// server address
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(ip_address);
server_address.sin_port = htons(80);
// connect to server
status = connect(client_socket, (struct sockaddr *) &server_address, sizeof(server_address));
if (status != 0) {
perror("ERROR");
exit(1);
}
puts("INFO: Connected to server.");
// add request_buffer to request_buffer buffer
snprintf(request_buffer, sizeof(request_buffer), request, path, domain);
// send request to server
bytes_sent = send(client_socket, request_buffer, strlen(request_buffer), 0);
if (bytes_sent < 0) {
perror("ERROR");
exit(1);
}
printf("INFO: Request send (%lu bytes):\n%s", strlen(request_buffer), request_buffer);
// remove file from current directory
remove(filename);
FILE *fd = fopen(filename, "wb");
// save response to response buffer
ptr = response_buffer + 4;
bytes_received = recv(client_socket, ptr, 1, 0);
while (bytes_received) {
if (bytes_received == -1) {
perror("ERROR");
exit(1);
}
if ((ptr[-3] == '\r') && (ptr[-2] == '\n') && (ptr[-1] == '\r') && (*ptr == '\n')) {
break;
}
ptr++;
bytes_received = recv(client_socket, ptr, 1, 0);
}
// reset pointer
*ptr = 0;
ptr = response_buffer + 4;
// find content length
ptr = strstr(ptr, "Content-Length:");
if (ptr) {
sscanf(ptr, "%*s %d", &content_length);
} else {
content_length = -1;
}
printf("INFO: Incoming content length: %d bytes.\n", content_length);
// receive file
bytes_received = recv(client_socket, buffer, 1024, 0);
while (bytes_received) {
if (bytes_received == -1) {
perror("ERROR");
exit(1);
}
// write to disk
fwrite(buffer, 1, bytes_received, fd);
bytes_written += bytes_received;
// all downloaded
if (bytes_written == content_length) {
break;
}
// receive next segment of file
bytes_received = recv(client_socket, buffer, 1024, 0);
}
printf("INFO: Finished downloading file: %s.\n", filename);
puts("Done.");
// close descriptors
fclose(fd);
close(client_socket);
return 0;
}