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