SSブログ

Socket Debuggerを使ってみる! ポート:11!Mime Typeの簡単な実装 [NETWORK]

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

webServer_005.png前回まででは、コンテンツはhtml文として決め打ちでしたが、今回はmime typeを指定していろいろなファイルの転送を可能とします。


webServer_003.pngこれにより、ブラウザの左上にfaviconが表示されるようになったり、


webServer_006.pngCSVファイルのダウンロードなんかもできますね。


変更部分のみ
---------------------------------------------
-- 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 = {}
      local mimeType = ""
      fileData, mimeType = methodGet( urlStr, httpVersionStr )
      if fileData ~= nil and mimeType ~= "" then 
        index200( fileData, mimeType )
        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", mime = "text/html" },
    { url = "/index.html", path = "D:\\temp\\index.html", mime = "text/html" },
    { url = "/index.htm",  path = "D:\\temp\\index.html", mime = "text/html" },
    { url = "/favicon.ico",  path = "D:\\temp\\favicon.ico", mime = "image/png" },
    { url = "/text.txt",  path = "D:\\temp\\text.txt", mime = "text/plain" },
    { url = "/photo1.jpg",  path = "D:\\temp\\photo1.jpg", mime = "image/jpeg" },
    { url = "/image1.png",  path = "D:\\temp\\image1.png", mime = "image/png" },
    { url = "/image2.bmp",  path = "D:\\temp\\image2.bmp", mime = "image/bmp" },
    { url = "/test.csv",  path = "D:\\temp\\test.csv", mime = "text/csv" },
--    { url = "/test.bin",  path = "D:\\temp\\test.bin", mime = "application/octet-stream" },
  }


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


  return nil,""
end


---------------------------------------------
-- http request status code = 200
---------------------------------------------
function index200( data, mimeType )
  local contentLength = #data
  local contentHeader = 'HTTP/1.1 200 OK\r\n'
    .. 'Server: SocketeEbugger Web Server/ ver.0.1\r\n'
    .. 'Content-Type: ' .. mimeType .. ' charset=UTF-8\r\n'
    .. 'Content-Length: ' .. contentLength
    .. '\r\n'
    .. 'Connection: close\r\n\r\n'

  SendData( contentHeader )
  SendData( data )
end



※ お役立ちリンク
https://asahi-net.jp/support/guide/homepage/0017.html









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

nice! 0

コメント 0

コメントを書く

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

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

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