-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
212 lines (184 loc) · 5.1 KB
/
Copy pathserver.c
File metadata and controls
212 lines (184 loc) · 5.1 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
// CHEN Yi pu
// 19084858d
// COMP2322 Project
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
// Not sure whether the following libraries are needed
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
/* For server socket:
1. Create a socket
2. Bind to a access
3. Listen to a port
4. Accept the connection from a client
5. Send and receive data
6. Close
*/
void responseMessage(int newSocket2, char request[2048], char Header[5], char fileName[1024], char
version[1024], char display[1024], char hostName[1024]){
char data_to_send[1024], path[1024];
char *directory;
int fd, bytes_read;
FILE *eventLog;
eventLog = fopen("Client request.txt", "a");
directory = getenv("PWD");
//printf("%s\n", directory);
strcpy(path, directory);
strcpy(&path[strlen(directory)], fileName);
printf("file: %s\n", path);
printf("\n");
//printf("%s\n", request);
printf("%s\n", request);
if ( strncmp(Header, "GET", 3) == 0 ){
if ( strncmp( version, "HTTP/1.0", 8) != 0 && strncmp( version, "HTTP/1.1", 8) != 0 ){
write(newSocket2, "HTTP/1.0 400 Bad Request\n", 25);
//eventLog = fopen("Client request.txt", "w");
fprintf(eventLog, "%s/127.0.0.1 %s 400 Bad Request\n", hostName, version);
//fclose (eventLog);
}
else{
if ( (fd = open(path, O_RDONLY)) != -1 ){
send(newSocket2, "HTTP/1.0 200 OK\n\n", 17, 0);
//eventLog = fopen("Client request.txt", "w");
time_t currentTime;
time(¤tTime);
char time[100] = {0};
for(int i = 0; i < 24; i++){
time[i] = ctime(¤tTime)[i];
}
struct stat attrib;
stat(path, &attrib);
char modify[50];
strftime(modify, 50, "%Y-%m-%d %H:%M:%S", localtime(&attrib.st_mtime));
fprintf(eventLog, "%s/127.0.0.1 ", hostName);
fprintf(eventLog, "%s Last modified at: %s %s 200 OK\n", time, modify, display);
fclose (eventLog);
modify[0] = 0;
while ( (bytes_read=read(fd, data_to_send, 1024))>0 ){
write (newSocket2, data_to_send, bytes_read);
}
}
else{
write(newSocket2, "HTTP/1.0 404 Not Found\n", 23); //FILE NOT FOUND
//eventLog = fopen("Client request.txt", "w");
fprintf(eventLog, "%s/127.0.0.1 %s 404 Not Found\n", hostName, display);
//fclose (eventLog);
}
}
}
//Closing SOCKET
fclose (eventLog);
shutdown (newSocket2, SHUT_RDWR);
close(newSocket2);
}
int main(int argc, char *argv[]){
int newSocket;
int newSocket2;
int portNum;
struct sockaddr_in server, client;
int addrlen = sizeof(server);
// Step 1: Create a socket
// socket(int domain, int type, int protocol)
newSocket = socket(AF_INET, SOCK_STREAM, 0);
if(argc < 2){
printf("Error! Input format error!\n");
printf("Input format: ./server 2545\n");
exit(-1);
}
if(argc > 2){
printf("Error! Input format error!\n");
printf("Input format: ./server 2545\n");
exit(-1);
}
// Error if socket cannot be created
if(newSocket < 0){
printf("Error occurs at creating new socket!\n");
exit(-1);
}
char hostName[1024] = {"tommy@Tommyde-MBP"};
portNum = atoi(argv[1]);
// Step 2: Bind to a access
/* Reference website:
https://www.geeksforgeeks.org/socket-programming-cc/
https://www.bogotobogo.com/cplusplus/sockets_server_client.php
*/
server.sin_family = AF_INET; // Set up address for binding
server.sin_addr.s_addr = INADDR_ANY; // Automatically be filled with current host's IP address
server.sin_port = htons(portNum); // The network byte order of the port number
// bind() passes three parameters
// file descriptor, the address, and the length of the address
int Bind = bind(newSocket, (struct sockaddr *) &server, sizeof(server));
if(Bind < 0){
printf("Error on binding!\n");
exit(-1);
}
printf("\n");
// Step 3: Listen
while(1){
int count = 0;
int second = 0;
char request[2048] = {0};
char fileName[1024] = {0}, display[1024] = {0};
int Listen = listen(newSocket, SOMAXCONN);
if(Listen < 0){
printf("Listen failed!\n");
exit(-1);
}
newSocket2 = accept(newSocket, (struct sockaddr *) &server, (socklen_t*) &addrlen);
int valread = read(newSocket2 , request, 2048);
//printf("%s\n", request);
char Header[5] = {0};
for(int i = 0; i < 3; i++){
Header[i] = request[i];
}
for(int i = 0; i < 2048; i++){
if(request[i] == '/'){
count++;
}
if(count == 2){
second = i;
break;
}
}
//printf("%d\n", second);
int index = 5;
for(int i = 0; i < 1024; i++){
if(index == second - 5){
break;
}
display[i] = request[index];
index++;
}
index = 4;
for(int i = 0; i < 1024; i++){
if(index == second - 5){
break;
}
fileName[i] = request[index];
index++;
}
//printf("%s\n", fileName);
char version[1024] = {0};
index = second - 4;
for(int i = 0; i < 1024; i++){
if(index == second + 4){
break;
}
version[i] = request[index];
index++;
}
//printf("%s\n%s\n%s\n", Header, fileName, version);
responseMessage(newSocket2, request, Header, fileName, version, display, hostName);
}
}