-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-client.cpp
More file actions
126 lines (103 loc) · 3.36 KB
/
web-client.cpp
File metadata and controls
126 lines (103 loc) · 3.36 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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <fstream>
int main(int argc, char** arg)
{
for(int i = 1; i < argc; i++){
std::cout << "url " << i << std::endl;
char* iter = arg[i];
while(*iter){
*iter = tolower(*iter);
iter++;
}
char* domain;
char* port;
char* toget;
//http://adsadas:12323423/file
iter = strtok(arg[i], "://");
/*while(*iter){
iter++;
}
iter++;
while(*iter == '/'){
iter++;
}*/
domain = strtok(0, "://");
port = strtok(0, "/\0");
char tmp[] = "127.0.0.1";
if(!strcmp(domain, "localhost")){
domain = tmp;
}
//std::cout << *domain << std::endl;
toget = strtok(0, "\0");
std::cout << domain << std::endl << (port?port:"80") << std::endl << (toget?toget:"") << std::endl;
if(!strcmp(arg[i], "http")){
bool done = false;
char buf[1024] = {0};
while(!done){
int socketfd = socket(AF_INET, SOCK_STREAM, 0);
if(socketfd == -1){
std::cout << "Err Sock" << std::cout;
throw(1);
}
struct sockaddr_in adr = {0};
adr.sin_family = AF_INET;
adr.sin_addr.s_addr = inet_addr(domain);
adr.sin_port = htons(port?atoi(port):80);
if(connect(socketfd, (struct sockaddr*) &adr, sizeof(adr)) == -1){
std::cout << "Err connect" << std::endl;
throw(2);
}
std::cout << "connected" << std::endl;
std::string req = "GET /";
if(toget){
req += toget;
}
req += " http/1.0\r\n";
char* iter;
std::fstream savefile(toget, std::fstream::out);
send(socketfd, req.c_str(), req.length(), 0);
recv(socketfd, buf, sizeof(buf), 0);
char* body = strstr(buf, "\r\n\r\n");
iter = buf;
while(iter != body&&*iter){
*iter = tolower(*iter);
iter++;
}
strtok(buf, " ");
iter = strtok(0, " ");
int code = atoi(iter);
body = body + 4;
while(*iter){
iter++;
}
iter++;
std::cout << code << std::endl;
if(code == 301){
char* ptr = strstr(iter, "location");
ptr = strtok(ptr, ": \r\n");
toget = strtok(0, ": \r\n");
std::cout << ++toget << std::endl;
//sleep(1);
}else{
done = true;
std::cout << "Opening file " << toget << std::endl;
std::cout << body << std::endl;
savefile << body;
savefile.close();
}
shutdown(socketfd, 2);
}
}
}
//while(1);
return 0;
}