POST webpage data in v3.x

Discussion to talk about software related topics only.
Post Reply
SeeCwriter
Posts: 605
Joined: Mon May 12, 2008 10:55 am

POST webpage data in v3.x

Post by SeeCwriter »

I am converting a webpage used on a MOD5441X, built with v2.9.5 to use with v3.3.8. The webpage is
working with one exception that I am having trouble with.

One feature of the webpage is to get calibration data from the module and display it in a graph. This works.
But the webpage is also supposed to be able to send calibration data to the module. This is the part that
doesn't work.

To get the cal data, the webpage sends a GET command, which invokes a callback function declared with CallBackFunctionPageHandler.
The callback function calls SendTextHeader(sock) followed by a series of writestring calls. The cal data is stored as binary
integers on the module, but converted to ASCII hex values to send to the webpage. The cal data after converting to ASCII hex
is 6448 bytes plus a terminator \0.

To send the cal data to the module, this javascript is used:

Code: Select all

function UploadTable()
{
   var i=upload_step;
   var s;
   http = new XMLHttpRequest();
   http.open("POST","0.htm",true);
   start = i*2*0x1000;
   s = "CAL_TABLE"+i+"=";
   s += calfile.raw[i];
   http.send(s);
}
The above sends a 6.4k string of ASCII hex data prefixed with "CAL_TABLE0=".

In the module is this code:

Code: Select all

HtmlPostVariableListCallback mainformpost("0.htm", WebIncomingPost );

#define MAX_POST_DATA 6500 // Biggest block is cal table... (800+6) integers = 3224 bytes, x2 for hex string = 6448 + \0 terminator.
char cmd_post_string[MAX_POST_DATA]; 

void WebIncomingPost( int sock, PostEvents event, const char *pName, const char *data )
  {
  int i;

  switch ( event )
    {
    case eStartingPost: break;
    case eVariable:
      i = (int) strlen( data );
      if ( i == 0 ) return; // No data?
      
      iprintf( "WebPost pName: %s, data len = %d\r\n", pName,i );
      
      if ( StrMatch( pName, "cmd_string" ) )
        {
        siprintf( cmd_post_string, "%s\r\n", data );
        StoreCmdPacket( NULL, cmd_post_string, false, i+2 );  // add string to command fifo.
        }
      else if ( StrMatch( pName, "CAL_TABLE" ) )
        {
        CalTablePost( sock, data );  // save cal data.
        }
      break;
    case eFile:       break;
    case eEndOfPost:  break;
    }
  }
When the POST callback is invoked, the iprintf output is: "WebPost pName: CAL_TABLE0, data len = 255".
The 255 bytes of data are the first 255 bytes the cal data. But that's all that ever shows up.
However, wireshark captures over 6k of data being sent.
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: POST webpage data in v3.x

Post by pbreed »

A couple of ways to fix this...
What I would consider the cleanest is as follows:
create a function:
int MyDataProcess(int sock, HTTP_Request &pd)
{//Will be called with all the post data ready to read....

return 1; //Saying we handled it.
}

CallBackFunctionPostHandler cf("url.html",MyDataProcess);

Need to look at this more, present system does not really support large individual variables.....
SeeCwriter
Posts: 605
Joined: Mon May 12, 2008 10:55 am

Re: POST webpage data in v3.x

Post by SeeCwriter »

I followed your suggestion and created a callback function using example program SimplePostReceiver as a guide:

Code: Select all

CallBackFunctionPostHandler  GHB( "Cal.htm", callbackCalDataPostHandler );

int callbackCalDataPostHandler( int sock, HTTP_Request &pd )
  {
  char *pRxData = cmd_post_string;
  iprintf( "CalDataPost: content len %lu, rx len %lu\r\n", pd.content_length, pd.rx_length );
  while ( pd.content_length > pd.rx_length )
    {
    int rv = ReadWithTimeout( sock, pRxData, 806, TICKS_PER_SECOND * 2 );
    if ( rv > 0 )
      {
      pd.rx_length += rv;
      pRxData += rv;
      }
    }
  *pRxData = 0;
  iprintf( "CalDataPost: rx len %lu\r\n", pd.rx_length );
  if ( CalTablePost( sock, cmd_post_string ) ) return 1;
  return 0;
  }
I then changed the javascript by changing the url to "Cal.htm":

Code: Select all

function UploadTable()
{
   var i=upload_step;
   var s;
   http = new XMLHttpRequest();
   http.open("POST","Cal.htm",true);
   start = i*2*0x1000;
   s = "CAL_TABLE"+i+"=";
   s += calfile.raw[i];
   http.send(s);
}
But the callback function never gets called.
SeeCwriter
Posts: 605
Joined: Mon May 12, 2008 10:55 am

Re: POST webpage data in v3.x

Post by SeeCwriter »

To my post callback function I added function GetActiveHttpRequest to examine the request. I don't understand what parameter content_length is the length of. I thought it was the amount of data pointed to by pData, but that doesn't appear to be the case because when I print-out the data all bytes are always 0. And it doesn't matter if I read the data using ReadWithTimeout, per some examples, or use pr->pData.
Post Reply