2012-09-20 20:45:35 +08:00
|
|
|
#define USE_WEBSOCKET
|
2012-01-24 06:03:36 +08:00
|
|
|
#include "mongoose.c"
|
|
|
|
|
2012-06-08 04:57:40 +08:00
|
|
|
#define FATAL(str, line) do { \
|
|
|
|
printf("Fail on line %d: [%s]\n", line, str); \
|
|
|
|
abort(); \
|
|
|
|
} while (0)
|
|
|
|
#define ASSERT(expr) do { if (!(expr)) FATAL(#expr, __LINE__); } while (0)
|
|
|
|
|
|
|
|
static void test_parse_http_request() {
|
|
|
|
struct mg_request_info ri;
|
|
|
|
char req1[] = "GET / HTTP/1.1\r\n\r\n";
|
|
|
|
char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
|
|
|
|
char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
|
2012-07-27 13:24:11 +08:00
|
|
|
char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
|
2012-06-08 04:57:40 +08:00
|
|
|
|
2012-07-28 18:04:03 +08:00
|
|
|
ASSERT(parse_http_request(req1, sizeof(req1), &ri) == sizeof(req1) - 1);
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(strcmp(ri.http_version, "1.1") == 0);
|
|
|
|
ASSERT(ri.num_headers == 0);
|
|
|
|
|
2012-07-28 18:04:03 +08:00
|
|
|
ASSERT(parse_http_request(req2, sizeof(req2), &ri) == -1);
|
|
|
|
ASSERT(parse_http_request(req3, sizeof(req3), &ri) == -1);
|
2012-06-08 04:57:40 +08:00
|
|
|
|
2012-07-27 13:24:11 +08:00
|
|
|
// TODO(lsm): Fix this. Header value may span multiple lines.
|
2012-07-28 18:04:03 +08:00
|
|
|
ASSERT(parse_http_request(req4, sizeof(req4), &ri) == sizeof(req4) - 1);
|
2012-07-27 13:24:11 +08:00
|
|
|
ASSERT(ri.num_headers == 3);
|
|
|
|
ASSERT(strcmp(ri.http_headers[0].name, "A") == 0);
|
|
|
|
ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0);
|
|
|
|
ASSERT(strcmp(ri.http_headers[1].name, "B") == 0);
|
|
|
|
ASSERT(strcmp(ri.http_headers[1].value, "bar") == 0);
|
2012-07-28 18:04:03 +08:00
|
|
|
ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0);
|
2012-07-27 13:24:11 +08:00
|
|
|
ASSERT(strcmp(ri.http_headers[2].value, "") == 0);
|
|
|
|
|
2012-06-08 04:57:40 +08:00
|
|
|
// TODO(lsm): add more tests.
|
|
|
|
}
|
|
|
|
|
2012-06-05 05:49:16 +08:00
|
|
|
static void test_should_keep_alive(void) {
|
|
|
|
struct mg_connection conn;
|
|
|
|
struct mg_context ctx;
|
|
|
|
char req1[] = "GET / HTTP/1.1\r\n\r\n";
|
|
|
|
char req2[] = "GET / HTTP/1.0\r\n\r\n";
|
|
|
|
char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
|
|
|
|
char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
|
|
|
|
|
|
|
|
memset(&conn, 0, sizeof(conn));
|
|
|
|
conn.ctx = &ctx;
|
2012-07-28 18:04:03 +08:00
|
|
|
parse_http_request(req1, sizeof(req1), &conn.request_info);
|
2012-06-05 05:49:16 +08:00
|
|
|
|
|
|
|
ctx.config[ENABLE_KEEP_ALIVE] = "no";
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(should_keep_alive(&conn) == 0);
|
2012-06-05 05:49:16 +08:00
|
|
|
|
|
|
|
ctx.config[ENABLE_KEEP_ALIVE] = "yes";
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(should_keep_alive(&conn) == 1);
|
2012-06-05 05:49:16 +08:00
|
|
|
|
|
|
|
conn.must_close = 1;
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(should_keep_alive(&conn) == 0);
|
2012-06-05 05:49:16 +08:00
|
|
|
|
|
|
|
conn.must_close = 0;
|
2012-07-28 18:04:03 +08:00
|
|
|
parse_http_request(req2, sizeof(req2), &conn.request_info);
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(should_keep_alive(&conn) == 0);
|
2012-06-05 05:49:16 +08:00
|
|
|
|
2012-07-28 18:04:03 +08:00
|
|
|
parse_http_request(req3, sizeof(req3), &conn.request_info);
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(should_keep_alive(&conn) == 0);
|
2012-06-05 05:49:16 +08:00
|
|
|
|
2012-07-28 18:04:03 +08:00
|
|
|
parse_http_request(req4, sizeof(req4), &conn.request_info);
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(should_keep_alive(&conn) == 1);
|
2012-06-05 05:49:16 +08:00
|
|
|
|
2012-09-18 02:38:19 +08:00
|
|
|
conn.status_code = 401;
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(should_keep_alive(&conn) == 0);
|
2012-06-05 05:49:16 +08:00
|
|
|
|
2012-09-18 02:38:19 +08:00
|
|
|
conn.status_code = 200;
|
2012-06-05 05:49:16 +08:00
|
|
|
conn.must_close = 1;
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(should_keep_alive(&conn) == 0);
|
2012-06-05 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2012-02-10 06:55:24 +08:00
|
|
|
static void test_match_prefix(void) {
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(match_prefix("/api", 4, "/api") == 4);
|
|
|
|
ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3);
|
|
|
|
ASSERT(match_prefix("/a/", 3, "/ab/c") == -1);
|
|
|
|
ASSERT(match_prefix("/*/", 3, "/ab/c") == 4);
|
|
|
|
ASSERT(match_prefix("**", 2, "/a/b/c") == 6);
|
|
|
|
ASSERT(match_prefix("/*", 2, "/a/b/c") == 2);
|
|
|
|
ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2);
|
|
|
|
ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
|
|
|
|
ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
|
|
|
|
ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2);
|
|
|
|
ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2);
|
|
|
|
ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1);
|
|
|
|
ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
|
|
|
|
ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
|
|
|
|
ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
|
|
|
|
ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
|
|
|
|
ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
|
|
|
|
ASSERT(match_prefix("$", 1, "") == 0);
|
|
|
|
ASSERT(match_prefix("$", 1, "x") == -1);
|
|
|
|
ASSERT(match_prefix("*$", 2, "x") == 1);
|
|
|
|
ASSERT(match_prefix("/$", 2, "/") == 1);
|
|
|
|
ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
|
|
|
|
ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
|
|
|
|
ASSERT(match_prefix("*", 1, "/hello/") == 0);
|
|
|
|
ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
|
|
|
|
ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
|
|
|
|
ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);
|
2012-02-10 06:55:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_remove_double_dots() {
|
|
|
|
struct { char before[20], after[20]; } data[] = {
|
|
|
|
{"////a", "/a"},
|
|
|
|
{"/.....", "/."},
|
|
|
|
{"/......", "/"},
|
|
|
|
{"...", "..."},
|
|
|
|
{"/...///", "/./"},
|
|
|
|
{"/a...///", "/a.../"},
|
|
|
|
{"/.x", "/.x"},
|
|
|
|
{"/\\", "/"},
|
|
|
|
{"/a\\", "/a\\"},
|
2012-09-21 01:20:41 +08:00
|
|
|
{"/a\\\\...", "/a\\."},
|
2012-02-10 06:55:24 +08:00
|
|
|
};
|
|
|
|
size_t i;
|
2012-01-24 06:03:36 +08:00
|
|
|
|
2012-02-10 06:55:24 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(data); i++) {
|
2012-09-21 01:20:41 +08:00
|
|
|
printf("[%s] -> [%s]\n", data[i].before, data[i].after);
|
2012-02-10 06:55:24 +08:00
|
|
|
remove_double_dots_and_double_slashes(data[i].before);
|
2012-06-08 04:57:40 +08:00
|
|
|
ASSERT(strcmp(data[i].before, data[i].after) == 0);
|
2012-02-10 06:55:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-28 18:04:03 +08:00
|
|
|
static const char *fetch_data = "hello world!\n";
|
|
|
|
static void *event_handler(enum mg_event event,
|
2012-08-03 18:15:38 +08:00
|
|
|
struct mg_connection *conn) {
|
|
|
|
const struct mg_request_info *request_info = mg_get_request_info(conn);
|
2012-07-28 18:04:03 +08:00
|
|
|
if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/data")) {
|
|
|
|
mg_printf(conn, "HTTP/1.1 200 OK\r\n"
|
2012-07-29 15:33:00 +08:00
|
|
|
"Content-Length: %d\r\n"
|
|
|
|
"Content-Type: text/plain\r\n\r\n"
|
2012-07-28 18:04:03 +08:00
|
|
|
"%s", (int) strlen(fetch_data), fetch_data);
|
|
|
|
return "";
|
|
|
|
} else if (event == MG_EVENT_LOG) {
|
2012-09-18 02:38:19 +08:00
|
|
|
printf("%s\n", mg_get_log_message(conn));
|
2012-07-28 18:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_mg_fetch(void) {
|
|
|
|
static const char *options[] = {
|
|
|
|
"document_root", ".",
|
|
|
|
"listening_ports", "33796",
|
|
|
|
NULL,
|
|
|
|
};
|
2012-07-29 17:15:37 +08:00
|
|
|
char buf[2000], buf2[2000];
|
2012-07-28 18:04:03 +08:00
|
|
|
int length;
|
|
|
|
struct mg_context *ctx;
|
|
|
|
struct mg_request_info ri;
|
|
|
|
const char *tmp_file = "temporary_file_name_for_unit_test.txt";
|
2012-07-30 00:12:44 +08:00
|
|
|
struct mgstat st;
|
2012-07-28 18:04:03 +08:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
ASSERT((ctx = mg_start(event_handler, NULL, options)) != NULL);
|
|
|
|
|
|
|
|
// Failed fetch, pass invalid URL
|
2012-07-29 17:15:37 +08:00
|
|
|
ASSERT(mg_fetch(ctx, "localhost", tmp_file, buf, sizeof(buf), &ri) == NULL);
|
|
|
|
ASSERT(mg_fetch(ctx, "localhost:33796", tmp_file,
|
|
|
|
buf, sizeof(buf), &ri) == NULL);
|
|
|
|
ASSERT(mg_fetch(ctx, "http://$$$.$$$", tmp_file,
|
|
|
|
buf, sizeof(buf), &ri) == NULL);
|
2012-07-28 18:04:03 +08:00
|
|
|
|
|
|
|
// Failed fetch, pass invalid file name
|
|
|
|
ASSERT(mg_fetch(ctx, "http://localhost:33796/data",
|
2012-07-29 17:15:37 +08:00
|
|
|
"/this/file/must/not/exist/ever",
|
|
|
|
buf, sizeof(buf), &ri) == NULL);
|
2012-07-28 18:04:03 +08:00
|
|
|
|
|
|
|
// Successful fetch
|
|
|
|
ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/data",
|
2012-07-29 17:15:37 +08:00
|
|
|
tmp_file, buf, sizeof(buf), &ri)) != NULL);
|
2012-07-29 15:33:00 +08:00
|
|
|
ASSERT(ri.num_headers == 2);
|
|
|
|
ASSERT(!strcmp(ri.request_method, "HTTP/1.1"));
|
|
|
|
ASSERT(!strcmp(ri.uri, "200"));
|
|
|
|
ASSERT(!strcmp(ri.http_version, "OK"));
|
2012-07-28 18:04:03 +08:00
|
|
|
ASSERT((length = ftell(fp)) == (int) strlen(fetch_data));
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
2012-07-29 17:15:37 +08:00
|
|
|
ASSERT(fread(buf2, 1, length, fp) == (size_t) length);
|
|
|
|
ASSERT(memcmp(buf2, fetch_data, length) == 0);
|
2012-07-30 00:12:44 +08:00
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
// Fetch big file, mongoose.c
|
|
|
|
ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/mongoose.c",
|
|
|
|
tmp_file, buf, sizeof(buf), &ri)) != NULL);
|
|
|
|
ASSERT(mg_stat("mongoose.c", &st) == 0);
|
|
|
|
ASSERT(st.size == ftell(fp));
|
|
|
|
ASSERT(!strcmp(ri.request_method, "HTTP/1.1"));
|
2012-07-28 18:04:03 +08:00
|
|
|
|
|
|
|
remove(tmp_file);
|
|
|
|
mg_stop(ctx);
|
|
|
|
}
|
|
|
|
|
2012-09-19 19:23:54 +08:00
|
|
|
static void test_base64_encode(void) {
|
|
|
|
const char *in[] = {"a", "ab", "abc", "abcd", NULL};
|
|
|
|
const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
|
|
|
|
char buf[100];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; in[i] != NULL; i++) {
|
|
|
|
base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
|
|
|
|
printf("[%s] [%s]\n", out[i], buf);
|
|
|
|
ASSERT(!strcmp(buf, out[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-22 19:22:30 +08:00
|
|
|
static void test_mg_get_var(void) {
|
|
|
|
static const char *post[] = {"a=1&&b=2&d&=&c=3%20&e=", NULL};
|
|
|
|
char buf[20];
|
|
|
|
|
|
|
|
ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
|
|
|
|
ASSERT(buf[0] == '1' && buf[1] == '\0');
|
|
|
|
ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
|
|
|
|
ASSERT(buf[0] == '2' && buf[1] == '\0');
|
|
|
|
ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
|
|
|
|
ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
|
|
|
|
ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
|
|
|
|
ASSERT(buf[0] == '\0');
|
|
|
|
|
|
|
|
ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
|
|
|
|
ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -1);
|
|
|
|
|
|
|
|
ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
|
|
|
|
ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
|
|
|
|
}
|
|
|
|
|
2012-02-10 06:55:24 +08:00
|
|
|
int main(void) {
|
2012-09-19 19:23:54 +08:00
|
|
|
test_base64_encode();
|
2012-02-10 06:55:24 +08:00
|
|
|
test_match_prefix();
|
|
|
|
test_remove_double_dots();
|
2012-06-05 05:49:16 +08:00
|
|
|
test_should_keep_alive();
|
2012-06-08 04:57:40 +08:00
|
|
|
test_parse_http_request();
|
2012-07-28 18:04:03 +08:00
|
|
|
test_mg_fetch();
|
2012-09-22 19:22:30 +08:00
|
|
|
test_mg_get_var();
|
2012-01-24 06:03:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|