Skip to main content
Explorer II
March 14, 2024
Question

CORS with LWIP

  • March 14, 2024
  • 3 replies
  • 2648 views

Hi - can anyone tell me how to insert a CORS header into LWIP?

(Cross-Origin Resource Sharing) header directly into the httpd.c? file of LwIP)

i.e.Access-Control-Allow-Origin: *

I am using vscode to debug an index.shtml file file, but i need to get responses from my H743 webserver to be able to debug fully.

Any ideas?

Thanks

Chris

    This topic has been closed for replies.

    3 replies

    Super User
    March 14, 2024

    From https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS  it looks like they're just standard HTTP headers ?

    Explorer II
    March 15, 2024

    Thanks Andrew,

    Thats quite interesting - but I'm trying to find out where and how to insert these headers into the LWIP response CODE. I have found various places, but none of my attempts appear to work.

    Unfortunately, I am not an expert in LWIP webserver code and I do not have weeks of time to spare!

    Chris

     

    Super User
    March 15, 2024

    neither am I.

    But, given they're just ordinary HTTP headers, then inserting them should should be the as inserting any other headers?

    Explorer II
    March 15, 2024

    Apparently, you cant just add CORS headers directly to the HTML file.

    I'm trying to find exactly where in the LWIP (perhaps httpd.c) code I can add them!

    Super User
    March 15, 2024

    That doesn't sound right: HTTP is a transport protocol - HTML is a payload format.

    :thinking_face:

    Explorer II
    March 15, 2024

    From what I've deduced from searching, the way to dynamically add the CORS header into LWIP is somewhere in the httpd.c file.

    But where and how?

    Explorer
    March 22, 2024

    Hello!

    I am facing the same problem! I think we need to add the CORS policy headers in this section: 

    static const char *const g_psHTTPHeaderStrings[] = {
     "HTTP/1.0 200 OK\r\n",
     "HTTP/1.0 404 File not found\r\n",
     "HTTP/1.0 400 Bad Request\r\n",
     "HTTP/1.0 501 Not Implemented\r\n",
     "HTTP/1.1 200 OK\r\n",
     "HTTP/1.1 404 File not found\r\n",
     "HTTP/1.1 400 Bad Request\r\n",
     "HTTP/1.1 501 Not Implemented\r\n",
     "Content-Length: ",
     "Connection: Close\r\n",
     "Connection: keep-alive\r\n",
     "Connection: keep-alive\r\nContent-Length: ",
     "Server: "HTTPD_SERVER_AGENT"\r\n",
     // Add CORS headers
     "Access-Control-Allow-Origin: *\r\n",
     "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n",
     "Access-Control-Allow-Headers: Content-Type\r\n",
     "\r\n<html><body><h2>404: The requested file cannot be found.</h2></body></html>\r\n"
    #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
     , "Connection: keep-alive\r\nContent-Length: 77\r\n\r\n<html><body><h2>404: The requested file cannot be found.</h2></body></html>\r\n"
    #endif
    };

    But there is something missing.

    Let me know if you find any solution.

    Explorer II
    March 22, 2024

    Hi Jp - i've tried that already - makes no difference. I need to do a bit (lot) of studying!!

    Good Luck!