cancel
Showing results for 
Search instead for 
Did you mean: 

LwIP, HTTP POST, Firmware Update - size of Buffer less than Content-Length

HHuyn.4
Associate III

Hello there, I'm writing a STM32 update firmware function (using lwIP ) that accepts a HTTP Post Request to send a new .bin file.

Spoiler
HHuyn4_1-1734432440584.png

 


After selecting a .bin file to transfer from the PC to the MCU. I use the following line to capture the request:

 

netbuf_data(inbuf, (void**) &buf, &buflen);

 

Here is the buf content:

 

POST /index.html HTTP/1.1
Host: 192.168.2.158
Connection: keep-alive
Content-Length: 176815
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryGhYpLxBOxGv1PE1D
Accept: */*
Origin: http://192.168.2.158
Referer: http://192.168.2.158/index.html
Accept-Encoding: gzip, deflate
Accept-Language: vi-VN,vi;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,en;q=0.5,zh-TW;q=0.4,zh;q=0.3

 

You can see Content-Length is 176815 while buflen gets a value of 614.

I have some questions and would appreciate any suggestions from you:

1. Please help me point out any mistakes, if there are any.
2. Why is there a difference between Content-Length and buflen?
3. How can I receive the content of a .bin file to store it in flash? Any examples or suggestions would be greatly appreciated. Many thanks.

6 REPLIES 6
LCE
Principal

POST is a http "method" using TCP, so maximum single packet size is usually having a maximum data size limited by TCP to 1460 bytes.

Thus, anything larger must be sent with multiple TCP packets.
The size of these packets depends on your PC settings and on your browser (not sure, though, but I think the browser does the "packaging", windows has a registry setting for maximum TCP packet size).

So most importantly you have to make sure to receive these multiple packets correctly with lwIP.
Check httpd.c for the httpd_post_* functions.

HHuyn.4
Associate III

Thanks @LCE ,

This means that I will have to split the new firmware into multiple packages and send them to the STM one by one. I am currently doing it this way. If this is not the best approach, please let me know

LCE
Principal

No, you don't have to split anything, that is what the browser does for you.

On the STM32 side, you have to store the payload of each packet. Only the first packet has this extra length and file name info (plus already some data of the bin file).

 

LCE
Principal

Start by sending small and simple text files with some readable ASCII.
Then increase the text file so that it is sent via multiple packets.

If that's working, try with bin files.

Use Wireshark to check packets, and UART output on the STM32 side.

HHuyn.4
Associate III

Do you not use netcon and rely entirely on the httpd_post_* functions? 

LCE
Principal

What's netcon? :D