equal
deleted
inserted
replaced
1 #ifndef LIB_URL_H |
|
2 #define LIB_URL_H |
|
3 |
|
4 /* |
|
5 * A trivial parser for simple URLs |
|
6 * |
|
7 * [ <scheme> [ "+" <scheme> [ ... ] ] "://" ] [ <username> [ ":" <password> ] "@" ] [ <hostname> ] [ ":" <service> ] [ "/" <path> ] [ "?" [ <key> [ "=" <value> ] ] [ "&" [ <key> [ "=" <value> ] ] [ ... ] ] |
|
8 * |
|
9 * example.com |
|
10 * tcp://example.com:7348/ |
|
11 * psql://postgres@localhost/test_db?charset=utf8 |
|
12 * |
|
13 */ |
|
14 |
|
15 #include <sys/types.h> |
|
16 #include <stdio.h> |
|
17 |
|
18 /* |
|
19 * The schema |
|
20 */ |
|
21 struct url_schema { |
|
22 size_t count; |
|
23 const char *list[]; |
|
24 }; |
|
25 |
|
26 /* |
|
27 * The options at the end |
|
28 */ |
|
29 struct url_opts { |
|
30 size_t count; |
|
31 struct url_opt { |
|
32 const char *key; |
|
33 const char *value; |
|
34 } list[]; |
|
35 }; |
|
36 |
|
37 /* |
|
38 * A parsed URL |
|
39 */ |
|
40 struct url { |
|
41 struct url_schema *schema; |
|
42 const char *username; |
|
43 const char *password; |
|
44 const char *hostname; |
|
45 const char *service; |
|
46 const char *path; |
|
47 struct url_opts *opts; |
|
48 }; |
|
49 |
|
50 /* |
|
51 * Parse the given `text` as an URL, returning the result in `url`. Optional fields that are missing in the text will |
|
52 * cause those values to be returned unmodified. |
|
53 * |
|
54 * Returns zero if the url was valid and was parsed, nonzero if it was invalid. |
|
55 */ |
|
56 int url_parse (struct url *url, const char *text); |
|
57 |
|
58 /* |
|
59 * Prints a url in a debug-output format. |
|
60 */ |
|
61 void url_dump (const struct url *url, FILE *stream); |
|
62 |
|
63 #endif /* LIB_URL_H */ |
|