server.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. local read = require('read')
  2. local write = require('write')
  3. local net = net
  4. local string = string
  5. local srv = nil
  6. local debug_message = debug_message
  7. local pairs = pairs
  8. local print = print
  9. module(...)
  10. local function connect(conn)
  11. local query_data
  12. conn:on('receive', function(cn, req_data)
  13. debug_message('req_data:')
  14. debug_message(req_data)
  15. --below methodology is flawed - this callback handles statelessly per chunk,
  16. --however the client MAY send the POST payload in another chunk.
  17. --temporarily work around by examining stringy properties of chunk,
  18. --but it's totes insecure and will need updating.
  19. if string.find(req_data, 's=.*&p=.*&r=.*&s=Update') then
  20. new_settings = parse_post(req_data)
  21. write.new_settings(new_settings)
  22. end
  23. -- query_data = get_http_req(req_data)
  24. -- debug_message(query_data['METHOD'] .. ' ' .. ' ' .. query_data['User-Agent'])
  25. --TODO discriminate request types (POST --> update)
  26. -- if query_data['METHOD'] == 'POST' then
  27. -- debug_message('handling POST request')
  28. -- new_settings = parse_post(req_data)
  29. -- debug_message('new settings: ' .. (new_settings or 'nil'))
  30. -- write.new_settings(new_settings)
  31. -- else
  32. --TODO discriminate endpoints (/, /yo.css, /status, /favicon.ico)
  33. -- end
  34. send_index(cn)
  35. end)
  36. conn:on('sent', function(cn)
  37. cn:close()
  38. end)
  39. end
  40. function parse_post(req_data)
  41. --TODO refactor this function
  42. debug_message('server.parse_post')
  43. if req_data then
  44. local ssid_index = {req_data:find("s=")}
  45. local pass_index = {req_data:find("&p=")}
  46. local recipient_index = {req_data:find("&r=")}
  47. local submit_index = {req_data:find("&s=")}
  48. if ssid_index[1] ~= nil then
  49. local new_ssid = string.gsub(string.sub(req_data, ssid_index[2]+1, pass_index[1]-1), "+", " ")
  50. local new_password = string.gsub(string.sub(req_data, pass_index[2]+1, recipient_index[1]-1), "+", " ")
  51. local new_recipient = string.upper(string.sub(req_data, recipient_index[2]+1, submit_index[1]-1))
  52. debug_message('new ssid: ' .. new_ssid)
  53. debug_message('new password: ' .. new_password)
  54. debug_message('new recipient: ' .. new_recipient)
  55. return {
  56. ssid = new_ssid,
  57. password = new_password,
  58. yo_to = new_recipient
  59. }
  60. end
  61. else
  62. return nil
  63. end
  64. end
  65. -- Build and return a table of the http request data
  66. function get_http_req(instr)
  67. local t = {}
  68. local first = nil
  69. local key, v, strt_ndx, end_ndx
  70. for str in string.gmatch(instr, '([^\n]+)') do
  71. -- First line in the method and path
  72. if(first == nil) then
  73. first = 1
  74. strt_ndx, end_ndx = string.find(str, '([^ ]+)')
  75. v = trim(string.sub(str, end_ndx + 2))
  76. key = trim(string.sub(str, strt_ndx, end_ndx))
  77. t['METHOD'] = key
  78. t['REQUEST'] = v
  79. else -- Process and reamaining ':' fields
  80. strt_ndx, end_ndx = string.find(str, '([^:]+)')
  81. if(end_ndx ~= nil) then
  82. v = trim(string.sub(str, end_ndx + 2))
  83. key = trim(string.sub(str, strt_ndx, end_ndx))
  84. t[key] = v
  85. end
  86. end
  87. end
  88. return t
  89. end
  90. -- String trim left and right
  91. function trim(s)
  92. return(s:gsub('^%s*(.-)%s*$', '%1'))
  93. end
  94. function send_index(conn)
  95. debug_message('server.send_index')
  96. index = read.index()
  97. local settings = read.current_settings()
  98. index = string.gsub(index, 'S_', settings.ssid)
  99. index = string.gsub(index, 'T_', settings.status)
  100. index = string.gsub(index, 'R_', settings.yo_to)
  101. debug_message('sending index')
  102. debug_message('____________')
  103. debug_message(index)
  104. debug_message('____________')
  105. conn:send('HTTP/1.1 200 OK\r\n\r\n' .. index)
  106. end
  107. function is_serving()
  108. return srv ~= nil
  109. end
  110. function start()
  111. debug_message('server.start')
  112. debug_message(srv)
  113. if srv then
  114. srv = nil
  115. end
  116. srv = net.createServer(net.TCP, 30)
  117. srv:listen(80, connect)
  118. debug_message(srv)
  119. end
  120. function stop()
  121. debug_message('server.stop')
  122. debug_message(srv)
  123. if srv then
  124. srv:close()
  125. srv = nil
  126. end
  127. debug_message(srv)
  128. end