-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParse.cpp
170 lines (130 loc) · 3.72 KB
/
Parse.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
#include <unistd.h>
#include <bits/stdc++.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "Parse.h"
#include "send.h"
using namespace std;
char buf[512];
// This function have the current client structure, this function will parse the request and fill the fields
void Parse::parseRequest(clientIdentity c_id)
{
getcwd(buf,256);
string rootdir(buf);
clientInfo c_info_obj;
char buffer[4096];
int recvbytes=0;
char *p,*pbuffer;
fcntl( c_id.acceptId, O_NONBLOCK, 0 );//manipulate file descriptor
//Fetching header as string
if((recvbytes=(recv(c_id.acceptId,buffer,sizeof(buffer),0)))==-1)// recv(newSockfd,buffer,bufferlen,0)
perror("Receive:");
buffer[recvbytes] = '\0';
string header(buffer);
//cout<<header<<endl;
int current = 0;
int end = header.find_first_of("\r\n",current);
if(end >=0){
c_info_obj.r_firstline = header.substr(current,end-current);
string first_line_data[3];//array of string
pbuffer=new char[c_info_obj.r_firstline.size()+1];
copy(c_info_obj.r_firstline.begin(),c_info_obj.r_firstline.end(),pbuffer);
pbuffer[c_info_obj.r_firstline.size()]='\0';
p=strtok(pbuffer," /");// split string at delimiter and return char* to first token
first_line_data[0].assign(p);
int i=1;
while(i<3){
p=strtok(NULL," ");
first_line_data[i].assign(p);
i++;
}
i=0;
// Setting values into c_info_obj from client
c_info_obj.r_acceptid = c_id.acceptId;
c_info_obj.r_portno = c_id.portNo;
c_info_obj.r_ip = c_id.ip;
c_info_obj.r_time = c_id.requestTime;
c_info_obj.r_type = first_line_data[0];// GET is assigned
c_info_obj.r_filename = first_line_data[1];
int k=c_info_obj.r_filename.find_first_of("/",0);
int z=c_info_obj.r_filename.find_first_of("/",k+1);
if(z >=0)
c_info_obj.rootcheck = false;
else
c_info_obj.rootcheck = true;
c_info_obj.r_filename = rootdir + c_info_obj.r_filename;
c_info_obj.r_method = first_line_data[2];
delete [] pbuffer; // only memory block is deleted not pointer
pbuffer = NULL;
checkRequest(c_info_obj);
}
else{
// write(newsockfd,buffer,no_of_bytes);
write(c_info_obj.r_acceptid,"Error: Bad Request, Retry",25);
close(c_info_obj.r_acceptid);
}
}
bool Parse::fileExists(char *filename)
{
struct stat filenamecheck;
if (stat(filename, &filenamecheck) != -1)
return true;
return false;
}
void Parse::checkRequest(clientInfo c_info_obj)
{
//printf("tyagi\n");
int pos=c_info_obj.r_filename.find_last_of("/");
int next=c_info_obj.r_filename.find_first_of(".",pos);
if(next>0){
char *fname=new char[c_info_obj.r_filename.size()+1];
copy(c_info_obj.r_filename.begin(),c_info_obj.r_filename.end(),fname);
fname[c_info_obj.r_filename.size()]='\0';
ifstream::pos_type size=0;
// Knowing file status and size
ifstream file;
file.open(fname);
if(file.is_open()){
file.seekg(0,ios::end);
size=file.tellg();
}
file.close();
if(fileExists(fname)&& c_info_obj.r_type=="GET"){
c_info_obj.status_file = true;
c_info_obj.r_filesize = (int)size;
delete [] fname;
fname = NULL;
sendData s;
s.transferData(c_info_obj);
}
else if(fileExists(fname) && c_info_obj.r_type=="HEAD"){
c_info_obj.status_file = true;
c_info_obj.r_filesize = 0;
delete [] fname;
fname = NULL;
sendData s;
s.transferData(c_info_obj);
}
else{
delete [] fname;
fname = NULL;
c_info_obj.r_filesize = 0;
c_info_obj.status_file = false;
sendData s;
s.transferData(c_info_obj);
}
}
else{
c_info_obj.r_filesize=0;
c_info_obj.status_file=false;
sendData s;
s.transferData(c_info_obj);
}
}