/
main.c
145 lines (122 loc) · 3.41 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
// glooooooobals
int listen_socket;
int client_socket;
struct sockaddr_in listen_sock_addr;
struct sockaddr current_client;
char send_buffer[1024*1024];
char recv_buffer[1024*1024];
char request_method[10];
char request_uri[1024];
char filename[1024];
char filebuffer[1024*1024];
char* www_root = "www-root";
char* status_line_ok = "HTTP/1.1 200 OK\x0D\x0A";
char* status_line_not_found = "HTTP/1.1 404 Not Found\x0D\x0A\x0D\x0A";
// Functions
void init_socket() {
// set state
errno = 0;
listen_sock_addr.sin_family = AF_INET;
listen_sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
listen_sock_addr.sin_port = htons(8080);
// create socket
if((listen_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
perror("socket");
exit(-1);
}
//make it reusable
int sockopt_on = 1;
if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &sockopt_on, sizeof(int)) == -1) {
perror("setsockopt");
exit(-1);
}
// bind to :8080
if(bind(listen_socket, (struct sockaddr *) &listen_sock_addr, sizeof(listen_sock_addr)) == -1) {
perror("bind");
exit(-1);
}
// listen to the socket
if(listen(listen_socket, 100) == -1) {
perror("listen");
exit(-1);
}
}
void handle_request() {
// read request
int message_len;
if((message_len = recv(client_socket, &recv_buffer, sizeof(recv_buffer), 0)) == -1) {
perror("recv");
exit(-1);
}
// parse status line "Method SP Request-URI SP HTTP-Version CRLF";
sscanf(recv_buffer, "%s %s", request_method, request_uri);
// No uri path given, assume index.html
if(strlen(request_uri)==1) {
strcpy(request_uri, "/index.html");
}
// open requested file
sprintf(filename, "%s%s", www_root, request_uri);
FILE *filepointer;
int character;
filepointer=fopen(filename, "r"); /* filepointer points to data.txt */
if (filepointer==NULL) { /* error opening file returns NULL */
fprintf(stderr, "Requsted file %s not found, sent 404 as response to (METHOD:%s URI:%s)\n",
filename, request_method, request_uri);
// write status
if(send(client_socket, status_line_not_found, strlen(status_line_not_found), 0) == -1) {
perror("send");
exit(-1);
}
// close
if(close(client_socket) == -1) {
perror("close");
exit(-1);
}
return;
}
// write status
if(send(client_socket, status_line_ok, strlen(status_line_ok), 0) == -1) {
perror("send");
exit(-1);
}
// write header/body reparator
if(send(client_socket, "\x0D\x0A", 2, 0) == -1) {
perror("send");
exit(-1);
}
/* while character is not end of file */
while ((character=fgetc(filepointer)) != EOF) {
send_buffer[0] = character;
// write body
if(send(client_socket, send_buffer, 1, 0) == -1) {
perror("send");
exit(-1);
}
}
fclose(filepointer); /* close the file */
// close
if(close(client_socket) == -1) {
perror("close");
exit(-1);
}
}
int main(int argc, char **argv) {
init_socket();
socklen_t current_client_len;
while(1) {
current_client_len = sizeof(current_client);
if((client_socket = accept(listen_socket,
(struct sockaddr*) ¤t_client,
(socklen_t*)¤t_client_len)) == -1) {
perror("accept");
break;
}
handle_request();
}
return 0;
}