-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHttpClient.cpp
211 lines (155 loc) · 5.08 KB
/
HttpClient.cpp
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
#include <HttpHandler.h>
#include <poll.h>
#include "HttpClient.h"
/* constructor */
/*************************************/
HttpClient::HttpClient(string dataDirectory)
{
this->dataDirectory = dataDirectory;
timeInterval = 30 * 1000;
}
HttpClient::~HttpClient()
{
//PortHandler::closeConnection(socketfd);
}
/* interface methods */
/*************************************/
int
HttpClient::connectionInit(char *server_address, int port_no)
{
socketfd = 0;
struct sockaddr_in serv_addr;
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port_no);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, server_address, &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(socketfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
return 0; // successful
}
int
HttpClient::sendGETRequests(vector<Request> requests)
{
// send GET msg
for(Request requestObj : requests) {
string r = requestObj.toString();
int status = PortHandler::write(socketfd , (char* )r.c_str() , r.size());
if(status == -1){
return -1;
}
}
struct pollfd pollFd;
pollFd.fd = socketfd;
pollFd.events = POLLIN;
int file_counter = 0 ;
while(file_counter < requests.size()) {
int activity = poll(&pollFd, 1, timeInterval);
if ((activity <= 0) && (errno!=EINTR))
{
return activity;
}
vector<char> total(MAX_RES_SZ, 0);
// receive reponse
int valread = PortHandler::tryRead(socketfd, total, MAX_RES_SZ);
if (valread == -1) {
return -1;
}
if(valread == 0){
cout << "Stuck at receive\n";
}
string s = string(total.begin(), total.begin() + ((valread < MAX_RES_SZ) ? valread : MAX_RES_SZ));
// save data to directory
Response *responseObj = Parser::createResponse(s);
const char *data = 0;
//connection is dead
if (responseObj == NULL) {
return -1;
}
int len;
vector<char> rest;
string realS;
if (responseObj->getStatus() == 200) // file found
{
//IMPORTANT DON't DELETE IT
len = stoi(responseObj->getKey_val("Content-Length")) + responseObj->getHeadersSize();
PortHandler::readExact(socketfd, rest, len);
realS = string(rest.begin(), rest.end());
Response *realResponse = Parser::createResponse(realS);
string body = realResponse->getBody();
data = body.c_str();
IOHandler::writeData(Client, requests[file_counter].getFileName(), (char *) data, body.size());
// for testing....
cout << realResponse->toString() << endl;
delete realResponse;
} else if(responseObj->getStatus() == 404){
len = responseObj->getHeadersSize();
PortHandler::readExact(socketfd, rest, len);
realS = string(rest.begin(), rest.end());
Response *realResponse = Parser::createResponse(realS);
cout << realResponse->toString() << endl;
delete realResponse;
}
file_counter++;
delete responseObj;
}
return 0;
}
int
HttpClient::sendPOSTRequest(Request requestObj)
{
// read file
int sz = IOHandler::getFileSize(Client ,requestObj.getFileName());
vector<char> buffer(sz, 0);
int status = IOHandler::readData(Client , requestObj.getFileName(), &buffer[0], sz);
if(status == -1){
return -1;
}
string r = requestObj.toString();
// send POST request;
status = PortHandler::writeExact(socketfd , (char* )r.c_str() , r.size());
if(status < 0){
return -1;
}
struct pollfd pollFd;
pollFd.fd = socketfd;
pollFd.events = POLLIN;
int activity = poll(&pollFd, 1, timeInterval );
if ((activity <= 0) && (errno!=EINTR))
{
return -1 ;
}
// receive reponse
vector<char> retBuffer(MAX_RES_SZ, 0);
int valread = PortHandler::read(socketfd , retBuffer, MAX_RES_SZ);
if(valread < 0){
return -1;
}
r = string(retBuffer.begin(),retBuffer.begin() + ((valread < MAX_RES_SZ) ? valread : MAX_RES_SZ));
// make sure its OK to send data
Response *responseObj = Parser::createResponse(r);
cout << responseObj->toString() << endl;
if(responseObj->getStatus() == 200) // ready to receive file
{
PortHandler::writeExact(socketfd, buffer.data(), sz);
delete responseObj;
return 0;
}
delete responseObj;
return -1;
}
int HttpClient::closeConnection() {
return PortHandler::closeConnection(socketfd);
}