1/*	$Id: http.h,v 1.8 2019/06/07 08:07:52 florian Exp $ */
2/*
3 * Copyright (c) 2016 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#ifndef HTTP_H
18#define HTTP_H
19
20struct	source {
21	int	 family; /* 4 (PF_INET) or 6 (PF_INET6) */
22	char	*ip; /* IPV4 or IPV6 address */
23};
24
25struct	http;
26
27/*
28 * Write and read callbacks to allow HTTP and HTTPS.
29 * Both of these return the number of bytes read (or written) or -1 on
30 * failure.
31 * 0 bytes read means that the connection has closed.
32 */
33typedef	ssize_t (*writefp)(const void *, size_t, const struct http *);
34typedef	ssize_t (*readfp)(char *, size_t, const struct http *);
35
36/*
37 * HTTP/S header pair.
38 * There's also a cooked-up pair, "Status", with the status code.
39 * Both strings are NUL-terminated.
40 */
41struct	httphead {
42	const char	*key;
43	const char	*val;
44};
45
46/*
47 * Grab all information from a transfer.
48 * DO NOT free any parts of this, and editing the parts (e.g., changing
49 * the underlying strings) will persist; so in short, don't.
50 * All of these values will be set upon http_get() success.
51 */
52struct	httpget {
53	struct httpxfer	*xfer; /* underlying transfer */
54	struct http	*http; /* underlying connection */
55	int		 code; /* return code */
56	struct httphead	*head; /* headers */
57	size_t		 headsz; /* number of headers */
58	char		*headpart; /* header buffer */
59	size_t		 headpartsz; /* size of headpart */
60	char		*bodypart; /* body buffer */
61	size_t		 bodypartsz; /* size of bodypart */
62};
63
64int		 http_init(void);
65
66/* Convenience functions. */
67struct httpget	*http_get(const struct source *, size_t,
68			const char *, short, const char *, int,
69			const void *, size_t);
70void		 http_get_free(struct httpget *);
71
72/* Allocation and release. */
73struct http	*http_alloc(const struct source *, size_t,
74			const char *, short, const char *);
75void		 http_free(struct http *);
76struct httpxfer	*http_open(const struct http *, int, const void *, size_t);
77void		 http_close(struct httpxfer *);
78void		 http_disconnect(struct http *);
79
80/* Access. */
81char		*http_head_read(const struct http *,
82			struct httpxfer *, size_t *);
83struct httphead	*http_head_parse(const struct http *,
84			struct httpxfer *, size_t *);
85char		*http_body_read(const struct http *,
86			struct httpxfer *, size_t *);
87int		 http_head_status(const struct http *,
88			struct httphead *, size_t);
89struct httphead	*http_head_get(const char *,
90			struct httphead *, size_t);
91
92#endif /* HTTP_H */
93