cancel
Showing results for 
Search instead for 
Did you mean: 

CORS with LWIP

clock.1166
Senior

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

8 REPLIES 8

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

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

 

neither am I.

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

clock.1166
Senior

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!

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

:thinking_face:

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?

Jporcel777
Associate

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.

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

Good Luck!