-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
393 lines (342 loc) · 9.02 KB
/
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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
Client.c
Author:Roi Cohen
ID:301063814
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <limits.h>
#include <ctype.h>
#define TRUE 0
#define FALSE 1
#define FAILURE -1
#define TIMESIZE 128
#define CHARSIZE 4
#define REQUEST_SIZE 2048
#define RESPONSE_SIZE 2048
#define BUFFER_SIZE 1024
#define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT"
typedef enum {false, true} bool;
/*Deceleration*/
void timehandle(char**,int*,char*,int*,char*,int*,char*,int*);
void fixTime(char***,char**,int**);
void urlhandle(char**,char **,char **,char **,char **,char*,int*);
bool check(int);
void check2(int,int,int,int,int);
void createConnection(char** ,char*,char*,char*,int,int,int,int);
int cToi(const char *s);
void printUsage();
int main(int argc, char* argv[]){
/*Variables->>if not modify the date so d,m,h=1*/
char * ch_port;
char * ch_host;
char * ch_path;
char * ch_protocol;
char *url;
char timebuf[TIMESIZE];
char d[CHARSIZE]="0";
char h[CHARSIZE]="0";
char m[CHARSIZE]="0";
int h_flag=FAILURE;
int d_flag=FAILURE;
int time_flag=FAILURE;
int url_flag=FAILURE;
int port;
int day=0;
int hour=0;
int minute=0;
int i=1;
int counter_d=0;
int counter_h=0;
int counter_t=0;
int counter_u=0;
time_t currtime;
char const colon[CHARSIZE]=":";
char const *http="http://";
/*-while loop to get place and insert to flags*/
while(i<argc && (check(argc)==TRUE))
{
if(!(strcmp(argv[i],"-h"))){
h_flag=i;
counter_h++;
}
if((!(strcmp(argv[i],"-d"))))
{
d_flag=i;
counter_d++;
if(argv[i+1]!=NULL){
time_flag=d_flag+1;
counter_t++;
}
if(argv[i+1]==NULL){
printUsage();
exit(FAILURE);
}
}
if(((strstr(argv[i],http)))){
url_flag=i;
counter_u++;
}
i++;
}
/*Check input*/
check2(argc,counter_d,counter_u,counter_h,counter_t);
if((counter_d > 1) || (counter_u > 1) || (counter_h > 1) || (counter_t > 1)){
printUsage();
exit(FAILURE);
}
int tmp=counter_d + counter_u + counter_h + counter_t;
if(tmp!=(argc-1)){
printf("wrong input\n");
exit(FAILURE);
}
/*-----------------------URL handle-----------------------*/
if(url_flag!=FAILURE && (check(argc)==TRUE)){
url=argv[url_flag];
urlhandle(&url,&ch_host,&ch_port,&ch_protocol,&ch_path,":",&port);
}
else
{
printUsage();
exit(FAILURE);
}
/*-----------------------Time handle-----------------------
add this line just if the user insert time--------------*/
if(time_flag!=FAILURE && (check(argc)==TRUE))
{
char *tmp;
int counter=0;
tmp=argv[time_flag];
while(*tmp){
if(*tmp==':')
counter++;
tmp++;
}
if(counter==2){
char *ch=strtok(argv[time_flag],colon);
timehandle(&ch,&time_flag,d,&day,h,&hour,m,&minute);
}
else{
printf("wrong input\n");
exit(FAILURE);
}
}
currtime= time(NULL);
currtime-=(day*24*3600+hour*3600+minute*60);
strftime(timebuf,TIMESIZE, RFC1123FMT, gmtime(&currtime));
// myPrint(d,h,m,ch_protocol,ch_host,ch_path,timebuf,port,time_flag,h_flag);//DEBUGG
createConnection(argv,ch_host,ch_path,timebuf,port,h_flag,url_flag,time_flag);
return 0;
}
/*--------------------create Connection method------------------------*/
void createConnection(char ** argv,char* host,char* path,char* time,int port,int h_flag,int url_flag,int time_flag){
int sd;
struct sockaddr_in cli;
int cli_len = sizeof(cli);
struct hostent *hp;
char req[REQUEST_SIZE];
int received_response_bytes;
memset(&cli, 0, cli_len);
/*get the host from argv then use the method gethostbyname*/
if(!(hp=gethostbyname(host))){
printf("wrong input\n");
exit(FAILURE);
}
/*update port on sockaddr_in(cli) then do DNS finally use the Internet add family*/
bzero((char *) &cli,cli_len);
cli.sin_port =htons(port);
cli.sin_family=AF_INET;
cli.sin_addr.s_addr =((struct in_addr*)(hp->h_addr))->s_addr;
/*Create Socket*/
if ((sd = socket(PF_INET,SOCK_STREAM, 0)) < 0){
perror(" Socket");
exit(FAILURE);
}
/*Connect to socket*/
if((connect(sd,(struct sockaddr*)&cli,cli_len))<0){
perror("connect");
exit(FAILURE);
}
char buff[BUFFER_SIZE];
int resp_len=RESPONSE_SIZE;
int bytes_r=0;
memset(&buff, 0, sizeof(buff));
memset(&req, 0, sizeof(req));
/*create the response */
char* response = (char*)calloc(resp_len, sizeof(char));
if(response == NULL) {
perror("calloc");
exit(FAILURE);
}
/*if the user insert head*/
if(h_flag!=FAILURE)
strcat(req,"HEAD ");
else
strcat(req,"GET ");
/*add the http:// , the host and the version HTTP/1.0 to the head of the request*/
strcat(req,"http://");
strcat(req,host);
/*add path to the request,dont forget to put / before*/
if(path!=NULL){
strcat(req,"/");
strcat(req,path);
}
strcat(req, " HTTP/1.0\r\n\n");
/*add time to the request if the user inesrt -d [<time interval>]*/
if (time_flag!=FAILURE){
strcat(req, "If-Modified-Since: ");
strcat(req,time);
strcat(req, "\r\n\n");
}
printf("HTTP request =\n%s\nLEN = %d\n", req, (int) strlen(req));
/*Write to Socket*/
if((write(sd,req, sizeof(req))) < 0){
perror("write");
exit(FAILURE);
}
/*read from buffer*/
while((received_response_bytes = read(sd,buff,sizeof(buff))) > 0)
{
if(received_response_bytes< 0) {
perror("read");
exit(FAILURE);
}
printf("%s",buff);
bytes_r+=received_response_bytes;
if(received_response_bytes>= (resp_len-bytes_r))
{
response = realloc(response, (resp_len *= 2));
if(response==NULL){
perror("realloc");
exit(FAILURE);
}
}
}
printf("\nTotal received response bytes:%d\n",bytes_r);
}
/*----------------<<<<<END----->createConnection method>>>>>-------------*/
/*-----------------------timehandle method---------------------
take the ch** that is the time with colon and then
first check if I got time and the ch differnt from NULL
then use strcpy each time ,cToi method and strtok method
each time I take the *ch insert to d/m/y then convert to int(for the connect)
then cut it with strtok.
-----------------------timehandle method---------------------*/
void timehandle(char** ch,int* time_flag,char* d,int* day,char* h,int* hour,char* m,int* minute){
int i=0;
while (*time_flag!=FAILURE && ch != NULL){
if(i==0) /*DAY*/
if(*ch!=NULL){
fixTime(&ch,&d,&day);
}
if(i==1) /*HOUR*/
if(*ch!=NULL)
fixTime(&ch,&h,&hour);
if(i==2){ /*MINUTE*/
if(*ch!=NULL)
{
strcpy(m,*ch);
if(!(isdigit(*m))){
printUsage();
exit(FAILURE);
}
*minute=atoi(m);
break;
}
else{
printUsage();
exit(FAILURE);
}
break;
}
i++;
}
}
/*------------------fix Time methood----------------------
Checks id the time isnt char----------------------------*/
void fixTime(char*** ch,char** t,int** it){
strcpy(*t,**ch);
if(!(isdigit(**t))){
printUsage();
exit(FAILURE);
}
**it=cToi(*t);
**ch = strtok(NULL,":");
}
/*-------------<<<<<<<END--->timehandle method>>>>>>---------------*/
/*--------------------urlhandle method--------------------
First we take to protocol till colon found
Then take from host // till /
In ch_port insert from colon till end of host string,
If port not insert then update just port(int)
Then insert to ch_path all the rest
Take off the port from the host
Finally Delete : from port
--------------------urlhandle method--------------------*/
void urlhandle(char** url,char ** ch_host,char ** ch_port,char ** ch_protocol,char ** ch_path,char* colon,int *port)
{
*ch_protocol=strtok(*url,colon);
*ch_host=strtok(NULL,"/");
if(*ch_host==NULL){ //insert just http://
printf("wrong input\n");
exit(FAILURE);
}
if((strchr(*ch_host,':'))){ //if port is inserted
*ch_port=strchr(*ch_host,':');
*port=cToi(*ch_port);
}
else
*port=80;
*ch_path=strtok(NULL," ");
*ch_host=strtok(*ch_host,colon);
*ch_port+=1;
}
/*------------<<<<<<<<END--->urlhandle method>>>>>>>-------------*/
/*----------------------my check methods-----------------
Checks if the user insert correct input
----------------------my check method-----------------*/
bool check(int argc){
if(argc>=2 && argc<=5)
return TRUE;
return FALSE;
}
void check2(int argc,int counter_d,int counter_u,int counter_h,int counter_t){
if((counter_d > 1) || (counter_u > 1) || (counter_h > 1) || (counter_t > 1)){
printUsage();
exit(FAILURE);
}
int tmp=counter_d + counter_u + counter_h + counter_t;
if(tmp!=(argc-1)){
printf("wrong input\n");
exit(FAILURE);
}
}
/*------------<<<<<END--->my check method>>>>>>>>>--------*/
/*----------------------my atoi method-----------------
When I need atoi method to convert from char* to int
I use this method to convert from String to int.
----------------------my atoi method-----------------*/
int cToi(const char *s)
{
int sign=1;
if(*s == '-')
sign = FAILURE;
s++;
int num=0;
while(*s){
num=((*s)-'0')+num*10;
s++;
}
return num*sign;
}
/*------------------print Usage method------------------*/
void printUsage() {
printf("Usage: client [-h] [-d <time-interval> ] <URL>\n");
}