Shop OBEX P1 Docs P2 Docs Learn Events
How to get unencoded data from browser to Spinneret server — Parallax Forums

How to get unencoded data from browser to Spinneret server

agsags Posts: 386
edited 2013-05-14 06:58 in Accessories
Not sure if this is an HTTP or HTML question (or both). I have a rudimentary server running on my project (Prop1 & W5100 based - similar but not identical to the Spinneret). I created form with text input, which I want to use to pass ascii characters to the Prop. I'm using the GET method.

I now realize that anything other than standard alphanumeric characters are encoded by the browser (I presume) and when received on the Prop require decoding. Is there another way to get all printable characters sent using text input? I read a note about a different encoding, but that is only available using POST method.

Have others dealt with this? (I'm attempting to use a web interface instead of a serial terminal to provide access to functionality on the Prop, without PST and a serial connection) Is the recommended way to just implement a decoder for the encoded text, or use POST and (I think it's possible) avoid the encoding, or is there another way?

On final extra-credit challenge: how can I include a carriage return (CR) character? Some commands require a <cr>, some do not. I can do some post-processing on the Prop (server) side if needed, but wonder if there isn't some escape sequence to embed a carriage return in the text.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-05-13 10:44
    The way arbitrary binary data blocks are usually sent is via Base64 encoding:

    -Phil
  • agsags Posts: 386
    edited 2013-05-13 11:28
    Thanks Phil. I'm looking for a way to allow a user to enter text from a keyboard into a textfield presented by the server to the browser. Think of it as using a browser on a remote machine to communicate with an Ethernet-connected Prop instead of a standard serial terminal and USB connection. The text field replaces the terminal emulator (or Propeller Serial Tool) just to get characters (but including things like punctuation) to the Propeller.
  • Mike GMike G Posts: 2,702
    edited 2013-05-13 16:24
    Create a web form to submit either an HTTP GET or HTTP POST to the Spinneret.
  • agsags Posts: 386
    edited 2013-05-13 20:29
    Sorry that I'm not being precise - partly because this is not my area of expertise.

    I have implemented a form, using text input (and submit button) and it works as documented. The problem is that anything other than [a-z][A-Z][0-9] is encoded before sending. When I parse the string, all spaces are collapsed and replaced by a "+", and any other characters are encoded as a sequence %<ASCII value>. I can write code to parse and restore the string (other than any duplicate white spaces), but I'm wondering if there is a way to suppress the encoding (on the client side) as an easier way to deal with this. Has anyone run into this problem, and if so, how did you deal with it?

    Thanks.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-05-13 20:39
    No, you do not want to suppress the encoding. It's there for a reason, so that it does not screw up the HTTP transfer. HTTP is an ASCII protocol. If just any character were allowed in the body of a request, the receiving end might confuse part of the content as an HTTP command. This is particularly true of GET requests, in which the content is embedded in the URL. (Not all ASCII characters are allowed in URLs.) The encoding is done to prevent confusion with other parts of the protocol.

    -Phil
  • agsags Posts: 386
    edited 2013-05-13 21:11
    Phil, you make good points. That also may offer a reason (if I understood what I quickly read late last night) why it is possible to send unencoded text using the POST method. IIRC in the POST method the text is not part of the URL, but it is in the body of the html document. Maybe...

    So it does seem that this is a definitive answer to part of my question: it is not possible to defeat the encoding by text input in a form using the GET method. This is by design and necessity as the value is embedded in the URL and would otherwise cause problems.

    Now I have to decide if it's worth implementing support for the POST method, just to avoid the encoding. I suspect it will just be easier to decode (unless there is some other benefit to using POST (or having support for POST for future use) or downside to using GET).
  • Mike GMike G Posts: 2,702
    edited 2013-05-14 06:58
    ags, HTTP is a text based protocol as stated by Phil in post #6. That includes the body. Binary data can be sent via HTTP and is usually related to file upload but decoding URL encoded data is pretty easy. This method was Posted by Roy E. IIRC, I modified the method to fix a bug - but I can't remember. Anyway the method works well.
    PRI DecodeString(source) | char, inPlace, outPlace
      inPlace := outPlace := 0
      repeat
        'TODO: Handle HTML encoing ie &alt;  
        char := byte[source][inPlace++]
        if (char == "%") ' convert %## back into a character
          ' first nibble
          char := byte[source][inPlace++] - 48
          if (char > 9)
            char -= 7
          char := char << 4
          byte[source][outPlace] := char
          ' second nibble
          char := byte[source][inPlace++] - 48
          if (char > 9)
            char -= 7
          byte[source][outPlace++] += char
          ' since we trashed char doing the decode, we need this to keep the loop going
          char := "x"
        elseif (char == "+") ' convert + back to a space
          byte[source][outPlace++] := " "
        else ' no conversion needed, just set the character
          byte[source][outPlace++] := char
      until (char == 0)
        
      byte[source][outPlace-1] := 0 ' terminate the string at it's new shorter size
    

    This method is part of a larger lib that tokenizes and enumerates an HTTP header.
    http://code.google.com/p/propeller-w5200-driver/source/browse/trunk/%20propeller-w5200-driver/HttpHeader.spin

    A proxy can be used to send data to the spinneret. The proxy handles the HTML and URL encoding then forwards the message to the Spinneret via UDP or TCP.
Sign In or Register to comment.