5 years, 8 months ago.

Parsing a HTTP post response

Hi All, I am hoping someone can give me some guidance. I am trying to write a program that accesses a webservice to receive a pending command. I think I have the post setup but its the processing of the returned string of data. I do see a number of parsing samples but all these options are confusing me. Is there a simple parser out there with some tutorial on using it? I do have a C parser that I am currently using but I believe there are better options out there. Just having trouble figuring out the correct option to use. Thanks for any help you can give me. Best Regards, Rob Berry

1 Answer

5 years, 8 months ago.

Depends on how fast you need to extract the post data, how many connections you are expecting and Wi-Fi or Ethernet. If you don't pass the data fast enough another HTTP request can come which can happen with slow Wi-Fi / Ethernet adapters. Also buffered serial with lots of sscanf functions can be to slow particularly if you are serving dynamic web pages with jQuery scripts.

This is something I'm working on atm from some examples I saw on Mbed that simply looks for the requests in a ring buffer (rxBuff) as the data comes in.

As soon as see's 0x0d it ignores any further data, usually around 350 extra unwanted bytes from the HTTP request is ignored.

This works fast enough even with slower Wi-Fi adapters (115200 baud).

uint32_t ESP8266::readHTTPRequests()
{    
    char rxByte;
    static char state=0;
    while(bufferReadIndex_IpdSearch!=bufferWriteIndex)
    {
        rxByte=rxBuff[bufferReadIndex_IpdSearch++];
        if(bufferReadIndex_IpdSearch>=BUFFER_SIZE)
            bufferReadIndex_IpdSearch=0;
        switch(state)
        {
            case 0:if(rxByte=='+')state=1;break;
            case 1:if(rxByte=='I')state=2;else state=0;break;
            case 2:if(rxByte=='P')state=3;else state=0;break;
            case 3:if(rxByte=='D')state=4;else state=0;break;
            case 4:if(rxByte==',')state=5;else state=0;break;            
            case 5:if(rxByte!=0x0d){reqLineBuff[reqLinBuffIndex++]=rxByte;}
                else{
                    reqLineBuff[reqLinBuffIndex]=0;
                    state=0;
                    reqLinBuffIndex=0;
                 //   debug_if(debugOn, "AT< %s \r\n", reqLineBuff);
                    return 1;
                    }
            }
        }
    return 0;
}

// use something like this to filter the request's
sscanf(esp.reqLineBuff,"%d,%d:%s %s",&linkId,&ipdLen,requestType,request);