summaryrefslogtreecommitdiffstats
path: root/man3/nxt_unit_request_read.3
blob: ba07b4744238da4aa6a82118f5cd62d80748204c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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