Socket Debuggerを使ってみる! ポート:11!Mime Typeの簡単な実装 [NETWORK]
SocketDebugger
こことか、 https://www.udom.co.jp/sdg/
こことか、 http://sdg.ex-group.jp/lua.html
をまず見てね!
前回まででは、コンテンツはhtml文として決め打ちでしたが、今回はmime typeを指定していろいろなファイルの転送を可能とします。
これにより、ブラウザの左上にfaviconが表示されるようになったり、
CSVファイルのダウンロードなんかもできますね。
変更部分のみ
※ お役立ちリンク
https://asahi-net.jp/support/guide/homepage/0017.html
こことか、 https://www.udom.co.jp/sdg/
こことか、 http://sdg.ex-group.jp/lua.html
をまず見てね!
前回まででは、コンテンツはhtml文として決め打ちでしたが、今回はmime typeを指定していろいろなファイルの転送を可能とします。
これにより、ブラウザの左上にfaviconが表示されるようになったり、
CSVファイルのダウンロードなんかもできますね。
変更部分のみ
--------------------------------------------- -- 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
M5Stick ミニ開発キットESP32 1.3'OLEDオプションのブザーIRトランスミッタMpu9250
- 出版社/メーカー: M5Stack
- メディア: