From cdcb56c3fbcd7f9319b128b0a0fdaf0e005b18e8 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 16 Nov 2015 13:56:06 +0000 Subject: [PATCH] Fix parsing of fragment in urls --- src/libserver/url.c | 7 ++++++- test/lua/unit/url.lua | 21 ++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/libserver/url.c b/src/libserver/url.c index 95773baa0..186697483 100644 --- a/src/libserver/url.c +++ b/src/libserver/url.c @@ -754,7 +754,7 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, } break; case parse_domain: - if (t == '/' || t == ':' || t == '?') { + if (t == '/' || t == ':' || t == '?' || t == '#') { if (p - c == 0) { goto out; } @@ -767,6 +767,11 @@ rspamd_web_parse (struct http_parser_url *u, const gchar *str, gsize len, st = parse_query; c = p + 1; } + else if (t == '#') { + SET_U (u, UF_HOST); + st = parse_part; + c = p + 1; + } else if (!user_seen) { /* * Here we can have both port and password, hence we need diff --git a/test/lua/unit/url.lua b/test/lua/unit/url.lua index 9836ed127..e6b112870 100644 --- a/test/lua/unit/url.lua +++ b/test/lua/unit/url.lua @@ -9,7 +9,7 @@ context("URL check functions", function() void rspamd_url_init (const char *tld_file); unsigned ottery_rand_range(unsigned top); ]] - + local test_dir = string.gsub(debug.getinfo(1).source, "^@(.+/)[^/]+$", "%1") ffi.C.rspamd_url_init(string.format('%s/%s', test_dir, "test_tld.dat")) @@ -33,21 +33,21 @@ context("URL check functions", function() for _,c in ipairs(cases) do local res = url.create(pool, c[1]) - + assert_not_nil(res, "cannot parse " .. c[1]) local t = res:to_table() --local s = logger.slog("%1 -> %2", c[1], t) --print(s) assert_not_nil(t, "cannot convert to table " .. c[1]) assert_equal(c[2][1], t['host']) - + if c[2][2] then assert_equal(c[2][2], t['user']) end end pool:destroy() end) - + -- Some cases from https://code.google.com/p/google-url/source/browse/trunk/src/url_canon_unittest.cc test("Parse urls", function() local pool = mpool.create() @@ -81,16 +81,19 @@ context("URL check functions", function() {"http://[::eeee:192.168.0.1]", true, { host = '::eeee:c0a8:1' }}, + {"http://twitter.com#test", true, { + host = 'twitter.com', fragment = 'test' + }}, } - + for _,c in ipairs(cases) do local res = url.create(pool, c[1]) - + if c[2] then assert_not_nil(res, "cannot parse " .. c[1]) - + local uf = res:to_table() - + for k,v in pairs(c[3]) do assert_not_nil(uf[k], k .. ' is missing in url, must be ' .. v) assert_equal(uf[k], v, 'expected ' .. v .. ' for ' .. k .. ' but got ' .. uf[k] .. ' in url ' .. c[1]) @@ -106,4 +109,4 @@ context("URL check functions", function() end end ) -end) \ No newline at end of file +end) -- 2.39.5