00001 00018 #ifndef HTTPREAD_H 00019 #define HTTPREAD_H 00020 00021 /* event types (passed to callback) */ 00022 enum httpread_event { 00023 HTTPREAD_EVENT_FILE_READY = 1, /* including reply ready */ 00024 HTTPREAD_EVENT_TIMEOUT = 2, 00025 HTTPREAD_EVENT_ERROR = 3 /* misc. error, esp malloc error */ 00026 }; 00027 00028 00029 /* header type detected 00030 * available to callback via call to httpread_reply_code_get() 00031 */ 00032 enum httpread_hdr_type { 00033 HTTPREAD_HDR_TYPE_UNKNOWN = 0, /* none of the following */ 00034 HTTPREAD_HDR_TYPE_REPLY = 1, /* hdr begins w/ HTTP/ */ 00035 HTTPREAD_HDR_TYPE_GET = 2, /* hdr begins with GET<sp> */ 00036 HTTPREAD_HDR_TYPE_HEAD = 3, /* hdr begins with HEAD<sp> */ 00037 HTTPREAD_HDR_TYPE_POST = 4, /* hdr begins with POST<sp> */ 00038 HTTPREAD_HDR_TYPE_PUT = 5, /* hdr begins with ... */ 00039 HTTPREAD_HDR_TYPE_DELETE = 6, /* hdr begins with ... */ 00040 HTTPREAD_HDR_TYPE_TRACE = 7, /* hdr begins with ... */ 00041 HTTPREAD_HDR_TYPE_CONNECT = 8, /* hdr begins with ... */ 00042 HTTPREAD_HDR_TYPE_NOTIFY = 9, /* hdr begins with ... */ 00043 HTTPREAD_HDR_TYPE_M_SEARCH = 10, /* hdr begins with ... */ 00044 HTTPREAD_HDR_TYPE_M_POST = 11, /* hdr begins with ... */ 00045 HTTPREAD_HDR_TYPE_SUBSCRIBE = 12, /* hdr begins with ... */ 00046 HTTPREAD_HDR_TYPE_UNSUBSCRIBE = 13, /* hdr begins with ... */ 00047 00048 HTTPREAD_N_HDR_TYPES /* keep last */ 00049 }; 00050 00051 00052 /* control instance -- opaque struct declaration 00053 */ 00054 struct httpread; 00055 00056 00057 /* httpread_destroy -- if h is non-NULL, clean up 00058 * This must eventually be called by the application following 00059 * call of the application's callback and may be called 00060 * earlier if desired. 00061 */ 00062 void httpread_destroy(struct httpread *h); 00063 00064 /* httpread_create -- start a new reading session making use of eloop. 00065 * The new instance will use the socket descriptor for reading (until 00066 * it gets a file and not after) but will not close the socket, even 00067 * when the instance is destroyed (the application must do that). 00068 * Return NULL on error. 00069 * 00070 * Provided that httpread_create successfully returns a handle, 00071 * the callback fnc is called to handle httpread_event events. 00072 * The caller should do destroy on any errors or unknown events. 00073 * 00074 * Pass max_bytes == 0 to not read body at all (required for e.g. 00075 * reply to HEAD request). 00076 */ 00077 struct httpread * httpread_create( 00078 int sd, /* descriptor of TCP socket to read from */ 00079 void (*cb)(struct httpread *handle, void *cookie, 00080 enum httpread_event e), /* call on event */ 00081 void *cookie, /* pass to callback */ 00082 int max_bytes, /* maximum file size else abort it */ 00083 int timeout_seconds /* 0; or total duration timeout period */ 00084 ); 00085 00086 /* httpread_hdr_type_get -- When file is ready, returns header type. 00087 */ 00088 enum httpread_hdr_type httpread_hdr_type_get(struct httpread *h); 00089 00090 00091 /* httpread_uri_get -- When file is ready, uri_get returns (translated) URI 00092 * or possibly NULL (which would be an error). 00093 */ 00094 char *httpread_uri_get(struct httpread *h); 00095 00096 /* httpread_reply_code_get -- When reply is ready, returns reply code */ 00097 int httpread_reply_code_get(struct httpread *h); 00098 00099 00100 /* httpread_length_get -- When file is ready, returns file length. */ 00101 int httpread_length_get(struct httpread *h); 00102 00103 /* httpread_data_get -- When file is ready, returns file content 00104 * with null byte appened. 00105 * Might return NULL in some error condition. 00106 */ 00107 void * httpread_data_get(struct httpread *h); 00108 00109 /* httpread_hdr_get -- When file is ready, returns header content 00110 * with null byte appended. 00111 * Might return NULL in some error condition. 00112 */ 00113 char * httpread_hdr_get(struct httpread *h); 00114 00115 /* httpread_hdr_line_get -- When file is ready, returns pointer 00116 * to line within header content matching the given tag 00117 * (after the tag itself and any spaces/tabs). 00118 * 00119 * The tag should end with a colon for reliable matching. 00120 * 00121 * If not found, returns NULL; 00122 */ 00123 char * httpread_hdr_line_get(struct httpread *h, const char *tag); 00124 00125 #endif /* HTTPREAD_H */ 00126