OSDN Git Service

*** empty log message ***
[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 #include "ap_regex.h"
19
20 static device_table_t  UNKNOWN_DEVICE      = {
21     NULL, "","UNKNOWN", CHXJ_SPEC_UNKNOWN,  0,  0,0,0,0,0,0,0,0,0,0,0,0, ""};
22
23 /**
24  * The device is specified from UserAgent. 
25  * @param r Request_rec is appointed.
26  * @param userAgent UserAgent is appointed here,
27  * @return The style which corresponds is returned.
28  */
29 device_table_t*
30 chxj_specified_device(request_rec* r, const char* user_agent) 
31 {
32   ap_regex_t*          regexp;
33   ap_regmatch_t        match[10];
34   device_table_t*      returnType = &UNKNOWN_DEVICE;
35   device_table_list_t* dtl;
36   device_table_t* dt;
37   mod_chxj_config_t* conf; 
38   int rtn;
39   char* device_id;
40
41   ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "start chxj_specified_device()");
42   conf = ap_get_module_config(r->per_dir_config, &chxj_module);
43   for (dtl = conf->devices; dtl; dtl = dtl->next) 
44   {
45     if (dtl->pattern == NULL)
46     {
47       ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "pattern is null");
48       continue;
49     }
50
51     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "pattern is [%s]", dtl->pattern);
52     regexp = ap_pregcomp(r->pool, (const char*)dtl->pattern, AP_REG_EXTENDED|AP_REG_ICASE);
53     if (regexp == NULL) 
54     {
55       ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "compile failed.");
56       return returnType;
57     }
58
59     rtn = ap_regexec(regexp, user_agent, regexp->re_nsub + 1, match, 0);
60     if (rtn == 0) 
61     {
62       device_id = ap_pregsub(r->pool, "$1", user_agent, regexp->re_nsub + 1, match);
63       ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "device_id:[%s]", device_id);
64       for (dt = dtl->table; dt; dt = dt->next) 
65       {
66         if (strcasecmp(device_id, dt->device_id) == 0) 
67         {
68           ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "device_name:[%s]", dt->device_name);
69           returnType = dt;
70           break;
71         }
72       }
73       if (dt == NULL) 
74       {
75         for (dt = dtl->table; dt; dt = dt->next) 
76         {
77           if (dt->next == NULL) {
78             break;
79           }
80         }
81         if (dt != NULL)
82         {
83           returnType = dt;
84         }
85       }
86     }
87     ap_pregfree(r->pool, regexp);
88     if (returnType != &UNKNOWN_DEVICE) 
89     {
90       ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "end chxj_specified_device()");
91       return returnType;
92     }
93   }
94   ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "end chxj_specified_device()");
95
96   return returnType;
97 }
98
99 /*
100  * vim:ts=2 et
101  */
102