SSブログ

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

SocketDebugger

こことか、 https://www.udom.co.jp/sdg/
こことか、 http://sdg.ex-group.jp/lua.html

をまず見てね!

なにか意味が有るのかと思わなくもないが、沈みかかった船なのでWeb Serverとか

webServer_002.pngWeb ServerをSocket Debugger上に構築してブラウザからアクセスしてみた様子。内容はindex.htmlファイルとしてディスク上に保存、それを読み出す。


webServer_001.pngPCのブラウザとのやり取りの様子。


恒例のluaスクリプト
---------------------------------------------
-- 接続完了通知
---------------------------------------------
function OnConnected()
  Logput(1,'OnConnected')

  recvHttpTable = {}

  return 0
end
---------------------------------------------
-- 送信ボタン押下
---------------------------------------------
function OnSendPush()
    Logput(1,'OnSendPush')
    a = GetEditorData()
    SendData(a)
    return 0
end
---------------------------------------------
-- タイマー通知
---------------------------------------------
function OnTimer(id)
    Logput(1,'OnTimer')
    return 0
end
---------------------------------------------
-- 受信通知
-- 0x0D is CR,0x0A is LF
---------------------------------------------
function OnReceive(recv)
  Logput(1,'OnReceive')

  recvHttpTable = tableConcat( recvHttpTable, recv )
  if #recvHttpTable < 4 then return 0 end

  result = httpRequest( recvHttpTable )
  if result ~= 0 then
--[[
    if result == 200 then
    elseif result == 400 then
    elseif result == 404 then
    elseif result == 405 then
    else
    end
]]
    Disconnect()
  end

  return 0
end
---------------------------------------------
-- 切断通知
---------------------------------------------
function OnDisConnected()
  Logput(1,'OnDisConnected')
  Disconnect()
  return 0
end

---------------------------------------------
-- ASCIIテーブルをコード変換後に文字列化
--
-- ASCIIの例
-- 入力: { 0x30, 0x31, 0x32 }
-- 返値: "012"
--  http://amlaid.air-nifty.com/blog/2015/10/serial-debugger.html
-- 0x0D is CR,0x0A is LF
---------------------------------------------
function CharFromTbl(tbl)
  tblChar = {}

  for i=1, #tbl do
    if tbl[i] == 0x0D or 
       tbl[i] == 0x0A then
      tblChar[i] = string.char(tbl[i])
    else
      tblChar[i] = string.char(tbl[i])
    end
  end

  return table.concat(tblChar)
end


---------------------------------------------
-- table concat
---------------------------------------------
function tableConcat( tableA, tableB )
  local tableALen = #tableA

  for i = 1, #tableB do
    tableA[ tableALen + i ] = tableB[i]
  end

  return tableA
end


---------------------------------------------
-- http request
---------------------------------------------
function httpRequest( request )
  local len = #request
  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 line1 = getLine( request )

    local pos
    local method,url,httpVersion
    method,pos = split( line1, 0x20 )  -- 0x20 is ascii space
    line1 = tableCopyFrom( line1, pos + 1 )
    url,pos = split( line1, 0x20 )  -- 0x20 is ascii space
    httpVersion,pos = tableCopyFrom( line1, pos + 1 )

    local methodStr = CharFromTbl( method )
    if string.match( methodStr, 'GET' ) ~= nil then
      local data = methodGet( url,httpVersion )
      if data ~= nil then 
        index200( data )
        result = 200
      else
        index404()
        result = 404
      end
    elseif string.match( methodStr, 'PUT' ) ~= nil then
      index405()
      result = 405
    elseif string.match( methodStr, 'POST' ) ~= nil then
      index405()
      result = 405
    elseif string.match( methodStr, 'DELETE' ) ~= nil then
      index405()
      result = 405
    else
      index400()
      result = 400
    end
  end

  return result
end


---------------------------------------------
-- method get
---------------------------------------------
function methodGet( url, ver )
  local urlLen = #url
  local file = {}

  if urlLen == 1 then
    if url[1] == 0x2F then  -- 0x2F is /
      file = FileRead( "D:\\temp\\index.html" )
    else
      file = nil
    end
  else
    local urlStr = CharFromTbl( url )
    if string.match( urlStr, '/index.html' ) ~= nil or
       string.match( urlStr, '/index.htm' ) ~= nil then
      file = FileRead( "D:\\temp\\index.html" )
    else
      file = nil
    end
  end

  return file
end


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

  SendData( contentHeader )
  SendData( data )
end


---------------------------------------------
-- http request status code = 404
---------------------------------------------
function index404()
  local contentHeader = 'HTTP/1.0 404 Not Found\r\n'
    .. 'Connection: close\r\n\r\n'
    .. '<html><head><title>404 Not Found</title></head>\r\n'
    .. '<body>\r\n'
    .. '<h1>Not Found</h1>\r\n'
    .. '</body></html>\r\n'

  SendData( contentHeader )
