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