You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by sf...@apache.org on 2012/06/17 00:46:30 UTC

svn commit: r1351018 - /httpd/httpd/trunk/modules/lua/mod_lua.c

Author: sf
Date: Sat Jun 16 22:46:30 2012
New Revision: 1351018

URL: http://svn.apache.org/viewvc?rev=1351018&view=rev
Log:
factor common code into utility function

also improve logging a bit and adjust some log levels

Modified:
    httpd/httpd/trunk/modules/lua/mod_lua.c

Modified: httpd/httpd/trunk/modules/lua/mod_lua.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/lua/mod_lua.c?rev=1351018&r1=1351017&r2=1351018&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/lua/mod_lua.c (original)
+++ httpd/httpd/trunk/modules/lua/mod_lua.c Sat Jun 16 22:46:30 2012
@@ -78,73 +78,114 @@ static int lua_open_hook(lua_State *L, a
     return OK;
 }
 
+static const char *scope_to_string(unsigned int scope)
+{
+    switch (scope) {
+    case AP_LUA_SCOPE_ONCE:
+    case AP_LUA_SCOPE_UNSET:
+        return "once";
+    case AP_LUA_SCOPE_REQUEST:
+        return "request";
+    case AP_LUA_SCOPE_CONN:
+        return "conn";
+#if APR_HAS_THREADS
+    case AP_LUA_SCOPE_THREAD:
+        return "thread";
+#endif
+    default:
+        ap_assert(0);
+    }
+}
+
+static ap_lua_vm_spec *create_vm_spec(apr_pool_t **lifecycle_pool,
+                                      request_rec *r,
+                                      const ap_lua_dir_cfg *cfg,
+                                      const ap_lua_server_cfg *server_cfg,
+                                      const char *filename,
+                                      const char *bytecode,
+                                      apr_size_t bytecode_len,
+                                      const char *function,
+                                      const char *what)
+{
+    apr_pool_t *pool;
+    ap_lua_vm_spec *spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
+
+    spec->scope = cfg->vm_scope;
+    spec->pool = r->pool;
+    spec->package_paths = cfg->package_paths;
+    spec->package_cpaths = cfg->package_cpaths;
+    spec->cb = &lua_open_callback;
+    spec->cb_arg = NULL;
+    spec->bytecode = bytecode;
+    spec->bytecode_len = bytecode_len;
+
+    if (filename) {
+        char *file;
+        apr_filepath_merge(&file, server_cfg->root_path,
+                           filename, APR_FILEPATH_NOTRELATIVE, r->pool);
+        spec->file = file;
+    }
+    else {
+        spec->file = r->filename;
+    }
+    ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, APLOGNO()
+                  "%s details: scope: %s, file: %s, func: %s",
+                  what, scope_to_string(spec->scope), spec->file,
+                  function ? function : "-");
+
+    switch (spec->scope) {
+    case AP_LUA_SCOPE_ONCE:
+    case AP_LUA_SCOPE_UNSET:
+        apr_pool_create(&pool, r->pool);
+        break;
+    case AP_LUA_SCOPE_REQUEST:
+        pool = r->pool;
+        break;
+    case AP_LUA_SCOPE_CONN:
+        pool = r->connection->pool;
+        break;
+#if APR_HAS_THREADS
+    case AP_LUA_SCOPE_THREAD:
+        pool = apr_thread_pool_get(r->connection->current_thread);
+        break;
+#endif
+    default:
+        ap_assert(0);
+    }
+
+    *lifecycle_pool = pool;
+    return spec;
+}
+
 
 /**
  * "main"
  */
 static int lua_handler(request_rec *r)
 {
-    ap_lua_dir_cfg *dcfg;
-    apr_pool_t *pool;
     if (strcmp(r->handler, "lua-script")) {
         return DECLINED;
     }
-  
-    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01472) "handling [%s] in mod_lua",
-                  r->filename);
-    dcfg = ap_get_module_config(r->per_dir_config, &lua_module);
+    ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(01472)
+                  "handling [%s] in mod_lua", r->filename);
 
+    /* XXX: This seems wrong because it may generate wrong headers for HEAD requests */
     if (!r->header_only) {
         lua_State *L;
+        apr_pool_t *pool;
         const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
                                                          &lua_module);
-        ap_lua_vm_spec *spec = NULL;
+        ap_lua_vm_spec *spec = create_vm_spec(&pool, r, cfg, NULL, NULL, NULL,
+                                              0, "handle", "request handler");
 
