1 /*
2  * httpread - Manage reading file(s) from HTTP/TCP socket
3  * Author: Ted Merrill
4  * Copyright 2008 Atheros Communications
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #ifndef HTTPREAD_H
11 #define HTTPREAD_H
12 
13 /* event types (passed to callback) */
14 enum httpread_event {
15 	HTTPREAD_EVENT_FILE_READY = 1,        /* including reply ready */
16 	HTTPREAD_EVENT_TIMEOUT = 2,
17 	HTTPREAD_EVENT_ERROR = 3      /* misc. error, esp malloc error */
18 };
19 
20 
21 /* header type detected
22  * available to callback via call to httpread_reply_code_get()
23  */
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 ... */
39 
40 	HTTPREAD_N_HDR_TYPES    /* keep last */
41 };
42 
43 
44 /* control instance -- opaque struct declaration
45  */
46 struct httpread;
47 
48 
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
52  * earlier if desired.
53  */
54 void httpread_destroy(struct httpread *h);
55 
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.
61  *
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.
65  *
66  * Pass max_bytes == 0 to not read body at all (required for e.g.
67  * reply to HEAD request).
68  */
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 */
76 	);
77 
78 /* httpread_hdr_type_get -- When file is ready, returns header type.
79  */
80 enum httpread_hdr_type httpread_hdr_type_get(struct httpread *h);
81 
82 
83 /* httpread_uri_get -- When file is ready, uri_get returns (translated) URI
84  * or possibly NULL (which would be an error).
85  */
86 char *httpread_uri_get(struct httpread *h);
87 
88 /* httpread_reply_code_get -- When reply is ready, returns reply code */
89 int httpread_reply_code_get(struct httpread *h);
90 
91 
92 /* httpread_length_get -- When file is ready, returns file length. */
93 int httpread_length_get(struct httpread *h);
94 
95 /* httpread_data_get -- When file is ready, returns file content
96  * with null byte appened.
97  * Might return NULL in some error condition.
98  */
99 void * httpread_data_get(struct httpread *h);
100 
101 /* httpread_hdr_get -- When file is ready, returns header content
102  * with null byte appended.
103  * Might return NULL in some error condition.
104  */
105 char * httpread_hdr_get(struct httpread *h);
106 
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).
110  *
111  * The tag should end with a colon for reliable matching.
112  *
113  * If not found, returns NULL;
114  */
115 char * httpread_hdr_line_get(struct httpread *h, const char *tag);
116 
117 #endif /* HTTPREAD_H */
118