xref: /openbsd/usr.sbin/acme-client/http.h (revision 82b65d8a)
1 /*	$Id: http.h,v 1.6 2018/08/08 17:47:44 deraadt 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 #define DEFAULT_CA_FILE "/etc/ssl/cert.pem"
65 int		 http_init(void);
66 
67 /* Convenience functions. */
68 struct httpget	*http_get(const struct source *, size_t,
69 			const char *, short, const char *,
70 			const void *, size_t);
71 void		 http_get_free(struct httpget *);
72 
73 /* Allocation and release. */
74 struct http	*http_alloc(const struct source *, size_t,
75 			const char *, short, const char *);
76 void		 http_free(struct http *);
77 struct httpxfer	*http_open(const struct http *, const void *, size_t);
78 void		 http_close(struct httpxfer *);
79 void		 http_disconnect(struct http *);
80 
81 /* Access. */
82 char		*http_head_read(const struct http *,
83 			struct httpxfer *, size_t *);
84 struct httphead	*http_head_parse(const struct http *,
85 			struct httpxfer *, size_t *);
86 char		*http_body_read(const struct http *,
87 			struct httpxfer *, size_t *);
88 int		 http_head_status(const struct http *,
89 			struct httphead *, size_t);
90 struct httphead	*http_head_get(const char *,
91 			struct httphead *, size_t);
92 
93 #endif /* HTTP_H */
94