|
| 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 | +} |
0 commit comments