How to setup service like REST with FreeRTOS Plus TCP

joporez wrote on Thursday, December 01, 2016:

I would like to build service that would managed and responding by simple JSON object. I’ve created project based on FreeRTOS, FreeRTOS-Plus-TCP and FreeRTOS-plus-FAT sources. I was able to set up network connection between browser and MCU. But i am wondering how to implement such behaviour, because according to FreeRTOS-Plus-TCP : FreeRTOS_HTTP-Server.c responds with file (web page) on every request. But maybe there is a built-in mechanism to catch every Http request and handle it without modification of FreeRTOS-Plus-TCP sources.

rtel wrote on Thursday, December 01, 2016:

Do you need your new service and the main HTTP server running at the
same time? Perhaps just don’t create the HTTP server and have your new
service respond to all requests.

joporez wrote on Thursday, December 01, 2016:

Actually, i need both of them. In some cases it is good to respond by pages, in other (i.e. to make chain of data processing, to handle data from mobile devices it is neccessary to use it like REST (i.e. having some widgets on the screen). in addition there is one more thing: i am configuring useHTTP by config in FreeRTOSIP.config so in this case a ma switching onbuilt in htttp processing. So to exclude i shoul proccess raw TCP or modify existing HTTP Server.

heinbali01 wrote on Friday, December 02, 2016:

Michael,

Have you had a look at ipconfigHTTP_HAS_HANDLE_REQUEST_HOOK already?

A user-hook will be called for every GET received:

    /*
     * A GET request is received containing a special character,
     * usually a question mark.
     * const char *pcURLData;    // A request, e.g. "/request?temperature=?"
     * char *pcBuffer;           // Here the answer can be written
     * size_t uxBufferLength;    // Size of the buffer
     * 
     */
    int xGetTemperature()
    {
    static int temp = 23;
        return temp++;
    }
    size_t uxApplicationHTTPHandleRequestHook( const char *pcURLData, char *pcBuffer, size_t uxBufferLength )
    {
        size_t xResult;
        const char pcMyRequest[] = "temperature=?";

        pcURLData = strchr( pcURLData, '?' );
        if( ( pcURLData != NULL ) && ( strncmp( pcMyRequest, pcURLData + 1, sizeof( pcMyRequest ) - 1 ) == 0 ) )
        {
            xResult = snprintf( pcBuffer, uxBufferLength, "/request?temperature=%d", xGetTemperature() );
        }
        else
        {
            xResult = 0;
        }
        return xResult;
    }

In the same way, it is also possible to respond to complex JSON expressions.
As long as the hook returns zero, the url after GET is treated as a filename like here:

    GET /css/images/ui-icons_ffffff_256x240.png HTTP/1.1

Here is a more complex JSON example:

    GET /audio_set?sample={"start_date":20161128,"end_date":20161128,"start_time":100,"end_time":240000}&records=?

The question-mark itself may never be used in an url that indicates a file name. So the hook will first test on the occurrence of a ?. If present, the request needs special treatment.

I hope this makes sense?

heinbali01 wrote on Monday, December 05, 2016:

Ping ping, does that answer your question? Will that work for you?

joporez wrote on Thursday, December 08, 2016:

Sorry, that have not answered you, I understood the idea but, unfortunately i was unable to catch the hook. I’ll keep my researches and when i receive result i write about it. However according to sniffer i started to receive 404, but i had to sufficiently decrease ipconfigTCP_WIN_SEG_COUNT up to 12 . What do you think is this decreasing could influence on parallel working with web server or on performance.

heinbali01 wrote on Thursday, December 08, 2016:

Hi Michael,

Normally when people develop a new +TCP driver, I get in touch with them directly. If you want, write me at “h point tibosch on freertos point org”. Then we’ll probably get through this a lot quicker.

The feature ipconfigHTTP_HAS_HANDLE_REQUEST_HOOK is fairly new, and only available in the latest 160919 release. It is more a sketch rather than a real solution. I would like to get feedback about what solution fits best for most users.