SSブログ

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) 

nice! 0

コメント 0

コメントを書く

お名前:[必須]
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

※ブログオーナーが承認したコメントのみ表示されます。

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