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