author | Tero Marttila <terom@fixme.fi> |
Wed, 08 Oct 2008 22:05:13 +0300 | |
changeset 15 | a8d183e79ed9 |
parent 14 | 115067dfba55 |
child 16 | 74fb62022fb3 |
permissions | -rw-r--r-- |
13
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
#ifndef LIB_URL_H |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
#define LIB_URL_H |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
|
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
/* |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
* A trivial parser for simple URLs |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
* |
14 | 7 |
* [ <scheme> [ "+" <scheme> [ ... ] ] "://" ] [ <username> [ ":" <password> ] "@" ] [ <hostname> ] [ ":" <service> ] [ "/" <path> ] [ "?" [ <key> [ "=" <value> ] ] [ "&" [ <key> [ "=" <value> ] ] [ ... ] ] |
13
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
* |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
* example.com |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
* tcp://example.com:7348/ |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
* psql://postgres@localhost/test_db?charset=utf8 |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
* |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
*/ |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
|
15 | 15 |
#include <sys/types.h> |
16 |
||
13
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
/* |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
18 |
* The schema |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
*/ |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
struct url_schema { |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
21 |
size_t count; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
const char **list; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
23 |
}; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
|
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
25 |
/* |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
26 |
* The options at the end |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
27 |
*/ |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
28 |
struct url_opts { |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
29 |
size_t count; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
30 |
struct url_opt { |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
31 |
const char *key; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
32 |
const char *value; |
15 | 33 |
} **list; |
13
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
34 |
}; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
35 |
|
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
/* |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
* A parsed URL |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
38 |
*/ |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
39 |
struct url { |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
40 |
struct url_schema *schema; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
41 |
const char *username; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
42 |
const char *password; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
43 |
const char *hostname; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
44 |
const char *service; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
45 |
const char *path; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
46 |
struct url_opts *opts; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
47 |
}; |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
48 |
|
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
49 |
/* |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
50 |
* Parse the given `text` as an URL, returning the result in `url`. Optional fields that are missing in the text will |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
* cause those values to be returned unmodified. |
14 | 52 |
* |
53 |
* Returns zero if the url was valid and was parsed, nonzero if it was invalid. |
|
13
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
54 |
*/ |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
55 |
int url_parse (struct url *url, const char *text); |
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
56 |
|
385b9a10d096
inital playing around with a lexer/url parser
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
#endif /* LIB_URL_H */ |