]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/web-util.c
util-lib: move character class definitions to string-util.h
[thirdparty/systemd.git] / src / basic / web-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdbool.h>
23
24 #include "string-util.h"
25 #include "utf8.h"
26 #include "web-util.h"
27
28 bool http_etag_is_valid(const char *etag) {
29 if (isempty(etag))
30 return false;
31
32 if (!endswith(etag, "\""))
33 return false;
34
35 if (!startswith(etag, "\"") && !startswith(etag, "W/\""))
36 return false;
37
38 return true;
39 }
40
41 bool http_url_is_valid(const char *url) {
42 const char *p;
43
44 if (isempty(url))
45 return false;
46
47 p = startswith(url, "http://");
48 if (!p)
49 p = startswith(url, "https://");
50 if (!p)
51 return false;
52
53 if (isempty(p))
54 return false;
55
56 return ascii_is_valid(p);
57 }
58
59 bool documentation_url_is_valid(const char *url) {
60 const char *p;
61
62 if (isempty(url))
63 return false;
64
65 if (http_url_is_valid(url))
66 return true;
67
68 p = startswith(url, "file:/");
69 if (!p)
70 p = startswith(url, "info:");
71 if (!p)
72 p = startswith(url, "man:");
73
74 if (isempty(p))
75 return false;
76
77 return ascii_is_valid(p);
78 }