Shop OBEX P1 Docs P2 Docs Learn Events
How to load correct page after a POST received - using HTTPServer — Parallax Forums

How to load correct page after a POST received - using HTTPServer

homosapienhomosapien Posts: 147
edited 2013-07-12 11:32 in Accessories
New to the Spinneret and HTML, looks like a great tool.

Mike G. - excellent info on your site for getting started with web work and the Spinneret - thank you. Have already started acquiring books on HTML and internet protocols after examining your tutorials and spinneret code.

My question: I am using a slightly modified version of HTTPServer. In my application, the server(Spinneret) sends a form to a browser and the browser user responds by filling out input fields and hitting a 'submit' button and the server gets the POST response. Is there a 'clean' way to have the server send a page back to the browser after receiving the POST response (so the user sees something other than the input form)?

I am thinking I could use the Request.setName method (and other SET methods from Request.spin as needed) to alter the POST response header (after processing the needed input form data) and call the staticFileHandler method, but this seems kind of kludgy.

Am I missing something obvious here?

If this question is too vague or not enough info, please let me know. Not trying to be abstruse, beginner HTML/Spinneret/HTTPServer here....


Thanks,
Nate

Comments

  • Mike GMike G Posts: 2,702
    edited 2013-07-07 09:15
    My question: I am using a slightly modified version of HTTPServer. In my application, the server(Spinneret) sends a form to a browser and the browser user responds by filling out input fields and hitting a 'submit' button and the server gets the POST response. Is there a 'clean' way to have the server send a page back to the browser after receiving the POST response (so the user sees something other than the input form)?
    Sure, there are a few of options. Here are two.
    • Add an "action=page.htm" attribute to the form tag. In the dispatcher filter for "page" and the "POST" HTTP method. Process the POST. Let the StaticFileHandler render static page.htm.
    • If the resulting page is dynamic, then you must create page dynamicly. Again filter for the requested page and the "POST" HTTP method in the Dispatcher method. Do NOT let the StaticFileHandler execute. Instead, build a dynamic page by taking advantage of the PushDynamicContent() method. Tx the dynamic page as the response.
    Option 2:

    HTML page name is postme.htm. Notice the Action="postex.htm" Form attribute. This action is passed to the server. The server must filter for postex.htm not the page name postme.htm.
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>POSTing to the Server</title>
    </head>
    <body>
    <div style="width:600px;margin:auto;">
      <h1>POSTing to the Server</h1>
      <form action="postex.htm" method="post" name="post" id="post">
        Data:
        <input id="data" type="text" name="data" value=""/>
        <input type="submit" name="Submit" value="Click Me!" />
      </form>
    </div>
    </body>
    </html>
    

    HttpServer snippet Filter for the file name "postex" and HTTP POST. invoke a custom handler - PostEx. PostEx builds a custom response. The if block returns without executing the StaticFileHandler.
    PRI Dispatcher(id)
    '' HTTP request handler
    
      if(strcomp(Request.GetName(id), string("postex")) AND strcomp(Request.GetMethod(id), string("POST")))
        PostEx(id)
        return
    
      ' Send requested file content
      StaticFileHandler(id)
      return
     
    PRI PostEx(id) | headerOffset
      'Set the buffer location
      dynamicContentPtr := @tempBuff
    
      'Create the response header
      headerOffset := Response.BuildHeader(string("htm"), 200, false)
    
      'dynamic data
      PushDynamicContent(string("Dynamic Content"))
    
      'Send the header and dynamic content
      Socket.txTCP(id, @txdata, headerOffset)
      StringSend(id, @tempBuff)
    
      'Zero the buffer
      bytefill(@tempBuff, 0, TEMP_BUFFER)
      return
    

    While the two options above will work great. It is better to use the Spinneret as a service. Spinneret as a Service would be similar to option two except use AJAX (client JavaScript) to submit the request and parse the response. This method allows the programmer to offload processing to the client and the intent of HttpServer. This method would require design changes though.
  • homosapienhomosapien Posts: 147
    edited 2013-07-09 08:16
    Hi Mike,

    The <form action= > was exactly what I was looking for. Thanks.

    One issue I had: A form was POSTing four separate text input fields. If the inputted text and variables were over a certain length (POST header, 'Content-length' was over ~62) the last text variables were not being seen by the Response.Post method (it appeared to be returning a '0'.) On closer inspection of the Respone.Post method:
    PUB Post(id, pname) | t1, t2, strt
    ''look for string attached to 'pname' variable.  will return ptr to value associated, or if no such variable will return null ('0' I believe)  NGH
    
    
      t1 := MatchPattern(@@queryString[id], pname, 0, false)
      if(t1 == -1 or t1 > 50)                 
        return  null
    
    
      return @@queryString[id] + t1 + 1 + strsize(pname)
    
    
    

    It appears to have a limit of 50 placed on the return position. I checked out the PASM code for the stringMethods, there seems to be a 2k upper limit on the searched string length (ie, appears as if there IS a bounding limit on the search). I removed the 't1 > 50' condition from the Response.Post method, and the problem seems to be solved. Can you tell me if I am creating a problem (is there something that uses the Response.Post method that should NOT look for more than 50 chars?), or is the upper limit of 50 a vestigial piece of code?

    Thanks,
    Nate
  • Mike GMike G Posts: 2,702
    edited 2013-07-09 09:07
    There is a finite amount of buffer space for each request. POST parameters are located in the message body which is at the end of the request.

    Stop using HttpServer and start using the new driver. The new driver has a much better request handler, has been thoroughly tested, is smaller, has more features, and is much more extensible.
    http://code.google.com/p/propeller-w5200-driver/source/browse/trunk/#trunk%2F%20propeller-w5200-driver%2FSpinneret
  • homosapienhomosapien Posts: 147
    edited 2013-07-12 06:21
    Ok Mike.

    I was using the code on from your 'servebeer' spinneret because it was convenient to your tutorial and I was unsure what was the most current.

    After reading around in this forum more, I have downloaded the latest from Google. I'm sure I'm going to have more questions now that the newest has DHCP and other bells and whistles...
  • Mike GMike G Posts: 2,702
    edited 2013-07-12 08:01
    Thanks man... I've been trying to get folks on the newer driver. The DHCP, DNS, and web server libraries have been thoroughly vetted by forum members. Feel free to ask any questions. I'll do my best to fix any bugs.

    I realize the tutorial is based on the HttpServer. Mybe one of these days I'll find time to update the tutorial. If you have a handle on the HttpServer, the new stuff is much the same. The difference being a layered design. The cool thing is all the W5200 libraries can but used with the Spinneret and visa versa.

    Check out Tom Cantrell's article. Tom did a great job explaining the firmware.
    http://www.digikey.com/us/en/techzone/wireless/resources/articles/ethernet-orb-sees-all.html
  • homosapienhomosapien Posts: 147
    edited 2013-07-12 11:32
    I have to admit, the file path/tree for Webserver_5100_RTC looked a little intimidating... but having spent some time with HttpServer first helps a lot.

    I will post future questions under appropriate thread titles to help other people leaning about the Spinneret and your code and also so perhaps you will not have to keep answering the same questions over and over again... Great Digikey article.

    Thanks for your help so far.



    Nate
Sign In or Register to comment.