Added CORS support to WICED http_server.c to make JSON testing easier

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
AnSa_1225656
Level 4
Level 4
50 replies posted 25 replies posted 10 replies posted

I added the

    "Access-Control-Allow-Origin: *\r\n" // This line added to enable cross site scripting

line in the top of the http_server.c in Library/deamons.  It allows me to build my web site as files on my PC and go against real JSON data from my device.  Thought it might be handy to others.  Let me know if there is a better approach to adding CORS support.

<snip>

static const char ok_header[] =

    "HTTP/1.0 200 OK\r\n"

    "Access-Control-Allow-Origin: *\r\n" // This line added to enable cross site scripting

    "Content-Type: ";

</snip>

0 Likes
1 Reply
AnSa_1225656
Level 4
Level 4
50 replies posted 25 replies posted 10 replies posted

For 3.1.2

----In http_server.c----

    /* Content-Type: xx/yy\r\n */

    wiced_tcp_stream_write( stream, HTTP_HEADER_CONTENT_TYPE, strlen( HTTP_HEADER_CONTENT_TYPE ) );

    wiced_tcp_stream_write( stream, http_mime_array[mime_type], strlen( http_mime_array[mime_type] ) );

    wiced_tcp_stream_write( stream, CRLF, strlen( CRLF ) );

    // Add following two lines to allow cross site scripting

    wiced_tcp_stream_write( stream, HTTP_HEADER_CORS, strlen( HTTP_HEADER_CORS ) );

    wiced_tcp_stream_write( stream, CRLF, strlen( CRLF ) );

----In http_server.h----

#define HTTP_HEADER_CONTENT_TYPE       "Content-Type: "

// Add the following line

#define HTTP_HEADER_CORS               "Access-Control-Allow-Origin: *"

#define HTTP_HEADER_CHUNKED            "Transfer-Encoding: chunked"

0 Likes