-        spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
-        spec->scope = dcfg->vm_scope;
-        spec->pool = r->pool;
-        spec->file = r->filename;
-        spec->package_paths = cfg->package_paths;
-        spec->package_cpaths = cfg->package_cpaths;
-        spec->cb = &lua_open_callback;
-        spec->cb_arg = NULL;
-      
-        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01473)
-                      "request details scope:%u, filename:%s, function:%s",
-                      spec->scope,
-                      spec->file,
-                      "handle");
-
-        switch (spec->scope) {
-        case AP_LUA_SCOPE_ONCE:
-        case AP_LUA_SCOPE_UNSET:
-          apr_pool_create(&pool, r->pool);
-          break;
-        case AP_LUA_SCOPE_REQUEST:
-          pool = r->pool;
-          break;
-        case AP_LUA_SCOPE_CONN:
-          pool = r->connection->pool;
-          break;
-        case AP_LUA_SCOPE_THREAD:
-          #if APR_HAS_THREADS
-          pool = apr_thread_pool_get(r->connection->current_thread);
-          break;
-          #endif
-        default:
-          ap_assert(0);
-        }
-
-        L = ap_lua_get_lua_state(pool,
-                                 spec);
-        
+        L = ap_lua_get_lua_state(pool, spec);
         if (!L) {
             /* TODO annotate spec with failure reason */
             r->status = HTTP_INTERNAL_SERVER_ERROR;
             ap_rputs("Unable to compile VM, see logs", r);
             return HTTP_INTERNAL_SERVER_ERROR;
         }
-        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01474) "got a vm!");
+        ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r, APLOGNO(01474) "got a vm!");
         lua_getglobal(L, "handle");
         if (!lua_isfunction(L, -1)) {
             ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01475)
@@ -163,7 +204,6 @@ static int lua_handler(request_rec *r)
 
 
 
-
 /* ---------------- Configury stuff --------------- */
 
 /** harnesses for magic hooks **/
@@ -184,54 +224,18 @@ static int lua_request_rec_hook_harness(
     if (hook_specs) {
         int i;
         for (i = 0; i < hook_specs->nelts; i++) {
-            char *file;
             ap_lua_mapped_handler_spec *hook_spec =
                 ((ap_lua_mapped_handler_spec **) hook_specs->elts)[i];
 
             if (hook_spec == NULL) {
                 continue;
             }
-            spec = apr_pcalloc(r->pool, sizeof(ap_lua_vm_spec));
-
-            spec->file = hook_spec->file_name;
-            spec->scope = cfg->vm_scope;
-            spec->bytecode = hook_spec->bytecode;
-            spec->bytecode_len = hook_spec->bytecode_len;
-            spec->pool = r->pool;
-            spec->package_paths = cfg->package_paths;
-            spec->package_cpaths = cfg->package_cpaths;
-            spec->cb = &lua_open_callback;
-            spec->cb_arg = NULL;
-
-            apr_filepath_merge(&file, server_cfg->root_path,
-                               spec->file, APR_FILEPATH_NOTRELATIVE, r->pool);
-            spec->file = file;
-
-            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01476)
-                          "request details scope:%u, filename:%s, function:%s",
-                          spec->scope,
-                          spec->file,
-                          hook_spec->function_name ? hook_spec->function_name : "-");
-
-            switch (spec->scope) {
-            case AP_LUA_SCOPE_ONCE:
-            case AP_LUA_SCOPE_UNSET:
-             apr_pool_create(&pool, r->pool);
-              break;
-            case AP_LUA_SCOPE_REQUEST:
-              pool = r->pool;
-              break;
-            case AP_LUA_SCOPE_CONN:
-              pool = r->connection->pool;
-              break;
-            case AP_LUA_SCOPE_THREAD:
-              #if APR_HAS_THREADS
-              pool = apr_thread_pool_get(r->connection->current_thread);
-              break;
-              #endif
-            default:
-              ap_assert(0);
-            }
+            spec = create_vm_spec(&pool, r, cfg, server_cfg,
+                                  hook_spec->file_name,
+                                  hook_spec->bytecode,
+                                  hook_spec->bytecode_len,
+                                  hook_spec->function_name,
+                                  "request hook");
 
             L = ap_lua_get_lua_state(pool, spec);