1*b8795492Sinoguchi /* $Id: http.h,v 1.3 2017/01/25 13:52:53 inoguchi Exp $ */ 2471c6e53Sbeck /* 3471c6e53Sbeck * Copyright (c) 2016 Kristaps Dzonsons <kristaps@bsd.lv> 4471c6e53Sbeck * 5471c6e53Sbeck * Permission to use, copy, modify, and distribute this software for any 6471c6e53Sbeck * purpose with or without fee is hereby granted, provided that the above 7471c6e53Sbeck * copyright notice and this permission notice appear in all copies. 8471c6e53Sbeck * 9471c6e53Sbeck * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10471c6e53Sbeck * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11471c6e53Sbeck * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12471c6e53Sbeck * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13471c6e53Sbeck * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14471c6e53Sbeck * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15471c6e53Sbeck * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16471c6e53Sbeck */ 17471c6e53Sbeck #ifndef HTTP_H 18471c6e53Sbeck #define HTTP_H 19471c6e53Sbeck 20471c6e53Sbeck struct source { 21471c6e53Sbeck int family; /* 4 (PF_INET) or 6 (PF_INET6) */ 22471c6e53Sbeck char *ip; /* IPV4 or IPV6 address */ 23471c6e53Sbeck }; 24471c6e53Sbeck 25471c6e53Sbeck struct http; 26471c6e53Sbeck 27471c6e53Sbeck /* 28471c6e53Sbeck * Write and read callbacks to allow HTTP and HTTPS. 29471c6e53Sbeck * Both of these return the number of bytes read (or written) or -1 on 30471c6e53Sbeck * failure. 31471c6e53Sbeck * 0 bytes read means that the connection has closed. 32471c6e53Sbeck */ 33471c6e53Sbeck typedef ssize_t (*writefp)(const void *, size_t, const struct http *); 34471c6e53Sbeck typedef ssize_t (*readfp)(char *, size_t, const struct http *); 35471c6e53Sbeck 36471c6e53Sbeck /* 37471c6e53Sbeck * HTTP/S header pair. 38471c6e53Sbeck * There's also a cooked-up pair, "Status", with the status code. 39fa0b7f9cSderaadt * Both strings are NUL-terminated. 40471c6e53Sbeck */ 41471c6e53Sbeck struct httphead { 42471c6e53Sbeck const char *key; 43471c6e53Sbeck const char *val; 44471c6e53Sbeck }; 45471c6e53Sbeck 46471c6e53Sbeck /* 47471c6e53Sbeck * Grab all information from a transfer. 48471c6e53Sbeck * DO NOT free any parts of this, and editing the parts (e.g., changing 49471c6e53Sbeck * the underlying strings) will persist; so in short, don't. 50471c6e53Sbeck * All of these values will be set upon http_get() success. 51471c6e53Sbeck */ 52471c6e53Sbeck struct httpget { 53471c6e53Sbeck struct httpxfer *xfer; /* underlying transfer */ 54471c6e53Sbeck struct http *http; /* underlying connection */ 55471c6e53Sbeck int code; /* return code */ 56471c6e53Sbeck struct httphead *head; /* headers */ 57471c6e53Sbeck size_t headsz; /* number of headers */ 58471c6e53Sbeck char *headpart; /* header buffer */ 59471c6e53Sbeck size_t headpartsz; /* size of headpart */ 60471c6e53Sbeck char *bodypart; /* body buffer */ 61471c6e53Sbeck size_t bodypartsz; /* size of bodypart */ 62471c6e53Sbeck }; 63471c6e53Sbeck 64471c6e53Sbeck int http_init(void); 65471c6e53Sbeck 66471c6e53Sbeck /* Convenience functions. */ 67471c6e53Sbeck struct httpget *http_get(const struct source *, size_t, 68471c6e53Sbeck const char *, short, const char *, 69471c6e53Sbeck const void *, size_t); 70471c6e53Sbeck void http_get_free(struct httpget *); 71471c6e53Sbeck 72471c6e53Sbeck /* Allocation and release. */ 73471c6e53Sbeck struct http *http_alloc(const struct source *, size_t, 74471c6e53Sbeck const char *, short, const char *); 75471c6e53Sbeck void http_free(struct http *); 76471c6e53Sbeck struct httpxfer *http_open(const struct http *, const void *, size_t); 77471c6e53Sbeck void http_close(struct httpxfer *); 78471c6e53Sbeck void http_disconnect(struct http *); 79471c6e53Sbeck 80471c6e53Sbeck /* Access. */ 81471c6e53Sbeck char *http_head_read(const struct http *, 82471c6e53Sbeck struct httpxfer *, size_t *); 83471c6e53Sbeck struct httphead *http_head_parse(const struct http *, 84471c6e53Sbeck struct httpxfer *, size_t *); 85471c6e53Sbeck char *http_body_read(const struct http *, 86471c6e53Sbeck struct httpxfer *, size_t *); 87471c6e53Sbeck int http_head_status(const struct http *, 88471c6e53Sbeck struct httphead *, size_t); 89471c6e53Sbeck struct httphead *http_head_get(const char *, 90471c6e53Sbeck struct httphead *, size_t); 91471c6e53Sbeck 92471c6e53Sbeck #endif /* HTTP_H */ 93