end


---------------------------------------------
-- http request status code = 400
---------------------------------------------
function index400()
  local contentHeader = 'HTTP/1.0 400 Bad Request\r\n'
    .. 'Connection: close\r\n\r\n'
    .. '<html><head><title>400 Bad Request</title></head>\r\n'
    .. '<body>\r\n'
    .. '<h1>Bad Request</h1>\r\n'
    .. '</body></html>\r\n'

  SendData( contentHeader )
end


---------------------------------------------
-- http request status code = 405
---------------------------------------------
function index405()
  local contentHeader = 'HTTP/1.0 405 Method Not Allowed\r\n'
    .. 'Connection: close\r\n\r\n'
    .. '<html><head><title>405 Method Not Allowed</title></head>\r\n'
    .. '<body>\r\n'
    .. '<h1>Method Not Allowed</h1>\r\n'
    .. '</body></html>\r\n'

  SendData( contentHeader )
end


---------------------------------------------
-- get line from string
---------------------------------------------
function getLine( strTbl )
  local line = {}

  -- search line
  position = getDelimiterPosition( strTbl, 0x0D )  -- 0x0D is CR
  if position == 0 then return nil end

  position = position + 1
  for i = 1, position do
    line[i] = strTbl[i]
  end

  return line
end

---------------------------------------------
-- split table
---------------------------------------------
function split( tbl, delimiter )
  local index = 1
  local copyTbl = {}

  for i = 1, #tbl do
    if tbl[i] == delimiter then break end
    index = index + 1
    copyTbl[i] = tbl[i]
  end

  if index > #tbl then return nil,0 end

  return copyTbl,index
end

---------------------------------------------
-- get delimiter position
---------------------------------------------
function getDelimiterPosition( tbl, delimiter )
  local index = 1

  for i = 1, #tbl do
    if tbl[i] == delimiter then break end
    index = index + 1
  end

  if index > #tbl then return 0 end

  return index
end


---------------------------------------------
-- table copy from
---------------------------------------------
function tableCopyFrom( tbl, pos )
  local copyTbl = {}
  local index = 1

  for i = pos, #tbl do
    copyTbl[ index ] = tbl[ i ]
    index = index + 1
  end

  return copyTbl
end


PCのコントロールキーのパッドの中にちっさなゴミが入ったのか、気になって仕方がない、、、
※もう少し上手く書ける気がする。修行が足らん、、、









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

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

SocketDebugger

こことか、 https://www.udom.co.jp/sdg/
こことか、 http://sdg.ex-group.jp/lua.html

をまず見てね!

気を取り直して、Web Clienteとして動作させてみます。HTTP文を色々考えるのは面倒なので、Arduino IDEのEtherntのWeb Clienteの例題を流用してみます。と言ってもスクリプトは非常に簡単ですがね。
取得したHTTP文はファイルに保存します。ではでは、設定から。

webCliente_001.png今回はポート1を使います。通信設定->接続:TCPクライアント動作とします。相手先アドレスが判らなくても右横の...ってボタンをクリックすればhost名で入力できますから。


webCliente_002.png通信設定->動作:スクリプトで動かします。


webCliente_003.png受信したデータをファイル化し、ブラウザで表示してみました。HTML文の前にHTTPの応答文が入っています。


webCliente_004.pngWebサーバーとのやり取りです。Webサーバーはリクエストに答えると、基本切断して来ることが判ります。


今回作成したスクリプトです。作成したところのみを掲載しています。
---------------------------------------------
-- 送信ボタン押下
---------------------------------------------
function OnSendPush()
  Logput(1,'OnSendPush')

  local httpRequest = 'GET /search?q=arduino HTTP/1.1\r\n'
    .. 'Host: www.google.com\r\n'
    .. 'Connection: close\r\n\r\n'  -- http request message.

  SendData( httpRequest )  -- send a request message to host.

  recvHtmlMsg = {}  -- make a table for recieve message.

  return 0
end
---------------------------------------------
-- 受信通知
---------------------------------------------
function OnReceive(recv)
  Logput(1,'OnReceive')

  recvHtmlMsg = tableConcat( recvHtmlMsg, recv )  -- append a message to variable

  return 0
end
---------------------------------------------
-- 切断通知
---------------------------------------------
function OnDisConnected()
  Logput(1,'OnDisConnected')

  FileWrite( recvHtmlMsg, "D:\\temp\\receive.html" )  -- file write

  return 0
end

---------------------------------------------
-- table concat
---------------------------------------------
function tableConcat( tableA, tableB )
  local tableALen = #tableA
  for i = 1, #tableB do
    tableA[ tableALen + i ] = tableB[i]
  end
  return tableA
end










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

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