Hi to all participants.
I’m porting some app from mongoose 6.x to 7.x.
I really appreciate some simplifications made available, but I found new APIs very unconfortable when I need to handle cookie manually.
Suppose I have sent a cookie, be mycookie. Then, to next HTTP calls cookie header will contains something like:
"mycookie=foo bar baz
Connection: keep-alive"
Suppose I have set the cookie “mycookie” to “foo bar baz”. So, with mongoose 6.x branch I can retrieve the cookie value with mg_http_parse_header2()
function, as follows:
// with mongoose webserver 6.18
struct mg_http_message *hm = (struct mg_http_message*) data;
struct mg_str *cookie_header = mg_get_http_header(hm, "cookie");
char buf[25];
char *bptr = buf;
if (mg_http_parse_header2(cookie_header, "mycookie", &bptr,
sizeof(bptr))) {
// now bptr buffer contains "foo bar baz" string
}
But… function mg_http_parse_header2()
appears to be removed since mongoose 7.x branch. I have no choice than manually parsing the cookie header for finding the desired value.
// with mongoose webserver 7.x (e.g. 7.2)
struct mg_http_message *hm = (struct mg_http_message*) data;
struct mg_str *cookie_header = mg_http_get_header(hm, "cookie");
// copy entire header content in buffer
char buf[25];
strncpy(buf, cookie_header->ptr, 25);
// fetch the first line
char *str = strtok(buf, "\n"); // str = "mycookie=foo bar baz"
char *token = strtok(str, "="); // str = "mycookie"
char *z = NULL;
while (token != NULL) {
z = token;
token = strtok(NULL, "=");
}
// now, str buffer finally contains the desired value "foo bar baz"...
This appears much more confusing and error-prone than previous version. Note that doing so, I’m silently assuming that searched value is at first line…
Is this right? Really this is the only recommended / available solution for reading a single cookie value? I can’t believe that mongoose 7.x don’t offer a more handy solution for so common problem, as previous versions do. I probably miss something and I’m doing the wrong way; reading docs / source of examples I’m unable to find any suggestions.
Can anyone point me in the right direction?
Thanks.