Problem with changing pages in HTML handling ..
# help
b
In the following http.Request handler I am trying to grab a number that the user types in a form page
/start.html
store it away, then force a page change back to
index.html
The code works apart from the last two lines, it won't change to the index page .. Handler is:
Copy code
handle request/http.Request writer/http.ResponseWriter -> none:
  query := url.QueryString.parse request.path
  resource := query.resource
  if resource == "/": 
    if bucket["element"]:   
      resource = "index.html"   
    else:
      resource = "start.html"   // 1st time power up,  throw up the start.html page
  if resource == "/start": resource = "start.html" 
  if resource == "/extended": resource = "extended.html" 
  if resource.starts_with "/": resource = resource[1..]


  TEMPORARY_REDIRECTS.get resource --if_present=:
    writer.headers.set "Location" it
    writer.write_headers 302
    return

  result := RESOURCE_MAP.get resource --if_absent=:
    writer.headers.set "Content-Type" "text/plain"
    writer.write_headers 404
    writer.write "Not found: $resource"
    return

  if result is string:
    result = result.substitute: look_up_variable it
  writer.headers.set "Content-Type" (mime_type resource)
  if compression_type resource:
    writer.headers.set "Content-Encoding" (compression_type resource)

  writer.write result

  if query.parameters.is_empty: return 
  Element := query.parameters["Element"]
  element-size := int.parse Element
  bucket["element"] = element-size    // Save new element size to flash bucket.

  writer.headers.set "Location" "index.html"
  writer.write_headers 302
f
This night be http thing, but I don't think you can set the content type header and then redirect. I would expect that a redirect must be on its own. No body, or other headers.
b
What would be the correct way to do this, I know nothing re HTML 🙂
f
Do I understand correctly that you want to send a resource to the user (basically a download) and then, when that's finished redirect to another page?
b
No, just present a form for the user to fill in (initially during commision), then when that is submitted, change to the
normal
index.html page. Currently my hack is to reset .. but would like to do it a bit better ..
f
I don't understand the code: you are writing something as a response (
writer.write result
) but then want to redirect. Why do you write something if you just want to redirect?
b
The
write
is for pages that have variables ... maybe I need to do my redirect before it gets to there ..
f
That's probably it. If you have to redirect do it before writing anything as response.
And the return eagerly
b
Yep, thanks will try that out shortly ..
@floitsch It works ... see below:
Copy code
handle request/http.Request writer/http.ResponseWriter -> none:
  query := url.QueryString.parse request.path
  resource := query.resource
  if resource == "/": 
    if bucket["element"]:   
      resource = "index.html"   
    else:
      resource = "start.html"   // 1st time power up,  throw up the start.html page
  if resource == "/start": resource = "start.html" 
  if resource == "/extended": resource = "extended.html" 
  if resource.starts_with "/": resource = resource[1..]


  TEMPORARY_REDIRECTS.get resource --if_present=:
    writer.headers.set "Location" it
    writer.write_headers 302
    return

  result := RESOURCE_MAP.get resource --if_absent=:
    writer.headers.set "Content-Type" "text/plain"
    writer.write_headers 404
    writer.write "Not found: $resource"
    return
  
  if not query.parameters.is-empty:
    Element := query.parameters["Element"]
    element-size := int.parse Element
    bucket["element"] = element-size    // Save new element size to flash bucket.
    writer.headers.set "Location" "index.html"
    writer.write_headers 302
    return

  if result is string:
    result = result.substitute: look_up_variable it
  writer.headers.set "Content-Type" (mime_type resource)
  if compression_type resource:
    writer.headers.set "Content-Encoding" (compression_type resource)

  writer.write result
f
Lgtm
b
Huh?
f
Looks good to me. Standard clause used in Google reviews 🙂
b
Ha ... great thanks 🙂
@floitsch Quick question: Is there a way to push web page refresh action to the client's web page periodically. Is there any Toit support to do this?
Thinking something like this:
Copy code
<meta http-equiv="refresh" content="5; URL=http://www.yourdomain.xx/yourpage.php">
or
Copy code
header( 'refresh: 5; url=http://www.example.net' );
but know idea how to use these .. just found these tricks googling around
Ha, just doing
<meta http-equiv="refresh" content="5 />
in the page head section did the trick ..
You learn something every day 😎