1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-message-body.c: SoupMessage request/response bodies
5 * Copyright (C) 2000-2003, Ximian, Inc.
10 #include "soup-message-body.h"
13 * SECTION:soup-message-body
14 * @short_description: HTTP message body
15 * @see_also: #SoupMessage
17 * #SoupMessageBody represents the request or response body of a
20 * In addition to #SoupMessageBody, libsoup also defines a "smaller"
21 * data buffer type, #SoupBuffer, which is primarily used as a
22 * component of #SoupMessageBody. In particular, when using chunked
23 * encoding to transmit or receive a message, each chunk is
24 * represented as a #SoupBuffer.
29 * @SOUP_MEMORY_STATIC: The memory is statically allocated and
30 * constant; libsoup can use the passed-in buffer directly and not
31 * need to worry about it being modified or freed.
32 * @SOUP_MEMORY_TAKE: The caller has allocated the memory for the
33 * #SoupBuffer's use; libsoup will assume ownership of it and free it
34 * (with g_free()) when it is done with it.
35 * @SOUP_MEMORY_COPY: The passed-in data belongs to the caller; the
36 * #SoupBuffer will copy it into new memory, leaving the caller free
37 * to reuse the original memory.
38 * @SOUP_MEMORY_TEMPORARY: The passed-in data belongs to the caller,
39 * but will remain valid for the lifetime of the #SoupBuffer. The
40 * difference between this and @SOUP_MEMORY_STATIC is that if you copy
41 * a @SOUP_MEMORY_TEMPORARY buffer, it will make a copy of the memory
42 * as well, rather than reusing the original memory.
44 * Describes how #SoupBuffer should use the data passed in by the
47 * See also soup_buffer_new_with_owner(), which allows to you create a
48 * buffer containing data which is owned by another object.
51 /* Internal SoupMemoryUse values */
53 SOUP_MEMORY_SUBBUFFER = SOUP_MEMORY_TEMPORARY + 1,
59 * @data: (type gpointer): the data
60 * @length: length of @data
62 * A data buffer, generally used to represent a chunk of a
65 * @data is a #char because that's generally convenient; in some
66 * situations you may need to cast it to #guchar or another type.
75 GDestroyNotify owner_dnotify;
80 * @use: how @data is to be used by the buffer
82 * @length: length of @data
84 * Creates a new #SoupBuffer containing @length bytes from @data.
86 * Return value: the new #SoupBuffer.
89 soup_buffer_new (SoupMemoryUse use, gconstpointer data, gsize length)
91 SoupBufferPrivate *priv = g_slice_new0 (SoupBufferPrivate);
93 if (use == SOUP_MEMORY_COPY) {
94 data = g_memdup (data, length);
95 use = SOUP_MEMORY_TAKE;
98 priv->buffer.data = data;
99 priv->buffer.length = length;
103 if (use == SOUP_MEMORY_TAKE) {
104 priv->owner = (gpointer)data;
105 priv->owner_dnotify = g_free;
108 return (SoupBuffer *)priv;
112 * soup_buffer_new_take:
113 * @data: (array length=length) (transfer full): data
114 * @length: length of @data
116 * Creates a new #SoupBuffer containing @length bytes from @data.
118 * This function is exactly equivalent to soup_buffer_new() with
119 * %SOUP_MEMORY_TAKE as first argument; it exists mainly for
120 * convenience and simplifying language bindings.
122 * Return value: the new #SoupBuffer.
124 * Rename to: soup_buffer_new
128 soup_buffer_new_take (guchar *data, gsize length)
130 return soup_buffer_new (SOUP_MEMORY_TAKE, data, length);
134 * soup_buffer_new_subbuffer:
135 * @parent: the parent #SoupBuffer
136 * @offset: offset within @parent to start at
137 * @length: number of bytes to copy from @parent
139 * Creates a new #SoupBuffer containing @length bytes "copied" from
140 * @parent starting at @offset. (Normally this will not actually copy
141 * any data, but will instead simply reference the same data as
144 * Return value: the new #SoupBuffer.
147 soup_buffer_new_subbuffer (SoupBuffer *parent, gsize offset, gsize length)
149 SoupBufferPrivate *priv;
151 /* Normally this is just a ref, but if @parent is TEMPORARY,
152 * it will do an actual copy.
154 parent = soup_buffer_copy (parent);
156 priv = g_slice_new0 (SoupBufferPrivate);
157 priv->buffer.data = parent->data + offset;
158 priv->buffer.length = length;
159 priv->use = SOUP_MEMORY_SUBBUFFER;
160 priv->owner = parent;
161 priv->owner_dnotify = (GDestroyNotify)soup_buffer_free;
164 return (SoupBuffer *)priv;
168 * soup_buffer_new_with_owner:
170 * @length: length of @data
171 * @owner: pointer to an object that owns @data
172 * @owner_dnotify: (allow-none): a function to free/unref @owner when
173 * the buffer is freed
175 * Creates a new #SoupBuffer containing @length bytes from @data. When
176 * the #SoupBuffer is freed, it will call @owner_dnotify, passing
177 * @owner to it. You must ensure that @data will remain valid until
178 * @owner_dnotify is called.
180 * For example, you could use this to create a buffer containing data
181 * returned from libxml without needing to do an extra copy:
183 * <informalexample><programlisting>
184 * xmlDocDumpMemory (doc, &xmlbody, &len);
185 * return soup_buffer_new_with_owner (xmlbody, len, xmlbody,
186 * (GDestroyNotify)xmlFree);
187 * </programlisting></informalexample>
189 * In this example, @data and @owner are the same, but in other cases
190 * they would be different (eg, @owner would be a object, and @data
191 * would be a pointer to one of the object's fields).
193 * Return value: the new #SoupBuffer.
196 soup_buffer_new_with_owner (gconstpointer data, gsize length,
197 gpointer owner, GDestroyNotify owner_dnotify)
199 SoupBufferPrivate *priv = g_slice_new0 (SoupBufferPrivate);
201 priv->buffer.data = data;
202 priv->buffer.length = length;
203 priv->use = SOUP_MEMORY_OWNED;
205 priv->owner_dnotify = owner_dnotify;
208 return (SoupBuffer *)priv;
212 * soup_buffer_get_owner:
213 * @buffer: a #SoupBuffer created with soup_buffer_new_with_owner()
215 * Gets the "owner" object for a buffer created with
216 * soup_buffer_new_with_owner().
218 * Return value: (transfer none): the owner pointer
221 soup_buffer_get_owner (SoupBuffer *buffer)
223 SoupBufferPrivate *priv = (SoupBufferPrivate *)buffer;
225 g_return_val_if_fail ((int)priv->use == (int)SOUP_MEMORY_OWNED, NULL);
230 * soup_buffer_get_data:
231 * @buffer: a #SoupBuffer
232 * @data: (out) (array length=length) (transfer none): the pointer
233 * to the buffer data is stored here
234 * @length: (out): the length of the buffer data is stored here
236 * This function exists for use by language bindings, because it's not
237 * currently possible to get the right effect by annotating the fields
243 soup_buffer_get_data (SoupBuffer *buffer,
247 *data = (const guint8 *)buffer->data;
248 *length = buffer->length;
253 * @buffer: a #SoupBuffer
255 * Makes a copy of @buffer. In reality, #SoupBuffer is a refcounted
256 * type, and calling soup_buffer_copy() will normally just increment
257 * the refcount on @buffer and return it. However, if @buffer was
258 * created with #SOUP_MEMORY_TEMPORARY memory, then soup_buffer_copy()
259 * will actually return a copy of it, so that the data in the copy
260 * will remain valid after the temporary buffer is freed.
262 * Return value: the new (or newly-reffed) buffer
265 soup_buffer_copy (SoupBuffer *buffer)
267 SoupBufferPrivate *priv = (SoupBufferPrivate *)buffer;
269 /* For non-TEMPORARY buffers, this is just a ref */
270 if (priv->use != SOUP_MEMORY_TEMPORARY) {
275 /* For TEMPORARY buffers, we need to do a real copy the first
276 * time, and then after that, we just keep returning the copy.
277 * We store the copy in priv->owner, which is technically
278 * backwards, but it saves us from having to keep an extra
279 * pointer in SoupBufferPrivate.
283 priv->owner = soup_buffer_new (SOUP_MEMORY_COPY,
286 priv->owner_dnotify = (GDestroyNotify)soup_buffer_free;
288 return soup_buffer_copy (priv->owner);
293 * @buffer: a #SoupBuffer
295 * Frees @buffer. (In reality, as described in the documentation for
296 * soup_buffer_copy(), this is actually an "unref" operation, and may
297 * or may not actually free @buffer.)
300 soup_buffer_free (SoupBuffer *buffer)
302 SoupBufferPrivate *priv = (SoupBufferPrivate *)buffer;
304 if (!--priv->refcount) {
305 if (priv->owner_dnotify)
306 priv->owner_dnotify (priv->owner);
307 g_slice_free (SoupBufferPrivate, priv);
312 * soup_buffer_get_as_bytes:
313 * @buffer: a #SoupBuffer
315 * Returns: (transfer full): a new #GBytes which has the same content
316 * as the #SoupBuffer.
321 soup_buffer_get_as_bytes (SoupBuffer *buffer)
325 copy = soup_buffer_copy (buffer);
326 return g_bytes_new_with_free_func (copy->data, copy->length,
327 (GDestroyNotify)soup_buffer_free,
332 soup_buffer_get_type (void)
334 static volatile gsize type_volatile = 0;
336 if (g_once_init_enter (&type_volatile)) {
337 GType type = g_boxed_type_register_static (
338 g_intern_static_string ("SoupBuffer"),
339 (GBoxedCopyFunc) soup_buffer_copy,
340 (GBoxedFreeFunc) soup_buffer_free);
341 g_once_init_leave (&type_volatile, type);
343 return type_volatile;
350 * @length: length of @data
352 * A #SoupMessage request or response body.
354 * Note that while @length always reflects the full length of the
355 * message body, @data is normally %NULL, and will only be filled in
356 * after soup_message_body_flatten() is called. For client-side
357 * messages, this automatically happens for the response body after it
358 * has been fully read, unless you set the
359 * %SOUP_MESSAGE_OVERWRITE_CHUNKS flags. Likewise, for server-side
360 * messages, the request body is automatically filled in after being
363 * As an added bonus, when @data is filled in, it is always terminated
364 * with a '\0' byte (which is not reflected in @length).
368 SoupMessageBody body;
369 GSList *chunks, *last;
370 SoupBuffer *flattened;
374 } SoupMessageBodyPrivate;
377 * soup_message_body_new:
379 * Creates a new #SoupMessageBody. #SoupMessage uses this internally; you
380 * will not normally need to call it yourself.
382 * Return value: a new #SoupMessageBody.
385 soup_message_body_new (void)
387 SoupMessageBodyPrivate *priv;
389 priv = g_slice_new0 (SoupMessageBodyPrivate);
390 priv->accumulate = TRUE;
393 return (SoupMessageBody *)priv;
397 * soup_message_body_set_accumulate:
398 * @body: a #SoupMessageBody
399 * @accumulate: whether or not to accumulate body chunks in @body
401 * Sets or clears the accumulate flag on @body. (The default value is
402 * %TRUE.) If set to %FALSE, @body's %data field will not be filled in
403 * after the body is fully sent/received, and the chunks that make up
404 * @body may be discarded when they are no longer needed.
406 * In particular, if you set this flag to %FALSE on an "incoming"
407 * message body (that is, the #SoupMessage:response_body of a
408 * client-side message, or #SoupMessage:request_body of a server-side
409 * message), this will cause each chunk of the body to be discarded
410 * after its corresponding #SoupMessage::got_chunk signal is emitted.
411 * (This is equivalent to setting the deprecated
412 * %SOUP_MESSAGE_OVERWRITE_CHUNKS flag on the message.)
414 * If you set this flag to %FALSE on the #SoupMessage:response_body of
415 * a server-side message, it will cause each chunk of the body to be
416 * discarded after its corresponding #SoupMessage::wrote_chunk signal
419 * If you set the flag to %FALSE on the #SoupMessage:request_body of a
420 * client-side message, it will block the accumulation of chunks into
421 * @body's %data field, but it will not normally cause the chunks to
422 * be discarded after being written like in the server-side
423 * #SoupMessage:response_body case, because the request body needs to
424 * be kept around in case the request needs to be sent a second time
425 * due to redirection or authentication. However, if you set the
426 * %SOUP_MESSAGE_CAN_REBUILD flag on the message, then the chunks will
427 * be discarded, and you will be responsible for recreating the
428 * request body after the #SoupMessage::restarted signal is emitted.
433 soup_message_body_set_accumulate (SoupMessageBody *body,
436 SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
438 priv->accumulate = accumulate;
442 * soup_message_body_get_accumulate:
443 * @body: a #SoupMessageBody
445 * Gets the accumulate flag on @body; see
446 * soup_message_body_set_accumulate() for details.
448 * Return value: the accumulate flag for @body.
453 soup_message_body_get_accumulate (SoupMessageBody *body)
455 SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
457 return priv->accumulate;
461 append_buffer (SoupMessageBody *body, SoupBuffer *buffer)
463 SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
466 priv->last = g_slist_append (priv->last, buffer);
467 priv->last = priv->last->next;
469 priv->chunks = priv->last = g_slist_append (NULL, buffer);
471 if (priv->flattened) {
472 soup_buffer_free (priv->flattened);
473 priv->flattened = NULL;
476 body->length += buffer->length;
480 * soup_message_body_append:
481 * @body: a #SoupMessageBody
482 * @use: how to use @data
483 * @data: (array length=length) (element-type guint8): data to append
484 * @length: length of @data
486 * Appends @length bytes from @data to @body according to @use.
489 soup_message_body_append (SoupMessageBody *body, SoupMemoryUse use,
490 gconstpointer data, gsize length)
493 append_buffer (body, soup_buffer_new (use, data, length));
494 else if (use == SOUP_MEMORY_TAKE)
495 g_free ((gpointer)data);
499 * soup_message_body_append_take:
500 * @body: a #SoupMessageBody
501 * @data: (array length=length) (transfer full): data to append
502 * @length: length of @data
504 * Appends @length bytes from @data to @body.
506 * This function is exactly equivalent to soup_message_body_append()
507 * with %SOUP_MEMORY_TAKE as second argument; it exists mainly for
508 * convenience and simplifying language bindings.
510 * Rename to: soup_message_body_append
514 soup_message_body_append_take (SoupMessageBody *body,
515 guchar *data, gsize length)
517 soup_message_body_append(body, SOUP_MEMORY_TAKE, data, length);
521 * soup_message_body_append_buffer:
522 * @body: a #SoupMessageBody
523 * @buffer: a #SoupBuffer
525 * Appends the data from @buffer to @body. (#SoupMessageBody uses
526 * #SoupBuffers internally, so this is normally a constant-time
527 * operation that doesn't actually require copying the data in
531 soup_message_body_append_buffer (SoupMessageBody *body, SoupBuffer *buffer)
533 g_return_if_fail (buffer->length > 0);
534 append_buffer (body, soup_buffer_copy (buffer));
538 * soup_message_body_truncate:
539 * @body: a #SoupMessageBody
541 * Deletes all of the data in @body.
544 soup_message_body_truncate (SoupMessageBody *body)
546 SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
549 for (iter = priv->chunks; iter; iter = iter->next)
550 soup_buffer_free (iter->data);
551 g_slist_free (priv->chunks);
552 priv->chunks = priv->last = NULL;
553 priv->base_offset = 0;
555 if (priv->flattened) {
556 soup_buffer_free (priv->flattened);
557 priv->flattened = NULL;
564 * soup_message_body_complete:
565 * @body: a #SoupMessageBody
567 * Tags @body as being complete; Call this when using chunked encoding
568 * after you have appended the last chunk.
571 soup_message_body_complete (SoupMessageBody *body)
573 append_buffer (body, soup_buffer_new (SOUP_MEMORY_STATIC, NULL, 0));
577 * soup_message_body_flatten:
578 * @body: a #SoupMessageBody
580 * Fills in @body's data field with a buffer containing all of the
581 * data in @body (plus an additional '\0' byte not counted by @body's
584 * Return value: a #SoupBuffer containing the same data as @body.
585 * (You must free this buffer if you do not want it.)
588 soup_message_body_flatten (SoupMessageBody *body)
590 SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
595 g_return_val_if_fail (priv->accumulate == TRUE, NULL);
597 if (!priv->flattened) {
598 #if GLIB_SIZEOF_SIZE_T < 8
599 g_return_val_if_fail (body->length < G_MAXSIZE, NULL);
602 buf = ptr = g_malloc (body->length + 1);
603 for (iter = priv->chunks; iter; iter = iter->next) {
605 memcpy (ptr, chunk->data, chunk->length);
606 ptr += chunk->length;
610 priv->flattened = soup_buffer_new (SOUP_MEMORY_TAKE,
612 body->data = priv->flattened->data;
615 return soup_buffer_copy (priv->flattened);
619 * soup_message_body_get_chunk:
620 * @body: a #SoupMessageBody
623 * Gets a #SoupBuffer containing data from @body starting at @offset.
624 * The size of the returned chunk is unspecified. You can iterate
625 * through the entire body by first calling
626 * soup_message_body_get_chunk() with an offset of 0, and then on each
627 * successive call, increment the offset by the length of the
628 * previously-returned chunk.
630 * If @offset is greater than or equal to the total length of @body,
631 * then the return value depends on whether or not
632 * soup_message_body_complete() has been called or not; if it has,
633 * then soup_message_body_get_chunk() will return a 0-length chunk
634 * (indicating the end of @body). If it has not, then
635 * soup_message_body_get_chunk() will return %NULL (indicating that
636 * @body may still potentially have more data, but that data is not
637 * currently available).
639 * Return value: a #SoupBuffer, or %NULL.
642 soup_message_body_get_chunk (SoupMessageBody *body, goffset offset)
644 SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
646 SoupBuffer *chunk = NULL;
648 offset -= priv->base_offset;
649 for (iter = priv->chunks; iter; iter = iter->next) {
652 if (offset < chunk->length || offset == 0)
655 offset -= chunk->length;
662 return soup_buffer_copy (chunk);
664 return soup_buffer_new_subbuffer (chunk, offset,
665 chunk->length - offset);
670 * soup_message_body_got_chunk:
671 * @body: a #SoupMessageBody
672 * @chunk: a #SoupBuffer received from the network
674 * Handles the #SoupMessageBody part of receiving a chunk of data from
675 * the network. Normally this means appending @chunk to @body, exactly
676 * as with soup_message_body_append_buffer(), but if you have set
677 * @body's accumulate flag to %FALSE, then that will not happen.
679 * This is a low-level method which you should not normally need to
685 soup_message_body_got_chunk (SoupMessageBody *body, SoupBuffer *chunk)
687 SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
689 if (!priv->accumulate)
692 soup_message_body_append_buffer (body, chunk);
696 * soup_message_body_wrote_chunk:
697 * @body: a #SoupMessageBody
698 * @chunk: a #SoupBuffer returned from soup_message_body_get_chunk()
700 * Handles the #SoupMessageBody part of writing a chunk of data to the
701 * network. Normally this is a no-op, but if you have set @body's
702 * accumulate flag to %FALSE, then this will cause @chunk to be
703 * discarded to free up memory.
705 * This is a low-level method which you should not need to use, and
706 * there are further restrictions on its proper use which are not
712 soup_message_body_wrote_chunk (SoupMessageBody *body, SoupBuffer *chunk)
714 SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
717 if (priv->accumulate)
720 chunk2 = priv->chunks->data;
721 g_return_if_fail (chunk->length == chunk2->length);
722 g_return_if_fail (chunk == chunk2 || ((SoupBufferPrivate *)chunk2)->use == SOUP_MEMORY_TEMPORARY);
724 priv->chunks = g_slist_remove (priv->chunks, chunk2);
728 priv->base_offset += chunk2->length;
729 soup_buffer_free (chunk2);
732 static SoupMessageBody *
733 soup_message_body_copy (SoupMessageBody *body)
735 SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
742 * soup_message_body_free:
743 * @body: a #SoupMessageBody
745 * Frees @body. You will not normally need to use this, as
746 * #SoupMessage frees its associated message bodies automatically.
749 soup_message_body_free (SoupMessageBody *body)
751 SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
753 if (--priv->ref_count == 0) {
754 soup_message_body_truncate (body);
755 g_slice_free (SoupMessageBodyPrivate, priv);
760 soup_message_body_get_type (void)
762 static volatile gsize type_volatile = 0;
764 if (g_once_init_enter (&type_volatile)) {
765 GType type = g_boxed_type_register_static (
766 g_intern_static_string ("SoupMessageBody"),
767 (GBoxedCopyFunc) soup_message_body_copy,
768 (GBoxedFreeFunc) soup_message_body_free);
769 g_once_init_leave (&type_volatile, type);
771 return type_volatile;