[Libwebsockets] Send http response after parsing request body

Andy Green andy at warmcat.com
Wed Mar 13 23:28:31 CET 2019



On March 14, 2019 4:28:22 AM GMT+08:00, Kun Zhao <kzhdev at gmail.com> wrote:
>Hi Andy,
>
>I need to response a POST after parsing the request body. Can I call
>lws_add_http_common_headers and lws_finalize_write_http_header in
>LWS_CALLBACK_HTTP_BODY_COMPLETION? In this case, should I call
>lws_callback_on_writable(wsi) in LWS_CALLBACK_HTTP or in
>LWS_CALLBACK_HTTP_BODY_COMPLETION? In your minimal samples, you simply
>redirect the request to another page. Could you add an example to show
>how
>to properly send a dynamic response after parsing the request body?

Take a look at the demo post protocol plugin, it shows how to do all that.

https://libwebsockets.org/git/libwebsockets/tree/plugins/protocol_post_demo.c

The minimal examples try to be minimal so it's not hard to see how to do whatever the main point is.  But historically the demos were in big monolithic apps... these got mostly split out into clean protocol plugins a while back... so there are more examples to be found in /plugins.  These plugins are used in lwsws on https://libwebsockets.org/testserver and built into the now much smaller libwebsockets-test-server

https://libwebsockets.org/git/libwebsockets/tree/test-apps/test-server.c

>in addition, when returning the http_callack function, what is the
>meaning
>of return 0, 1, and -1?

Unless the specific callback interprets it differently, 0 indicates no problem (unhandled callbacks should return 0), and nonzero means something bad happened and the stream (h2) or connection (h1) must be closed.

-Andy

>Thanks,
>Kun


More information about the Libwebsockets mailing list