diff options
Diffstat (limited to 'man3/nxt_unit_request_read.3')
-rw-r--r-- | man3/nxt_unit_request_read.3 | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/man3/nxt_unit_request_read.3 b/man3/nxt_unit_request_read.3 new file mode 100644 index 000000000..ba07b4744 --- /dev/null +++ b/man3/nxt_unit_request_read.3 @@ -0,0 +1,142 @@ +.\" (C) 2023, NGINX, Inc. +.\" +.TH nxt_unit_request_read 3 (date) "NGINX Unit (unreleased)" +.SH Name +nxt_unit_request_read +\- +read content of a response in Unit app +.SH Library +NGINX Unit library +.RI ( libunit ", " -lunit ) +.SH Synopsis +.nf +.B #include <nxt_unit.h> +.PP +.BI "ssize_t nxt_unit_request_read(nxt_unit_request_info_t *" req , +.BI " void " dst [. size "], size_t " size ); +.fi +.SH Arguments +.TP +.I req +Request object. +.TP +.I dst +Buffer where the request message body will be written. +It's a buffer of +.I size +bytes. +.SH Description +.MR nxt_unit_request_read 3 +copies the client request message body into a buffer. +.PP +This function does not read +the header fields nor the request line, +which are available in +.IR req\->request . +.SH Return value +The number of bytes read on success, +or \-1 on error. +.SH Errors +Errors will be reported in the Unit debug log. +.TP +.B NXT_UNIT_ERROR +.RS +.PD 0 +.IP \[bu] 3 +Failed to read content. +.PD +.RE +.SH Examples +Below is a request and response pair, +and the source code to produce it. +It implements an echo web application. +.PP +See +.MR nxt_unit_init 3 +for an example where the +.IR request_handler () +function defined below is used. +.SS Request +.EX +.RB "$ " "echo \-e \[aq]foo\enbar\[aq] | curl \-\-data\-binary @\- localhost;" +foo +bar +.EE +.SS C code +.EX +#define _GNU_SOURCE +#include <string.h> +\& +#include <nxt_unit.h> +\& +void +request_handler(nxt_unit_request_info_t *req) +{ + int rc; + ssize_t res; + nxt_unit_buf_t *buf; +\& + rc = nxt_unit_response_init(req, 200, 1, strlen("Content\-Type") + + strlen("text/plain")); + if (rc != 0) { + goto fail; + } +\& + rc = nxt_unit_response_add_field(req, "Content\-Type", + strlen("Content\-Type"), + "text/plain", + strlen("text/plain")); + if (rc != 0) { + goto fail; + } +\& + rc = nxt_unit_response_send(req); + if (rc != 0) { + goto fail; + } +\& + while (req\->content_length) { + buf = nxt_unit_response_buf_alloc(req, req\->content_length); + if (buf == NULL) { + rc = NXT_UNIT_ERROR; + goto fail; + } +\& + res = nxt_unit_request_read(req, buf\->free, + buf\->end \- buf\->free); + if (res == \-1) { + goto fail; + } +\& + buf\->free += res; +\& + rc = nxt_unit_buf_send(buf); + if (rc != 0) { + goto fail; + } + } +\& +fail: + nxt_unit_request_done(req, rc); +} +.EE +.SH Copyright +(C) 2017-2023, NGINX, Inc. +.PP +SPDX-License-Identifier: Apache-2.0 +.SH See also +.MR nxt_unit_init 3 , +.MR nxt_unit_response_init 3 , +.MR unitd 8 +.PP +.UR https://unit.nginx.org +Website +.UE +.PP +.UR https://mailman.nginx.org/mailman/listinfo/unit +Mailing list +.UE +.PP +.UR https://github.com/nginx/unit +GitHub +.UE |