1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-filter-input-stream.c
5 * Copyright 2012 Red Hat, Inc.
14 #include "soup-filter-input-stream.h"
17 /* This is essentially a subset of GDataInputStream, except that we
18 * can do the equivalent of "fill_nonblocking()" on it. (We could use
19 * an actual GDataInputStream, and implement the nonblocking semantics
20 * via fill_async(), but that would be more work...)
23 struct _SoupFilterInputStreamPrivate {
26 gboolean in_read_until;
29 static void soup_filter_input_stream_pollable_init (GPollableInputStreamInterface *pollable_interface, gpointer interface_data);
31 G_DEFINE_TYPE_WITH_CODE (SoupFilterInputStream, soup_filter_input_stream, G_TYPE_FILTER_INPUT_STREAM,
32 G_ADD_PRIVATE (SoupFilterInputStream)
33 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
34 soup_filter_input_stream_pollable_init))
37 soup_filter_input_stream_init (SoupFilterInputStream *stream)
39 stream->priv = soup_filter_input_stream_get_instance_private (stream);
43 soup_filter_input_stream_finalize (GObject *object)
45 SoupFilterInputStream *fstream = SOUP_FILTER_INPUT_STREAM (object);
47 g_clear_pointer (&fstream->priv->buf, g_byte_array_unref);
49 G_OBJECT_CLASS (soup_filter_input_stream_parent_class)->finalize (object);
53 read_from_buf (SoupFilterInputStream *fstream, gpointer buffer, gsize count)
55 GByteArray *buf = fstream->priv->buf;
59 memcpy (buffer, buf->data, count);
61 if (count == buf->len) {
62 g_byte_array_free (buf, TRUE);
63 fstream->priv->buf = NULL;
65 memmove (buf->data, buf->data + count,
67 g_byte_array_set_size (buf, buf->len - count);
74 soup_filter_input_stream_read_fn (GInputStream *stream,
77 GCancellable *cancellable,
80 SoupFilterInputStream *fstream = SOUP_FILTER_INPUT_STREAM (stream);
82 if (!fstream->priv->in_read_until)
83 fstream->priv->need_more = FALSE;
85 if (fstream->priv->buf && !fstream->priv->in_read_until) {
86 return read_from_buf (fstream, buffer, count);
88 return g_pollable_stream_read (G_FILTER_INPUT_STREAM (fstream)->base_stream,
90 TRUE, cancellable, error);
95 soup_filter_input_stream_is_readable (GPollableInputStream *stream)
97 SoupFilterInputStream *fstream = SOUP_FILTER_INPUT_STREAM (stream);
99 if (fstream->priv->buf && !fstream->priv->need_more)
102 return g_pollable_input_stream_is_readable (G_POLLABLE_INPUT_STREAM (G_FILTER_INPUT_STREAM (fstream)->base_stream));
106 soup_filter_input_stream_read_nonblocking (GPollableInputStream *stream,
111 SoupFilterInputStream *fstream = SOUP_FILTER_INPUT_STREAM (stream);
113 if (!fstream->priv->in_read_until)
114 fstream->priv->need_more = FALSE;
116 if (fstream->priv->buf && !fstream->priv->in_read_until) {
117 return read_from_buf (fstream, buffer, count);
119 return g_pollable_stream_read (G_FILTER_INPUT_STREAM (fstream)->base_stream,
126 soup_filter_input_stream_create_source (GPollableInputStream *stream,
127 GCancellable *cancellable)
129 SoupFilterInputStream *fstream = SOUP_FILTER_INPUT_STREAM (stream);
130 GSource *base_source, *pollable_source;
132 if (fstream->priv->buf && !fstream->priv->need_more)
133 base_source = g_timeout_source_new (0);
135 base_source = g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM (G_FILTER_INPUT_STREAM (fstream)->base_stream), cancellable);
137 g_source_set_dummy_callback (base_source);
138 pollable_source = g_pollable_source_new (G_OBJECT (stream));
139 g_source_add_child_source (pollable_source, base_source);
140 g_source_unref (base_source);
142 return pollable_source;
146 soup_filter_input_stream_class_init (SoupFilterInputStreamClass *stream_class)
148 GObjectClass *object_class = G_OBJECT_CLASS (stream_class);
149 GInputStreamClass *input_stream_class = G_INPUT_STREAM_CLASS (stream_class);
151 object_class->finalize = soup_filter_input_stream_finalize;
153 input_stream_class->read_fn = soup_filter_input_stream_read_fn;
157 soup_filter_input_stream_pollable_init (GPollableInputStreamInterface *pollable_interface,
158 gpointer interface_data)
160 pollable_interface->is_readable = soup_filter_input_stream_is_readable;
161 pollable_interface->read_nonblocking = soup_filter_input_stream_read_nonblocking;
162 pollable_interface->create_source = soup_filter_input_stream_create_source;
166 soup_filter_input_stream_new (GInputStream *base_stream)
168 return g_object_new (SOUP_TYPE_FILTER_INPUT_STREAM,
169 "base-stream", base_stream,
170 "close-base-stream", FALSE,
175 soup_filter_input_stream_read_line (SoupFilterInputStream *fstream,
180 GCancellable *cancellable,
183 return soup_filter_input_stream_read_until (fstream, buffer, length,
190 soup_filter_input_stream_read_until (SoupFilterInputStream *fstream,
193 const void *boundary,
194 gsize boundary_length,
196 gboolean include_boundary,
197 gboolean *got_boundary,
198 GCancellable *cancellable,
201 gssize nread, read_length;
202 guint8 *p, *buf, *end;
203 gboolean eof = FALSE;
204 GError *my_error = NULL;
206 g_return_val_if_fail (SOUP_IS_FILTER_INPUT_STREAM (fstream), -1);
207 g_return_val_if_fail (!include_boundary || (boundary_length < length), -1);
209 *got_boundary = FALSE;
210 fstream->priv->need_more = FALSE;
212 if (!fstream->priv->buf || fstream->priv->buf->len < boundary_length) {
216 if (!fstream->priv->buf)
217 fstream->priv->buf = g_byte_array_new ();
218 prev_len = fstream->priv->buf->len;
219 g_byte_array_set_size (fstream->priv->buf, length);
220 buf = fstream->priv->buf->data;
222 fstream->priv->in_read_until = TRUE;
223 nread = g_pollable_stream_read (G_INPUT_STREAM (fstream),
224 buf + prev_len, length - prev_len,
226 cancellable, &my_error);
227 fstream->priv->in_read_until = FALSE;
230 fstream->priv->buf->len = prev_len;
232 g_byte_array_free (fstream->priv->buf, TRUE);
233 fstream->priv->buf = NULL;
236 if (nread == 0 && prev_len)
239 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
240 fstream->priv->need_more = TRUE;
242 g_propagate_error (error, my_error);
248 g_propagate_error (error, my_error);
250 fstream->priv->buf->len = prev_len + nread;
252 buf = fstream->priv->buf->data;
254 /* Scan for the boundary within the range we can possibly return. */
255 if (include_boundary)
256 end = buf + MIN (fstream->priv->buf->len, length) - boundary_length;
258 end = buf + MIN (fstream->priv->buf->len - boundary_length, length);
259 for (p = buf; p <= end; p++) {
260 if (*p == *(guint8*)boundary &&
261 !memcmp (p, boundary, boundary_length)) {
262 if (include_boundary)
263 p += boundary_length;
264 *got_boundary = TRUE;
269 if (!*got_boundary && fstream->priv->buf->len < length && !eof)
272 if (eof && !*got_boundary)
273 read_length = MIN (fstream->priv->buf->len, length);
275 read_length = p - buf;
276 return read_from_buf (fstream, buffer, read_length);