Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

wap-maps.c

Go to the documentation of this file.
00001 /* ==================================================================== 
00002  * The Kannel Software License, Version 1.0 
00003  * 
00004  * Copyright (c) 2001-2008 Kannel Group  
00005  * Copyright (c) 1998-2001 WapIT Ltd.   
00006  * All rights reserved. 
00007  * 
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions 
00010  * are met: 
00011  * 
00012  * 1. Redistributions of source code must retain the above copyright 
00013  *    notice, this list of conditions and the following disclaimer. 
00014  * 
00015  * 2. Redistributions in binary form must reproduce the above copyright 
00016  *    notice, this list of conditions and the following disclaimer in 
00017  *    the documentation and/or other materials provided with the 
00018  *    distribution. 
00019  * 
00020  * 3. The end-user documentation included with the redistribution, 
00021  *    if any, must include the following acknowledgment: 
00022  *       "This product includes software developed by the 
00023  *        Kannel Group (http://www.kannel.org/)." 
00024  *    Alternately, this acknowledgment may appear in the software itself, 
00025  *    if and wherever such third-party acknowledgments normally appear. 
00026  * 
00027  * 4. The names "Kannel" and "Kannel Group" must not be used to 
00028  *    endorse or promote products derived from this software without 
00029  *    prior written permission. For written permission, please  
00030  *    contact org@kannel.org. 
00031  * 
00032  * 5. Products derived from this software may not be called "Kannel", 
00033  *    nor may "Kannel" appear in their name, without prior written 
00034  *    permission of the Kannel Group. 
00035  * 
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00039  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
00040  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
00041  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
00042  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
00043  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
00044  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
00045  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
00046  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00047  * ==================================================================== 
00048  * 
00049  * This software consists of voluntary contributions made by many 
00050  * individuals on behalf of the Kannel Group.  For more information on  
00051  * the Kannel Group, please see <http://www.kannel.org/>. 
00052  * 
00053  * Portions of this software are based upon software originally written at  
00054  * WapIT Ltd., Helsinki, Finland for the Kannel project.  
00055  */ 
00056 
00057 /*
00058  * gw/wap-maps.c - URL mapping 
00059  * 
00060  * Bruno Rodrigues  <bruno.rodrigues@litux.org>
00061  */
00062 
00063 #include "gwlib/gwlib.h"
00064 #include "wap-maps.h"
00065 
00066 struct url_map_struct {
00067     Octstr *name;
00068     Octstr *url;
00069     Octstr *map_url;
00070     Octstr *send_msisdn_query;
00071     Octstr *send_msisdn_header;
00072     Octstr *send_msisdn_format;
00073     int accept_cookies;
00074 };
00075 
00076 struct user_map_struct {
00077     Octstr *name;
00078     Octstr *user;
00079     Octstr *pass;
00080     Octstr *msisdn;
00081 };
00082 
00083 
00084 /*
00085  * XXX All mapping functions should be implemented with Dicts instead of
00086  * Lists! Linear scans in lists are pretty slow against hash table lookups,
00087  * espacially when you have *lot* of entries, which is the case in URL re-
00088  * writting in general.
00089  * TODO: identify a hash key that can be used and use that as lookup.
00090  */
00091 
00092 /* mapping storrage */
00093 static List *url_map = NULL;
00094 static Dict *user_map = NULL;
00095 
00096 
00097 /********************************************************************
00098  * Creation and destruction of mapping entries
00099  */
00100 
00101 void wap_map_add_url(Octstr *name, Octstr *url, Octstr *map_url,
00102                      Octstr *send_msisdn_query,
00103                      Octstr *send_msisdn_header,
00104                      Octstr *send_msisdn_format,
00105                      int accept_cookies) {
00106     struct url_map_struct *entry;
00107 
00108     if (url_map == NULL) 
00109         url_map = gwlist_create();
00110 
00111     entry = gw_malloc(sizeof(*entry));
00112     entry->name = name;
00113     entry->url = url;
00114     entry->map_url = map_url;
00115     entry->send_msisdn_query = send_msisdn_query;
00116     entry->send_msisdn_header = send_msisdn_header;
00117     entry->send_msisdn_format = send_msisdn_format;
00118     entry->accept_cookies = accept_cookies;
00119     
00120     gwlist_append(url_map, entry);
00121 }
00122 
00123 
00124 static void wap_user_map_destroy(void *i) 
00125 {
00126     struct user_map_struct *entry = i;
00127 
00128     octstr_destroy(entry->name);
00129     octstr_destroy(entry->user);
00130     octstr_destroy(entry->pass);
00131     octstr_destroy(entry->msisdn);
00132     gw_free(entry);
00133 }
00134 
00135 
00136 void wap_map_add_user(Octstr *name, Octstr *user, Octstr *pass,
00137                       Octstr *msisdn) {
00138     struct user_map_struct *entry;
00139 
00140     if (user_map == NULL) 
00141         user_map = dict_create(32, wap_user_map_destroy);
00142 
00143     entry = gw_malloc(sizeof(*entry));
00144     entry->name = name;
00145     entry->user = user;
00146     entry->pass = pass;
00147     entry->msisdn = msisdn;
00148     dict_put(user_map, entry->user, entry);
00149 }
00150 
00151 
00152 void wap_map_destroy(void) 
00153 {
00154     long i;
00155     struct url_map_struct *entry;
00156 
00157     if (url_map != NULL) {
00158         for (i = 0; i < gwlist_len(url_map); i++) {
00159             entry = gwlist_get(url_map, i);
00160             octstr_destroy(entry->name);
00161             octstr_destroy(entry->url);
00162             octstr_destroy(entry->map_url);
00163             octstr_destroy(entry->send_msisdn_query);
00164             octstr_destroy(entry->send_msisdn_header);
00165             octstr_destroy(entry->send_msisdn_format);
00166             gw_free(entry);
00167         }
00168         gwlist_destroy(url_map, NULL);
00169     }
00170     url_map = NULL;
00171 }
00172 
00173 
00174 void wap_map_user_destroy(void)
00175 {
00176     dict_destroy(user_map);
00177     user_map = NULL;
00178 }
00179 
00180 
00181 /********************************************************************
00182  * Public functions
00183  */
00184 
00185 void wap_map_url_config(char *s)
00186 {
00187     char *in, *out;
00188     
00189     s = gw_strdup(s);
00190     in = strtok(s, " \t");
00191     if (!in) 
00192         return;
00193     out = strtok(NULL, " \t");
00194     if (!out) 
00195         return;
00196     wap_map_add_url(octstr_imm("unknown"), octstr_create(in), 
00197                      octstr_create(out), NULL, NULL, NULL, 0);
00198     gw_free(s);
00199 }
00200 
00201 void wap_map_url_config_device_home(char *to)
00202 {
00203     wap_map_add_url(octstr_imm("Device Home"), octstr_imm("DEVICE:home*"),
00204                      octstr_create(to), NULL, NULL, NULL, -1);
00205 }
00206 
00207 
00208 void wap_map_url(Octstr **osp, Octstr **send_msisdn_query, 
00209                  Octstr **send_msisdn_header, 
00210                  Octstr **send_msisdn_format, int *accept_cookies)
00211 {
00212     long i;
00213     Octstr *newurl, *tmp1, *tmp2;
00214 
00215     newurl = tmp1 = tmp2 = NULL;
00216     *send_msisdn_query = *send_msisdn_header = *send_msisdn_format = NULL;
00217     *accept_cookies = -1;
00218 
00219     debug("wsp",0,"WSP: Mapping url <%s>", octstr_get_cstr(*osp));
00220     for (i = 0; url_map && i < gwlist_len(url_map); i++) {
00221         struct url_map_struct *entry;
00222         entry = gwlist_get(url_map, i);
00223 
00224         /* 
00225         debug("wsp",0,"WSP: matching <%s> with <%s>", 
00226               octstr_get_cstr(entry->url), octstr_get_cstr(entry->map_url)); 
00227         */
00228 
00229         /* DAVI: I only have '*' terminated entry->url implementation for now */
00230         tmp1 = octstr_duplicate(entry->url);
00231         octstr_delete(tmp1, octstr_len(tmp1)-1, 1); /* remove last '*' */
00232         tmp2 = octstr_copy(*osp, 0, octstr_len(tmp1));
00233 
00234         debug("wsp",0,"WSP: Matching <%s> with <%s>", 
00235               octstr_get_cstr(tmp1), octstr_get_cstr(tmp2));
00236 
00237         if (octstr_case_compare(tmp2, tmp1) == 0) {
00238             /* rewrite url if configured to do so */
00239             if (entry->map_url != NULL) {
00240                 if (octstr_get_char(entry->map_url, 
00241                                     octstr_len(entry->map_url)-1) == '*') {
00242                     newurl = octstr_duplicate(entry->map_url);
00243                     octstr_delete(newurl, octstr_len(newurl)-1, 1);
00244                     octstr_append(newurl, octstr_copy(*osp, 
00245                     octstr_len(entry->url)-1, 
00246                     octstr_len(*osp)-octstr_len(entry->url)+1));
00247                 } else {
00248                     newurl = octstr_duplicate(entry->map_url);
00249                 }
00250                 debug("wsp",0,"WSP: URL Rewriten from <%s> to <%s>", 
00251                       octstr_get_cstr(*osp), octstr_get_cstr(newurl));
00252                 octstr_destroy(*osp);
00253                 *osp = newurl;
00254             }
00255             *accept_cookies = entry->accept_cookies;
00256             *send_msisdn_query = octstr_duplicate(entry->send_msisdn_query);
00257             *send_msisdn_header = octstr_duplicate(entry->send_msisdn_header);
00258             *send_msisdn_format = octstr_duplicate(entry->send_msisdn_format);
00259             octstr_destroy(tmp1);
00260             octstr_destroy(tmp2);
00261             break;
00262         }
00263         octstr_destroy(tmp1);
00264         octstr_destroy(tmp2);
00265     }
00266 }
00267 
00268 int wap_map_user(Octstr **msisdn, Octstr *user, Octstr *pass)
00269 {
00270     struct user_map_struct *entry;
00271 
00272     entry = dict_get(user_map, user);
00273     if (entry != NULL && octstr_compare(pass, entry->pass) == 0) {
00274         *msisdn = octstr_duplicate(entry->msisdn);
00275         return 1;
00276     }
00277     return 0;
00278 }
00279 
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.