-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgi.c
155 lines (133 loc) · 3.56 KB
/
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
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
/*
* =====================================================================================
*
* Filename: cgi.c
*
* Description: cgi server
*
* Version: 1.0
* Created: 2018/01/30 20时02分54秒
* Revision: none
* Compiler: gcc
*
* Author: ssj
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <signal.h>
#define SERV_PORT 6006
#define BUFSIZE 1024
typedef char* pchar;
char *str_join(char *str1, char *str2);
void handle_one_connection(int client_fd);
void make_response(int client_fd, char *result);
int main(void)
{
struct sockaddr_in server_addr;
int listen_fd;
if((listen_fd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("create socket failed");
exit(1);
}
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERV_PORT);
int opt = 1;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if(bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind error");
exit(1);
}
if(listen(listen_fd, 128) == -1) {
perror("listen error");
exit(1);
}
while(1) {
struct sockaddr_in client_addr;
int client_fd;
socklen_t client_len;
client_len = sizeof(client_addr);
if ((client_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &client_len)) == -1) {
perror("accept error\n");
continue;
}
int pid = fork();
if (pid < 0) {
perror("fork error");
exit(1);
}
if (pid > 0) {
close(client_fd);
} else {
close(listen_fd);
handle_one_connection(client_fd);
exit(0);
}
}
close(listen_fd);
return 0;
}
void handle_one_connection(int client_fd) {
char input[BUFSIZE];
FILE *cin;
cin = fdopen(client_fd, "r");
fgets(input, BUFSIZE, cin);
printf("request:%s\n", input);
pchar method, filename, query_string;
method = strtok(input, " ");
char *p = strtok(NULL, " ");
filename = strtok(p, "?");
if (strcmp(filename, "/favicon.ico") == 0) {
make_response(client_fd, "favicon.ico");
return;
}
query_string = strtok(NULL, "?");
if (!query_string) {
make_response(client_fd, "no query string");
return;
}
putenv(str_join("QUERY_STRING=", query_string));
int pp[2];
pipe(pp);
if (fork() == 0) {
close(1);
dup(pp[1]);
close(pp[0]);
close(pp[1]);
char *file = str_join(".", filename);
execl(file, "", NULL);
} else {
close(0);
dup(pp[0]);
close(pp[0]);
close(pp[1]);
char result[BUFSIZE];
size_t count = read(0, result, sizeof(result)-1);
result[count] = '\0';
make_response(client_fd, result);
}
}
char *str_join(char *str1, char *str2)
{
char *result = malloc(strlen(str1) + strlen(str2) + 1);
if (result == NULL) exit (1);
strcpy(result, str1);
strcat(result, str2);
return result;
}
void make_response(int client_fd, char *result) {
char *response_header = "HTTP/1.1 200 OK\r\nContent-Type:text/html\r\nContent-Length: %d\r\nServer: CGI\r\n\r\n%s";
char output[BUFSIZE];
sprintf(output, response_header, strlen(result), result);
write(client_fd, output, sizeof(output));
close(client_fd);
}