VLC 4.0.0-dev
Loading...
Searching...
No Matches
message.h
Go to the documentation of this file.
1/*****************************************************************************
2 * message.h: HTTP request/response
3 *****************************************************************************
4 * Copyright (C) 2015 RĂ©mi Denis-Courmont
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
20
21#include <stdint.h>
22
23/**
24 * \defgroup http_msg Messages
25 * HTTP messages, header formatting and parsing
26 * \ingroup http
27 * @{
28 * \file message.h
29 */
30
31struct vlc_http_msg;
33
34/**
35 * Creates an HTTP request.
36 *
37 * Allocates an HTTP request message.
38 *
39 * @param method request method (e.g. "GET")
40 * @param scheme protocol scheme (e.g. "https")
41 * @param authority target host (e.g. "www.example.com:8080")
42 * @param path request path (e.g. "/dir/page.html")
43 * @return an HTTP stream or NULL on allocation failure
44 */
45struct vlc_http_msg *
46vlc_http_req_create(const char *method, const char *scheme,
47 const char *authority, const char *path) VLC_USED;
48
49/**
50 * Creates an HTTP response.
51 *
52 * Allocates an HTTP response message.
53 *
54 * @param status HTTP status code
55 * @return an HTTP stream or NULL on allocation failure
56 */
58
59/**
60 * Destroys an HTTP message.
61 */
63
64/**
65 * Formats a header field.
66 *
67 * Adds an HTTP message header to an HTTP request or response.
68 * All headers must be formatted before the message is sent.
69 *
70 * @param name header field name
71 * @param fmt printf-style format string
72 * @return 0 on success, -1 on error (out of memory)
73 */
74int vlc_http_msg_add_header(struct vlc_http_msg *, const char *name,
75 const char *fmt, ...) VLC_FORMAT(3,4);
76
77/**
78 * Formats an authority.
79 *
80 * @param host host name (cannot be NULL)
81 * @param port port number (0 for unspecified)
82 * @return the formatted authority as a heap-allocated nul-terminated string,
83 * or NULL on allocation failure
84 */
85char *vlc_http_authority(const char *host, unsigned port);
86
87/**
88 * Sets the agent field.
89 *
90 * Sets the User-Agent or Server header field.
91 */
92int vlc_http_msg_add_agent(struct vlc_http_msg *, const char *);
93
94/**
95 * Gets the agent field.
96 *
97 * Gets the User-Agent or Server header field.
98 */
99const char *vlc_http_msg_get_agent(const struct vlc_http_msg *);
100
101/**
102 * Parses a timestamp header field.
103 *
104w* @param msg the response to get the timestamp header from
105 * @param name header field name
106 * @return a timestamp value, or -1 on error.
107 */
108time_t vlc_http_msg_get_time(const struct vlc_http_msg *msg, const char *name);
109
110/**
111 * Adds a timestamp header field.
112 *
113 * @param msg the request to add the header to
114 * @param name header field name
115 * @param t pointer to timestamp
116 * @return 0 on success, -1 on error (errno is set accordingly)
117 */
118int vlc_http_msg_add_time(struct vlc_http_msg *msg, const char *name,
119 const time_t *t);
120
121/**
122 * Adds a Date header field.
123 */
125
126/**
127 * Gets message date.
128 *
129 * Extracts the original date of the message from the HTTP Date header.
130 *
131 * @return a time value on success, -1 on error.
132 */
133time_t vlc_http_msg_get_atime(const struct vlc_http_msg *);
134
135/**
136 * Gets resource date.
137 *
138 * Extracts the last modification date of the message content from the HTTP
139 * Last-Modified header.
140 *
141 * @return a time value on success, -1 on error.
142 */
143time_t vlc_http_msg_get_mtime(const struct vlc_http_msg *);
144
145/**
146 * Gets retry timeout.
147 *
148 * Extracts the time (in seconds) until the expiration of the "retry-after"
149 * time-out in the HTTP message. If the header value is an absolute date, it
150 * is converted relative to the current time.
151 *
152 * @return the time in seconds, zero if the date is overdue or on error.
153 */
154unsigned vlc_http_msg_get_retry_after(const struct vlc_http_msg *);
155
156void vlc_http_msg_get_cookies(const struct vlc_http_msg *,
157 struct vlc_http_cookie_jar_t *,
158 const char *host, const char *path);
160 struct vlc_http_cookie_jar_t *);
161
162char *vlc_http_msg_get_basic_realm(const struct vlc_http_msg *);
163
164/**
165 * Adds Basic credentials.
166 *
167 * Formats a plain username and password pair using HTTP Basic (RFC7617)
168 * syntax.
169 *
170 * @param msg the request to add the credentials to
171 * @param proxy true for proxy authentication,
172 * false for origin server authentication
173 * @param username null-terminated username
174 * @param password null-terminated password
175 * @return 0 on success, -1 on out-of-memory (ENOMEM) or if username or
176 * password are invalid (EINVAL).
177 */
178int vlc_http_msg_add_creds_basic(struct vlc_http_msg *msg, bool proxy,
179 const char *username, const char *password);
180
181
182/**
183 * Looks up an header field.
184 *
185 * Finds an HTTP header field by (case-insensitive) name inside an HTTP
186 * message header. If the message has more than one matching field, their value
187 * are folded (as permitted by protocol specifications).
188 *
189 * @return header field value (valid until message is destroyed),
190 * or NULL if no fields matched
191 */
192const char *vlc_http_msg_get_header(const struct vlc_http_msg *,
193 const char *name);
194
195/**
196 * Gets response status code.
197 *
198 * @return status code (e.g. 404), or negative if request
199 */
200int vlc_http_msg_get_status(const struct vlc_http_msg *m);
201
202/**
203 * Gets request method.
204 *
205 * @return request method (e.g. "GET"), or NULL if response
206 */
207const char *vlc_http_msg_get_method(const struct vlc_http_msg *);
208
209/**
210 * Gets request scheme.
211 *
212 * @return request scheme (e.g. "https"), or NULL if absent
213 */
214const char *vlc_http_msg_get_scheme(const struct vlc_http_msg *);
215
216/**
217 * Gets request authority.
218 *
219 * @return request authority (e.g. "www.example.com:8080"),
220 * or NULL if response
221 */
222const char *vlc_http_msg_get_authority(const struct vlc_http_msg *);
223
224/**
225 * Gets request absolute path.
226 *
227 * @return request absolute path (e.g. "/index.html"), or NULL if absent
228 */
229const char *vlc_http_msg_get_path(const struct vlc_http_msg *);
230
231/**
232 * Looks up a token in a header field.
233 *
234 * Finds the first occurrence of a token within a HTTP field header.
235 *
236 * @param msg the request to get the token from
237 * @param field HTTP header field name
238 * @param token HTTP token name
239 * @return the first byte of the token if found, NULL if not found.
240 */
241const char *vlc_http_msg_get_token(const struct vlc_http_msg *msg,
242 const char *field, const char *token);
243
244/**
245 * Finds next token.
246 *
247 * Finds the following token in a HTTP header field value.
248 *
249 * @return First character of the following token,
250 * or NULL if there are no further tokens
251 */
252const char *vlc_http_next_token(const char *);
253
254/**
255 * Gets HTTP payload length.
256 *
257 * Determines the total length (in bytes) of the payload associated with the
258 * HTTP message.
259 *
260 * @return byte length, or (uintmax_t)-1 if unknown.
261 */
262uintmax_t vlc_http_msg_get_size(const struct vlc_http_msg *);
263
264/**
265 * Gets next response headers.
266 *
267 * Discards the current response headers and gets the next set of response
268 * headers for the same request. This is intended for HTTP 1xx continuation
269 * responses and for message trailers.
270 *
271 * @param m current response headers (destroyed by the call)
272 *
273 * @return next response headers or NULL on error
274 */
276
277/**
278 * Gets final response headers.
279 *
280 * Skips HTTP 1xx continue headers until a final set of response headers is
281 * received. This is a convenience wrapper around vlc_http_msg_iterate() for
282 * use when continuation headers are not useful (e.g. GET or CONNECT).
283 *
284 * @param m current response headers or NULL
285 *
286 * @return the final response headers (m if it was already final),
287 * NULL if the parameter was NULL, or NULL on error
288 */
290
291/**
292 * Receives HTTP data.
293 *
294 * Dequeues the next block of data from an HTTP message. If no pending data has
295 * been received, waits until data is received, the stream ends or the
296 * underlying connection fails.
297 *
298 * @return data block
299 * @retval NULL on end-of-stream
300 * @retval vlc_http_error on fatal error
301 */
303
304/**
305 * Sends HTTP data.
306 *
307 * Queues the next block of data for an HTTP message payload.
308 *
309 * @note This function takes ownership of the passed data block(s).
310 *
311 * @param msg the request to append the data block to
312 * @param b chain of block of data to be sent
313 * @param eos true to indicate the end of the payload
314 * @retval 0 success
315 * @retval -1 fatal error
316 */
317int vlc_http_msg_write(struct vlc_http_msg *msg, block_t *b, bool eos);
318
319/** @} */
320
321/**
322 * \defgroup http_stream Streams
323 * \ingroup http_connmgr
324 *
325 * HTTP request/response streams
326 *
327 * A stream is initiated by a client-side request header. It includes a
328 * final response header, possibly preceded by one or more continuation
329 * response headers. After the response header, a stream usually carries
330 * a response payload.
331 *
332 * A stream may also carry a request payload (this is not supported so far).
333 *
334 * The HTTP stream constitutes the interface between an HTTP connection and
335 * the higher-level HTTP messages layer.
336 * @{
337 */
338
339struct vlc_http_stream;
340
341/**
342 * Error pointer value
343 *
344 * This is an error value for some HTTP functions that can return NULL in
345 * non-error circumstances. Another return value is necessary to express
346 * error/failure, which this is.
347 * This compares different to NULL and to any valid pointer.
348 *
349 * @warning Dereferencing this pointer is undefined.
350 */
351extern void *const vlc_http_error;
352
353void vlc_http_msg_attach(struct vlc_http_msg *m, struct vlc_http_stream *s);
356
357/** HTTP stream callbacks
358 *
359 * Connection-specific callbacks for stream manipulation
360 */
362{
363 struct vlc_http_msg *(*read_headers)(struct vlc_http_stream *);
364 ssize_t (*write)(struct vlc_http_stream *, const void *, size_t, bool eos);
365 block_t *(*read)(struct vlc_http_stream *);
366 void (*close)(struct vlc_http_stream *, bool abort);
367};
368
369/** HTTP stream */
371{
373};
374
375/**
376 * Reads one message header.
377 *
378 * Reads the next message header of an HTTP stream from the network.
379 * There is always exactly one request header per stream. There is usually
380 * one response header per stream, except for continuation (1xx) headers.
381 *
382 * @warning The caller is responsible for reading headers at appropriate
383 * times as intended by the protocol. Failure to do so may result in protocol
384 * dead lock, and/or (HTTP 1.x) connection failure.
385 *
386 * @param s HTTP stream to read from
387 *
388 */
389static inline
391{
392 return s->cbs->read_headers(s);
393}
394
395/**
396 * Write message payload data.
397 *
398 * Writes data as message payload of an HTTP stream.
399 *
400 * @todo Take a block structure rather than a byte array.
401 *
402 * @param s HTTP stream to write to
403 * @param base start address of data to write
404 * @param length length in bytes of data to write
405 * @param eos whether this is the last write on the stream
406 * @retval len success
407 * @retval -1 error
408 */
409static inline ssize_t vlc_http_stream_write(struct vlc_http_stream *s,
410 const void *base, size_t length,
411 bool eos)
412{
413 return s->cbs->write(s, base, length, eos);
414}
415
416/**
417 * Reads message payload data.
418 *
419 * Reads the next block of data from the message payload of an HTTP stream.
420 *
421 * @param s HTTP stream to read from
422 * @return a block of data (use block_Release() to free it)
423 * @retval NULL The end of the stream was reached.
424 * @retval vlc_http_error The stream encountered a fatal error.
425 */
427{
428 return s->cbs->read(s);
429}
430
431/**
432 * Closes an HTTP stream.
433 *
434 * Releases all resources associated or held by an HTTP stream. Any unread
435 * header or data is discarded.
436 *
437 * @param s HTTP stream to close
438 * @param abort whether to close the connection and prevent re-use
439 */
440static inline void vlc_http_stream_close(struct vlc_http_stream *s, bool abort)
441{
442 s->cbs->close(s, abort);
443}
444
445/** @} */
446
447/**
448 * Formats an HTTP 1.1 message header.
449 *
450 * Formats an message header in HTTP 1.x format, using HTTP version 1.1.
451 *
452 * @param m message to format/serialize
453 * @param lenp location to write the length of the formatted message in bytes
454 * [OUT]
455 * @param proxied whether the message is meant for sending to a proxy rather
456 * than an origin (only relevant for requests)
457 * @param chunked whether to append a chunked transfer encoding header line
458 * @return A heap-allocated nul-terminated string or *lenp bytes,
459 * or NULL on error
460 */
461char *vlc_http_msg_format(const struct vlc_http_msg *m, size_t *restrict lenp,
462 bool proxied, bool chunked) VLC_USED VLC_MALLOC;
463
464/**
465 * Parses an HTTP 1.1 message header.
466 */
467struct vlc_http_msg *vlc_http_msg_headers(const char *msg) VLC_USED;
468
469struct vlc_h2_frame;
470
471/**
472 * Formats an HTTP 2.0 HEADER frame.
473 */
474struct vlc_h2_frame *vlc_http_msg_h2_frame(const struct vlc_http_msg *m,
475 uint_fast32_t stream_id, bool eos);
476
477/**
478 * Parses an HTTP 2.0 header table.
479 */
481 const char *const headers[][2]);
size_t count
Definition core.c:403
#define VLC_USED
Definition fourcc_gen.c:32
#define VLC_MALLOC
Definition vlc_common.h:157
#define VLC_FORMAT(x, y)
String format function annotation.
Definition vlc_common.h:193
const char * vlc_http_msg_get_header(const struct vlc_http_msg *, const char *name)
Looks up an header field.
Definition message.c:156
void vlc_http_msg_get_cookies(const struct vlc_http_msg *, struct vlc_http_cookie_jar_t *, const char *host, const char *path)
Definition message.c:934
struct vlc_http_msg * vlc_http_msg_get_final(struct vlc_http_msg *) VLC_USED
Gets final response headers.
Definition message.c:284
int vlc_http_msg_add_time(struct vlc_http_msg *msg, const char *name, const time_t *t)
Adds a timestamp header field.
Definition message.c:811
time_t vlc_http_msg_get_atime(const struct vlc_http_msg *)
Gets message date.
Definition message.c:872
int vlc_http_msg_add_header(struct vlc_http_msg *, const char *name, const char *fmt,...) VLC_FORMAT(3
Formats a header field.
int char * vlc_http_authority(const char *host, unsigned port)
Formats an authority.
Definition message.c:573
const char * vlc_http_msg_get_method(const struct vlc_http_msg *)
Gets request method.
Definition message.c:173
block_t * vlc_http_msg_read(struct vlc_http_msg *) VLC_USED
Receives HTTP data.
Definition message.c:291
void vlc_http_msg_destroy(struct vlc_http_msg *)
Destroys an HTTP message.
Definition message.c:193
const char * vlc_http_msg_get_scheme(const struct vlc_http_msg *)
Gets request scheme.
Definition message.c:178
int vlc_http_msg_add_agent(struct vlc_http_msg *, const char *)
Sets the agent field.
Definition message.c:783
time_t vlc_http_msg_get_mtime(const struct vlc_http_msg *)
Gets resource date.
Definition message.c:877
const char * vlc_http_msg_get_agent(const struct vlc_http_msg *)
Gets the agent field.
Definition message.c:795
const char * vlc_http_msg_get_authority(const struct vlc_http_msg *)
Gets request authority.
Definition message.c:183
const char * vlc_http_msg_get_path(const struct vlc_http_msg *)
Gets request absolute path.
Definition message.c:188
const char * vlc_http_msg_get_token(const struct vlc_http_msg *msg, const char *field, const char *token)
Looks up a token in a header field.
Definition message.c:709
int vlc_http_msg_write(struct vlc_http_msg *msg, block_t *b, bool eos)
Sends HTTP data.
Definition message.c:299
uintmax_t vlc_http_msg_get_size(const struct vlc_http_msg *)
Gets HTTP payload length.
Definition message.c:905
const char * vlc_http_next_token(const char *)
Finds next token.
Definition message.c:642
unsigned vlc_http_msg_get_retry_after(const struct vlc_http_msg *)
Gets retry timeout.
Definition message.c:882
struct vlc_http_msg * vlc_http_msg_iterate(struct vlc_http_msg *) VLC_USED
Gets next response headers.
Definition message.c:265
int vlc_http_msg_add_creds_basic(struct vlc_http_msg *msg, bool proxy, const char *username, const char *password)
Adds Basic credentials.
Definition message.c:1005
int vlc_http_msg_add_atime(struct vlc_http_msg *)
Adds a Date header field.
Definition message.c:826
int vlc_http_msg_add_cookies(struct vlc_http_msg *, struct vlc_http_cookie_jar_t *)
Definition message.c:946
int vlc_http_msg_get_status(const struct vlc_http_msg *m)
Gets response status code.
Definition message.c:168
time_t vlc_http_msg_get_time(const struct vlc_http_msg *msg, const char *name)
Parses a timestamp header field.
Definition message.c:864
char * vlc_http_msg_get_basic_realm(const struct vlc_http_msg *)
Definition message.c:987
struct vlc_http_msg * vlc_http_req_create(const char *method, const char *scheme, const char *authority, const char *path) VLC_USED
Creates an HTTP request.
Definition message.c:213
struct vlc_http_msg * vlc_http_resp_create(unsigned status) VLC_USED
Creates an HTTP response.
Definition message.c:241
void vlc_http_msg_attach(struct vlc_http_msg *m, struct vlc_http_stream *s)
Definition message.c:259
static void vlc_http_stream_close(struct vlc_http_stream *s, bool abort)
Closes an HTTP stream.
Definition message.h:440
static ssize_t vlc_http_stream_write(struct vlc_http_stream *s, const void *base, size_t length, bool eos)
Write message payload data.
Definition message.h:409
void *const vlc_http_error
Error pointer value.
Definition message.c:57
static block_t * vlc_http_stream_read(struct vlc_http_stream *s)
Reads message payload data.
Definition message.h:426
struct vlc_http_msg * vlc_http_msg_get_initial(struct vlc_http_stream *s) VLC_USED
Definition message.c:276
static struct vlc_http_msg * vlc_http_stream_read_headers(struct vlc_http_stream *s)
Reads one message header.
Definition message.h:390
const char name[16]
Definition httpd.c:1298
struct vlc_http_msg * vlc_http_msg_headers(const char *msg) VLC_USED
Parses an HTTP 1.1 message header.
Definition message.c:366
struct vlc_http_msg * vlc_http_msg_h2_headers(unsigned count, const char *const headers[][2])
Parses an HTTP 2.0 header table.
Definition message.c:487
char * vlc_http_msg_format(const struct vlc_http_msg *m, size_t *restrict lenp, bool proxied, bool chunked) VLC_USED VLC_MALLOC
Formats an HTTP 1.1 message header.
Definition message.c:331
struct vlc_h2_frame * vlc_http_msg_h2_frame(const struct vlc_http_msg *m, uint_fast32_t stream_id, bool eos)
Formats an HTTP 2.0 HEADER frame.
Definition message.c:425
Definition vlc_frame.h:123
Definition h2frame.h:31
Definition message.c:43
char * path
Definition message.c:48
char * scheme
Definition message.c:46
char *(* headers)[2]
Definition message.c:49
short status
Definition message.c:44
char * method
Definition message.c:45
char * authority
Definition message.c:47
HTTP stream callbacks.
Definition message.h:362
struct vlc_http_msg *(* read_headers)(struct vlc_http_stream *)
Definition message.h:363
block_t *(* read)(struct vlc_http_stream *)
Definition message.h:365
ssize_t(* write)(struct vlc_http_stream *, const void *, size_t, bool eos)
Definition message.h:364
void(* close)(struct vlc_http_stream *, bool abort)
Definition message.h:366
HTTP stream.
Definition message.h:371
const struct vlc_http_stream_cbs * cbs
Definition message.h:372