File: Apache2__Directive.h

package info (click to toggle)
libapache2-mod-perl2 2.0.9~1624218-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 11,840 kB
  • sloc: perl: 95,064; ansic: 14,522; makefile: 49; sh: 18
file content (204 lines) | stat: -rw-r--r-- 5,935 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define mpxs_Apache2__Directive_conftree() ap_conftree

/* XXX: this is only useful for <Perl> at the moment */
static MP_INLINE SV *mpxs_Apache2__Directive_as_string(pTHX_
                                                      ap_directive_t *self)
{
    ap_directive_t *d;
    SV *sv = newSVpv("", 0);

    for (d = self->first_child; d; d = d->next) {
        sv_catpv(sv, d->directive);
        sv_catpv(sv, " ");
        sv_catpv(sv, d->args);
        sv_catpv(sv, "\n");
    }

    return sv;
}


/* Adds an entry to a hash, vivifying hash/array for multiple entries */
static void hash_insert(pTHX_ HV *hash, const char *key,
                        int keylen, const char *args,
                        int argslen, SV *value)
{
    HV *subhash;
    AV *args_array;
    SV **hash_ent = hv_fetch(hash, key, keylen, 0);

    if (value) {
        if (!hash_ent) {
            subhash = newHV();
            (void)hv_store(hash, key, keylen, newRV_noinc((SV *)subhash), 0);
        }
        else {
            subhash = (HV *)SvRV(*hash_ent);
        }

        (void)hv_store(subhash, args, argslen, value, 0);
    }
    else {
        if (hash_ent) {
            if (SvROK(*hash_ent) && (SVt_PVAV == SvTYPE(SvRV(*hash_ent)))) {
                args_array = (AV *)SvRV(*hash_ent);
            }
            else {
                args_array = newAV();
                av_push(args_array, newSVsv(*hash_ent));
                (void)hv_store(hash, key, keylen,
                               newRV_noinc((SV *)args_array), 0);
            }
            av_push(args_array, newSVpv(args, argslen));
        }
        else {
            (void)hv_store(hash, key, keylen, newSVpv(args, argslen), 0);
        }
    }
}

static MP_INLINE SV *mpxs_Apache2__Directive_as_hash(pTHX_
                                                    ap_directive_t *tree)
{
    const char *directive;
    int directive_len;
    const char *args;
    int args_len;

    HV *hash = newHV();
    SV *subtree;

    while (tree) {
        directive = tree->directive;
        directive_len = strlen(directive);
        args = tree->args;
        args_len = strlen(args);

        if (tree->first_child) {

            /* Skip the prefix '<' */
            if ('<' == directive[0]) {
                directive++;
                directive_len--;
            }

            /* Skip the postfix '>' */
            if ('>' == args[args_len-1]) {
                args_len--;
            }

            subtree = mpxs_Apache2__Directive_as_hash(aTHX_ tree->first_child);
            hash_insert(aTHX_ hash, directive, directive_len,
                        args, args_len, subtree);
        }
        else {
            hash_insert(aTHX_ hash, directive, directive_len,
                        args, args_len, (SV *)NULL);
        }

        tree = tree->next;
    }

    return newRV_noinc((SV *)hash);
}

MP_STATIC XS(MPXS_Apache2__Directive_lookup)
{
    dXSARGS;

    if (items < 2 || items > 3) {
            Perl_croak(aTHX_
                       "Usage: Apache2::Directive::lookup(self, key, [args])");
    }

    mpxs_PPCODE({
        Apache2__Directive tree;
        char *value;
        const char *directive;
        const char *args;
        int args_len;
        int directive_len;

        char *key = (char *)SvPV_nolen(ST(1));
        int scalar_context = (G_SCALAR == GIMME_V);

            if (SvROK(ST(0)) && sv_derived_from(ST(0), "Apache2::Directive")) {
                IV tmp = SvIV((SV*)SvRV(ST(0)));
                tree = INT2PTR(Apache2__Directive,tmp);
            }
            else {
                tree = ap_conftree;
            }

            if (items < 3) {
                value = NULL;
            }
            else {
                value = (char *)SvPV_nolen(ST(2));
            }

        while (tree) {
            directive = tree->directive;
            directive_len = strlen(directive);

            /* Remove starting '<' for container directives */
            if (directive[0] == '<') {
                directive++;
                directive_len--;
            }

            if (0 == strncasecmp(directive, key, directive_len)) {

                if (value) {
                    args = tree->args;
                    args_len = strlen(args);

                    /* Skip the postfix '>' */
                    if ('>' == args[args_len-1]) {
                        args_len--;
                    }

                }

                if ( (!value) || (0 == strncasecmp(args, value, args_len)) ) {
                    if (tree->first_child) {
                        XPUSHs(sv_2mortal(mpxs_Apache2__Directive_as_hash(
                                              aTHX_ tree->first_child)));
                    }
                    else {
                       XPUSHs(sv_2mortal(newSVpv(tree->args, 0)));
                    }

                    if (scalar_context) {
                        break;
                    }
                }
            }

            tree = tree->next ? tree->next : NULL;
        }
    });
}

/*
 * Local Variables:
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */