-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* This file is part of Valum. | ||
* | ||
* Valum is free software: you can redistribute it and/or modify it under the | ||
* terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* Valum is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Valum. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
using Soup; | ||
|
||
namespace Valum { | ||
|
||
/** | ||
* Negociate a HTTP header against a given expectation. | ||
* | ||
* The header is extracted as a quality list and a lookup is performed to | ||
* see if the expected value is accepted by the user agent. | ||
* | ||
* @since 0.3 | ||
* | ||
* @param header_name header to negociate | ||
* @param expectation expected value in the quality list | ||
*/ | ||
public MatcherCallback negociate (string header_name, string expectation) { | ||
return (req) => { | ||
var header = req.headers.get_list (header_name); | ||
return header != null && header_parse_quality_list (header, null).find_custom (expectation, strcmp) != null; | ||
}; | ||
} | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public MatcherCallback accept (string content_type) { | ||
return negociate ("Accept", content_type); | ||
} | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public MatcherCallback accept_charset (string charset) { | ||
return negociate ("Accept-Charset", charset); | ||
} | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public MatcherCallback accept_encoding (string encoding) { | ||
return negociate ("Accept-Encoding", encoding); | ||
} | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public MatcherCallback accept_language (string language) { | ||
return negociate ("Accept-Language", language); | ||
} | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public MatcherCallback accept_ranges (string ranges) { | ||
return negociate ("Accept-Ranges", ranges); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Valum; | ||
using VSGI.Test; | ||
|
||
/** | ||
* @since 0.3 | ||
*/ | ||
public void test_negociate () { | ||
var req = new Request (new Connection (), "GET", new Soup.URI ("http://localhost/")); | ||
|
||
req.headers.append ("Accept", "text/html; q=0.9, text/xml; q=0"); | ||
|
||
assert (negociate ("Accept", "text/html") (req, null)); | ||
assert (!negociate ("Accept", "text/xml") (req, null)); | ||
assert (!negociate ("Accept-Encoding", "utf-8") (req, null)); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters