-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.c
More file actions
103 lines (71 loc) · 2.56 KB
/
thread.c
File metadata and controls
103 lines (71 loc) · 2.56 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
//
// Created by simone_mancini on 27/10/16.
//
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "thread.h"
#include "error_management.h"
#include "server_http_parser.h"
#include "files.h"
#include "send_recv.h"
#include "typedef.h"
void send_404_page_not_found(int sock_fd) {
FILE *f;
long file_length;
char file_size[256];
f = open_file_read("404pnf.html");
file_length = get_file_size(f);
sprintf(file_size, "%ld", file_length);
char ptr[file_length];
read_file(f, ptr, file_length);
close_file(f);
sprintf(file_size, "Content-Length: %ld\r\n", file_length);
sendn(sock_fd, "HTTP/1.1 404 Not Found\r\n", 0);
sendn(sock_fd, file_size, 0);
sendn(sock_fd, "Connection: Keep-Alive", 0);
sendn(sock_fd, "Server: Caronte/Private\r\n\r\n", 0);
sendn(sock_fd, ptr, 0);
//free(file_size);
}
void *thread_routine(void *arg){
thread_data *td = (thread_data *) arg;
printf("thread %ld alive\n", (long) td->tid);
td->msg = malloc(sizeof(rcv_msg));
if(td->msg == NULL)
die_with_error("msg == NULL");
parse_data(td->msg, td->buf, td->received);
//printf("%.*s\n", len, url);
printf("Host: %.*s\n", (int) td->msg->host_length, td->msg->host);
printf("User-Agent: %.*s\n", (int) td->msg->user_agent_length, td->msg->user_agent);
printf("Connection: %.*s\n", (int) td->msg->connection_length, td->msg->connection);
printf("Referer: %.*s\n", (int) td->msg->referer_length, td->msg->referer);
if(strcmp(td->msg->url, "/") == 0) /*if request is "IP:PORT_NUMBER/" send index.html page*/
td->msg->url = "/index.html";
FILE *f;
long file_length;
f = open_file_read(td->msg->url + 1);
if(f == NULL){
send_404_page_not_found(td->sock_fd);
}else {
file_length = get_file_size(f);
sprintf(td->file_size, "%ld", file_length);
char ptr[file_length];
read_file(f, ptr, file_length);
close_file(f);
sprintf(td->file_size, "Content-Length: %ld\r\n", file_length);
sendn(td->sock_fd, "HTTP/1.1 200 OK\r\n", 0);
sendn(td->sock_fd, td->file_size, 0);
sendn(td->sock_fd, "Connection: Keep-Alive", 0);
sendn(td->sock_fd, "Server: Caronte/Private\r\n\r\n", 0);
sendn(td->sock_fd, ptr, 0);
}
free(td->msg->url);
printf("thread %ld is dieing\n", (long) td->tid);
return NULL;
}
void spawn_thread(thread_data *td){
if(pthread_create(&td->tid, NULL, thread_routine, td) != 0)
die_with_error("error in pthread_create");
}