1214734Srpaulo/*
2189251Ssam * httpread - Manage reading file(s) from HTTP/TCP socket
3189251Ssam * Author: Ted Merrill
4189251Ssam * Copyright 2008 Atheros Communications
5189251Ssam *
6252726Srpaulo * This software may be distributed under the terms of the BSD license.
7252726Srpaulo * See README for more details.
8189251Ssam */
9189251Ssam
10189251Ssam#ifndef HTTPREAD_H
11189251Ssam#define HTTPREAD_H
12189251Ssam
13189251Ssam/* event types (passed to callback) */
14189251Ssamenum httpread_event {
15189251Ssam	HTTPREAD_EVENT_FILE_READY = 1,        /* including reply ready */
16189251Ssam	HTTPREAD_EVENT_TIMEOUT = 2,
17189251Ssam	HTTPREAD_EVENT_ERROR = 3      /* misc. error, esp malloc error */
18189251Ssam};
19189251Ssam
20189251Ssam
21189251Ssam/* header type detected
22189251Ssam * available to callback via call to httpread_reply_code_get()
23189251Ssam */
24189251Ssamenum httpread_hdr_type {
25189251Ssam	HTTPREAD_HDR_TYPE_UNKNOWN = 0,      /* none of the following */
26189251Ssam	HTTPREAD_HDR_TYPE_REPLY = 1,        /* hdr begins w/ HTTP/ */
27189251Ssam	HTTPREAD_HDR_TYPE_GET = 2,          /* hdr begins with GET<sp> */
28189251Ssam	HTTPREAD_HDR_TYPE_HEAD = 3,         /* hdr begins with HEAD<sp> */
29189251Ssam	HTTPREAD_HDR_TYPE_POST = 4,         /* hdr begins with POST<sp> */
30189251Ssam	HTTPREAD_HDR_TYPE_PUT = 5,          /* hdr begins with ... */
31189251Ssam	HTTPREAD_HDR_TYPE_DELETE = 6,       /* hdr begins with ... */
32189251Ssam	HTTPREAD_HDR_TYPE_TRACE = 7,        /* hdr begins with ... */
33189251Ssam	HTTPREAD_HDR_TYPE_CONNECT = 8,      /* hdr begins with ... */
34189251Ssam	HTTPREAD_HDR_TYPE_NOTIFY = 9,       /* hdr begins with ... */
35189251Ssam	HTTPREAD_HDR_TYPE_M_SEARCH = 10,    /* hdr begins with ... */
36189251Ssam	HTTPREAD_HDR_TYPE_M_POST = 11,      /* hdr begins with ... */
37189251Ssam	HTTPREAD_HDR_TYPE_SUBSCRIBE = 12,   /* hdr begins with ... */
38189251Ssam	HTTPREAD_HDR_TYPE_UNSUBSCRIBE = 13, /* hdr begins with ... */
39189251Ssam
40189251Ssam	HTTPREAD_N_HDR_TYPES    /* keep last */
41189251Ssam};
42189251Ssam
43189251Ssam
44189251Ssam/* control instance -- opaque struct declaration
45189251Ssam */
46189251Ssamstruct httpread;
47189251Ssam
48189251Ssam
49189251Ssam/* httpread_destroy -- if h is non-NULL, clean up
50189251Ssam * This must eventually be called by the application following
51189251Ssam * call of the application's callback and may be called
52189251Ssam * earlier if desired.
53189251Ssam */
54189251Ssamvoid httpread_destroy(struct httpread *h);
55189251Ssam
56189251Ssam/* httpread_create -- start a new reading session making use of eloop.
57189251Ssam * The new instance will use the socket descriptor for reading (until
58189251Ssam * it gets a file and not after) but will not close the socket, even
59189251Ssam * when the instance is destroyed (the application must do that).
60189251Ssam * Return NULL on error.
61189251Ssam *
62189251Ssam * Provided that httpread_create successfully returns a handle,
63189251Ssam * the callback fnc is called to handle httpread_event events.
64189251Ssam * The caller should do destroy on any errors or unknown events.
65189251Ssam *
66189251Ssam * Pass max_bytes == 0 to not read body at all (required for e.g.
67189251Ssam * reply to HEAD request).
68189251Ssam */
69189251Ssamstruct httpread * httpread_create(
70189251Ssam	int sd,         /* descriptor of TCP socket to read from */
71189251Ssam	void (*cb)(struct httpread *handle, void *cookie,
72189251Ssam		    enum httpread_event e),  /* call on event */
73189251Ssam	void *cookie,    /* pass to callback */
74189251Ssam	int max_bytes,          /* maximum file size else abort it */
75189251Ssam	int timeout_seconds     /* 0; or total duration timeout period */
76189251Ssam	);
77189251Ssam
78189251Ssam/* httpread_hdr_type_get -- When file is ready, returns header type.
79189251Ssam */
80189251Ssamenum httpread_hdr_type httpread_hdr_type_get(struct httpread *h);
81189251Ssam
82189251Ssam
83189251Ssam/* httpread_uri_get -- When file is ready, uri_get returns (translated) URI
84189251Ssam * or possibly NULL (which would be an error).
85189251Ssam */
86189251Ssamchar *httpread_uri_get(struct httpread *h);
87189251Ssam
88189251Ssam/* httpread_reply_code_get -- When reply is ready, returns reply code */
89189251Ssamint httpread_reply_code_get(struct httpread *h);
90189251Ssam
91189251Ssam
92189251Ssam/* httpread_length_get -- When file is ready, returns file length. */
93189251Ssamint httpread_length_get(struct httpread *h);
94189251Ssam
95189251Ssam/* httpread_data_get -- When file is ready, returns file content
96189251Ssam * with null byte appened.
97189251Ssam * Might return NULL in some error condition.
98189251Ssam */
99189251Ssamvoid * httpread_data_get(struct httpread *h);
100189251Ssam
101189251Ssam/* httpread_hdr_get -- When file is ready, returns header content
102189251Ssam * with null byte appended.
103189251Ssam * Might return NULL in some error condition.
104189251Ssam */
105189251Ssamchar * httpread_hdr_get(struct httpread *h);
106189251Ssam
107189251Ssam/* httpread_hdr_line_get -- When file is ready, returns pointer
108189251Ssam * to line within header content matching the given tag
109189251Ssam * (after the tag itself and any spaces/tabs).
110189251Ssam *
111189251Ssam * The tag should end with a colon for reliable matching.
112189251Ssam *
113189251Ssam * If not found, returns NULL;
114189251Ssam */
115189251Ssamchar * httpread_hdr_line_get(struct httpread *h, const char *tag);
116189251Ssam
117189251Ssam#endif /* HTTPREAD_H */
118