summaryrefslogtreecommitdiff
path: root/src/libfast/request.h
blob: c9c1f13e2c7afd717f6c7d1d61f555daf74780ea (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (C) 2007 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

/**
 * @defgroup request request
 * @{ @ingroup libfast
 */

#ifndef REQUEST_H_
#define REQUEST_H_

#include <fcgiapp.h>
#include <library.h>

typedef struct request_t request_t;

/**
 * A HTTP request, encapsulates FCGX_Request.
 *
 * The response is also handled through the request object.
 */
struct request_t {

	/**
	 * Add a cookie to the reply (Set-Cookie header).
	 *
	 * @param name		name of the cookie to set
	 * @param value		value of the cookie
	 */
	void (*add_cookie)(request_t *this, char *name, char *value);

	/**
	 * Get a cookie the client sent in the request.
	 *
	 * @param name		name of the cookie
	 * @return			cookie value, NULL if no such cookie found
	 */
	char* (*get_cookie)(request_t *this, char *name);

	/**
	 * Get the request path relative to the application.
	 *
	 * @return			path
	 */
	char* (*get_path)(request_t *this);

	/**
	 * Get the base path of the application.
	 *
	 * @return			base path
	 */
	char* (*get_base)(request_t *this);

	/**
	 * Get the remote host address of this request.
	 *
	 * @return			host address as string
	 */
	char* (*get_host)(request_t *this);

	/**
	 * Get the user agent string.
	 *
	 * @return			user agent string
	 */
	char* (*get_user_agent)(request_t *this);

	/**
	 * Get a post/get variable included in the request.
	 *
	 * @param name		name of the POST/GET variable
	 * @return			value, NULL if not found
	 */
	char* (*get_query_data)(request_t *this, char *name);

	/**
	 * Get an arbitrary environment variable.
	 *
	 * @param name		name of the environment variable
	 * @return			value, NULL if not found
	 */
	char* (*get_env_var)(request_t *this, char *name);

	/**
	 * Read raw POST/PUT data from HTTP request.
	 *
	 * @param buf		buffer to read data into
	 * @param len		size of the supplied buffer
	 * @return			number of bytes read, < 0 on error
	 */
	int (*read_data)(request_t *this, char *buf, int len);

	/**
	 * Close the session and it's context after handling.
	 */
	void (*close_session)(request_t *this);

	/**
	 * Has the session been closed by close_session()?
	 *
	 * @return			TRUE if session has been closed
	 */
	bool (*session_closed)(request_t *this);

	/**
	 * Redirect the client to another location.
	 *
	 * @param fmt		location format string
	 * @param ...		variable argument for fmt
	 */
	void (*redirect)(request_t *this, char *fmt, ...);

	/**
	 * Get the HTTP referer.
	 *
	 * @return			HTTP referer
	 */
	char* (*get_referer)(request_t *this);

	/**
	 * Redirect back to the referer.
	 */
	void (*to_referer)(request_t *this);

	/**
	 * Set a template value.
	 *
	 * @param key		key to set
	 * @param value		value to set key to
	 */
	void (*set)(request_t *this, char *key, char *value);

	/**
	 * Set a template value using format strings.
	 *
	 * Format string is in the form "key=value", where printf like format
	 * substitution occurs over the whole string.
	 *
	 * @param format	printf like format string
	 * @param ...		variable argument list
	 */
	void (*setf)(request_t *this, char *format, ...);

	/**
	 * Render a template.
	 *
	 * The render() function additionally sets a HDF variable "base"
	 * which points to the root of the web application and allows to point to
	 * other targets without to worry about path location.
	 *
	 * @param template	clearsilver template file location
	 */
	void (*render)(request_t *this, char *template);

	/**
	 * Stream a format string to the client.
	 *
	 * Stream is not closed and may be called multiple times to allow
	 * server-push functionality.
	 *
	 * @param format	printf like format string
	 * @param ...		argmuent list to format string
	 * @return			number of streamed bytes, < 0 if stream closed
	 */
	int (*streamf)(request_t *this, char *format, ...);

	/**
	 * Serve a request with headers and a body.
	 *
	 * @param headers	HTTP headers, \n separated
	 * @param chunk		body to write to output
	 */
	void (*serve)(request_t *this, char *headers, chunk_t chunk);

	/**
	 * Increase the reference count to the stream.
	 *
	 * @return			this with increased refcount
	 */
	request_t* (*get_ref)(request_t *this);

	/**
	 * Destroy the request_t.
	 */
	void (*destroy) (request_t *this);
};

/**
 * Create a request from the fastcgi struct.
 *
 * @param fd			file descripter opened with FCGX_OpenSocket
 * @param debug			no stripping, no compression, timing information
 */
request_t *request_create(int fd, bool debug);

#endif /** REQUEST_H_ @}*/