aboutsummaryrefslogtreecommitdiffstats
path: root/src/wps/httpread.h
blob: fb1ecb7de36cfdf5dd0cfe63b0b1d3f8bfa9dd57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * httpread - Manage reading file(s) from HTTP/TCP socket
 * Author: Ted Merrill
 * Copyright 2008 Atheros Communications
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Alternatively, this software may be distributed under the terms of BSD
 * license.
 *
 * See README and COPYING for more details.
 */

#ifndef HTTPREAD_H
#define HTTPREAD_H

/* event types (passed to callback) */
enum httpread_event {
	HTTPREAD_EVENT_FILE_READY = 1,        /* including reply ready */
	HTTPREAD_EVENT_TIMEOUT = 2,
	HTTPREAD_EVENT_ERROR = 3      /* misc. error, esp malloc error */
};


/* header type detected
 * available to callback via call to httpread_reply_code_get()
 */
enum httpread_hdr_type {
	HTTPREAD_HDR_TYPE_UNKNOWN = 0,      /* none of the following */
	HTTPREAD_HDR_TYPE_REPLY = 1,        /* hdr begins w/ HTTP/ */
	HTTPREAD_HDR_TYPE_GET = 2,          /* hdr begins with GET<sp> */
	HTTPREAD_HDR_TYPE_HEAD = 3,         /* hdr begins with HEAD<sp> */
	HTTPREAD_HDR_TYPE_POST = 4,         /* hdr begins with POST<sp> */
	HTTPREAD_HDR_TYPE_PUT = 5,          /* hdr begins with ... */
	HTTPREAD_HDR_TYPE_DELETE = 6,       /* hdr begins with ... */
	HTTPREAD_HDR_TYPE_TRACE = 7,        /* hdr begins with ... */
	HTTPREAD_HDR_TYPE_CONNECT = 8,      /* hdr begins with ... */
	HTTPREAD_HDR_TYPE_NOTIFY = 9,       /* hdr begins with ... */
	HTTPREAD_HDR_TYPE_M_SEARCH = 10,    /* hdr begins with ... */
	HTTPREAD_HDR_TYPE_M_POST = 11,      /* hdr begins with ... */
	HTTPREAD_HDR_TYPE_SUBSCRIBE = 12,   /* hdr begins with ... */
	HTTPREAD_HDR_TYPE_UNSUBSCRIBE = 13, /* hdr begins with ... */

	HTTPREAD_N_HDR_TYPES    /* keep last */
};


/* control instance -- opaque struct declaration
 */
struct httpread;


/* httpread_destroy -- if h is non-NULL, clean up
 * This must eventually be called by the application following
 * call of the application's callback and may be called
 * earlier if desired.
 */
void httpread_destroy(struct httpread *h);

/* httpread_create -- start a new reading session making use of eloop.
 * The new instance will use the socket descriptor for reading (until
 * it gets a file and not after) but will not close the socket, even
 * when the instance is destroyed (the application must do that).
 * Return NULL on error.
 *
 * Provided that httpread_create successfully returns a handle,
 * the callback fnc is called to handle httpread_event events.
 * The caller should do destroy on any errors or unknown events.
 *
 * Pass max_bytes == 0 to not read body at all (required for e.g.
 * reply to HEAD request).
 */
struct httpread * httpread_create(
	int sd,         /* descriptor of TCP socket to read from */
	void (*cb)(struct httpread *handle, void *cookie,
		    enum httpread_event e),  /* call on event */
	void *cookie,    /* pass to callback */
	int max_bytes,          /* maximum file size else abort it */
	int timeout_seconds     /* 0; or total duration timeout period */
	);

/* httpread_hdr_type_get -- When file is ready, returns header type.
 */
enum httpread_hdr_type httpread_hdr_type_get(struct httpread *h);


/* httpread_uri_get -- When file is ready, uri_get returns (translated) URI
 * or possibly NULL (which would be an error).
 */
char *httpread_uri_get(struct httpread *h);

/* httpread_reply_code_get -- When reply is ready, returns reply code */
int httpread_reply_code_get(struct httpread *h);


/* httpread_length_get -- When file is ready, returns file length. */
int httpread_length_get(struct httpread *h);

/* httpread_data_get -- When file is ready, returns file content
 * with null byte appened.
 * Might return NULL in some error condition.
 */
void * httpread_data_get(struct httpread *h);

/* httpread_hdr_get -- When file is ready, returns header content
 * with null byte appended.
 * Might return NULL in some error condition.
 */
char * httpread_hdr_get(struct httpread *h);

/* httpread_hdr_line_get -- When file is ready, returns pointer
 * to line within header content matching the given tag
 * (after the tag itself and any spaces/tabs).
 *
 * The tag should end with a colon for reliable matching.
 *
 * If not found, returns NULL;
 */
char * httpread_hdr_line_get(struct httpread *h, const char *tag);

#endif /* HTTPREAD_H */