summaryrefslogtreecommitdiff
path: root/libsoup/soup-message-handlers.c
blob: 20553e35ea16030c38c269a2783d4dc23503c204 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * soup-message-handlers.c: HTTP response handlers
 *
 * Copyright (C) 2000-2003, Ximian, Inc.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "soup-message.h"
#include "soup-message-private.h"

typedef enum {
	SOUP_HANDLER_HEADER = 1,
	SOUP_HANDLER_STATUS_CODE,
	SOUP_HANDLER_STATUS_CLASS
} SoupHandlerKind;

typedef struct {
	SoupHandlerPhase         phase;
	SoupMessageCallbackFn    handler_cb;
	gpointer                 user_data;

	SoupHandlerKind          kind;
	union {
		guint            status_code;
		SoupStatusClass  status_class;
		const char      *header;
	} data;
} SoupHandlerData;

static inline void
run_handler (SoupMessage     *msg,
	     SoupHandlerPhase invoke_phase,
	     SoupHandlerData *data)
{
	if (data->phase != invoke_phase)
		return;

	switch (data->kind) {
	case SOUP_HANDLER_HEADER:
		if (!soup_message_get_header (msg->response_headers,
					      data->data.header))
			return;
		break;
	case SOUP_HANDLER_STATUS_CODE:
		if (msg->status_code != data->data.status_code)
			return;
		break;
	case SOUP_HANDLER_STATUS_CLASS:
		if (msg->status_code < data->data.status_class * 100 ||
		    msg->status_code >= (data->data.status_class + 1) * 100)
			return;
		break;
	default:
		break;
	}

	(*data->handler_cb) (msg, data->user_data);
}

/**
 * soup_message_run_handlers:
 * @msg: a #SoupMessage
 * @invoke_phase: which group of handlers to run
 *
 * Run each @invoke_phase handler on @msg. If a handler requeues the
 * message, we stop processing at that point.
 */
void
soup_message_run_handlers (SoupMessage *msg, SoupHandlerPhase invoke_phase)
{
	SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
	GSList *copy, *list;

	g_return_if_fail (SOUP_IS_MESSAGE (msg));

	/* Jump through hoops to deal with callbacks that modify the list. */
	copy = g_slist_copy (priv->content_handlers);

	for (list = copy; list; list = list->next) {
		if (!g_slist_find (priv->content_handlers, list->data))
			continue;
		run_handler (msg, invoke_phase, list->data);

		if (SOUP_MESSAGE_IS_STARTING (msg))
			break;
	}

	g_slist_free (copy);
}

static void
add_handler (SoupMessage           *msg,
	     SoupHandlerPhase       phase,
	     SoupMessageCallbackFn  handler_cb,
	     gpointer               user_data,
	     SoupHandlerKind        kind,
	     const char            *header,
	     guint                  status_code,
	     SoupStatusClass        status_class)
{
	SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
	SoupHandlerData *data;

	data = g_new0 (SoupHandlerData, 1);
	data->phase = phase;
	data->handler_cb = handler_cb;
	data->user_data = user_data;
	data->kind = kind;

	switch (kind) {
	case SOUP_HANDLER_HEADER:
		data->data.header = header;
		break;
	case SOUP_HANDLER_STATUS_CODE:
		data->data.status_code = status_code;
		break;
	case SOUP_HANDLER_STATUS_CLASS:
		data->data.status_class = status_class;
		break;
	default:
		break;
	}

	priv->content_handlers =
		g_slist_append (priv->content_handlers, data);
}

/**
 * soup_message_add_header_handler:
 * @msg: a #SoupMessage
 * @header: HTTP response header to match against
 * @phase: processing phase to run the handler in
 * @handler_cb: the handler
 * @user_data: data to pass to @handler_cb
 *
 * Adds a handler to @msg for messages containing the given response
 * header.
 **/
void
soup_message_add_header_handler (SoupMessage           *msg,
				 const char            *header,
				 SoupHandlerPhase       phase,
				 SoupMessageCallbackFn  handler_cb,
				 gpointer               user_data)
{
	g_return_if_fail (SOUP_IS_MESSAGE (msg));
	g_return_if_fail (header != NULL);
	g_return_if_fail (handler_cb != NULL);

	add_handler (msg, phase, handler_cb, user_data,
		     SOUP_HANDLER_HEADER,
		     header, 0, 0);
}

/**
 * soup_message_add_status_code_handler:
 * @msg: a #SoupMessage
 * @status_code: HTTP status code to match against
 * @phase: processing phase to run the handler in
 * @handler_cb: the handler
 * @user_data: data to pass to @handler_cb
 *
 * Adds a handler to @msg for messages receiving the given status
 * code.
 **/
void
soup_message_add_status_code_handler (SoupMessage           *msg,
				      guint                  status_code,
				      SoupHandlerPhase       phase,
				      SoupMessageCallbackFn  handler_cb,
				      gpointer               user_data)
{
	g_return_if_fail (SOUP_IS_MESSAGE (msg));
	g_return_if_fail (status_code != 0);
	g_return_if_fail (handler_cb != NULL);

	add_handler (msg, phase, handler_cb, user_data,
		     SOUP_HANDLER_STATUS_CODE,
		     NULL, status_code, 0);
}

/**
 * soup_message_add_status_class_handler:
 * @msg: a #SoupMessage
 * @status_class: HTTP status code class to match against
 * @phase: processing phase to run the handler in
 * @handler_cb: the handler
 * @user_data: data to pass to @handler_cb
 *
 * Adds a handler to @msg for messages receiving a status code in
 * the given class.
 **/
void
soup_message_add_status_class_handler (SoupMessage           *msg,
				       SoupStatusClass        status_class,
				       SoupHandlerPhase       phase,
				       SoupMessageCallbackFn  handler_cb,
				       gpointer               user_data)
{
	g_return_if_fail (SOUP_IS_MESSAGE (msg));
	g_return_if_fail (status_class != 0);
	g_return_if_fail (handler_cb != NULL);

	add_handler (msg, phase, handler_cb, user_data,
		     SOUP_HANDLER_STATUS_CLASS,
		     NULL, 0, status_class);
}

/**
 * soup_message_add_handler:
 * @msg: a #SoupMessage
 * @phase: processing phase to run the handler in
 * @handler_cb: the handler
 * @user_data: data to pass to @handler_cb
 *
 * Adds a handler to @msg for all messages
 **/
void
soup_message_add_handler (SoupMessage           *msg,
			  SoupHandlerPhase       phase,
			  SoupMessageCallbackFn  handler_cb,
			  gpointer               user_data)
{
	g_return_if_fail (SOUP_IS_MESSAGE (msg));
	g_return_if_fail (handler_cb != NULL);

	add_handler (msg, phase, handler_cb, user_data, 0, NULL, 0, 0);
}

/**
 * soup_message_remove_handler:
 * @msg: a #SoupMessage
 * @phase: processing phase to run the handler in
 * @handler_cb: the handler
 * @user_data: data to pass to @handler_cb
 *
 * Removes all matching handlers from @msg
 **/
void
soup_message_remove_handler (SoupMessage           *msg,
			     SoupHandlerPhase       phase,
			     SoupMessageCallbackFn  handler_cb,
			     gpointer               user_data)
{
	SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);

	GSList *iter = priv->content_handlers;

	while (iter) {
		SoupHandlerData *data = iter->data;

		if (data->handler_cb == handler_cb &&
		    data->user_data == user_data &&
		    data->phase == phase) {
			priv->content_handlers =
				g_slist_remove (priv->content_handlers,
						data);
			g_free (data);
			break;
		}

		iter = iter->next;
	}
}