SSブログ

Socket Debuggerを使ってみる! ポート:10!簡単なWeb Server [NETWORK]

SocketDebugger
こことか、 https://www.udom.co.jp/sdg/
こことか、 http://sdg.ex-group.jp/lua.html
をまず見てね!

と言う訳で先人の知恵を借りて、文字列で処理をする事に。差分のみ

---------------------------------------------
-- http request
---------------------------------------------
function httpRequest( request )
  local len = #request
  if len < 4 then return 0 end

  local result = 0
  if request[len - 3] == 0x0D and
     request[len - 2] == 0x0A and 
     request[len - 1] == 0x0D and 
     request[len - 0] == 0x0A then
    Logput( 1, 'end of the request message.' )


    local reqStr = CharFromTbl( request )
    local lineTbl = strSplit( reqStr, "\r\n" )
    local line1Tbl = strSplit( lineTbl[1], " " )

    local methodStr = line1Tbl[1]
    local urlStr = line1Tbl[2]
    local httpVersionStr = line1Tbl[3]

    if strCmp( methodStr, 'GET' ) == 0 then
      local fileData = methodGet( urlStr, httpVersionStr )
      if fileData ~= nil then 
        index200( fileData )
        result = 200
      else
        index404()
        result = 404
      end
    elseif strCmp( methodStr, 'PUT' ) == 0 then
      index405()
      result = 405
    elseif strCmp( methodStr, 'POST' ) == 0 then
      index405()
      result = 405
    elseif strCmp( methodStr, 'DELETE' ) == 0 then
      index405()
      result = 405
    else
      index400()
      result = 400
    end

  end

  return result
end


---------------------------------------------
-- method get
---------------------------------------------
function methodGet( urlStr, verStr )
  local file = {}
  local methodTbl = 
  {
    { url = "/",           path = "D:\\temp\\index.html" },
    { url = "/index.html", path = "D:\\temp\\index.html" },
    { url = "/index.htm",  path = "D:\\temp\\index.html" },
  }


  for i = 1, #methodTbl do
    if strCmp( urlStr, methodTbl[i].url ) == 0 then
      file = FileRead( methodTbl[i].path )
      return file
    end
  end


  return nil
end


---------------------------------------------
-- divide string by ts and enter table.
--  https://qiita.com/peg/items/c472f7a7d9fcca1b5cb7
---------------------------------------------
function strSplit(str, ts)
  -- 引数がないときは空tableを返す
  if ts == nil then return {} end

  local t = {} ; 
  i=1
  for s in string.gmatch(str, "([^"..ts.."]+)") do
    t[i] = s
    i = i + 1
  end

  return t
end


※ お役立ちリンク
https://qiita.com/peg/items/c472f7a7d9fcca1b5cb7
https://symfoware.blog.fc2.com/blog-entry-455.html
※ ともすれば変数がテーブルなのか?文字列なのか?それとも他の型なのか判らなくなってくるので、変数名、、、。










nice!(0)  コメント(0) 

Socket Debuggerを使ってみる! ポート:9!簡単なWeb Server [NETWORK]

SocketDebugger
こことか、 https://www.udom.co.jp/sdg/
こことか、 http://sdg.ex-group.jp/lua.html
をまず見てね!

前回のスクリプトがイマイチだったので、method GETのスクリプトをまず改造してみる。
今回はファイルパスを検索するのに連想配列を使う。ファイルの追加は容易になると思う。

---------------------------------------------
-- method get
---------------------------------------------
function methodGet( url, ver )
  local file = {}
  local methodTbl = 
  {
    { url = "/",           path = "D:\\temp\\index.html" },
    { url = "/index.html", path = "D:\\temp\\index.html" },
    { url = "/index.htm",  path = "D:\\temp\\index.html" },
  }

  for i = 1, #methodTbl do
    local urlStr = CharFromTbl( url )
    -- local verStr = CharFromTbl( ver )

    if strCmp( urlStr, methodTbl[i].url ) == 0 then
      file = FileRead( methodTbl[i].path )
      return file
    end
  end

  return nil

end

---------------------------------------------
-- compare string A and string B.
---------------------------------------------
function strCmp( stringA, stringB )
  local strLen = string.len( stringA )
  if strLen ~= string.len( stringB ) then 
    return -1 
  end

  for i = 1, strLen do
    if string.byte( stringA, i ) ~= string.byte( stringB, i ) then
      return -1
    end
  end

  return 0
end


※ もう少し上手く書ける気がする。修行が足らん、、、
※ お役立ちリンク
https://qiita.com/peg/items/c472f7a7d9fcca1b5cb7
https://symfoware.blog.fc2.com/blog-entry-455.html
※ 文字列で処理した方がお得なのか?、テーブルで処理した方がお得なのか?、、、悩みますな。









nice!(0)  コメント(0) 

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。