Skip to content

Commit 1320c9a

Browse files
committed
Provide middlewares for content negociation
They will make more sense with #133, but right now they can be used with 'Router.matcher'. Add minimal tests.
1 parent ad76df8 commit 1320c9a

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

src/valum-negociate.vala

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* This file is part of Valum.
3+
*
4+
* Valum is free software: you can redistribute it and/or modify it under the
5+
* terms of the GNU Lesser General Public License as published by the Free
6+
* Software Foundation, either version 3 of the License, or (at your option) any
7+
* later version.
8+
*
9+
* Valum is distributed in the hope that it will be useful, but WITHOUT ANY
10+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11+
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12+
* details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with Valum. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
using Soup;
19+
20+
namespace Valum {
21+
22+
/**
23+
* Negociate a HTTP header against a given expectation.
24+
*
25+
* The header is extracted as a quality list and a lookup is performed to
26+
* see if the expected value is accepted by the user agent.
27+
*
28+
* @since 0.3
29+
*
30+
* @param header_name header to negociate
31+
* @param expectation expected value in the quality list
32+
*/
33+
public MatcherCallback negociate (string header_name, string expectation) {
34+
return (req) => {
35+
var header = req.headers.get_list (header_name);
36+
return header != null && header_parse_quality_list (header, null).find_custom (expectation, strcmp) != null;
37+
};
38+
}
39+
40+
/**
41+
* @since 0.3
42+
*/
43+
public MatcherCallback accept (string content_type) {
44+
return negociate ("Accept", content_type);
45+
}
46+
47+
/**
48+
* @since 0.3
49+
*/
50+
public MatcherCallback accept_charset (string charset) {
51+
return negociate ("Accept-Charset", charset);
52+
}
53+
54+
/**
55+
* @since 0.3
56+
*/
57+
public MatcherCallback accept_encoding (string encoding) {
58+
return negociate ("Accept-Encoding", encoding);
59+
}
60+
61+
/**
62+
* @since 0.3
63+
*/
64+
public MatcherCallback accept_language (string language) {
65+
return negociate ("Accept-Language", language);
66+
}
67+
68+
/**
69+
* @since 0.3
70+
*/
71+
public MatcherCallback accept_ranges (string ranges) {
72+
return negociate ("Accept-Ranges", ranges);
73+
}
74+
}

tests/negociate-test.vala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Valum;
2+
using VSGI.Test;
3+
4+
/**
5+
* @since 0.3
6+
*/
7+
public void test_negociate () {
8+
var req = new Request (new Connection (), "GET", new Soup.URI ("http://localhost/"));
9+
10+
req.headers.append ("Accept", "text/html; q=0.9, text/xml; q=0");
11+
12+
assert (negociate ("Accept", "text/html") (req, null));
13+
assert (!negociate ("Accept", "text/xml") (req, null));
14+
assert (!negociate ("Accept-Encoding", "utf-8") (req, null));
15+
}
16+
17+

tests/tests.vala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public int main (string[] args) {
2424
Test.init (ref args);
2525
Test.bug_base ("https://github.com/valum-framework/valum/issues/%s");
2626

27+
Test.add_func ("/negociate", test_negociate);
28+
2729
Test.add_func ("/router", test_router);
2830
Test.add_func ("/router/handle", test_router_handle);
2931
Test.add_func ("/router/custom_method", test_router_custom_method);

0 commit comments

Comments
 (0)