server.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. module(...)
  8. local function connect(conn, data)
  9. local query_data
  10. conn:on('receive',
  11. function(cn, req_data)
  12. query_data = get_http_req(req_data)
  13. debug_message(query_data['METHOD'] .. ' ' .. ' ' .. query_data['User-Agent'])
  14. --TODO discriminate request types (POST --> update)
  15. if query_data['METHOD'] == 'POST' then
  16. write.new_settings(parse_post(req_data))
  17. -- else
  18. --TODO discriminate endpoints (/, /yo.css, /status, /favicon.ico)
  19. end
  20. send_index(cn)
  21. cn:close()
  22. end
  23. )
  24. end
  25. function parse_post(req_data)
  26. --TODO refactor this function
  27. if req_data then
  28. local ssid_index = {req_data:find("s=")}
  29. local pass_index = {req_data:find("&p=")}
  30. local recipient_index = {req_data:find("&r=")}
  31. local submit_index = {req_data:find("&s=")}
  32. if ssid_index[1] ~= nil then
  33. local new_ssid = string.gsub(string.sub(req_data, ssid_index[2]+1, pass_index[1]-1), "+", " ")
  34. local new_password = string.gsub(string.sub(req_data, pass_index[2]+1, recipient_index[1]-1), "+", " ")
  35. local new_recipient = string.upper(string.sub(req_data, recipient_index[2]+1, submit_index[1]-1))
  36. debug_message(new_ssid)
  37. debug_message(new_password)
  38. debug_message(new_recipient)
  39. return {
  40. ssid = new_ssid,
  41. password = new_password,
  42. yo_to = new_recipient
  43. }
  44. end
  45. else
  46. return nil
  47. end
  48. end
  49. -- Build and return a table of the http request data
  50. function get_http_req(instr)
  51. local t = {}
  52. local first = nil
  53. local key, v, strt_ndx, end_ndx
  54. for str in string.gmatch(instr, '([^\n]+)') do
  55. -- First line in the method and path
  56. if(first == nil) then
  57. first = 1
  58. strt_ndx, end_ndx = string.find(str, '([^ ]+)')
  59. v = trim(string.sub(str, end_ndx + 2))
  60. key = trim(string.sub(str, strt_ndx, end_ndx))
  61. t['METHOD'] = key
  62. t['REQUEST'] = v
  63. else -- Process and reamaining ':' fields
  64. strt_ndx, end_ndx = string.find(str, '([^:]+)')
  65. if(end_ndx ~= nil) then
  66. v = trim(string.sub(str, end_ndx + 2))
  67. key = trim(string.sub(str, strt_ndx, end_ndx))
  68. t[key] = v
  69. end
  70. end
  71. end
  72. return t
  73. end
  74. -- String trim left and right
  75. function trim(s)
  76. return(s:gsub('^%s*(.-)%s*$', '%1'))
  77. end
  78. function send_index(conn)
  79. debug_message('server.send_index')
  80. index = read.index()
  81. local settings = read.current_settings()
  82. index = string.gsub(index, 'S_', settings.ssid)
  83. index = string.gsub(index, 'T_', settings.status)
  84. index = string.gsub(index, 'R_', settings.yo_to)
  85. debug_message('sending index')
  86. debug_message('____________')
  87. debug_message(index)
  88. debug_message('____________')
  89. conn:send('HTTP/1.1 200 OK\n\n' .. index)
  90. end
  91. function start()
  92. debug_message('server.start')
  93. debug_message(srv)
  94. if srv then
  95. srv = nil
  96. end
  97. srv = net.createServer(net.TCP, 30)
  98. srv:listen(80, connect)
  99. debug_message(srv)
  100. end
  101. function stop()
  102. debug_message('server.stop')
  103. debug_message(srv)
  104. if srv then
  105. srv:close()
  106. srv = nil
  107. end
  108. debug_message(srv)
  109. end