1 /* Domain-specific options infrastructure */
11 #include "config/domain.h"
12 #include "config/options.h"
13 #include "protocol/uri.h"
14 #include "session/location.h"
15 #include "session/session.h"
16 #include "util/lists.h"
17 #include "viewer/text/vs.h"
20 INIT_LIST_OF(struct domain_tree
, domain_trees
);
23 /* Look for the option with the given name in all domain shadow-trees that
24 * match the given domain-name. Return the option from the the shadow tree
25 * that best matches the given domain name. */
26 static struct option
*
27 get_domain_option(unsigned char *domain_name
, int domain_len
,
30 struct option
*opt
, *longest_match_opt
= NULL
;
31 struct domain_tree
*longest_match
= NULL
;
32 struct domain_tree
*domain
;
37 foreach (domain
, domain_trees
)
38 if ((!longest_match
|| domain
->len
> longest_match
->len
)
39 && is_in_domain(domain
->name
, domain_name
, domain_len
)
40 && (opt
= get_opt_rec_real(domain
->tree
, name
))) {
41 longest_match
= domain
;
42 longest_match_opt
= opt
;
45 return longest_match_opt
;
49 get_domain_option_from_session(unsigned char *name
, struct session
*ses
)
56 if (!have_location(ses
))
59 uri
= cur_loc(ses
)->vs
.uri
;
60 if (!uri
->host
|| !uri
->hostlen
)
63 return get_domain_option(uri
->host
, uri
->hostlen
, name
);
66 /* Return the shadow shadow tree for the given domain name, and
67 * if the domain does not yet have a shadow tree, create it. */
69 get_domain_tree(unsigned char *domain_name
)
71 struct domain_tree
*domain
;
77 foreach (domain
, domain_trees
)
78 if (!strcasecmp(domain
->name
, domain_name
))
81 domain_len
= strlen(domain_name
);
82 /* One byte is reserved for domain in struct domain_tree. */
83 domain
= mem_alloc(sizeof(*domain
) + domain_len
);
84 if (!domain
) return NULL
;
86 domain
->tree
= copy_option(config_options
, CO_SHALLOW
87 | CO_NO_LISTBOX_ITEM
);
93 memcpy(domain
->name
, domain_name
, domain_len
+ 1);
94 domain
->len
= domain_len
;
96 add_to_list(domain_trees
, domain
);
102 done_domain_trees(void)
104 struct domain_tree
*domain
, *next
;
106 foreachsafe (domain
, next
, domain_trees
) {
107 delete_option(domain
->tree
);
109 del_from_list(domain
);