As the title says... Chat on...

User avatar
By picstart
#7879 I am confused. Web server would send a client a
<IMG SRC="http://lua server ip/mygif.gif"

the server would at the point the client is displaying the web page and encounters the above get a call via GET to serve the image mygif.gif
Now mygif.gif is in the lua file system.
The issue
the html code that contains the
<IMG SRC="http://lua server ip/mygif.gif"
is also in the file system.
When the client contacts the lua server the lua code reads the html file and sends it to the client it then must service the GET
and open and send mygif.gif. Within a file read lua must open another file read to service the GET of mygif.gif.
I don't know if this is possible and if it is possible some example lua code would be nice.
The goal is to mimic a serving a web page with an embedded gif and both the page and the gif residing in the lua file system.
User avatar
By Hans174
#7881
Within a file read lua must open another file read to service the GET of mygif.gif.


The client browser reads all of the HTML file. After reading the HTML file he issued a GET for all other files mentioned in the HTML file.
There is no need for the LUA server to open two files at the same time.
User avatar
By Mike
#7887 I managed to create a Web-Server with a html-page serving an image using bradvoy's "Another web Server" found at viewtopic.php?f=19&t=1099

The html-page looks like this:
Code: Select all<html><head><title>Matchbox-Srvr</title></head><body>
     <h1> <img src="box.jpg"> Welcome to the Matchbox!</h1><hr>
...
    <hr></body></html>


To respond to the get-request to the homepage and the Image and i use this Elements in the pages-aray:

Code: Select all-- define pages:
    pages = {}

    pages["/box.jpg"] = function(request)
     file.open("box1r.jpg")
     X = file.read()
     file.close()   
     tmr.wdclr()
     return X , "image/jpg"
    end
   
    pages["/"] = function(request)
     file.open("home.html")
     X=file.read()
     file.close()
     tmr.wdclr()
     X = X ..F()
     return X
    end   


So far the Pro's.

Not so fine:
Due to the Limitations of "Another web server" all pages must be smaller than 1.4Kb.
It's not really easy to find a picture flie that fits into this size. :(

Due to the limited Heap-resources nodeMCU will reboot if the Server-Project becomes too complex (=memory-consuming).

So it is possible to serve a page with a small image OR start some cgi-scripts doing some "beeps" or "pwms" OR GPIO-toggeling but if you combine only few of them into one Project, free Heap will drop under 3-4K and the Server reboots :(

So far nodeMCU and the Web-Server is great for a quick proof of concept, but due to the memory-restrictions i didn't manage to realize a "real world-Project" like a HTML-controlled moodlight with a standalone ESP8266 & nodeMCU.

(If someone knows how to get around the menory-restricitions, please let me know..)
User avatar
By picstart
#7908 I can't get the code to work as the code reads the
<img src="led.gif">
into the pages[] table

The solution to sending more then 1460 bytes is along the lines below
Code: Select all-- start a web server.  Return the web server object
function startWeb(cfg)
  -- define member variables
  local config = cfg
  local state = "inactive"
  local server = net.createServer(net.TCP)

  -- define functions
  local close = function()
    server:close()
    state = "inactive"
  end

  local getStatus = function()
    return state
  end

  local parseRequest = function(request)
    -- the first line of the request is in the form METHOD URL PROTOCOL
    _, _, method, url = string.find(request, "(%a+)%s([^%s]+)")
    _, _, path, queryString = string.find(url, "([^%s]+)%?([^%s]+)")
    if queryString then
      query = {}
      for name, value in string.gfind(queryString, "([^&=]+)=([^&=]+)") do
        query[name] = value
      end
    else
      path = url
      query = nil
    end
    return { method = method, url = url, path = path, query = query, queryString = queryString}
  end

  -- start listening for requests
  server:listen(80,function(s)
    s:on("receive", function(s, rawRequest)
   local isopen = false
      request = parseRequest(rawRequest)
      print("Request received: ", request.method, request.url,request.path)
      if config.pages[request.path] then
        response = config.pages[request.path](request)
        status = "200 OK"
      else
        response = "<html><body><p>" .. request.url .. " doesn't exist.</p></body></html>"
        status = "404 Not Found"
      end
      headers = "HTTP/1.1 " .. status .. "\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\nContent-Length: " .. string.len(response) .. "\r\n\r\n"
      s:send(headers .. response)
    end)
   s:on("sent", function(s)
                if not isopen then
                   isopen = true
                   file.open("led_gif.html", "r")
                end
                local data = file.read()
                if data then
                   s:send(data)
                else
                   file.close()
                   s:close()
                   s = nil
                end
             end)
  end)

  state = "listening"

  return { getStatus = getStatus, close = close }


end
pages = {}

pages["/led.gif"] = function(request)
    file.open("led.gif")
     X = file.read()
     file.close()
     tmr.wdclr()
     return X , "image/gif"
    end

pages["/"] = function(request)
     file.open("led_gif.html")
     X=file.read()
     file.close()
     tmr.wdclr()
     X = X ..F()
     return X
    end


I am having difficulty in understanding
config.pages[request.path]
in the code below
I hope someone can explain where config. comes from and what it does?

The code above is difficult for me to understand

if config.pages[request.path] then
response = config.pages[request.path](request)
status = "200 OK"
else
response = "<html><body><p>" .. request.url .. " doesn't exist.</p></body></html>"
status = "404 Not Found"
end