-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
76 lines (68 loc) · 2.04 KB
/
main.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
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#define PORT 5500
#define MAXIMUM_REQUET_SIZE 2048
#define ROOT "./src"
void handling(int sock){
char request[MAXIMUM_REQUET_SIZE] = {0};
recv(sock, request, MAXIMUM_REQUET_SIZE, 0);
char method[10], path[255], protocol[20];
sscanf(request, "%s%s%s", method, path, protocol);
char filePath[265];
sprintf(filePath, "%s%s", ROOT, path);
if(strcmp(path, "/") == 0){
sprintf(filePath, "%s/index.html", ROOT);
}
int file = open(filePath, O_RDONLY);
if(file == -1){
char res[] = "HTTP/1.1 404 NOT FOUND\r\n\r\n";
send(sock, res, strlen(res), 0);
} else{
char res[] = "HTTP/1.1 2000 OK\r\n\r\n";
send(sock, res, strlen(res), 0);
char buf[1024];
ssize_t readBytes;
while((readBytes = read(file, buf, sizeof(buf))) > 0){
send(sock, buf, readBytes, 0);
}
close(file);
}
close(sock);
}
int main(){
int ServerSock, clientSock;
struct sockaddr_in serverAddr, clientAddr;
socklen_t lenAddr = sizeof(struct sockaddr_in);
ServerSock = socket(AF_INET, SOCK_STREAM, 0);
if(ServerSock == -1){
perror("Socket failed badly...");
exit(EXIT_FAILURE);
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
if(bind(ServerSock, (struct sockAddr *)&serverAddr, sizeof(serverAddr)) == -1){
perror("Binding failed\n");
exit(EXIT_FAILURE);
}
if (listen(ServerSock, 10) < 0){
perror("Bad ear, failed to listen...");
exit(EXIT_FAILURE);
}
printf("Listing to port...%d", PORT);
while(1){
clientSock = accept(ServerSock, (struct sockaddr *)&clientAddr, &lenAddr);
if (clientSock < 0){
perror("Proposal declined...");
exit(EXIT_FAILURE);
}
handling(clientSock);
}
return 0;
}