OSDN Git Service

* Fixed Bug.
[modchxj/mod_chxj.git] / src / chxj_specified_device.c
index 6de4e7d..68a97a7 100644 (file)
@@ -1,6 +1,6 @@
 /*
+ * Copyright (C) 2005-2011 Atsushi Konno All rights reserved.
  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
- * Copyright (C) 2005 Atsushi Konno All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 #include "mod_chxj.h"
+#include "chxj_add_device_env.h"
 
 static device_table  UNKNOWN_DEVICE      = {
   .next = NULL,
@@ -57,6 +58,7 @@ static device_table  UNKNOWN_DEVICE      = {
   .color = 15680000,
   .emoji_type = NULL,
 };
+static device_table * s_get_device_data(request_rec *r, const char *device_id, device_table_list *dtl);
 
 /**
  * The device is specified from UserAgent. 
@@ -64,73 +66,109 @@ static device_table  UNKNOWN_DEVICE      = {
  * @param userAgent UserAgent is appointed here,
  * @return The style which corresponds is returned.
  */
-device_table*
-chxj_specified_device(request_rec* r, const char* user_agent) 
+device_table *
+chxj_specified_device(request_rec *r, const char *user_agent) 
 {
   ap_regmatch_t        match[10];
-  device_table*        returnType = &UNKNOWN_DEVICE;
-  device_table_list*   dtl;
-  device_table*        dt;
-  mod_chxj_config*     conf; 
-  char*                device_id;
+  device_table         *returnType = &UNKNOWN_DEVICE;
+  device_table_list    *dtl;
+  mod_chxj_config      *conf; 
+  mod_chxj_req_config  *request_conf; 
+  char                 *device_id;
 
-  if (! user_agent) 
+  DBG(r,"REQ[%x] start %s()", TO_ADDR(r), __func__ );
+
+  if (! user_agent) {
+    DBG(r,"REQ[%x] end %s() (User-Agent is NULL)", TO_ADDR(r),__func__);
     return returnType;
-            
+  }
 
-  DBG(r, "start chxj_specified_device()");
+  /*
+   * Get per request config.
+   */
+  request_conf = (mod_chxj_req_config *)chxj_get_module_config(r->request_config, &chxj_module);
+  if (!request_conf) {
+    request_conf = apr_pcalloc(r->pool, sizeof(mod_chxj_req_config));
+    request_conf->spec = NULL;
+    request_conf->user_agent = NULL;
+    request_conf->f = NULL;
+    request_conf->entryp = NULL;
+    chxj_set_module_config(r->request_config, &chxj_module, request_conf);
+  }
+  if (user_agent) {
+    request_conf->user_agent = apr_pstrdup(r->pool, user_agent);
+  }
+  else {
+    request_conf->user_agent = "";
+  }
 
-  conf = ap_get_module_config(r->per_dir_config, &chxj_module);
+  conf = chxj_get_module_config(r->per_dir_config, &chxj_module);
   if (! conf->devices) {
-    DBG(r, "device_data.xml load failure");
+    ERR(r,"REQ[%X] device_data.xml load failure", TO_ADDR(r));
+    DBG(r,"REQ[%x] end %s()", TO_ADDR(r),__func__);
     return returnType;
   }
 
   for (dtl = conf->devices; dtl; dtl = dtl->next) {
     if (! dtl->pattern) {
-      DBG(r, "pattern is null");
+      /* DBG(r, "REQ[%X] pattern is null", TO_ADDR(r)); */
       continue;
     }
 
-    DBG1(r, "pattern is [%s]", dtl->pattern);
+    /* DBG(r, "REQ[%X] pattern is [%s]", TO_ADDR(r), dtl->pattern); */
     if (! dtl->regexp) {
-      DBG(r,"compile failed.");
+      ERR(r,"REQ[%X] compile failed.", TO_ADDR(r));
+      DBG(r,"REQ[%x] end %s()", TO_ADDR(r),__func__);
       return returnType;
     }
 
-    if (ap_regexec(dtl->regexp, user_agent, dtl->regexp->re_nsub + 1, match, 0) == 0) {
+    if (ap_regexec((const ap_regex_t *)dtl->regexp, user_agent, (apr_size_t)dtl->regexp->re_nsub + 1, match, 0) == 0) {
       device_id = ap_pregsub(r->pool, "$1", user_agent, dtl->regexp->re_nsub + 1, match);
-      DBG1(r, "device_id:[%s]", device_id);
-      for (dt = dtl->table; dt; dt = dt->next) {
-        if (strcasecmp(device_id, dt->device_id) == 0) {
-          DBG1(r, "device_name:[%s]", dt->device_name);
-          returnType = dt;
-          break;
+      DBG(r,"REQ[%X] device_id:[%s]", TO_ADDR(r), device_id);
+      returnType = s_get_device_data(r, device_id, dtl);
+      if (! returnType) {
+        if (dtl->tail) {
+          returnType = dtl->tail;
         }
-      }
-
-      if (! dt) {
-        for (dt = dtl->table; dt; dt = dt->next) {
-          if (dt->next == NULL)
-            break;
+        else {
+          returnType = &UNKNOWN_DEVICE;
         }
-
-        if (dt)
-          returnType = dt;
       }
-    }
-
-    if (returnType != &UNKNOWN_DEVICE) {
-      DBG(r,"end chxj_specified_device()");
+      request_conf->spec = returnType;
+      DBG(r,"REQ[%X] end %s() (Found User-Agent Type)", TO_ADDR(r),__func__);
       return returnType;
     }
   }
 
-  DBG(r,"end chxj_specified_device()");
+  request_conf->spec = &UNKNOWN_DEVICE;
+  DBG(r,"REQ[%X] end %s() (Not Found User-Agent Type) [%s]",TO_ADDR(r), __func__,user_agent);
 
-  return returnType;
+  return &UNKNOWN_DEVICE;
 }
 
+
+#include <stdlib.h>
+static int 
+s_compar(const void *a, const void *b)
+{
+  device_table *aa = *(device_table **)a;
+  device_table *bb = *(device_table **)b;
+  /* Not strcasecmp. for LOAD */
+  return strcmp(aa->device_id, bb->device_id);
+}
+static device_table *
+s_get_device_data(request_rec *r, const char *device_id, device_table_list *dtl)
+{
+  device_table dt;
+  device_table *_dt;
+  dt.device_id = device_id;
+  _dt = &dt;
+  device_table **ret = bsearch(&_dt, dtl->sort_table, dtl->table_count, sizeof(device_table *), s_compar);
+  if (ret && *ret) {
+    return *ret;
+  }
+  return NULL;
+}
 /*
  * vim:ts=2 et
  */