EYEN 2025. 1. 12. 14:36
require "nixio.util"
require "luci.http"
require "luci.sys"
require "luci.dispatcher"
require "luci.ltn12"

nixio.util: 시스템 유틸리티 함수
luci.http: HTTP 요청과 응답을 다루기 위한 라이브러리
luci.sys: 시스템 정보(메모리, 프로세스 등)을 제공하는 유틸리티
luci.dispatcher: Luci 라우팅과 요청 디스패치를 처리
luci.ltn12: Lua 테이블 네트워크 인터페이스, 스트림 데이터 처리에 사용
 

function handle_request(env)

env: http 요청 환경 정보를 포함한 테이블, uhttpd 서버에서 호출되며 요청을 처리하고 응답을 반환
 

local renv = {
    CONTENT_LENGTH  = env.CONTENT_LENGTH,
    CONTENT_TYPE    = env.CONTENT_TYPE,
    REQUEST_METHOD  = env.REQUEST_METHOD,
    REQUEST_URI     = env.REQUEST_URI,
    PATH_INFO       = env.PATH_INFO,
    SCRIPT_NAME     = env.SCRIPT_NAME:gsub("/+$", ""),
    SCRIPT_FILENAME = env.SCRIPT_NAME,
    SERVER_PROTOCOL = env.SERVER_PROTOCOL,
    QUERY_STRING    = env.QUERY_STRING
}

http 요청의 주요 요소를 새로운 테이블 renv에 저장. :gsub("/+$") 끝에 붙은 슬래시 제거
파이썬이랑 비슷하구만
 
이외 요청 헤더 테이블에서 env.headers 에서 헤더 키를 대문자로 변환 밑 '-'을 '_'로 변경
 

local len = tonumber(env.CONTENT_LENGTH) or 0
local function recv()
    if len > 0 then
        local rlen, rbuf = uhttpd.recv(4096)
        if rlen >= 0 then
            len = len - rlen
            return rbuf
        end
    end
    return nil
end

len: 요청 본문의 길이 저장
recv: 최대 4096바이트씩 데이터를 읽음
 

local send = uhttpd.send

이후 응답데이터를 클라이언트로 전송함
 

local req = luci.http.Request(
    renv, recv, luci.ltn12.sink.file(io.stderr)
)

요청 데이터를 관리하는 객체를 생성
recv 본문데이터를 읽고 표준 에러 출력으로 스트리밍
 

local x = coroutine.create(luci.dispatcher.httpdispatch)

luci.dispatcher.httpdispatch: 요청 URI를 분석하고 적절한 처리기를 호출하는 디스패처
coroutine.create: 디스패처를 코루틴으로 실행
 

root@DESKTOP-R7RCA96:/home/eyen/vendor/wavlink/_WN531P3-WAVLINK-WO-20230525.bin.extracted/squashfs-root# cat usr/lib/opkg/info/uhttpd.list
/usr/sbin/uhttpd
/etc/init.d/uhttpd
/etc/config/uhttpd
/lib/upgrade/keep.d/uhttpd

 

root@DESKTOP-R7RCA96:/home/eyen/vendor/wavlink/_WN531P3-WAVLINK-WO-20230525.bin.extracted/squashfs-root# cat etc/config/uhttpd
# Server configuration
config uhttpd main

        # HTTP listen addresses, multiple allowed
        list listen_http        0.0.0.0:81
        list listen_http        [::]:81

        # HTTPS listen addresses, multiple allowed
        list listen_https       0.0.0.0:443
        list listen_https       [::]:443

        # Redirect HTTP requests to HTTPS if possible
        option redirect_https   1

        # Server document root
        option home             /www_luci2

        # Reject requests from RFC1918 IP addresses
        # directed to the servers public IP(s).
        # This is a DNS rebinding countermeasure.
        option rfc1918_filter 1

        # Maximum number of concurrent requests.
        # If this number is exceeded, further requests are
        # queued until the number of running requests drops
        # below the limit again.
        option max_requests 3

        # Maximum number of concurrent connections.
        # If this number is exceeded, further TCP connection
        # attempts are queued until the number of active
        # connections drops below the limit again.
        option max_connections 100

        # Certificate and private key for HTTPS.
        # If no listen_https addresses are given,
        # the key options are ignored.
        option cert             /etc/uhttpd.crt
        option key              /etc/uhttpd.key

        # CGI url prefix, will be searched in docroot.
        # Default is /cgi-bin
        option cgi_prefix       /cgi-bin

        # List of extension->interpreter mappings.
        # Files with an associated interpreter can
        # be called outside of the CGI prefix and do
        # not need to be executable.
#       list interpreter        ".php=/usr/bin/php-cgi"
#       list interpreter        ".cgi=/usr/bin/perl"

        # Lua url prefix and handler script.
        # Lua support is disabled if no prefix given.
        #option lua_prefix      /luci
        #option lua_handler     /usr/lib/lua/luci/sgi/uhttpd.lua

        # Specify the ubus-rpc prefix and socket path.
        option ubus_prefix      /ubus
        #option ubus_socket     /vra/run/ubus.sock

81, 443번 포트를 사용한다고 함
 
lighttpd는 443포트관련 config가 없음. uhttpd에는 443포트관련 config가 있음
lighttpd는 http, uhttpd는 https를 담당하는 것 같음
 
netstat으로 실행중인 공유기 네트워크 포트 확인결과 443만 존재!!