Explorar o código

Add content length and type to the Lua environment

bel %!s(int64=11) %!d(string=hai) anos
pai
achega
ba13b79d27
Modificáronse 2 ficheiros con 12 adicións e 1 borrados
  1. 11 0
      src/mod_lua.inl
  2. 1 1
      test/page2.lua

+ 11 - 0
src/mod_lua.inl

@@ -892,6 +892,7 @@ enum {
 static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
 {
     char src_addr[IP_ADDR_STR_LEN] = "";
+    const char *s;
     int i;
 
     sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
@@ -909,6 +910,16 @@ static void prepare_lua_request_info(struct mg_connection *conn, lua_State *L)
     reg_int(L, "remote_port", conn->request_info.remote_port);
     reg_int(L, "num_headers", conn->request_info.num_headers);
     reg_int(L, "server_port", ntohs(conn->client.lsa.sin.sin_port));
+    
+    if (conn->request_info.content_length >= 0) {
+        /* reg_int64: content_length */
+        lua_pushstring(L, "content_length");
+        lua_pushnumber(L, conn->request_info.content_length);
+        lua_rawset(L, -3);
+    }
+    if ((s = mg_get_header(conn, "Content-Type")) != NULL) {
+        reg_string(L, "content_type", s);
+    }
 
     if (conn->request_info.remote_user != NULL) {
         reg_string(L, "remote_user", conn->request_info.remote_user);

+ 1 - 1
test/page2.lua

@@ -60,7 +60,7 @@ print_if_available(connect, "connect function")
 mg.write("</ul></p>\n");
 mg.write("<p> Today is " .. os.date("%A") .. "</p>\n");
 
-l = mg.request_info.http_headers["Content-Length"]
+l = mg.request_info.content_length
 if l then
   mg.write("<p>Content-Length = "..l..":<br>\n<pre>\n")
   mg.write(mg.read())