OSDN Git Service

* version up for RELEASE
[modchxj/mod_chxj.git] / src / chxj_jreserved_tag.c
1 /*
2  * Copyright (C) 2005-2011 Atsushi Konno All rights reserved.
3  * Copyright (C) 2005 QSDN,Inc. 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 "chxj_jreserved_tag.h"
19 #include "chxj_url_encode.h"
20
21 typedef struct r_table {
22   char lower;
23   char upper;
24   char *name;
25 } r_table_t;
26
27 static r_table_t reserved_start_with_table[] = {
28   {
29     .lower = 'j',
30     .upper = 'J',
31     .name  = "jsky",
32   },
33 };
34
35
36 #define RESERVED_NELT            (16)
37 #define RESERVED_NELT_START_WITH (1)
38
39 static r_table_t reserved_table[] = {
40   {
41     .lower = 'p',
42     .upper = 'P',
43     .name = "pid",
44   },
45   {
46     .lower = 's',
47     .upper = 'S',
48     .name = "sid",
49   },
50   { 
51     .lower = 'u',
52     .upper = 'U',
53     .name  = "uid",
54   },
55   {
56     .lower = 'l',
57     .upper = 'L',
58     .name  = "lid",
59   },
60   {
61     .lower = 'g',
62     .upper = 'G',
63     .name  = "gid",
64   },
65   {
66     .lower = 'r',
67     .upper = 'R',
68     .name  = "rpid",
69   },
70   {
71     .lower = 'r',
72     .upper = 'R',
73     .name  = "rsid",
74   },
75   {
76     .lower = 'n',
77     .upper = 'N',
78     .name  = "nl",
79   },
80   {
81     .lower = 'c',
82     .upper = 'C',
83     .name  = "cl",
84   },
85   {
86     .lower = 'o',
87     .upper = 'O',
88     .name  = "ol",
89   },
90   {
91     .lower = 'p',
92     .upper = 'P',
93     .name  = "pl",
94   },
95   {
96     .lower = 'p',
97     .upper = 'P',
98     .name  = "prc",
99   },
100   {
101     .lower = 'c',
102     .upper = 'C',
103     .name  = "cnt",
104   },
105   {
106     .lower = 'r',
107     .upper = 'R',
108     .name  = "reg",
109   },
110   {
111     .lower = 'v',
112     .upper = 'V',
113     .name  = "vsekey",
114   },
115   {
116     .lower = 'v',
117     .upper = 'V',
118     .name  = "vsernk",
119   },
120 };
121
122
123 int
124 chxj_is_jreserved_tag(const char *src) 
125 {
126   int ii;
127   for (ii=0;ii<RESERVED_NELT;ii++) {
128     if (STRCASEEQ(reserved_table[ii].lower,
129                   reserved_table[ii].upper,
130                   reserved_table[ii].name, 
131                   src)) {
132       return 1;
133     }
134   }
135   return 0;
136 }
137
138 char *
139 chxj_jreserved_to_safe_tag(request_rec *r, const char *src, chxjconvrule_entry *entryp)
140 {
141   int ii;
142   if (entryp->action & CONVRULE_JRCONV_OFF_BIT) {
143     return (char *)src;
144   }
145   for (ii=0;ii<RESERVED_NELT;ii++) {
146     if (STRCASEEQ(reserved_table[ii].lower,
147                   reserved_table[ii].upper,
148                   reserved_table[ii].name, 
149                   src)) {
150       return apr_psprintf(r->pool, "%s%s", CHXJ_SOFTBANK_RESERVED_TAG_PREFIX, reserved_table[ii].name);
151     }
152   }
153   for (ii=0; ii<RESERVED_NELT_START_WITH; ii++) {
154     if (strncasecmp(reserved_start_with_table[ii].name, src, sizeof(reserved_start_with_table[ii].name)) == 0) {
155       return apr_psprintf(r->pool, "%s%s", CHXJ_SOFTBANK_RESERVED_TAG_PREFIX, src);
156     }
157   }
158   return (char *)src;
159 }
160
161
162 char *
163 chxj_safe_to_jreserved_tag(request_rec *r, const char *src)
164 {
165   if (strncasecmp(CHXJ_SOFTBANK_RESERVED_TAG_PREFIX, src, sizeof(CHXJ_SOFTBANK_RESERVED_TAG_PREFIX)-1) == 0) {
166     return apr_pstrdup(r->pool, &src[sizeof(CHXJ_SOFTBANK_RESERVED_TAG_PREFIX)-1]);
167   }
168   return (char *)src;
169 }
170
171 char *
172 chxj_jreserved_tag_to_safe_for_query_string(request_rec *r, const char *query_string, chxjconvrule_entry  *entryp, int xmlflag)
173 {
174   apr_pool_t *pool;
175   apr_pool_create(&pool, r->pool);
176   char *s = apr_pstrdup(pool, query_string);
177   char *fname;
178
179   if (entryp->action & CONVRULE_JRCONV_OFF_BIT) {
180     return s;
181   }
182   if (!s) return apr_pstrdup(pool, "");
183   char *result = s;
184   s = strchr(s, '?');
185   if (!s) return result;
186   *s = 0;
187   s++;
188   fname = apr_pstrdup(pool, result);
189   result = NULL;
190         
191         char *pstat;
192   char *pstat2;
193   for (;;) {
194     char *pair = NULL;
195     pair = apr_strtok(s, "&", &pstat);
196     
197     if (! pair) break;
198     s = NULL;
199                 
200                 if(strncasecmp(pair,"amp;",4) == 0){
201                         pair += 4;
202                 }
203                 
204     char *key = apr_strtok(pair, "=",  &pstat2);
205     char *val = "";
206     if (key) {
207       val = apr_strtok(NULL, "=", &pstat2);
208                         
209       if (!val) val = "";
210     }
211     char *tmp = NULL;
212     if (strcasecmp(key, "guid") == 0) {
213       tmp = apr_psprintf(pool, "%s=%s", key, val);
214       if (result) {
215                                 if(xmlflag)
216                                         result = apr_pstrcat(pool, result, "&amp;" ,tmp, NULL);
217                                 else
218                 result = apr_pstrcat(pool, result, "&" ,tmp, NULL);
219       }
220       else {
221         result = tmp;
222       }
223     }
224     else {
225       tmp = apr_psprintf(pool, "%s=%s", chxj_jreserved_to_safe_tag(r, key, entryp), chxj_url_decode(pool, val));
226       if (result) {
227         if (xmlflag) {
228           result = apr_pstrcat(pool, result, "&amp;" ,tmp, NULL);
229         }
230         else {
231           result = apr_pstrcat(pool, result, "&" ,tmp, NULL);
232         }
233       }
234       else {
235         result = tmp;
236       }
237     }
238   }
239   return apr_pstrcat(pool, fname, "?" , result, NULL);
240 }