-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtinyweb2.c
173 lines (154 loc) · 6.06 KB
/
tinyweb2.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "tinyweb2.h"
#include "membuf.h"
#include <uv.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <memory.h>
//Tinyweb v2, a tiny web server based on libuv, by liigo, 2013-6-24.
uv_tcp_t _server;
uv_tcp_t _client;
uv_loop_t* _loop = NULL;
static void tinyweb_on_connection(uv_stream_t* server, int status);
//start web server, linstening ip:port
//ip can be NULL or "", which means "0.0.0.0"
void tinyweb_start(uv_loop_t* loop, const char* ip, int port) {
struct sockaddr_in addr;
uv_ip4_addr((ip && ip[0]) ? ip : "0.0.0.0", port, &addr);
_loop = loop;
uv_tcp_init(_loop, &_server);
uv_tcp_bind(&_server, (const struct sockaddr*) &addr, 0);
uv_listen((uv_stream_t*)&_server, 8, tinyweb_on_connection);
}
static void after_uv_close_client(uv_handle_t* client) {
membuf_uninit((membuf_t*)client->data); //see: tinyweb_on_connection()
free(client->data); //membuf_t*: request buffer
free(client);
}
static void after_uv_write(uv_write_t* w, int status) {
if(w->data)
free(w->data); //copyed data
uv_close((uv_handle_t*)w->handle, after_uv_close_client);
free(w);
}
//close the connect of a client
static void tinyweb_close_client(uv_stream_t* client) {
uv_close((uv_handle_t*)client, after_uv_close_client);
}
//write data to client
//data: the data to be sent
//len: the size of data, can be -1 if data is string
//need_copy_data: copy data or not
//need_free_data: free data or not, ignore this arg if need_copy_data is non-zero
//write_uv_data() will close client connection after writien success, because tinyweb use short connection.
static void write_uv_data(uv_stream_t* client, const void* data, unsigned int len, int need_copy_data, int need_free_data) {
uv_buf_t buf;
uv_write_t* w;
void* newdata = (void*)data;
if(data == NULL || len == 0) return;
if(len ==(unsigned int)-1)
len = strlen((char*)data);
if(need_copy_data) {
newdata = malloc(len);
memcpy(newdata, data, len);
}
buf = uv_buf_init((char*)newdata, len);
w = (uv_write_t*)malloc(sizeof(uv_write_t));
w->data = (need_copy_data || need_free_data) ? newdata : NULL;
uv_write(w, client, &buf, 1, after_uv_write); //free w and w->data in after_uv_write()
}
//status: "200 OK"
//content_type: "text/html"
//content: any utf-8 data, need html-encode if content_type is "text/html"
//content_length: can be -1 if content is string
//returns malloc()ed respone, need free() by caller
char* format_http_respone(const char* status, const char* content_type, const void* content, int content_length) {
int totalsize;
char* respone;
if(content_length < 0)
content_length = content ? strlen((char*)content) : 0;
totalsize = strlen(status) + strlen(content_type) + content_length + 128;
respone = (char*) malloc(totalsize);
sprintf(respone, "HTTP/1.1 %s\r\n"
"Content-Type:%s;charset=utf-8\r\n"
"Content-Length:%d\r\n\r\n",
status, content_type, content_length);
if(content) {
memcpy(respone + strlen(respone), content, content_length);
}
return respone;
}
#if defined(WIN32)
#define snprintf _snprintf
#endif
//invoked by tinyweb when GET request comes in
//please invoke write_uv_data() once and only once on every request, to send respone to client and close the connection.
//if not handle this request (by invoking write_uv_data()), you can close connection using tinyweb_close_client(client).
//pathinfo: "/" or "/book/view/1"
//query_stirng: the string after '?' in url, such as "id=0&value=123", maybe NULL or ""
static void tinyweb_on_request_get(uv_stream_t* client, const char* pathinfo, const char* query_stirng) {
if(strcmp(pathinfo, "/") == 0) {
char* respone = format_http_respone("200 OK", "text/html", "Welcome to tinyweb", -1);
write_uv_data(client, respone, -1, 0, 1);
} else if(strcmp(pathinfo, "/404") == 0) {
char* respone = format_http_respone("404 Not Found", "text/html", "<h3>404 Page Not Found<h3>", -1);
write_uv_data(client, respone, -1, 0, 1);
} else {
char* respone;
char content[1024];
snprintf(content, sizeof(content), "<p>pathinfo: %s</p><p>query stirng: %s</p>", pathinfo, query_stirng);
respone = format_http_respone("200 OK", "text/html", content, -1);
write_uv_data(client, respone, -1, 0, 1);
}
}
static void on_uv_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
*buf = uv_buf_init(malloc(suggested_size), suggested_size);
}
static void on_uv_read(uv_stream_t* client, ssize_t nread, const uv_buf_t* buf) {
if(nread > 0) {
char* crln2;
membuf_t* membuf = (membuf_t*) client->data; //see tinyweb_on_connection()
assert(membuf);
membuf_append_data(membuf, buf->base, nread);
if((crln2 = strstr((const char*)membuf->data, "\r\n\r\n")) != NULL) {
const char* request_header = membuf->data;
*crln2 = '\0';
printf("\n----Tinyweb client request: ----\n%s\n", request_header);
//handle GET request
if(request_header[0]=='G' && request_header[1]=='E' && request_header[2]=='T') {
char *query_stirng, *end;
const char* pathinfo = request_header + 3;
while(isspace(*pathinfo)) pathinfo++;
end = strchr(pathinfo, ' ');
if(end) *end = '\0';
query_stirng = strchr(pathinfo, '?'); //split pathinfo and query_stirng using '?'
if(query_stirng) {
*query_stirng = '\0';
query_stirng++;
}
tinyweb_on_request_get(client, pathinfo, query_stirng);
//write_uv_data -> after_uv_write -> uv_close, will close the client, so additional processing is not needed.
} else {
tinyweb_close_client(client);
}
}
} else if(nread == -1) {
tinyweb_close_client(client);
}
if(buf->base)
free(buf->base);
}
static void tinyweb_on_connection(uv_stream_t* server, int status) {
assert(server == (uv_stream_t*)&_server);
if(status == 0) {
uv_tcp_t* client = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
client->data = malloc(sizeof(membuf_t));
membuf_init((membuf_t*)client->data, 128);
uv_tcp_init(_loop, client);
uv_accept((uv_stream_t*)&_server, (uv_stream_t*)client);
uv_read_start((uv_stream_t*)client, on_uv_alloc, on_uv_read);
//write_uv_data((uv_stream_t*)client, http_respone, -1, 0);
//close client after uv_write, and free it in after_uv_close()
}
}