-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpd.c
More file actions
129 lines (109 loc) · 2.18 KB
/
httpd.c
File metadata and controls
129 lines (109 loc) · 2.18 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
#include<stdio.h>
#include<string.h>
#define MAX_RECV_BUFF_LEN 1024
#ifdef DEBUG
#define debug( format, args... )do {fprintf(stderr, "DEBUG >>> %s->%s()->line.%d : " format "\n", __FILE__, __FUNCTION__, __LINE__, ##args);}while(0)
#endif
/*len is the buff max len,return the real len*/
int get_line(int socket,char *buff,int len)
{
debug("get_line ...");
strcpy(buff,"GET /voip/SIP_Account1.asp");
debug("get_line end");
return 0;
}
/*reply 404*/
int reply_not_found(int socket)
{
debug("reply_not_found ...");
debug("reply_not_found end");
return 0;
}
/*reply 500*/
int reply_internal_server(int socket)
{
debug("reply_internal_server ...");
debug("reply_internal_server end");
return 0;
}
int serve_file(int socket,char *path)
{
debug("serve_file ...");
debug("serve_file end");
return 0;
}
int execute_cgi(int socket,char *path,char *method,char *query)
{
debug("execute_cgi ...");
debug("execute_cgi end");
return 0;
}
int accept_request(int client)
{
char buff[MAX_RECV_BUFF_LEN]="";
int need_do_cgi = 0;
char *data_str = NULL;
char *query = NULL;
char *path = NULL;
char *method = NULL;
get_line(client,buff,MAX_RECV_BUFF_LEN);
/*ignore spaces in buff*/
data_str = buff;
while(data_str && *data_str == ' ') data_str++;
debug("data_str=%s",data_str);
/*post or get recved*/
if(strcasestr(data_str,"post")==NULL && strcasestr(data_str,"get")==NULL)
{
reply_not_found(client);
return -1;
}
/*if "POST" recved,do cgi*/
if (strcasestr(data_str,"post"))
{
method = "POST";
need_do_cgi=1;
}
/*if GET recved*/
if (strcasestr(data_str,"get"))
{
method = "GET";
query = strstr(data_str,"?");
if (query != NULL)
{
query++;
debug("query=%s",query);
need_do_cgi=1;
}
else
{
/*get file path*/
path = strstr(data_str,"/");
debug("path=%s",path);
}
}
/*if found query,do cgi*/
/*if can't excute,reply 503*/
if(need_do_cgi)
{
if(execute_cgi(client,path,method,query) < 0)
{
reply_internal_server(client);
return -1;
}
}
else
{
if(serve_file(client,path))
{
reply_not_found(client);
return -1;
}
}
return 0;
}
int main(int argc, const char *argv[])
{
int socket = 666;
accept_request(socket);
return 0;
}