-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_cgi.c
68 lines (57 loc) · 1.59 KB
/
handle_cgi.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
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
void handle_cgi(char* path, char* query_string, char* method, FILE* request)
{
char* post_data = "\n";
char* content_length_str = get_header("Content-Length");
if (content_length_str==NULL) content_length_str="0";
int content_length = atoi(content_length_str);
// Setter opp en pipe fil
int fd[2];
pipe(fd);
if(strcmp(method, "POST") == 0)
{
// Leser body inn i post_data
post_data = malloc(content_length);
fread(post_data, content_length, 1, request);
post_data[content_length+1] = 0;
}
else if(strcmp(method, "GET") != 0)
{
// Error if not get or post
error(405);
return;
}
char* argv[] = {path, 0};
char* cookie = get_header("Cookie");
char* referer = get_header("Referer");
// Konstruerer envp
char* envp[6];
envp[0] = malloc(strlen(query_string) + 14);
envp[1] = malloc(strlen(method) + 16);
envp[2] = malloc(strlen(content_length_str) + 16);
envp[3] = malloc(strlen(cookie) + 8);
envp[4] = malloc(strlen(referer) + 9);
envp[5] = malloc(1);
sprintf(envp[0], "QUERY_STRING=%s", query_string);
sprintf(envp[1], "REQUEST_METHOD=%s", method);
sprintf(envp[2], "CONTENT_LENGTH=%d", content_length);
sprintf(envp[3], "COOKIE=%s", cookie);
sprintf(envp[4], "REFERER=%s", referer);
envp[5] = (char*)0;
// Send statuskode ok
printf("HTTP/1.1 200 OK\n");
fflush(stdout);
// Forker og sender post_data
if(fork() == 0)
{
close(0);
dup2(fd[0], 0);
execve(path, &argv[0], envp);
dprintf(2, "%s. Path: %s\n", strerror(errno), path);
}
dprintf(fd[1], "%s", post_data);
dprintf(2,"200 OK\n"); // Log
exit(0);
}