summaryrefslogtreecommitdiffstats
path: root/man3/nxt_unit_response_add_field.3
blob: b47e37c3f2b8a79f51919bdac488d81a8f30ff60 (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
.\" (C) 2023, NGINX, Inc.
.\"
.TH nxt_unit_response_add_field 3 (date) "NGINX Unit (unreleased)"
.SH Name
nxt_unit_response_add_field \- add field to HTTP response in Unit app
.SH Library
NGINX Unit library
.RI ( libunit ", " -lunit )
.SH Synopsis
.nf
.B #include <nxt_unit.h>
.PP
.BI "int nxt_unit_response_add_field(nxt_unit_request_info_t *" req ,
.BI "                                const char " name [. nlen "], uint8_t " nlen ,
.BI "                                const char " value [. vlen "], uint32_t " vlen );
.fi
.SH Arguments
.TP
.I req
Request object.
.TP
.I name
Name of the field.
It is a character sequence of length
.IR nlen .
.TP
.I value
Value of the field.
It is a character sequence of length
.IR vlen .
.SH Description
.MR nxt_unit_response_add_field 3
adds a field to the header of an HTTP response.
.PP
The response should have been initialized previously with
.MR nxt_unit_response_init 3 .
.PP
If the
.B \%Content\-Length
header field is not sent,
Unit will send a
.B \%Transfer-Encoding:\ chunked
response,
and will produce the header if necessary.
.SH Return value
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
Response not initialized.
.IP \[bu]
Too many response fields.
.IP \[bu]
Field is longer than the response buffer.
.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
Content\-Length: 42
Server: Unit/1.30.0
Date: Sat, 22 May 2023 20:45:50 GMT
\&
Hello world!
.EE
.SS C code
.EX
#include <string.h>
\&
#include <nxt_unit.h>
\&
void
request_handler(nxt_unit_request_info_t *req)
{
    int  rc;
\&
    rc = nxt_unit_response_init(req, 200, 2, strlen("Content\-Type")
                                             + strlen("text/plain")
                                             + strlen("Content\-Length")
                                             + strlen("42")
                                             + 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_field(req, "Content\-Length",
                                          strlen("Content\-Length"),
                                          "42",
                                          strlen("42"));
    if (rc != 0) {
        goto fail;
    }
\&
    rc = nxt_unit_response_add_content(req, "Hello", strlen("Hello"));
    if (rc != 0) {
        goto fail;
    }
    rc = nxt_unit_response_add_content(req, " world!\en",
                                            strlen(" world!\en"));
    if (rc != 0) {
        goto fail;
    }
\&
    rc = nxt_unit_response_send(req);
\&
    nxt_unit_req_debug(req, "Response sent.");
\&
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_add_content 3 ,
.MR nxt_unit_response_send 3 ,
.MR nxt_unit_request_done 3 ,
.MR unitd 8
.PP
.UR https://www.rfc-editor.org/\:rfc/\:rfc9112\:#section-5
RFC 9112, section 5: Field Syntax
.UE
.PP
Website
.UR https://unit.nginx.org
.UE
.PP
Mailing list
.UR https://mailman.nginx.org/\:mailman/\:listinfo/\:unit
.UE
.PP
GitHub
.UR https://github.com/\:nginx/\:unit
.UE