xref: /openbsd/usr.sbin/acme-client/http.h (revision 7b00f4e9)
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 
20 struct	source {
21 	int	 family; /* 4 (PF_INET) or 6 (PF_INET6) */
22 	char	*ip; /* IPV4 or IPV6 address */
23 };
24 
25 struct	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  */
33 typedef	ssize_t (*writefp)(const void *, size_t, const struct http *);
34 typedef	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  */
41 struct	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  */
52 struct	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 
64 int		 http_init(void);
65 
66 /* Convenience functions. */
67 struct httpget	*http_get(const struct source *, size_t,
68 			const char *, short, const char *, int,
69 			const void *, size_t);
70 void		 http_get_free(struct httpget *);
71 
72 /* Allocation and release. */
73 struct http	*http_alloc(const struct source *, size_t,
74 			const char *, short, const char *);
75 void		 http_free(struct http *);
76 struct httpxfer	*http_open(const struct http *, int, const void *, size_t);
77 void		 http_close(struct httpxfer *);
78 void		 http_disconnect(struct http *);
79 
80 /* Access. */
81 char		*http_head_read(const struct http *,
82 			struct httpxfer *, size_t *);
83 struct httphead	*http_head_parse(const struct http *,
84 			struct httpxfer *, size_t *);
85 char		*http_body_read(const struct http *,
86 			struct httpxfer *, size_t *);
87 int		 http_head_status(const struct http *,
88 			struct httphead *, size_t);
89 struct httphead	*http_head_get(const char *,
90 			struct httphead *, size_t);
91 
92 #endif /* HTTP_H */
93