You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by "Akins, Brian" <Br...@turner.com> on 2007/05/02 21:43:42 UTC

[PATCH] mod_wombat: add connection and server to request

Adds ability to get to r.connection and r.server.  Also renamed back to
apw_request_push per discussion with Brian M.

This works now:

function quick_handler(r)
  r.headers_out["Lua"] = "Rulez";
  h = r.headers_out
  val = r.headers_in["User-Agent"];
  h["Test"] = "HELP";
  h["Browser"] = val;

  r:puts("User-Agent: " .. val .. "\n");

  c = r.connection
  r:puts("hello " .. c.remote_ip .. "\n");

  s = r.server
  r:puts("Server name: " .. s.server_hostname .. "\n");

  if string.find(val, "Firefox") then
     r.headers_out["Location"] = "http://www.cnn.com";
     return apache2.HTTP_MOVED_TEMPORARILY
  end
  return apache2.OK
end


-- 
Brian Akins
Chief Operations Engineer
Turner Digital Media Technologies


Re: [PATCH] mod_wombat: add connection and server to request

Posted by "Akins, Brian" <Br...@turner.com>.
On 5/3/07 12:10 PM, "Brian McCallister" <br...@apache.org> wrote:

> We should probably figure out how to avoid pushing the various values
> into the Apache2.Request metatable -- we are going to need a general
> purpose solution sooner rather than later, I think.

I think I mostly figured this out:

static int server_index(lua_State* L) {
    server_rec* s = lua_unboxpointer(L, 1);
    const char* key = luaL_checkstring(L, 2);
    ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "server_index: %s", key);

    if (0 == apr_strnatcmp("server_hostname", key)) {
    lua_pushstring(L, s->server_hostname);
    return 1; 
    }

    return 0;
}
    
static const struct luaL_Reg server_methods[] = {
   {"__index", server_index},
    {NULL, NULL}
};

void apw_load_request_lmodule(lua_State *L) {
    luaL_newmetatable(L, "Apache2.Request"); // [metatable]
    lua_pushvalue(L, -1);

    lua_setfield(L, -2, "__index");
    luaL_register(L, NULL, request_methods); // [metatable]

    lua_pop(L, 2);

    luaL_newmetatable(L, "Apache2.Connection"); // [metatable]
    lua_pushvalue(L, -1);

    lua_setfield(L, -2, "__index");
    luaL_register(L, NULL, connection_methods); // [metatable]

    lua_pop(L, 2);

    luaL_newmetatable(L, "Apache2.Server");
    
    lua_pushstring(L, "__index");
    lua_pushvalue(L, -2);  /* pushes the metatable */
    lua_settable(L, -3);  /* metatable.__index = metatable */
    luaL_openlib(L, NULL, server_methods, 0);
    lua_pop(L, 2);
    
}


And this works in lua:

 s = r.server
 r:puts("Server name: " .. s.server_hostname .. "\n");


We could probably wrap the server_index type funtion into something so we
don't have to write huge if/else/ statements everytime.  Mayme just copy the
way Lua does it with LuaL_Reg.  Could wrap the whole metatable creation and
population thing, I suppose.  Maybe do that in apr_lua?

static const struct apr_lua_reg server_methods[] = {
 "blah", get_blah,
...
{NULL, NULL}

}


apr_lua_register("Apache2.Server", server_methods);

Would take care of all the details.  Would need to be able to get set, maybe
pass that in as to whetehr it is __index or __newindex???

(Just thinking out load on the apr_lua stuff...)



-- 
Brian Akins
Chief Operations Engineer
Turner Digital Media Technologies

Re: [PATCH] mod_wombat: add connection and server to request

Posted by Brian McCallister <br...@apache.org>.
On May 2, 2007, at 12:43 PM, Akins, Brian wrote:

> Adds ability to get to r.connection and r.server.  Also renamed  
> back to
> apw_request_push per discussion with Brian M.

Applied with minor changes to function names (sticking with the  
apw_push_* naming convention).

We should probably figure out how to avoid pushing the various values  
into the Apache2.Request metatable -- we are going to need a general  
purpose solution sooner rather than later, I think.

-Brian