summaryrefslogtreecommitdiffstats
path: root/man3/nxt_unit_response_buf_alloc.3
blob: 2a429b270bebd71ec0a505eded678a2acac91687 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
.\" (C) 2023, NGINX, Inc.
.\"
.TH nxt_unit_response_buf_alloc 3 (date) "NGINX Unit (unreleased)"
.SH Name
nxt_unit_response_buf_alloc,
nxt_unit_buf_send
\-
allocate buffer for response in Unit app
.SH Library
NGINX Unit library
.RI ( libunit ", " -lunit )
.SH Synopsis
.nf
.B #include <nxt_unit.h>
.PP
.B [[gnu::malloc(nxt_unit_buf_send)]]
.B nxt_unit_buf_t *_Nullable
.BI "nxt_unit_response_buf_alloc(nxt_unit_request_info_t *" req ", uint32_t " size );
.PP
.BI "int nxt_unit_buf_send(nxt_unit_buf_t *" buf );
.fi
.SH Arguments
.TP
.I req
Request object.
.TP
.I size
Size of the buffer.
.TP
.I buf
Buffer.
.SH Description
.MR nxt_unit_response_buf_alloc 3
allocates a buffer for a response to a request,
or for a chunk of it.
.PP
.MR nxt_unit_buf_send 3
sends and deallocates the buffer previously allocated with
.MR nxt_unit_response_buf_alloc 3 .
.SH Return value
.TP
.MR nxt_unit_response_buf_alloc 3
A pointer to the allocated buffer on success,
or NULL on error.
.TP
.MR nxt_unit_buf_send 3
0 on success,
or a non-zero error code on error.
.SH Errors
Errors will be reported in the Unit debug log.
.TP
.B NXT_UNIT_ERROR
.RS
.PD 0
.IP \[bu] 3
.I size
was too big.
.IP \[bu]
Allocation failed.
.IP \[bu]
Failed to share the buffer with
.MR unitd 8 .
.PD
.RE
.SH Examples
Below is a request and response pair,
and the source code to produce it.
.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]GET / HTTP/1.1\er\enHost: _\en\[aq] | ncat localhost 80;"
HTTP/1.1 200 OK
Content\-Type: text/plain
Server: Unit/1.30.0
Date: Sat, 20 May 2023 19:08:22 GMT
Transfer\-Encoding: chunked
\&
22
Hello world!
But send this first.
\&
15
Some extra contents.
\&
e
And some more
\&
0
\&
.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;
    nxt_unit_buf_t  *buf, *buf2;
\&
    rc = nxt_unit_response_init(req, 200, 1, strlen("Content\-Type")
                                             + strlen("text/plain")
                                             + strlen("Hello world!\en"));
    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_add_content(req, "Hello world!\en",
                                            strlen("Hello world!\en"));
    if (rc != 0) {
        goto fail;
    }
\&
    rc = nxt_unit_response_send(req);
    if (rc != 0) {
        goto fail;
    }
\&
    buf = nxt_unit_response_buf_alloc(req,
                                      strlen("Some extra contents.\en"));
    if (buf == NULL) {
        rc = NXT_UNIT_ERROR;
        goto fail;
    }
\&
    buf2 = nxt_unit_response_buf_alloc(req,
                                       strlen("But send this first.\en"));
    if (buf2 == NULL) {
        rc = NXT_UNIT_ERROR;
        goto fail;
    }
\&
    buf2\->free = mempcpy(buf2\->free, "But send this first.\en",
                                     strlen("But send this first.\en"));
    buf\->free  = mempcpy(buf\->free, "Some extra contents.\en",
                                    strlen("Some extra contents.\en"));
\&
    rc = nxt_unit_buf_send(buf2);
    if (rc != 0) {
        goto fail;
    }
\&
    rc = nxt_unit_buf_send(buf);
    if (rc != 0) {
        goto fail;
    }
\&
    rc = nxt_unit_response_write(req, "And some more\en",
                                      strlen("And some more\en"));
    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 nxt_unit_response_write 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