-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
104 lines (91 loc) · 2.9 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
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <string.h>
#include <dirent.h>
#include "hash_table.h"
#include "spawn.h"
#include "context.h"
#include "file_utils.h"
#define HEADER_POP 1
#define HEADER_DONE 1<<2
void
main (int argc, char** argv)
{
setup_mime_types();
char temp_buff[50];
write(STDOUT_FILENO,temp_buff,snprintf(temp_buff,sizeof(temp_buff),"HimaGET\nScanning directory... "));
hash_table* files = scan_files("./html/");
write(STDOUT_FILENO,temp_buff,snprintf(temp_buff,sizeof(temp_buff),"OK\nSpawning MicroENG... "));
ipc_pipe microeng = spawn_microeng("./microeng");;
ipc_context context;
read(microeng.read,temp_buff,sizeof("MicroENG\n"));
if (strncmp("MicroENG\n",temp_buff,sizeof("MicroENG")) == 0)
{
write(STDOUT_FILENO,temp_buff,snprintf(temp_buff,sizeof(temp_buff),"OK\nCreating context... "));
context = create_context(microeng,"test",*(int*)&"GET ");
if ( context.read.buffer == NULL || context.write.buffer == NULL )
{
write(STDOUT_FILENO,temp_buff,snprintf(temp_buff,sizeof(temp_buff),"FAILED\n"));
wait(NULL);
exit(EXIT_FAILURE);
}
write(STDOUT_FILENO,temp_buff,snprintf(temp_buff,sizeof(temp_buff),"OK\n"));
}
else
{
write(STDOUT_FILENO,temp_buff,snprintf(temp_buff,sizeof(temp_buff),"FAILED\n"));
wait(NULL);
exit(EXIT_FAILURE);
}
write(STDOUT_FILENO,temp_buff,snprintf(temp_buff,sizeof(temp_buff),"Spinning read buffer... "));
if (!spin_read(µeng))
{
write(STDOUT_FILENO,temp_buff,snprintf(temp_buff,sizeof(temp_buff),"FAILED\n"));
wait(NULL);
exit(EXIT_FAILURE);
}
write(microeng.write,"spin_wrb\n",sizeof("spin_wrb\n"));
write(STDOUT_FILENO,temp_buff,snprintf(temp_buff,sizeof(temp_buff),"OK\nListening\n"));
for(;;)
{
context.read.header->opcode = HEADER_POP;
while ( !( context.read.header->opcode == HEADER_DONE ) )
{
sleep(0);
}
//Get path
char path[255];
for (int i=0; i<sizeof(path); i++)
{
char curr_char = *(char*)(context.read.buffer+strlen("GET /")+i);
if (curr_char != ' ' && curr_char != '\n' && curr_char != '\0')
{
path[i] = curr_char;
continue;
}
path[i] = '\0';
break;
}
file* requested_file = hash_table_get(files,path);
if (requested_file == NULL)
{
continue;
}
char buf[256];
time_t rawtime = time(NULL);
struct tm *ptm = localtime(&rawtime);
strftime(buf, sizeof(buf), "%c", ptm);
size_t len = sprintf(context.write.buffer,"HTTP/1.1 200 OK\nDate: %s\nContent-Type: %s\nConnection: close\nContent-Length: %d\n\n",buf,requested_file->mime,requested_file->data.size);
memcpy(context.write.buffer+len,requested_file->data.bytes,requested_file->data.size);
context.write.header->len = len+requested_file->data.size;
context.write.header->argument = context.read.header->argument;
context.write.header->opcode = 1;
}
wait(NULL);
exit(EXIT_SUCCESS);
}