2 * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
4 * <2015> Tim-Philipp Müller <tim@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 * Unless otherwise indicated, Source Code is licensed under MIT license.
23 * See further explanation attached in License Statement (distributed in the file
26 * Permission is hereby granted, free of charge, to any person obtaining a copy of
27 * this software and associated documentation files (the "Software"), to deal in
28 * the Software without restriction, including without limitation the rights to
29 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
30 * of the Software, and to permit persons to whom the Software is furnished to do
31 * so, subject to the following conditions:
33 * The above copyright notice and this permission notice shall be included in all
34 * copies or substantial portions of the Software.
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
46 * SECTION:gstrtspmessage
47 * @title: GstRTSPMessage
48 * @short_description: RTSP messages
49 * @see_also: gstrtspconnection
51 * Provides methods for creating and parsing request, response and data messages.
59 #include <gst/gstutils.h>
60 #include "gstrtspmessage.h"
62 typedef struct _RTSPKeyValue
64 GstRTSPHeaderField field;
66 gchar *custom_key; /* custom header string (field is INVALID then) */
70 key_value_foreach (GArray * array, GFunc func, gpointer user_data)
74 g_return_if_fail (array != NULL);
76 for (i = 0; i < array->len; i++) {
77 (*func) (&g_array_index (array, RTSPKeyValue, i), user_data);
82 key_value_append (const RTSPKeyValue * kv, GArray * array)
85 g_return_if_fail (kv != NULL);
86 g_return_if_fail (array != NULL);
88 kvcopy.field = kv->field;
89 kvcopy.value = g_strdup (kv->value);
90 kvcopy.custom_key = g_strdup (kv->custom_key);
92 g_array_append_val (array, kvcopy);
95 static GstRTSPMessage *
96 gst_rtsp_message_boxed_copy (GstRTSPMessage * orig)
100 if (gst_rtsp_message_copy (orig, ©) == GST_RTSP_OK)
107 gst_rtsp_message_boxed_free (GstRTSPMessage * msg)
109 gst_rtsp_message_free (msg);
112 G_DEFINE_BOXED_TYPE (GstRTSPMessage, gst_rtsp_msg, gst_rtsp_message_boxed_copy,
113 gst_rtsp_message_boxed_free);
116 * gst_rtsp_message_new:
117 * @msg: (out) (transfer full): a location for the new #GstRTSPMessage
119 * Create a new initialized #GstRTSPMessage. Free with gst_rtsp_message_free().
121 * Returns: a #GstRTSPResult.
124 gst_rtsp_message_new (GstRTSPMessage ** msg)
126 GstRTSPMessage *newmsg;
128 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
130 newmsg = g_new0 (GstRTSPMessage, 1);
134 return gst_rtsp_message_init (newmsg);
138 * gst_rtsp_message_init:
139 * @msg: a #GstRTSPMessage
141 * Initialize @msg. This function is mostly used when @msg is allocated on the
142 * stack. The reverse operation of this is gst_rtsp_message_unset().
144 * Returns: a #GstRTSPResult.
147 gst_rtsp_message_init (GstRTSPMessage * msg)
149 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
151 gst_rtsp_message_unset (msg);
153 msg->type = GST_RTSP_MESSAGE_INVALID;
154 msg->hdr_fields = g_array_new (FALSE, FALSE, sizeof (RTSPKeyValue));
160 * gst_rtsp_message_get_type:
161 * @msg: a #GstRTSPMessage
163 * Get the message type of @msg.
165 * Returns: the message type.
168 gst_rtsp_message_get_type (GstRTSPMessage * msg)
170 g_return_val_if_fail (msg != NULL, GST_RTSP_MESSAGE_INVALID);
176 * gst_rtsp_message_new_request:
177 * @msg: (out) (transfer full): a location for the new #GstRTSPMessage
178 * @method: the request method to use
179 * @uri: (transfer none): the uri of the request
181 * Create a new #GstRTSPMessage with @method and @uri and store the result
182 * request message in @msg. Free with gst_rtsp_message_free().
184 * Returns: a #GstRTSPResult.
187 gst_rtsp_message_new_request (GstRTSPMessage ** msg, GstRTSPMethod method,
190 GstRTSPMessage *newmsg;
192 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
193 g_return_val_if_fail (uri != NULL, GST_RTSP_EINVAL);
195 newmsg = g_new0 (GstRTSPMessage, 1);
199 return gst_rtsp_message_init_request (newmsg, method, uri);
203 * gst_rtsp_message_init_request:
204 * @msg: a #GstRTSPMessage
205 * @method: the request method to use
206 * @uri: (transfer none): the uri of the request
208 * Initialize @msg as a request message with @method and @uri. To clear @msg
209 * again, use gst_rtsp_message_unset().
211 * Returns: a #GstRTSPResult.
214 gst_rtsp_message_init_request (GstRTSPMessage * msg, GstRTSPMethod method,
217 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
218 g_return_val_if_fail (uri != NULL, GST_RTSP_EINVAL);
220 gst_rtsp_message_unset (msg);
222 msg->type = GST_RTSP_MESSAGE_REQUEST;
223 msg->type_data.request.method = method;
224 msg->type_data.request.uri = g_strdup (uri);
225 msg->type_data.request.version = GST_RTSP_VERSION_1_0;
226 msg->hdr_fields = g_array_new (FALSE, FALSE, sizeof (RTSPKeyValue));
232 * gst_rtsp_message_parse_request:
233 * @msg: a #GstRTSPMessage
234 * @method: (out) (allow-none): location to hold the method
235 * @uri: (out) (allow-none) (transfer none): location to hold the uri
236 * @version: (out) (allow-none) (transfer none): location to hold the version
238 * Parse the request message @msg and store the values @method, @uri and
239 * @version. The result locations can be %NULL if one is not interested in its
242 * @uri remains valid for as long as @msg is valid and unchanged.
244 * Returns: a #GstRTSPResult.
247 gst_rtsp_message_parse_request (GstRTSPMessage * msg,
248 GstRTSPMethod * method, const gchar ** uri, GstRTSPVersion * version)
250 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
251 g_return_val_if_fail (msg->type == GST_RTSP_MESSAGE_REQUEST ||
252 msg->type == GST_RTSP_MESSAGE_HTTP_REQUEST, GST_RTSP_EINVAL);
255 *method = msg->type_data.request.method;
257 *uri = msg->type_data.request.uri;
259 *version = msg->type_data.request.version;
265 * gst_rtsp_message_new_response:
266 * @msg: (out) (transfer full): a location for the new #GstRTSPMessage
267 * @code: the status code
268 * @reason: (transfer none) (allow-none): the status reason or %NULL
269 * @request: (transfer none) (allow-none): the request that triggered the response or %NULL
271 * Create a new response #GstRTSPMessage with @code and @reason and store the
272 * result message in @msg. Free with gst_rtsp_message_free().
274 * When @reason is %NULL, the default reason for @code will be used.
276 * When @request is not %NULL, the relevant headers will be copied to the new
279 * Returns: a #GstRTSPResult.
282 gst_rtsp_message_new_response (GstRTSPMessage ** msg, GstRTSPStatusCode code,
283 const gchar * reason, const GstRTSPMessage * request)
285 GstRTSPMessage *newmsg;
287 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
289 newmsg = g_new0 (GstRTSPMessage, 1);
293 return gst_rtsp_message_init_response (newmsg, code, reason, request);
297 * gst_rtsp_message_init_response:
298 * @msg: a #GstRTSPMessage
299 * @code: the status code
300 * @reason: (transfer none) (allow-none): the status reason or %NULL
301 * @request: (transfer none) (allow-none): the request that triggered the response or %NULL
303 * Initialize @msg with @code and @reason.
305 * When @reason is %NULL, the default reason for @code will be used.
307 * When @request is not %NULL, the relevant headers will be copied to the new
310 * Returns: a #GstRTSPResult.
313 gst_rtsp_message_init_response (GstRTSPMessage * msg, GstRTSPStatusCode code,
314 const gchar * reason, const GstRTSPMessage * request)
316 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
318 gst_rtsp_message_unset (msg);
321 reason = gst_rtsp_status_as_text (code);
323 msg->type = GST_RTSP_MESSAGE_RESPONSE;
324 msg->type_data.response.code = code;
325 msg->type_data.response.reason = g_strdup (reason);
326 msg->type_data.response.version = GST_RTSP_VERSION_1_0;
327 msg->hdr_fields = g_array_new (FALSE, FALSE, sizeof (RTSPKeyValue));
330 if (request->type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
331 msg->type = GST_RTSP_MESSAGE_HTTP_RESPONSE;
332 if (request->type_data.request.version != GST_RTSP_VERSION_INVALID)
333 msg->type_data.response.version = request->type_data.request.version;
335 msg->type_data.response.version = GST_RTSP_VERSION_1_1;
340 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_CSEQ, &header,
342 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CSEQ, header);
345 /* copy session id */
346 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &header,
350 header = g_strdup (header);
351 if ((pos = strchr (header, ';'))) {
355 gst_rtsp_message_take_header (msg, GST_RTSP_HDR_SESSION, header);
358 /* FIXME copy more headers? */
366 * gst_rtsp_message_parse_response:
367 * @msg: a #GstRTSPMessage
368 * @code: (out) (allow-none): location to hold the status code
369 * @reason: (out) (allow-none) (transfer none): location to hold the status reason
370 * @version: (out) (allow-none) (transfer none): location to hold the version
372 * Parse the response message @msg and store the values @code, @reason and
373 * @version. The result locations can be %NULL if one is not interested in its
376 * @reason remains valid for as long as @msg is valid and unchanged.
378 * Returns: a #GstRTSPResult.
381 gst_rtsp_message_parse_response (GstRTSPMessage * msg,
382 GstRTSPStatusCode * code, const gchar ** reason, GstRTSPVersion * version)
384 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
385 g_return_val_if_fail (msg->type == GST_RTSP_MESSAGE_RESPONSE ||
386 msg->type == GST_RTSP_MESSAGE_HTTP_RESPONSE, GST_RTSP_EINVAL);
389 *code = msg->type_data.response.code;
391 *reason = msg->type_data.response.reason;
393 *version = msg->type_data.response.version;
399 * gst_rtsp_message_new_data:
400 * @msg: (out) (transfer full): a location for the new #GstRTSPMessage
401 * @channel: the channel
403 * Create a new data #GstRTSPMessage with @channel and store the
404 * result message in @msg. Free with gst_rtsp_message_free().
406 * Returns: a #GstRTSPResult.
409 gst_rtsp_message_new_data (GstRTSPMessage ** msg, guint8 channel)
411 GstRTSPMessage *newmsg;
413 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
415 newmsg = g_new0 (GstRTSPMessage, 1);
419 return gst_rtsp_message_init_data (newmsg, channel);
423 * gst_rtsp_message_init_data:
424 * @msg: a #GstRTSPMessage
425 * @channel: a channel
427 * Initialize a new data #GstRTSPMessage for @channel.
429 * Returns: a #GstRTSPResult.
432 gst_rtsp_message_init_data (GstRTSPMessage * msg, guint8 channel)
434 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
436 gst_rtsp_message_unset (msg);
438 msg->type = GST_RTSP_MESSAGE_DATA;
439 msg->type_data.data.channel = channel;
445 * gst_rtsp_message_parse_data:
446 * @msg: a #GstRTSPMessage
447 * @channel: (out): location to hold the channel
449 * Parse the data message @msg and store the channel in @channel.
451 * Returns: a #GstRTSPResult.
454 gst_rtsp_message_parse_data (GstRTSPMessage * msg, guint8 * channel)
456 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
457 g_return_val_if_fail (msg->type == GST_RTSP_MESSAGE_DATA, GST_RTSP_EINVAL);
460 *channel = msg->type_data.data.channel;
466 * gst_rtsp_message_unset:
467 * @msg: a #GstRTSPMessage
469 * Unset the contents of @msg so that it becomes an uninitialized
470 * #GstRTSPMessage again. This function is mostly used in combination with
471 * gst_rtsp_message_init_request(), gst_rtsp_message_init_response() and
472 * gst_rtsp_message_init_data() on stack allocated #GstRTSPMessage structures.
474 * Returns: #GST_RTSP_OK.
477 gst_rtsp_message_unset (GstRTSPMessage * msg)
479 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
482 case GST_RTSP_MESSAGE_INVALID:
484 case GST_RTSP_MESSAGE_REQUEST:
485 case GST_RTSP_MESSAGE_HTTP_REQUEST:
486 g_free (msg->type_data.request.uri);
488 case GST_RTSP_MESSAGE_RESPONSE:
489 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
490 g_free (msg->type_data.response.reason);
492 case GST_RTSP_MESSAGE_DATA:
495 g_return_val_if_reached (GST_RTSP_EINVAL);
498 if (msg->hdr_fields != NULL) {
501 for (i = 0; i < msg->hdr_fields->len; i++) {
502 RTSPKeyValue *keyval = &g_array_index (msg->hdr_fields, RTSPKeyValue, i);
504 g_free (keyval->value);
505 g_free (keyval->custom_key);
507 g_array_free (msg->hdr_fields, TRUE);
510 gst_buffer_replace (&msg->body_buffer, NULL);
512 memset (msg, 0, sizeof (GstRTSPMessage));
518 * gst_rtsp_message_free:
519 * @msg: a #GstRTSPMessage
521 * Free the memory used by @msg.
523 * Returns: a #GstRTSPResult.
526 gst_rtsp_message_free (GstRTSPMessage * msg)
530 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
532 res = gst_rtsp_message_unset (msg);
533 if (res == GST_RTSP_OK)
540 * gst_rtsp_message_copy:
541 * @msg: a #GstRTSPMessage
542 * @copy: (out) (transfer full): pointer to new #GstRTSPMessage
544 * Allocate a new copy of @msg and store the result in @copy. The value in
545 * @copy should be release with gst_rtsp_message_free function.
547 * Returns: a #GstRTSPResult
552 gst_rtsp_message_copy (const GstRTSPMessage * msg, GstRTSPMessage ** copy)
558 return GST_RTSP_EINVAL;
560 ret = gst_rtsp_message_new (copy);
561 if (ret != GST_RTSP_OK)
566 cp->type = msg->type;
568 case GST_RTSP_MESSAGE_INVALID:
570 case GST_RTSP_MESSAGE_REQUEST:
571 case GST_RTSP_MESSAGE_HTTP_REQUEST:
572 cp->type_data.request.method = msg->type_data.request.method;
573 cp->type_data.request.uri = g_strdup (msg->type_data.request.uri);
574 cp->type_data.request.version = msg->type_data.request.version;
576 case GST_RTSP_MESSAGE_RESPONSE:
577 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
578 cp->type_data.response.code = msg->type_data.response.code;
579 cp->type_data.response.reason = g_strdup (msg->type_data.response.reason);
580 cp->type_data.response.version = msg->type_data.response.version;
582 case GST_RTSP_MESSAGE_DATA:
583 cp->type_data.data.channel = msg->type_data.data.channel;
586 return GST_RTSP_EINVAL;
589 key_value_foreach (msg->hdr_fields, (GFunc) key_value_append, cp->hdr_fields);
591 gst_rtsp_message_set_body (cp, msg->body, msg->body_size);
593 gst_rtsp_message_set_body_buffer (cp, msg->body_buffer);
600 * gst_rtsp_message_take_header:
601 * @msg: a #GstRTSPMessage
602 * @field: a #GstRTSPHeaderField
603 * @value: (transfer full): the value of the header
605 * Add a header with key @field and @value to @msg. This function takes
606 * ownership of @value.
608 * Returns: a #GstRTSPResult.
611 gst_rtsp_message_take_header (GstRTSPMessage * msg, GstRTSPHeaderField field,
614 RTSPKeyValue key_value;
616 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
617 g_return_val_if_fail (value != NULL, GST_RTSP_EINVAL);
619 key_value.field = field;
620 key_value.value = value;
621 key_value.custom_key = NULL;
623 g_array_append_val (msg->hdr_fields, key_value);
629 * gst_rtsp_message_add_header:
630 * @msg: a #GstRTSPMessage
631 * @field: a #GstRTSPHeaderField
632 * @value: (transfer none): the value of the header
634 * Add a header with key @field and @value to @msg. This function takes a copy
637 * Returns: a #GstRTSPResult.
640 gst_rtsp_message_add_header (GstRTSPMessage * msg, GstRTSPHeaderField field,
643 return gst_rtsp_message_take_header (msg, field, g_strdup (value));
647 * gst_rtsp_message_remove_header:
648 * @msg: a #GstRTSPMessage
649 * @field: a #GstRTSPHeaderField
650 * @indx: the index of the header
652 * Remove the @indx header with key @field from @msg. If @indx equals -1, all
653 * headers will be removed.
655 * Returns: a #GstRTSPResult.
658 gst_rtsp_message_remove_header (GstRTSPMessage * msg, GstRTSPHeaderField field,
661 GstRTSPResult res = GST_RTSP_ENOTIMPL;
665 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
667 while (i < msg->hdr_fields->len) {
668 RTSPKeyValue *key_value = &g_array_index (msg->hdr_fields, RTSPKeyValue, i);
670 if (key_value->field == field && (indx == -1 || cnt++ == indx)) {
671 g_free (key_value->value);
672 g_array_remove_index (msg->hdr_fields, i);
684 * gst_rtsp_message_get_header:
685 * @msg: a #GstRTSPMessage
686 * @field: a #GstRTSPHeaderField
687 * @value: (out) (transfer none): pointer to hold the result
688 * @indx: the index of the header
690 * Get the @indx header value with key @field from @msg. The result in @value
691 * stays valid as long as it remains present in @msg.
693 * Returns: #GST_RTSP_OK when @field was found, #GST_RTSP_ENOTIMPL if the key
697 gst_rtsp_message_get_header (const GstRTSPMessage * msg,
698 GstRTSPHeaderField field, gchar ** value, gint indx)
703 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
705 /* no header initialized, there are no headers */
706 if (msg->hdr_fields == NULL)
707 return GST_RTSP_ENOTIMPL;
709 for (i = 0; i < msg->hdr_fields->len; i++) {
710 RTSPKeyValue *key_value = &g_array_index (msg->hdr_fields, RTSPKeyValue, i);
712 if (key_value->field == field && cnt++ == indx) {
714 *value = key_value->value;
719 return GST_RTSP_ENOTIMPL;
723 * gst_rtsp_message_add_header_by_name:
724 * @msg: a #GstRTSPMessage
725 * @header: (transfer none): header string
726 * @value: (transfer none): the value of the header
728 * Add a header with key @header and @value to @msg. This function takes a copy
731 * Returns: a #GstRTSPResult.
736 gst_rtsp_message_add_header_by_name (GstRTSPMessage * msg,
737 const gchar * header, const gchar * value)
739 GstRTSPHeaderField field;
741 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
742 g_return_val_if_fail (header != NULL, GST_RTSP_EINVAL);
743 g_return_val_if_fail (value != NULL, GST_RTSP_EINVAL);
745 field = gst_rtsp_find_header_field (header);
746 if (field != GST_RTSP_HDR_INVALID)
747 return gst_rtsp_message_take_header (msg, field, g_strdup (value));
749 return gst_rtsp_message_take_header_by_name (msg, header, g_strdup (value));
753 * gst_rtsp_message_take_header_by_name:
754 * @msg: a #GstRTSPMessage
755 * @header: (transfer none): a header string
756 * @value: (transfer full): the value of the header
758 * Add a header with key @header and @value to @msg. This function takes
759 * ownership of @value, but not of @header.
761 * Returns: a #GstRTSPResult.
766 gst_rtsp_message_take_header_by_name (GstRTSPMessage * msg,
767 const gchar * header, gchar * value)
769 RTSPKeyValue key_value;
771 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
772 g_return_val_if_fail (header != NULL, GST_RTSP_EINVAL);
773 g_return_val_if_fail (value != NULL, GST_RTSP_EINVAL);
775 key_value.field = GST_RTSP_HDR_INVALID;
776 key_value.value = value;
777 key_value.custom_key = g_strdup (header);
779 g_array_append_val (msg->hdr_fields, key_value);
784 /* returns -1 if not found, otherwise index position within msg->hdr_fields */
786 gst_rtsp_message_find_header_by_name (GstRTSPMessage * msg,
787 const gchar * header, gint index)
789 GstRTSPHeaderField field;
793 /* no header initialized, there are no headers */
794 if (msg->hdr_fields == NULL)
797 field = gst_rtsp_find_header_field (header);
798 for (i = 0; i < msg->hdr_fields->len; i++) {
799 RTSPKeyValue *key_val;
801 key_val = &g_array_index (msg->hdr_fields, RTSPKeyValue, i);
803 if (key_val->field != field)
806 if (key_val->custom_key != NULL &&
807 g_ascii_strcasecmp (key_val->custom_key, header) != 0)
810 if (index < 0 || cnt++ == index)
818 * gst_rtsp_message_remove_header_by_name:
819 * @msg: a #GstRTSPMessage
820 * @header: the header string
821 * @index: the index of the header
823 * Remove the @index header with key @header from @msg. If @index equals -1,
824 * all matching headers will be removed.
826 * Returns: a #GstRTSPResult
831 gst_rtsp_message_remove_header_by_name (GstRTSPMessage * msg,
832 const gchar * header, gint index)
834 GstRTSPResult res = GST_RTSP_ENOTIMPL;
838 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
839 g_return_val_if_fail (header != NULL, GST_RTSP_EINVAL);
842 pos = gst_rtsp_message_find_header_by_name (msg, header, index);
847 kv = &g_array_index (msg->hdr_fields, RTSPKeyValue, pos);
849 g_free (kv->custom_key);
850 g_array_remove_index (msg->hdr_fields, pos);
858 * gst_rtsp_message_get_header_by_name:
859 * @msg: a #GstRTSPMessage
860 * @header: a #GstRTSPHeaderField
861 * @value: (out) (transfer none): pointer to hold the result
862 * @index: the index of the header
864 * Get the @index header value with key @header from @msg. The result in @value
865 * stays valid as long as it remains present in @msg.
867 * Returns: #GST_RTSP_OK when @field was found, #GST_RTSP_ENOTIMPL if the key
873 gst_rtsp_message_get_header_by_name (GstRTSPMessage * msg,
874 const gchar * header, gchar ** value, gint index)
876 RTSPKeyValue *key_val;
879 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
880 g_return_val_if_fail (header != NULL, GST_RTSP_EINVAL);
882 pos = gst_rtsp_message_find_header_by_name (msg, header, index);
885 return GST_RTSP_ENOTIMPL;
887 key_val = &g_array_index (msg->hdr_fields, RTSPKeyValue, pos);
890 *value = key_val->value;
896 * gst_rtsp_message_append_headers:
897 * @msg: a #GstRTSPMessage
898 * @str: (transfer none): a string
900 * Append the currently configured headers in @msg to the #GString @str suitable
903 * Returns: #GST_RTSP_OK.
906 gst_rtsp_message_append_headers (const GstRTSPMessage * msg, GString * str)
910 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
911 g_return_val_if_fail (str != NULL, GST_RTSP_EINVAL);
913 for (i = 0; i < msg->hdr_fields->len; i++) {
914 RTSPKeyValue *key_value;
917 key_value = &g_array_index (msg->hdr_fields, RTSPKeyValue, i);
919 if (key_value->custom_key != NULL)
920 keystr = key_value->custom_key;
922 keystr = gst_rtsp_header_as_text (key_value->field);
924 g_string_append_printf (str, "%s: %s\r\n", keystr, key_value->value);
930 * gst_rtsp_message_set_body:
931 * @msg: a #GstRTSPMessage
932 * @data: (array length=size) (transfer none): the data
933 * @size: the size of @data
935 * Set the body of @msg to a copy of @data. Any existing body or body buffer
936 * will be replaced by the new body.
938 * Returns: #GST_RTSP_OK.
941 gst_rtsp_message_set_body (GstRTSPMessage * msg, const guint8 * data,
944 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
946 return gst_rtsp_message_take_body (msg, g_memdup2 (data, size), size);
950 * gst_rtsp_message_take_body:
951 * @msg: a #GstRTSPMessage
952 * @data: (array length=size) (transfer full): the data
953 * @size: the size of @data
955 * Set the body of @msg to @data and @size. This method takes ownership of
956 * @data. Any existing body or body buffer will be replaced by the new body.
958 * Returns: #GST_RTSP_OK.
961 gst_rtsp_message_take_body (GstRTSPMessage * msg, guint8 * data, guint size)
963 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
964 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
966 gst_buffer_replace (&msg->body_buffer, NULL);
970 msg->body_size = size;
976 * gst_rtsp_message_get_body:
977 * @msg: a #GstRTSPMessage
978 * @data: (out) (transfer none) (array length=size): location for the data
979 * @size: (out): location for the size of @data
981 * Get the body of @msg. @data remains valid for as long as @msg is valid and
984 * If the message body was set as a #GstBuffer before this will cause the data
985 * to be copied and stored in the message. The #GstBuffer will no longer be
986 * kept in the message.
988 * Returns: #GST_RTSP_OK.
991 gst_rtsp_message_get_body (const GstRTSPMessage * msg, guint8 ** data,
994 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
995 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
996 g_return_val_if_fail (size != NULL, GST_RTSP_EINVAL);
998 if (msg->body_buffer) {
1001 gst_buffer_extract_dup (msg->body_buffer, 0,
1002 gst_buffer_get_size (msg->body_buffer),
1003 (gpointer *) & ((GstRTSPMessage *) msg)->body, &size);
1004 gst_buffer_replace (&((GstRTSPMessage *) msg)->body_buffer, NULL);
1005 ((GstRTSPMessage *) msg)->body_size = size;
1009 *size = msg->body_size;
1015 * gst_rtsp_message_steal_body:
1016 * @msg: a #GstRTSPMessage
1017 * @data: (out) (transfer full) (array length=size): location for the data
1018 * @size: (out): location for the size of @data
1020 * Take the body of @msg and store it in @data and @size. After this method,
1021 * the body and size of @msg will be set to %NULL and 0 respectively.
1023 * Returns: #GST_RTSP_OK.
1026 gst_rtsp_message_steal_body (GstRTSPMessage * msg, guint8 ** data, guint * size)
1028 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
1029 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1030 g_return_val_if_fail (size != NULL, GST_RTSP_EINVAL);
1032 if (msg->body_buffer) {
1035 gst_buffer_extract_dup (msg->body_buffer, 0,
1036 gst_buffer_get_size (msg->body_buffer),
1037 (gpointer *) & msg->body, &size);
1038 gst_buffer_replace (&msg->body_buffer, NULL);
1039 msg->body_size = size;
1043 *size = msg->body_size;
1052 * gst_rtsp_message_set_body_buffer:
1053 * @msg: a #GstRTSPMessage
1054 * @buffer: a #GstBuffer
1056 * Set the body of @msg to @buffer. Any existing body or body buffer
1057 * will be replaced by the new body.
1059 * Returns: #GST_RTSP_OK.
1064 gst_rtsp_message_set_body_buffer (GstRTSPMessage * msg, GstBuffer * buffer)
1066 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
1068 return gst_rtsp_message_take_body_buffer (msg,
1069 buffer ? gst_buffer_ref (buffer) : NULL);
1073 * gst_rtsp_message_take_body_buffer:
1074 * @msg: a #GstRTSPMessage
1075 * @buffer: (transfer full): a #GstBuffer
1077 * Set the body of @msg to @buffer. This method takes ownership of @buffer.
1078 * Any existing body or body buffer will be replaced by the new body.
1080 * Returns: #GST_RTSP_OK.
1085 gst_rtsp_message_take_body_buffer (GstRTSPMessage * msg, GstBuffer * buffer)
1087 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
1091 if (msg->body_buffer)
1092 gst_buffer_unref (msg->body_buffer);
1093 msg->body_buffer = buffer;
1094 msg->body_size = buffer ? gst_buffer_get_size (buffer) : 0;
1100 * gst_rtsp_message_get_body_buffer:
1101 * @msg: a #GstRTSPMessage
1102 * @buffer: (out) (transfer none): location for the buffer
1104 * Get the body of @msg. @buffer remains valid for as long as @msg is valid and
1107 * If body data was set from raw memory instead of a #GstBuffer this function
1108 * will always return %NULL. The caller can check if there is a body buffer by
1109 * calling gst_rtsp_message_has_body_buffer().
1111 * Returns: #GST_RTSP_OK.
1116 gst_rtsp_message_get_body_buffer (const GstRTSPMessage * msg,
1117 GstBuffer ** buffer)
1119 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
1120 g_return_val_if_fail (buffer != NULL, GST_RTSP_EINVAL);
1122 *buffer = msg->body_buffer;
1128 * gst_rtsp_message_steal_body_buffer:
1129 * @msg: a #GstRTSPMessage
1130 * @buffer: (out) (transfer full): location for the buffer
1132 * Take the body of @msg and store it in @buffer. After this method,
1133 * the body and size of @msg will be set to %NULL and 0 respectively.
1135 * If body data was set from raw memory instead of a #GstBuffer this function
1136 * will always return %NULL. The caller can check if there is a body buffer by
1137 * calling gst_rtsp_message_has_body_buffer().
1139 * Returns: #GST_RTSP_OK.
1144 gst_rtsp_message_steal_body_buffer (GstRTSPMessage * msg, GstBuffer ** buffer)
1146 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
1147 g_return_val_if_fail (buffer != NULL, GST_RTSP_EINVAL);
1149 if (msg->body_buffer) {
1150 *buffer = msg->body_buffer;
1151 msg->body_buffer = NULL;
1161 * gst_rtsp_message_has_body_buffer:
1162 * @msg: a #GstRTSPMessage
1164 * Checks if @msg has a body and the body is stored as #GstBuffer.
1166 * Returns: %TRUE if @msg has a body and it's stored as #GstBuffer, %FALSE
1172 gst_rtsp_message_has_body_buffer (const GstRTSPMessage * msg)
1174 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
1176 return msg->body_buffer != NULL;
1180 dump_key_value (gpointer data, gpointer user_data G_GNUC_UNUSED)
1182 RTSPKeyValue *key_value = (RTSPKeyValue *) data;
1183 const gchar *key_string;
1185 if (key_value->custom_key != NULL)
1186 key_string = key_value->custom_key;
1188 key_string = gst_rtsp_header_as_text (key_value->field);
1190 g_print (" key: '%s', value: '%s'\n", key_string, key_value->value);
1194 * gst_rtsp_message_dump:
1195 * @msg: a #GstRTSPMessage
1197 * Dump the contents of @msg to stdout.
1199 * Returns: #GST_RTSP_OK.
1202 gst_rtsp_message_dump (GstRTSPMessage * msg)
1204 guint8 *data = NULL;
1206 GstBuffer *body_buffer = NULL;
1208 #define PRINT_BODY G_STMT_START { \
1209 gst_rtsp_message_get_body_buffer (msg, &body_buffer); \
1210 if (body_buffer) { \
1211 gst_util_dump_buffer (body_buffer); \
1213 gst_rtsp_message_get_body (msg, &data, &size); \
1215 gst_util_dump_mem (data, size); \
1219 g_return_val_if_fail (msg != NULL, GST_RTSP_EINVAL);
1221 switch (msg->type) {
1222 case GST_RTSP_MESSAGE_REQUEST:
1223 g_print ("RTSP request message %p\n", msg);
1224 g_print (" request line:\n");
1225 g_print (" method: '%s'\n",
1226 gst_rtsp_method_as_text (msg->type_data.request.method));
1227 g_print (" uri: '%s'\n", msg->type_data.request.uri);
1228 g_print (" version: '%s'\n",
1229 gst_rtsp_version_as_text (msg->type_data.request.version));
1230 g_print (" headers:\n");
1231 key_value_foreach (msg->hdr_fields, dump_key_value, NULL);
1232 g_print (" body:\n");
1235 case GST_RTSP_MESSAGE_RESPONSE:
1236 g_print ("RTSP response message %p\n", msg);
1237 g_print (" status line:\n");
1238 g_print (" code: '%d'\n", msg->type_data.response.code);
1239 g_print (" reason: '%s'\n", msg->type_data.response.reason);
1240 g_print (" version: '%s'\n",
1241 gst_rtsp_version_as_text (msg->type_data.response.version));
1242 g_print (" headers:\n");
1243 key_value_foreach (msg->hdr_fields, dump_key_value, NULL);
1246 case GST_RTSP_MESSAGE_HTTP_REQUEST:
1247 g_print ("HTTP request message %p\n", msg);
1248 g_print (" request line:\n");
1249 g_print (" method: '%s'\n",
1250 gst_rtsp_method_as_text (msg->type_data.request.method));
1251 g_print (" uri: '%s'\n", msg->type_data.request.uri);
1252 g_print (" version: '%s'\n",
1253 gst_rtsp_version_as_text (msg->type_data.request.version));
1254 g_print (" headers:\n");
1255 key_value_foreach (msg->hdr_fields, dump_key_value, NULL);
1256 g_print (" body:\n");
1259 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
1260 g_print ("HTTP response message %p\n", msg);
1261 g_print (" status line:\n");
1262 g_print (" code: '%d'\n", msg->type_data.response.code);
1263 g_print (" reason: '%s'\n", msg->type_data.response.reason);
1264 g_print (" version: '%s'\n",
1265 gst_rtsp_version_as_text (msg->type_data.response.version));
1266 g_print (" headers:\n");
1267 key_value_foreach (msg->hdr_fields, dump_key_value, NULL);
1270 case GST_RTSP_MESSAGE_DATA:
1271 g_print ("RTSP data message %p\n", msg);
1272 g_print (" channel: '%d'\n", msg->type_data.data.channel);
1273 g_print (" size: '%d'\n", msg->body_size);
1277 g_print ("unsupported message type %d\n", msg->type);
1278 return GST_RTSP_EINVAL;
1286 static const gchar *
1287 skip_lws (const gchar * s)
1289 while (g_ascii_isspace (*s))
1294 static const gchar *
1295 skip_commas (const gchar * s)
1297 /* The grammar allows for multiple commas */
1298 while (g_ascii_isspace (*s) || *s == ',')
1303 static const gchar *
1304 skip_scheme (const gchar * s)
1306 while (*s && !g_ascii_isspace (*s))
1311 static const gchar *
1312 skip_item (const gchar * s)
1314 gboolean quoted = FALSE;
1316 /* A list item ends at the last non-whitespace character
1317 * before a comma which is not inside a quoted-string. Or at
1318 * the end of the string.
1323 } else if (quoted) {
1324 if (*s == '\\' && *(s + 1))
1327 if (*s == ',' || g_ascii_isspace (*s))
1337 decode_quoted_string (gchar * quoted_string)
1341 src = quoted_string + 1;
1342 dst = quoted_string;
1343 while (*src && *src != '"') {
1344 if (*src == '\\' && *(src + 1))
1352 parse_auth_credentials (GPtrArray * auth_credentials, const gchar * header,
1353 GstRTSPHeaderField field)
1355 while (header[0] != '\0') {
1357 GstRTSPAuthCredential *auth_credential;
1359 /* Skip whitespace at the start of the string */
1360 header = skip_lws (header);
1361 if (header[0] == '\0')
1364 /* Skip until end of string or whitespace: end of scheme */
1365 end = skip_scheme (header);
1367 auth_credential = g_new0 (GstRTSPAuthCredential, 1);
1369 if (g_ascii_strncasecmp (header, "basic", 5) == 0) {
1370 auth_credential->scheme = GST_RTSP_AUTH_BASIC;
1371 } else if (g_ascii_strncasecmp (header, "digest", 6) == 0) {
1372 auth_credential->scheme = GST_RTSP_AUTH_DIGEST;
1374 /* Not supported, skip */
1375 g_free (auth_credential);
1380 /* Basic Authorization request has only an unformatted blurb following, all
1381 * other variants have comma-separated name=value pairs */
1382 if (end[0] != '\0' && field == GST_RTSP_HDR_AUTHORIZATION
1383 && auth_credential->scheme == GST_RTSP_AUTH_BASIC) {
1384 auth_credential->authorization = g_strdup (end + 1);
1386 } else if (end[0] != '\0') {
1389 params = g_ptr_array_new ();
1391 /* Space or start of param */
1394 /* Parse a header whose content is described by RFC2616 as
1395 * "#something", where "something" does not itself contain commas,
1396 * except as part of quoted-strings, into a list of allocated strings.
1399 const gchar *item_end;
1402 header = skip_commas (header);
1403 item_end = skip_item (header);
1405 for (eq = header; *eq != '\0' && *eq != '=' && eq < item_end; eq++);
1407 GstRTSPAuthParam *auth_param = g_new0 (GstRTSPAuthParam, 1);
1410 /* have an actual param */
1411 auth_param->name = g_strndup (header, eq - header);
1414 value = skip_lws (value);
1415 auth_param->value = g_strndup (value, item_end - value);
1416 if (value[0] == '"')
1417 decode_quoted_string (auth_param->value);
1419 g_ptr_array_add (params, auth_param);
1422 /* at next scheme, header at start of it */
1427 g_ptr_array_add (params, NULL);
1428 auth_credential->params =
1429 (GstRTSPAuthParam **) g_ptr_array_free (params, FALSE);
1433 g_ptr_array_add (auth_credentials, auth_credential);
1435 /* WWW-Authenticate allows multiple, Authorization allows one */
1436 if (field == GST_RTSP_HDR_AUTHORIZATION)
1442 * gst_rtsp_message_parse_auth_credentials:
1443 * @msg: a #GstRTSPMessage
1444 * @field: a #GstRTSPHeaderField
1446 * Parses the credentials given in a WWW-Authenticate or Authorization header.
1448 * Returns: (array zero-terminated=1):
1449 * %NULL-terminated array of GstRTSPAuthCredential or %NULL.
1453 GstRTSPAuthCredential **
1454 gst_rtsp_message_parse_auth_credentials (GstRTSPMessage * msg,
1455 GstRTSPHeaderField field)
1458 GPtrArray *auth_credentials;
1461 g_return_val_if_fail (msg != NULL, NULL);
1463 auth_credentials = g_ptr_array_new ();
1466 while (gst_rtsp_message_get_header (msg, field, &header, i) == GST_RTSP_OK) {
1467 parse_auth_credentials (auth_credentials, header, field);
1471 if (auth_credentials->len)
1472 g_ptr_array_add (auth_credentials, NULL);
1474 return (GstRTSPAuthCredential **) g_ptr_array_free (auth_credentials, FALSE);
1478 gst_rtsp_auth_param_copy (GstRTSPAuthParam * param)
1480 GstRTSPAuthParam *copy;
1485 copy = g_new0 (GstRTSPAuthParam, 1);
1486 copy->name = g_strdup (param->name);
1487 copy->value = g_strdup (param->value);
1493 gst_rtsp_auth_param_free (GstRTSPAuthParam * param)
1495 if (param != NULL) {
1496 g_free (param->name);
1497 g_free (param->value);
1502 G_DEFINE_BOXED_TYPE (GstRTSPAuthParam, gst_rtsp_auth_param,
1503 (GBoxedCopyFunc) gst_rtsp_auth_param_copy,
1504 (GBoxedFreeFunc) gst_rtsp_auth_param_free);
1507 gst_rtsp_auth_credential_free (GstRTSPAuthCredential * credential)
1509 GstRTSPAuthParam **p;
1511 if (credential == NULL)
1514 for (p = credential->params; p != NULL && *p != NULL; ++p)
1515 gst_rtsp_auth_param_free (*p);
1517 g_free (credential->params);
1518 g_free (credential->authorization);
1519 g_free (credential);
1522 static GstRTSPAuthCredential *
1523 gst_rtsp_auth_credential_copy (GstRTSPAuthCredential * cred)
1525 GstRTSPAuthCredential *copy;
1530 copy = g_new0 (GstRTSPAuthCredential, 1);
1531 copy->scheme = cred->scheme;
1533 guint i, n_params = g_strv_length ((gchar **) cred->params);
1535 copy->params = g_new0 (GstRTSPAuthParam *, n_params + 1);
1536 for (i = 0; i < n_params; ++i)
1537 copy->params[i] = gst_rtsp_auth_param_copy (cred->params[i]);
1539 copy->authorization = g_strdup (cred->authorization);
1544 * gst_rtsp_auth_credentials_free:
1545 * @credentials: a %NULL-terminated array of #GstRTSPAuthCredential
1547 * Free a %NULL-terminated array of credentials returned from
1548 * gst_rtsp_message_parse_auth_credentials().
1553 gst_rtsp_auth_credentials_free (GstRTSPAuthCredential ** credentials)
1555 GstRTSPAuthCredential **p;
1560 for (p = credentials; p != NULL && *p != NULL; ++p)
1561 gst_rtsp_auth_credential_free (*p);
1563 g_free (credentials);
1566 G_DEFINE_BOXED_TYPE (GstRTSPAuthCredential, gst_rtsp_auth_credential,
1567 (GBoxedCopyFunc) gst_rtsp_auth_credential_copy,
1568 (GBoxedFreeFunc) gst_rtsp_auth_credential_free);