-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.cc
302 lines (259 loc) · 8.18 KB
/
httpclient.cc
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// http://www.theinsanetechie.in/2014/02/a-simple-http-client-and-server-in-c.html
/**httpclient.c**/
#include "stdio.h"
#include "stdlib.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "string.h"
#include "netinet/in.h"
#include "netdb.h"
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <assert.h>
#include <time.h>
#include <sys/epoll.h>
#define BUF_SIZE 1024
int main_loop(char * url, char * port);
int isValidIP(char * ip);
int sockfd = -1;
char getrequest[1024];
int num_requests;
char path[1000];
bool use_epoll = true;
#define EPOLL_QUEUE_LEN 10
#define MAX_EPOLL_EVENTS_PER_RUN 10
struct epoll_event events[MAX_EPOLL_EVENTS_PER_RUN];
// timeout in ms
#define EPOLL_RUN_TIMEOUT (1000*10)
int epfd = -1;
struct epoll_event ev;
struct timespec ts_start, ts_stop;
void timer_start()
{
clock_gettime(CLOCK_REALTIME, &ts_start);
}
double timer_get_us()
{
clock_gettime(CLOCK_REALTIME, &ts_stop);
double result = (ts_stop.tv_sec - ts_start.tv_sec) * 1e6 + (ts_stop.tv_nsec - ts_start.tv_nsec) / 1e3;
return result;
}
/*extern "C"*/ pid_t gettid() __attribute((weak));
pid_t my_gettid() {
if (gettid)
return gettid();
else
return getpid();
}
#define debug(fmt, ...) if(1) { fprintf(stderr, "%s:%d tid=% 5d %s: " fmt, __FILE__, __LINE__, my_gettid(), __FUNCTION__, ##__VA_ARGS__); }
int main(int argc, char**argv) {
struct sockaddr_in addr, cl_addr;
int ret;
struct hostent * server;
char * url, * temp;
int portNumber;
if (argc < 3) {
printf("Usage: [URL] [port number] [num_requests]\n");
printf("Example: %s http://192.168.122.90/index_html 80 5\n");
exit(1);
}
num_requests = 10000;
url = argv[1];
portNumber = atoi(argv[2]);
switch (argc) {
case 4:
num_requests = atoi(argv[3]);
default:
break;
}
//checking the protocol specified
if ((temp = strstr(url, "http://")) != NULL) {
url = url + 7;
} else if ((temp = strstr(url, "https://")) != NULL) {
url = url + 8;
}
//checking the port number
if (portNumber > 65536 || portNumber < 0) {
printf("Invalid Port Number!");
exit(1);
}
ret = main_loop(url, argv[2]);
return 0;
}
int clnt_connect(char * url, char * port) {
char * ptr, * host;
struct sockaddr_in addr;
int ret;
if (isValidIP(url)) { //when an IP address is given
sprintf(getrequest, "GET / HTTP/1.1\nHOST: %s\n\n", url);
} else { //when a host name is given
if ((ptr = strstr(url, "/")) == NULL) {
//when hostname does not contain a slash
sprintf(getrequest, "GET / HTTP/1.1\nHOST: %s\n\n", url);
} else {
//when hostname contains a slash, it is a path to file
strcpy(path, ptr);
host = strtok(url, "/");
sprintf(getrequest, "GET %s HTTP/1.1\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);
if (use_epoll) {
epfd = epoll_create(EPOLL_QUEUE_LEN);
if (epfd == -1) {
printf("epoll_create Error!\n");
exit(1);
}
ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
ev.data.fd = sockfd;
if (0 != (ret = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev))) {
printf("epoll_ctl EPOLL_CTL_ADD Error!\n");
exit(1);
}
}
return 0;
}
int clnt_send_request() {
int ret;
ret = write(sockfd, getrequest, strlen(getrequest));
//debug(" INFO sent %d of %d bytes\n", ret, strlen(getrequest));
if (ret < strlen(getrequest)) {
printf("ERR clnt_send_request %d\n", ret);
return -1;
}
return 0;
}
int clnt_recv_respose() {
int ret;
char * url, * temp;
char buffer[BUF_SIZE];
char http_ok[] = "HTTP/1.1 200 OK\r\n";
char http_header_term[] = "\r\n\r\n";
char http_content_length[] = "\r\nContent-Length: ";
int buf_pos = 0;
bool response_done = false;
int header_length = 0;
int content_length = 0;
memset(&buffer, 0, sizeof(buffer));
while(response_done == false) {
if (use_epoll) {
int nfds = epoll_wait(epfd, events,
MAX_EPOLL_EVENTS_PER_RUN,
EPOLL_RUN_TIMEOUT);
if (nfds < 0) {
printf("Error in epoll_wait!");
exit(1);
}
// for each ready socket
for(int ii = 0; ii < nfds; ii++) {
int fd = events[ii].data.fd;
// TODO - recv and parse. But we have single conn only.
}
}
ret = recv(sockfd, buffer, BUF_SIZE, 0);
//debug(" INFO recv ret=%d, errno=%d\n", ret, errno); sleep(1);
if (ret < 0) {
printf("Error receiving HTTP status!\n");
}
else if (ret == 0) {
if (errno == 0) {
debug(" INFO recv ret=%d, errno=%d, server closed connection?\n", ret, errno);
break;
}
}
else {
//printf("%s\n", buffer);
// reply should start with "HTTP OK".
if (buf_pos == 0 &&
(temp = strstr(buffer, http_ok)) == NULL) {
debug(" ERROR reply is not HTTP 200 OK\n");
close(sockfd);
sockfd = -1;
return 1;
}
buf_pos += ret;
if (header_length == 0) {
if ((temp = strstr(buffer, http_header_term)) == NULL) {
debug(" ERROR reply is missing http_header_term\n");
close(sockfd);
sockfd = -1;
return 1;
}
header_length += temp + strlen(http_header_term) - buffer;
//debug(" INFO header_length=%d\n", header_length);
}
if (content_length == 0) {
if ((temp = strstr(buffer, http_content_length)) == NULL) {
debug(" ERROR reply is missing Content-Length\n");
close(sockfd);
sockfd = -1;
return 1;
}
temp += strlen(http_content_length);
content_length = atoi(temp);
//debug(" INFO content_length=%d\n", content_length);
}
if (content_length > 0) {
assert(header_length > 0);
//debug(" INFO header_length=%d content_length=%d, buf_pos=%d\n", header_length, content_length, buf_pos);
if(buf_pos == header_length+content_length) {
response_done = true;
}
}
}
}
return 0;
}
int main_loop(char * url, char * port) {
int ret;
double duration;
timer_start();
ret = clnt_connect(url, port);
if (ret) {
printf("ERR clnt_connect %d\n", ret);
return ret;
}
// writes the HTTP GET Request to the sockfd
int ii;
for (ii=0; ii<num_requests; ii++) {
ret = clnt_send_request();
if (ret) {
printf("ERR clnt_send_request %d\n", ret);
return ret;
}
ret = clnt_recv_respose();
if (ret) {
printf("ERR clnt_recv_respose %d\n", ret);
return ret;
}
}
duration = timer_get_us();
printf(" RATE % 6.0f req/s, DURATION % 4.3f s, NUM_REQUEST % 7d\n", num_requests*1e6/duration, duration/1e6, num_requests);
close(sockfd);
sockfd = -1;
}
int isValidIP(char * ip) {
struct sockaddr_in addr;
int valid = inet_pton(AF_INET, ip, &(addr.sin_addr));
return valid != 0;
}