http client with mongoose 5.3

195 views
Skip to first unread message

Fabrizio Dini

unread,
Mar 26, 2014, 6:24:21 AM3/26/14
to mongoos...@googlegroups.com
Hi Sergey! Hi everybody!

For a personal project of mine, I am trying to send http requests to an external web/cgi server. I managed to connect to the remote server with mg_connect() and get the event handler called with MG_CONNECT event. However, I wasn't able to forge a correct http GET request to a simple web server. I do get the request to be sent via my eth0 interface (I verified that with wireshark), but both the request and the response I get looks strange.

I could copy/paste them here, but that would be quite a lot of text... Instead, it would suffice to have a little more documentation on how mg_connect() work and how a client is expected to work with mongoose. In particular, since my application is supposed to serve local files and CGIs also, I would like to know if it is a good idea to use two separate mongoose servers to handle incoming and outgoing requests, or it is ok to use a single server for both.

Another critical question is: what are the correct return codes to be used in the event handler for MG_CONNECT, MG_REPLY and MG_CLOSE events? And, last but not least, what is the correct way to forge and send the http request? I am trying by sending text with mg_send_data(), but I am not sure I am doing it right. An insight of how mg_poll_server() works with outgoing requests would also be helpful.

Thanks in advance for your help!

Sergey Lyubka

unread,
Mar 26, 2014, 10:05:52 AM3/26/14
to mongoose-users
Hi Fabrizio!

On Wed, Mar 26, 2014 at 10:24 AM, Fabrizio Dini <dini.f...@gmail.com> wrote:
Hi Sergey! Hi everybody!

For a personal project of mine, I am trying to send http requests to an external web/cgi server. I managed to connect to the remote server with mg_connect() and get the event handler called with MG_CONNECT event. However, I wasn't able to forge a correct http GET request to a simple web server. I do get the request to be sent via my eth0 interface (I verified that with wireshark), but both the request and the response I get looks strange.

I could copy/paste them here, but that would be quite a lot of text...

Consider publishing your project on github. Then commenting and patching wouldn't be a problem!
 
Instead, it would suffice to have a little more documentation on how mg_connect() work and how a client is expected to work with mongoose. In particular, since my application is supposed to serve local files and CGIs also, I would like to know if it is a good idea to use two separate mongoose servers to handle incoming and outgoing requests, or it is ok to use a single server for both.

Another critical question is: what are the correct return codes to be used in the event handler for MG_CONNECT, MG_REPLY and MG_CLOSE events? And, last but not least, what is the correct way to forge and send the http request? I am trying by sending text with mg_send_data(), but I am not sure I am doing it right. An insight of how mg_poll_server() works with outgoing requests would also be helpful.

mongoose.h has some crude documentation on callback return values. I'll extend the API doc.

You can take a look at mg_connect() example code in unit_test.c, particularly, evh1() and evh2() functions.

mg_connect() creates a new connection, and adds it to the list of connections handled by a server. On each iteration of mg_poll_server(), all connections are multiplexed by select() syscall. All sockets are non-blocking, therefore connect() syscall that is done by mg_connect() just "schedules" the actual connection. select() will return the socket as ready on success or failure, and mongoose calls MG_CONNECT event, indicating success or failure through mg_connection::status_code. On success, usual mg_printf() could be used to send a request. When reply is received, mongoose sends MG_REPLY.

Hope that helps,
Sergey.

Fabrizio Dini

unread,
Mar 26, 2014, 10:58:52 AM3/26/14
to mongoos...@googlegroups.com
Hi Sergey,

thanks for your quick reply! By looking at evh1 and evh2 in unit_test.c it seems to me that the method I am following is 'almost' right. I'll give a deeper look to my code to find the differences, however, it could depend on some strange behavior of the web server on the other end of the connection. 

Thanks by now!

Fabrizio Dini

unread,
Mar 26, 2014, 11:06:59 AM3/26/14
to mongoos...@googlegroups.com
Hey! :D

I found the problem! Everything started to work fine after I changed mg_printf_data() with mg_printf()! :D But mg_printf() is undocumented... why? Did I miss something?

Anyway, it is working now... thanks!

Sergey Lyubka

unread,
Mar 26, 2014, 11:30:02 AM3/26/14
to mongoose-users
On Wed, Mar 26, 2014 at 3:06 PM, Fabrizio Dini <dini.f...@gmail.com> wrote:
Hey! :D

I found the problem! Everything started to work fine after I changed mg_printf_data() with mg_printf()! :D But mg_printf() is undocumented... why? Did I miss something?

I'll document mg_printf(), cause it is required for HTTP client.
Also, it makes sense to rename mg_printf_data() to mg_printf_chunked() to avoid future confusion.
Reply all
Reply to author
Forward
0 new messages