-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsenddata.cpp
More file actions
289 lines (241 loc) · 7.38 KB
/
senddata.cpp
File metadata and controls
289 lines (241 loc) · 7.38 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
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
#include<unistd.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fstream>
#include <time.h>
#include <pthread.h>
#include <sstream>
#include <dirent.h>
#include <vector>
#include<algorithm>
#include "parse.h"
#include "senddata.h"
#include<time.h>
#include<math.h>
#include<dirent.h>
using namespace std;
bool logging = true ;
string l_file ="logfile.txt";
bool r_daemon = false ;
void sendHeadData(clientInfo c)
{
char currtime[50];
char lastmodify[50];
struct stat buf;
//bool logging = true;
time_t tim=time(NULL);
tm *now=gmtime(&tim);
if (strftime(currtime, 50,"%c", now) == 0)
perror("Date Error");
char * fname = new char[c.r_filename.size() + 1];
std::copy(c.r_filename.begin(), c.r_filename.end(), fname);
fname[c.r_filename.size()] = '\0';
stat(fname,&buf);
strcpy(lastmodify,ctime(&buf.st_mtime));
delete[] fname;
fname = NULL;
int pos = c.r_filename.find_last_of(".");
string c_type = c.r_filename.substr(pos+1,c.r_filename.size());
if (c_type == "txt" || c_type == "html" || c_type == "htm")
c.r_ctype ="text/html";
else if(c_type == "gif" || c_type == "jpeg")
c.r_ctype = "image/"+c_type;
else
c.r_ctype = " ";
std::stringstream ss;
ss << c.r_filesize;
string filesize = ss.str();
string header = c.r_type + " "+ c.r_method +" 200 OK\r\nDate: ";
string time(currtime);
header = header + time;
header = header +"\r\n" +"Server: myhttpd 1.0\r\n" + "Last-Modified:";
string lmodify(lastmodify);
header = header + lmodify ;//+"\r\n";
// cout<<"thread_id of current thread is: ";
// cout<<pthread_self()<<endl;
header = header + "Content-Type:" + c.r_ctype +"\r\n" + "Content-Length:" + filesize +"\r\n\r\n";
if (send(c.r_acceptid, header.c_str(), strlen(header.c_str()), 0) == -1)
perror("send");
c.status_code = 200;
}
void sendGetData(clientInfo c)
{
char currtime[50];
char lastmodify[50];
struct stat buf;
time_t tim=time(NULL);
tm *now=gmtime(&tim);
if (strftime(currtime, 50,"%c", now) == 0)
perror("Date Error");
char * fname = new char[c.r_filename.size() + 1];
std::copy(c.r_filename.begin(), c.r_filename.end(), fname);
fname[c.r_filename.size()] = '\0';
stat(fname,&buf);
strcpy(lastmodify,ctime(&buf.st_mtime));
delete[] fname;
fname = NULL;
int pos = c.r_filename.find_last_of(".");
string c_type = c.r_filename.substr(pos+1,c.r_filename.size());
if (c_type == "txt" || c_type == "html" || c_type == "htm")
c.r_ctype ="text/html";
else if(c_type == "gif" || c_type == "jpeg")
c.r_ctype = "image/"+c_type;
else
c.r_ctype = " ";
std::stringstream ss;
ss << c.r_filesize;
string filesize = ss.str();
string header = c.r_type + " "+ c.r_method +" 200 OK\r\nDate: ";
string time(currtime);
header = header + time;
header = header +"\r\n" +"Server: myhttpd 1.0\r\n" + "Last-Modified:";
string lmodify(lastmodify);
header = header + lmodify ;//+"\r\n";
header = header + "Content-Type:" + c.r_ctype +"\r\n" + "Content-Length:" + filesize +"\r\n\r\n";
// cout<<"thread_id of current thread is: ";
// cout<<pthread_self()<<endl;
if (send(c.r_acceptid, header.c_str(), strlen(header.c_str()), 0) == -1)
perror("send");
ifstream file;
char *readblock;
size_t size;
file.open(c.r_filename.c_str());
if (file.is_open()) {
string read;
file.seekg (0, ios::end);
size = file.tellg();
readblock = new char [size];
file.seekg (0, ios::beg);
file.read(readblock, size);
} else {
cout<<"Never went Inside"<<endl;
}
if (send(c.r_acceptid, readblock, size, 0) == -1)
perror("send");
file.close();
c.status_code = 200;
delete [] readblock;
}
void sendGetData_dir(clientInfo c)
{
cout << "hey ========" << c.r_filename << endl;
char currtime[50];
char lastmodify[50];
struct stat buf;
time_t tim=time(NULL);
tm *now=gmtime(&tim);
if (strftime(currtime, 50,"%c", now) == 0)
perror("Date Error");
char * fname = new char[c.r_filename.size() + 1];
std::copy(c.r_filename.begin(), c.r_filename.end(), fname);
fname[c.r_filename.size()] = '\0';
stat(fname,&buf);
strcpy(lastmodify,ctime(&buf.st_mtime));
delete[] fname;
fname = NULL;
c.r_ctype ="text/html";
string result = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body><h1>Directory listing for ";
result = result + c.r_filename + "</h1><hr><ul>" ;
DIR *dir;
struct dirent *ent;
if ((dir = opendir (c.r_filename.c_str())) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
string temp(ent->d_name) ;
if(temp[0] == '.') continue ;
if(temp == ".." || temp == ".") continue ;
if(c.file_root[c.file_root.size()-1] != '/')c.file_root += "/" ;
result = result + "<li><a href=\""+c.file_root+temp+ "\">"+temp+"</a></li>";
// printf ("%s\n", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("error in accessing directory data\n");
}
result = result + "</ul><hr></body></html>" ;
string filesize = to_string(result.size());
string header = c.r_type + " "+ c.r_method +" 200 OK\r\nDate: ";
string time(currtime);
header = header + time;
header = header +"\r\n" +"Server: myhttpd 1.0\r\n" + "Last-Modified:";
string lmodify(lastmodify);
header = header + lmodify ;//+"\r\n";
header = header + "Content-Type:" + c.r_ctype +"\r\n" + "Content-Length:" + filesize +"\r\n\r\n";
// cout<<"thread_id of current thread is: ";
// cout<<pthread_self()<<endl;
if (send(c.r_acceptid, header.c_str(), strlen(header.c_str()), 0) == -1)
perror("send");
if (send(c.r_acceptid, result.c_str(), result.size(), 0) == -1)
perror("send");
c.status_code = 200;
// delete [] readblock;
}
void SendData::transfer_data(clientInfo c) // Sending data
{
if(c.status_file ==true && c.r_type =="GET")
{
sendGetData(c);
}
else if(c.status_dir ==true && c.r_type =="GET")
{
sendGetData_dir(c);
}
else if(c.status_file ==true && c.r_type =="HEAD")
{
sendHeadData(c);
}
else
{
if (c.rootcheck == true)
{
write(c.r_acceptid,"Error 404: File Not Found",25);
c.status_code = 404;
}
else
{
c.status_code = 404;
}
//listingDir(c);
}
if(logging) // If request need to be logged
generatingLog(c);
displaylog(c);
close(c.r_acceptid);
}
void SendData::generatingLog(clientInfo c)
{
ofstream logfile;
string file = l_file;
logfile.open(file.c_str(), std::ios::app);
if( !logfile ) {
// file couldn't be opened
cout << "Error: file could not be opened" << endl;
}
logfile<<c.r_ip<<" ["<<c.r_time<<"] ["<<c.r_servetime<<"] "<<c.r_firstline<<" "<<c.status_code<<" "<<c.r_filesize<<endl;
logfile.close();
}
void SendData::displaylog(clientInfo c)
{
time_t tim=time(NULL);
tm *now=gmtime(&tim);
char currtime[50];
if (strftime(currtime, 50,"%x:%X", now) == 0)
perror("Date Panga");
string servetime(currtime);
c.r_servetime = servetime;
if(!r_daemon)
{
usleep(0);
cout<<c.r_firstline<<" "<<"thread_id:"<<pthread_self()<< " time : "<< clock()<<endl;
}
else
cout<<c.r_ip<<" ["<<c.r_time<<"] ["<<c.r_servetime<<"] "<<c.r_firstline<<" "<<c.status_code<<" "<<c.r_filesize<<" "<<"thread_id:"<<pthread_self()<<endl;
}