-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleHTTPserver.c
More file actions
321 lines (227 loc) · 8.15 KB
/
simpleHTTPserver.c
File metadata and controls
321 lines (227 loc) · 8.15 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/************************************************************ SIMPLE HTTP SERVER *******************************************************************************/
/***************************************************************************************************************************************************************/
/***************************************************************************************************************************************************************/
/**************************************************************@author:Riccardo Vecchi**************************************************************************/
/***************************************************************************************************************************************************************/
/***************************************************************************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*********GLOBALI E STRUTTURE*******************************/
#define PORT 8080
#define ROOT "//home//ric//Scrivania//RetiDiCalcolatori//C"
#define MIMENUM 2
typedef enum state {FALSE, TRUE} state;
/*********************************************************/
/** PROTOTIPI ************************************************/
char* get_url(const char* request);
void send_dummy_response(int socket_fd, const char* request);
void send_response(int socket_fd, const char* request);
void send_right_response(int socket_fd, FILE* f, char* mime);
void send_error(int socket_fd);
void send_(int socket_fd, unsigned char* response, size_t lenght);
/************************************************************/
/*** FUNZIONI ****************************************************************************************************************************/
void print_request(const char* request){
printf("Client requested: %s", request);
}
void send_response(int socket_fd, const char* request){
char* url = get_url(request);
char mimetype[30];
char* MIMETYPES[] = {"text/html", "image/jpeg"};
char requested_page[1024];
strcpy(requested_page, ROOT);
strcat(requested_page, "//");
strcat(requested_page, url);
FILE* requested_file = fopen(requested_page, "rb");
if(requested_file == NULL){
send_error(socket_fd);
return;
}
//ciao
if(strstr(url, "html"))
strcpy(mimetype, "text/html");
else
strcpy(mimetype, "image/jpeg");
printf("\n\nREQUESTED FILE: %s\n\n", requested_page);
send_right_response(socket_fd, requested_file, mimetype);
}
void send_error(int socket_fd){
char* headers = "HTTP/1.1 404 BAD REQUEST \r\n"
"Content-type: text/html\r\n"
"Server: localhost"
"Connection: keep-alive\r\n";
char page[1024];
char response[1024];
int s_flag = sprintf(page, "\r\n<html><center><h1> ERROR 404: PAGE NOT FOUND </h1></center></html>\r\n");
if(s_flag < 0)
{
perror("error formatting response");
exit(EXIT_FAILURE);
}
strcpy(response,headers);
strcat(response,page);
send_(socket_fd, response, strlen(response));
}
void send_right_response(int socket_fd, FILE* requested_file, char* mime){
printf("%s MIME IS", mime);
unsigned char* headers = "HTTP/1.1 200 OK \r\n"
"Server: localhost\r\n"
"Connection: keep-alive\r\n";
unsigned char page[200000];
unsigned char content_type[1024];
unsigned char response[200000];
long size;
bzero(page, 200000);
bzero(response,200000);
bzero(content_type, 1024);
sprintf(content_type,"Content-type:%s\r\n\r\n", mime);
fseek(requested_file, 0L, SEEK_END);
size = ftell(requested_file);
rewind(requested_file);
printf("\n\n file size is: %ld \n\n", size);
//printf("\n\nFASE 1\n\n");
size_t Content_type_lenght = strlen(content_type);
size_t header_lenght = strlen(headers);
size_t total_lenght = Content_type_lenght + header_lenght + size + 2;
//printf("\n\nFASE 1.5\n\n");
if((fread(page, 1, size*sizeof(unsigned char), requested_file)) != (size_t)size * sizeof(unsigned char)){
perror("ERROR IN READING");
exit(EXIT_FAILURE);
}
//printf("\n\nFASE 2\n\n");
page[size] = '\r';
page[size + 1] = '\n';
memcpy(response, headers, strlen(headers));
memcpy(response + strlen(response), content_type, strlen(content_type));
memcpy(response + strlen(response), page, size + 2);
// printf("%s", response);
send_(socket_fd, response, 200000);
//printf("\n\nFASE 3\n\n");
}
void send_(int socket_fd, unsigned char* response, size_t size){
if((send(socket_fd, response, size, 0)) <0)
{
perror("error sending");
exit(EXIT_FAILURE);
}
}
/*
void send_dummy_response(int socket_fd, const char* request){
// manda una risposta banale //
char* url = get_url(request);
char* headers = "HTTP/1.1 200 OK \r\n"
"Content-type: text/html\r\n"
"Server: localhost"
"Connection: keep-alive\r\n";
char page[1024];
char response[1024];
int s_flag = sprintf(page, "\r\n<html><center><h1> you requested %s </h1></center></html>\r\n",url);
if(s_flag < 0)
{
perror("error formatting response");
exit(EXIT_FAILURE);
}
strcpy(response,headers);
strcat(response,page);
if((send(socket_fd, response, strlen(response), 0)) <0)
{
perror("error sending");
exit(EXIT_FAILURE);
}
} */
char* get_url(const char* request){
//estrae l'url dalla richiesta
char* url = malloc(1);
size_t url_size = 0;
/** ISOLO LA PRIMA RIGA DELLA RICHIESTA **/
for(size_t i = 0; i < strlen(request); i++){
if(request[i] != '\r'){
url[url_size] = request[i];
url_size++;
}
else{
url[url_size] = 0;
break;
}
}
/** ESTRAGGO L'URL **/
for(size_t i = 0, j = 0; i < url_size; i++){
if(url[i] == '/'){
for(size_t con = i + 1; url[con] != ' '; con++){
url[j++] = url[con];
}
url[j] = 0;
break;
}
}
if(!strcmp(url,""))
return "index.html";
return url;
}
/***************************************************************************************************************************************************/
int main(int argc, char** argv){
/*** CREAZIONE DEL SOCKET ****/
int server_fd, new_socket;
int opt = 1;
struct sockaddr_in address;
socklen_t address_len = sizeof(address);
char* message = "Hello world! I'm the server"; //messaggio da inviare
char buffer[1024]; //messaggio da ricevere
int valread;
if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) // AF_INET ---> ipv4, SOCK_STREAM --> tcp
{
perror("failure in creating socket");
exit(EXIT_FAILURE);
}
/*** FUNZIONE NECESSARIA PER FARE IN MODO CHE SI CHIUDA IL BINDING PORTA INDIRIZZO ***/
if(setsockopt(server_fd, SOL_SOCKET,
SO_REUSEADDR | SO_REUSEPORT, &opt,
sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
/*** PARAMETRI DA SETTARE ALLA STRUTTURA IN MODO CHE IL SOCKET FACCIA IL BINDING INDIRIZZO PORTA ***/
address.sin_family = AF_INET;
//address.sin_addr.s_addr = INADDR_ANY; //qualsiasi indirizzo (in caso di server web)
address.sin_addr.s_addr = inet_addr("127.0.0.1");// ci interessa solo localhost;
address.sin_port = htons(PORT);
/*** FUNZIONE DI BINDING ***/
if (bind(server_fd, (struct sockaddr*)&address,
sizeof(address))< 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
/**** SERVIZIO **************************************************************************************/
printf("\n\nlistening on port: %d\n\n", PORT);
if (listen(server_fd, 3) < 0){//3 è il valore di backlog, ovvero il numero di richieste contemporanee che possono stare in coda
perror("listening failed");
exit(EXIT_FAILURE);
}
/** prende il file descriptor del socket e crea un connected soket e ritorna un nuovo file descriptor **/
while(TRUE){
if((new_socket
= accept(server_fd, (struct sockaddr*)&address,
&address_len))
< 0) {
perror("accept error");
exit(EXIT_FAILURE);
}
bzero(buffer,1024);
if((valread = read(new_socket, buffer, 1024 - 1)) < 0) //** legge dal file descriptor new socket salva nel buffer 1023 byte
{
perror("reading failed");
exit(EXIT_FAILURE);
}
print_request(buffer);
send_response(new_socket,buffer);
/** chiusura dei socket **/
close(new_socket);
}
close(server_fd);
return 0;
}