1e5b75505Sopenharmony_ci/* 2e5b75505Sopenharmony_ci * httpread - Manage reading file(s) from HTTP/TCP socket 3e5b75505Sopenharmony_ci * Author: Ted Merrill 4e5b75505Sopenharmony_ci * Copyright 2008 Atheros Communications 5e5b75505Sopenharmony_ci * 6e5b75505Sopenharmony_ci * This software may be distributed under the terms of the BSD license. 7e5b75505Sopenharmony_ci * See README for more details. 8e5b75505Sopenharmony_ci */ 9e5b75505Sopenharmony_ci 10e5b75505Sopenharmony_ci#ifndef HTTPREAD_H 11e5b75505Sopenharmony_ci#define HTTPREAD_H 12e5b75505Sopenharmony_ci 13e5b75505Sopenharmony_ci/* event types (passed to callback) */ 14e5b75505Sopenharmony_cienum httpread_event { 15e5b75505Sopenharmony_ci HTTPREAD_EVENT_FILE_READY = 1, /* including reply ready */ 16e5b75505Sopenharmony_ci HTTPREAD_EVENT_TIMEOUT = 2, 17e5b75505Sopenharmony_ci HTTPREAD_EVENT_ERROR = 3 /* misc. error, esp malloc error */ 18e5b75505Sopenharmony_ci}; 19e5b75505Sopenharmony_ci 20e5b75505Sopenharmony_ci 21e5b75505Sopenharmony_ci/* header type detected 22e5b75505Sopenharmony_ci * available to callback via call to httpread_reply_code_get() 23e5b75505Sopenharmony_ci */ 24e5b75505Sopenharmony_cienum httpread_hdr_type { 25e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_UNKNOWN = 0, /* none of the following */ 26e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_REPLY = 1, /* hdr begins w/ HTTP/ */ 27e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_GET = 2, /* hdr begins with GET<sp> */ 28e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_HEAD = 3, /* hdr begins with HEAD<sp> */ 29e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_POST = 4, /* hdr begins with POST<sp> */ 30e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_PUT = 5, /* hdr begins with ... */ 31e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_DELETE = 6, /* hdr begins with ... */ 32e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_TRACE = 7, /* hdr begins with ... */ 33e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_CONNECT = 8, /* hdr begins with ... */ 34e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_NOTIFY = 9, /* hdr begins with ... */ 35e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_M_SEARCH = 10, /* hdr begins with ... */ 36e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_M_POST = 11, /* hdr begins with ... */ 37e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_SUBSCRIBE = 12, /* hdr begins with ... */ 38e5b75505Sopenharmony_ci HTTPREAD_HDR_TYPE_UNSUBSCRIBE = 13, /* hdr begins with ... */ 39e5b75505Sopenharmony_ci 40e5b75505Sopenharmony_ci HTTPREAD_N_HDR_TYPES /* keep last */ 41e5b75505Sopenharmony_ci}; 42e5b75505Sopenharmony_ci 43e5b75505Sopenharmony_ci 44e5b75505Sopenharmony_ci/* control instance -- opaque struct declaration 45e5b75505Sopenharmony_ci */ 46e5b75505Sopenharmony_cistruct httpread; 47e5b75505Sopenharmony_ci 48e5b75505Sopenharmony_ci 49e5b75505Sopenharmony_ci/* httpread_destroy -- if h is non-NULL, clean up 50e5b75505Sopenharmony_ci * This must eventually be called by the application following 51e5b75505Sopenharmony_ci * call of the application's callback and may be called 52e5b75505Sopenharmony_ci * earlier if desired. 53e5b75505Sopenharmony_ci */ 54e5b75505Sopenharmony_civoid httpread_destroy(struct httpread *h); 55e5b75505Sopenharmony_ci 56e5b75505Sopenharmony_ci/* httpread_create -- start a new reading session making use of eloop. 57e5b75505Sopenharmony_ci * The new instance will use the socket descriptor for reading (until 58e5b75505Sopenharmony_ci * it gets a file and not after) but will not close the socket, even 59e5b75505Sopenharmony_ci * when the instance is destroyed (the application must do that). 60e5b75505Sopenharmony_ci * Return NULL on error. 61e5b75505Sopenharmony_ci * 62e5b75505Sopenharmony_ci * Provided that httpread_create successfully returns a handle, 63e5b75505Sopenharmony_ci * the callback fnc is called to handle httpread_event events. 64e5b75505Sopenharmony_ci * The caller should do destroy on any errors or unknown events. 65e5b75505Sopenharmony_ci * 66e5b75505Sopenharmony_ci * Pass max_bytes == 0 to not read body at all (required for e.g. 67e5b75505Sopenharmony_ci * reply to HEAD request). 68e5b75505Sopenharmony_ci */ 69e5b75505Sopenharmony_cistruct httpread * httpread_create( 70e5b75505Sopenharmony_ci int sd, /* descriptor of TCP socket to read from */ 71e5b75505Sopenharmony_ci void (*cb)(struct httpread *handle, void *cookie, 72e5b75505Sopenharmony_ci enum httpread_event e), /* call on event */ 73e5b75505Sopenharmony_ci void *cookie, /* pass to callback */ 74e5b75505Sopenharmony_ci int max_bytes, /* maximum file size else abort it */ 75e5b75505Sopenharmony_ci int timeout_seconds /* 0; or total duration timeout period */ 76e5b75505Sopenharmony_ci ); 77e5b75505Sopenharmony_ci 78e5b75505Sopenharmony_ci/* httpread_hdr_type_get -- When file is ready, returns header type. 79e5b75505Sopenharmony_ci */ 80e5b75505Sopenharmony_cienum httpread_hdr_type httpread_hdr_type_get(struct httpread *h); 81e5b75505Sopenharmony_ci 82e5b75505Sopenharmony_ci 83e5b75505Sopenharmony_ci/* httpread_uri_get -- When file is ready, uri_get returns (translated) URI 84e5b75505Sopenharmony_ci * or possibly NULL (which would be an error). 85e5b75505Sopenharmony_ci */ 86e5b75505Sopenharmony_cichar *httpread_uri_get(struct httpread *h); 87e5b75505Sopenharmony_ci 88e5b75505Sopenharmony_ci/* httpread_reply_code_get -- When reply is ready, returns reply code */ 89e5b75505Sopenharmony_ciint httpread_reply_code_get(struct httpread *h); 90e5b75505Sopenharmony_ci 91e5b75505Sopenharmony_ci 92e5b75505Sopenharmony_ci/* httpread_length_get -- When file is ready, returns file length. */ 93e5b75505Sopenharmony_ciint httpread_length_get(struct httpread *h); 94e5b75505Sopenharmony_ci 95e5b75505Sopenharmony_ci/* httpread_data_get -- When file is ready, returns file content 96e5b75505Sopenharmony_ci * with null byte appened. 97e5b75505Sopenharmony_ci * Might return NULL in some error condition. 98e5b75505Sopenharmony_ci */ 99e5b75505Sopenharmony_civoid * httpread_data_get(struct httpread *h); 100e5b75505Sopenharmony_ci 101e5b75505Sopenharmony_ci/* httpread_hdr_get -- When file is ready, returns header content 102e5b75505Sopenharmony_ci * with null byte appended. 103e5b75505Sopenharmony_ci * Might return NULL in some error condition. 104e5b75505Sopenharmony_ci */ 105e5b75505Sopenharmony_cichar * httpread_hdr_get(struct httpread *h); 106e5b75505Sopenharmony_ci 107e5b75505Sopenharmony_ci/* httpread_hdr_line_get -- When file is ready, returns pointer 108e5b75505Sopenharmony_ci * to line within header content matching the given tag 109e5b75505Sopenharmony_ci * (after the tag itself and any spaces/tabs). 110e5b75505Sopenharmony_ci * 111e5b75505Sopenharmony_ci * The tag should end with a colon for reliable matching. 112e5b75505Sopenharmony_ci * 113e5b75505Sopenharmony_ci * If not found, returns NULL; 114e5b75505Sopenharmony_ci */ 115e5b75505Sopenharmony_cichar * httpread_hdr_line_get(struct httpread *h, const char *tag); 116e5b75505Sopenharmony_ci 117e5b75505Sopenharmony_ci#endif /* HTTPREAD_H */ 118