OSDN Git Service

a77223fea911a665a23e4a80320e956346a30c7b
[modchxj/mod_chxj.git] / src / chxj_specified_device.c
1 /*
2  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
3  * Copyright (C) 2005 Atsushi Konno All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include "mod_chxj.h"
18
19 static device_table_t  UNKNOWN_DEVICE      = {
20     NULL, "","UNKNOWN", CHXJ_SPEC_UNKNOWN,  0,  0,0,0,0,0,0,0,0,0,0,0,0, ""};
21
22 /**
23  * The device is specified from UserAgent. 
24  * @param r Request_rec is appointed.
25  * @param userAgent UserAgent is appointed here,
26  * @return The style which corresponds is returned.
27  */
28 device_table_t*
29 chxj_specified_device(request_rec* r, const char* user_agent) 
30 {
31   ap_regmatch_t        match[10];
32   device_table_t*      returnType = &UNKNOWN_DEVICE;
33   device_table_list_t* dtl;
34   device_table_t*      dt;
35   mod_chxj_config*     conf; 
36   char*                device_id;
37
38   if (! user_agent) 
39     return returnType;
40             
41
42   DBG(r, "start chxj_specified_device()");
43
44   conf = ap_get_module_config(r->per_dir_config, &chxj_module);
45
46   for (dtl = conf->devices; dtl; dtl = dtl->next) {
47     if (! dtl->pattern) {
48       DBG(r, "pattern is null");
49       continue;
50     }
51
52     DBG1(r, "pattern is [%s]", dtl->pattern);
53     if (! dtl->regexp) {
54       DBG(r,"compile failed.");
55       return returnType;
56     }
57
58     if (ap_regexec(dtl->regexp, user_agent, dtl->regexp->re_nsub + 1, match, 0) == 0) {
59       device_id = ap_pregsub(r->pool, "$1", user_agent, dtl->regexp->re_nsub + 1, match);
60       DBG1(r, "device_id:[%s]", device_id);
61       for (dt = dtl->table; dt; dt = dt->next) {
62         if (strcasecmp(device_id, dt->device_id) == 0) {
63           DBG1(r, "device_name:[%s]", dt->device_name);
64           returnType = dt;
65           break;
66         }
67       }
68
69       if (! dt) {
70         for (dt = dtl->table; dt; dt = dt->next) {
71           if (dt->next == NULL)
72             break;
73         }
74
75         if (dt)
76           returnType = dt;
77       }
78     }
79
80     if (returnType != &UNKNOWN_DEVICE) {
81       DBG(r,"end chxj_specified_device()");
82       return returnType;
83     }
84   }
85
86   DBG(r,"end chxj_specified_device()");
87
88   return returnType;
89 }
90
91 /*
92  * vim:ts=2 et
93  */
94