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