B4R Question ESP8266 - WebServer Headers Inssue

Bangdido

Member
Licensed User
Longtime User
Hello, Thank you for this magnific example:

https://www.b4x.com/android/forum/threads/esp8266-wifi-remote-configuration.68596/#post-464800

I'm using a ESP-01, i'm trying to parse http headers from browser (for example to get a cookie) but the header are split,

this is an example of the header from browser (on my delphi web server and apache web server):

GET /settings HTTP/1.1
Host: 192.168.1.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Cookie: session=6bOt36d0999T4da1e8214bc3a90328f50
Connection: keep-alive​

Using the same browser, i get it on esp-01 "Log(Buffer)" (using the same example and code of the demo above)

GET /settings HTTP/1.1
Host: 192.168.4.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0
Accept-Lang
uage: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Cookie: sessi
on=6bOt36d0999T4da1e8214bc3a90328f50

Connection: keep-alive

any clue about how to fix it ?
Thanks in advance
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should expect messages to be split or merged. This is how network communication works.

You can try to increase AsyncStreams.WaitForMoreData. The default value is 5. Increase it to 500. This will cause AsyncStreams to wait 500ms for more data.

A better solution will be to collect the data until you encounter a new line character and then parse it.
 
Upvote 0

Bangdido

Member
Licensed User
Longtime User
Increasing AsyncStreams.WaitForMoreData does not help,

any idea how to do that (collect data until new line)?
i'm having error here.
B4X:
Sub Process_Globals
   Private FBuffer(1)  As String
end sub

Private Sub APStream_NewData (RawData() As Byte)
   FBuffer(0) = JoinStrings(Array As String(FBuffer(0), RawData(0)))
end sub
 
Last edited:
Upvote 0

Bangdido

Member
Licensed User
Longtime User
I'm having problems to understand strings and array of bytes..
i don't know how to use them.
Digging in the code i saw it on AsyncStreams.cpp Line #3
#define MAX_SIZE_NO_PREFIX 100

so increased to #define MAX_SIZE_NO_PREFIX 1000

and now i'm getting all the header

B4X:
Length:328
[GET / HTTP/1.1
Host: 192.168.4.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-MX,es;q=0.7,en-US;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1
]
 
Upvote 0

Bangdido

Member
Licensed User
Longtime User
thank you, but you can help me with collecting data ?

i'm using a global String

B4X:
Sub Process_Globals
   Private FBuffer(1)  As String
End Sub

Private Sub APStream_NewData(RawData() As Byte)
   FBuffer(0) = JoinStrings(Array As String(FBuffer(0),bc.StringFromBytes(RawData)))   
  Log("FBuffer:[",FBuffer(0),"]")
End Sub

but i get a crash everytime i ran it, i'm starting with arduino, and dont know how to use strings and bytes yet.
 
Upvote 0

Bangdido

Member
Licensed User
Longtime User
Ok, let me try to explain,
i'm using a esp-01, it save data to eeprom memory (like ssid and password where to connect, access point name and password for local use)
firs time the module try to connect to ssid with password,
after 1 minute, if cant connect, switch to access point (AP) mode for configration

in AP open a webserver to dispatch compressed pages (static pages),
it have a login page where ask username and password to show/set configuration (ssid name and password, ap name and password)
that is where i'm trying to "parse" http headers, i'm using a POST method in login form to send username and password (i don't want to use GET
becose it send visible parameters in url)

if username and password are ok, i'll set a cookie on browser to navigate free on settings pages.
for some reason if i increase the length on AsyncStreams.cpp Line #3
#define MAX_SIZE_NO_PREFIX 100

i get the full header and i can process it, but when i use the POST method the esp-01 crashes (receiving the data)
and i dont know why.

 
Last edited:
Upvote 0