2007-06-01 Wim Taymans <wim@fluendo.com>
+ Patch by: Peter Kjellerstedt <pkj at axis com>
+
+ * gst/rtsp/rtspconnection.c: (rtsp_connection_create),
+ (rtsp_connection_connect), (add_date_header),
+ (rtsp_connection_send), (parse_response_status),
+ (parse_request_line), (parse_line), (rtsp_connection_receive):
+ * gst/rtsp/rtspdefs.c: (rtsp_version_as_text):
+ * gst/rtsp/rtspdefs.h:
+ * gst/rtsp/rtspmessage.c: (key_value_foreach),
+ (rtsp_message_init_request), (rtsp_message_init_response),
+ (rtsp_message_remove_header), (rtsp_message_append_headers),
+ (rtsp_message_dump):
+ * gst/rtsp/rtspmessage.h:
+ Improves version checking, allowing an RTSP server to reply with "505
+ RTSP Version not supported.
+ Adds a Date header to all messages.
+ Replies with RTSP_EPARSE rather than RTSP_EINVALID in cases where we
+ want to be able to send a response even if something in the request was
+ invalid. EINVAL is only used when passing wrong arguments to functions.
+ Do not handle an invalid method in parse_request_line(). Defer this to
+ the caller so it can respond with "405 Method Not Allowed".
+ Improves parsing of the timeout parameter to the Session header,
+ allowing whitespace after the semicolon.
+ Avoids a compiler warning due to variables shadowing a function argument.
+
+2007-06-01 Wim Taymans <wim@fluendo.com>
+
Based on Patch by: Daniel Charles <dcharles at ti dot com>
* gst/rtp/gstrtpamrdepay.c: (gst_rtp_amr_depay_setcaps),
g_return_val_if_fail (conn != NULL, RTSP_EINVAL);
- newconn = g_new (RTSPConnection, 1);
+ newconn = g_new0 (RTSPConnection, 1);
#ifdef G_OS_WIN32
/* This should work on UNIX too. PF_UNIX sockets replaced with pipe */
newconn->url = url;
newconn->fd = -1;
- newconn->cseq = 0;
- newconn->session_id[0] = 0;
newconn->timer = g_timer_new ();
newconn->auth_method = RTSP_AUTH_NONE;
rtsp_connection_connect (RTSPConnection * conn, GTimeVal * timeout)
{
gint fd;
- struct sockaddr_in sin;
+ struct sockaddr_in sa_in;
struct hostent *hostinfo;
char **addrs;
gchar *ip;
/* get the port from the url */
rtsp_url_get_port (url, &port);
- memset (&sin, 0, sizeof (sin));
- sin.sin_family = AF_INET; /* network socket */
- sin.sin_port = htons (port); /* on port */
- sin.sin_addr.s_addr = inet_addr (ip); /* on host ip */
+ memset (&sa_in, 0, sizeof (sa_in));
+ sa_in.sin_family = AF_INET; /* network socket */
+ sa_in.sin_port = htons (port); /* on port */
+ sa_in.sin_addr.s_addr = inet_addr (ip); /* on host ip */
fd = socket (AF_INET, SOCK_STREAM, 0);
if (fd == -1)
fcntl (fd, F_SETFL, O_NONBLOCK);
/* we are going to connect ASYNC now */
- ret = connect (fd, (struct sockaddr *) &sin, sizeof (sin));
+ ret = connect (fd, (struct sockaddr *) &sa_in, sizeof (sa_in));
if (ret == 0)
goto done;
if (errno != EINPROGRESS)
}
}
+static void
+add_date_header (RTSPMessage * message)
+{
+ GTimeVal tv;
+ gchar date_string[100];
+
+ g_get_current_time (&tv);
+ strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
+ gmtime (&tv.tv_sec));
+
+ rtsp_message_add_header (message, RTSP_HDR_DATE, date_string);
+}
+
RTSPResult
rtsp_connection_write (RTSPConnection * conn, const guint8 * data, guint size,
GTimeVal * timeout)
rtsp_connection_send (RTSPConnection * conn, RTSPMessage * message,
GTimeVal * timeout)
{
- GString *str = NULL;
+ GString *str;
RTSPResult res;
#ifdef G_OS_WIN32
/* append headers and body */
if (message->type != RTSP_MESSAGE_DATA) {
+ /* add date header */
+ add_date_header (message);
+
/* append headers */
rtsp_message_append_headers (message, str);
static RTSPResult
parse_response_status (gchar * buffer, RTSPMessage * msg)
{
+ RTSPResult res;
gchar versionstr[20];
gchar codestr[4];
gint code;
bptr = buffer;
read_string (versionstr, sizeof (versionstr), &bptr);
- if (strcmp (versionstr, "RTSP/1.0") != 0)
- goto wrong_version;
-
read_string (codestr, sizeof (codestr), &bptr);
code = atoi (codestr);
while (g_ascii_isspace (*bptr))
bptr++;
- rtsp_message_init_response (msg, code, bptr, NULL);
+ if (strcmp (versionstr, "RTSP/1.0") == 0)
+ RTSP_CHECK (rtsp_message_init_response (msg, code, bptr, NULL),
+ parse_error);
+ else if (strncmp (versionstr, "RTSP/", 5) == 0) {
+ RTSP_CHECK (rtsp_message_init_response (msg, code, bptr, NULL),
+ parse_error);
+ msg->type_data.response.version = RTSP_VERSION_INVALID;
+ } else
+ goto parse_error;
return RTSP_OK;
-wrong_version:
+parse_error:
{
- return RTSP_EINVAL;
+ return RTSP_EPARSE;
}
}
static RTSPResult
parse_request_line (gchar * buffer, RTSPMessage * msg)
{
+ RTSPResult res = RTSP_OK;
gchar versionstr[20];
gchar methodstr[20];
gchar urlstr[4096];
read_string (methodstr, sizeof (methodstr), &bptr);
method = rtsp_find_method (methodstr);
- if (method == RTSP_INVALID)
- goto wrong_method;
read_string (urlstr, sizeof (urlstr), &bptr);
+ if (*urlstr == '\0')
+ res = RTSP_EPARSE;
read_string (versionstr, sizeof (versionstr), &bptr);
- if (strcmp (versionstr, "RTSP/1.0") != 0)
- goto wrong_version;
- rtsp_message_init_request (msg, method, urlstr);
+ if (*bptr != '\0')
+ res = RTSP_EPARSE;
- return RTSP_OK;
-
-wrong_method:
- {
- return RTSP_EINVAL;
- }
-wrong_version:
- {
- return RTSP_EINVAL;
+ if (strcmp (versionstr, "RTSP/1.0") == 0) {
+ if (rtsp_message_init_request (msg, method, urlstr) != RTSP_OK)
+ res = RTSP_EPARSE;
+ } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
+ if (rtsp_message_init_request (msg, method, urlstr) != RTSP_OK)
+ res = RTSP_EPARSE;
+ msg->type_data.request.version = RTSP_VERSION_INVALID;
+ } else {
+ rtsp_message_init_request (msg, method, urlstr);
+ msg->type_data.request.version = RTSP_VERSION_INVALID;
+ res = RTSP_EPARSE;
}
+
+ return res;
}
/* parsing lines means reading a Key: Value pair */
no_column:
{
- return RTSP_EINVAL;
+ return RTSP_EPARSE;
}
}
{
gchar buffer[4096];
gint line;
- gchar *hdrval;
glong content_length;
RTSPResult res;
gboolean need_body;
/* read the rest of the body if needed */
if (need_body) {
+ gchar *session_id;
+ gchar *hdrval;
+
/* see if there is a Content-Length header */
if (rtsp_message_get_header (msg, RTSP_HDR_CONTENT_LENGTH,
&hdrval, 0) == RTSP_OK) {
}
/* save session id in the connection for further use */
- {
- gchar *session_id;
-
- if (rtsp_message_get_header (msg, RTSP_HDR_SESSION,
- &session_id, 0) == RTSP_OK) {
- gint sesslen, maxlen, i;
-
- /* default session timeout */
- conn->timeout = 60;
-
- sesslen = strlen (session_id);
- maxlen = sizeof (conn->session_id) - 1;
- /* the sessionid can have attributes marked with ;
- * Make sure we strip them */
- for (i = 0; i < sesslen; i++) {
- if (session_id[i] == ';') {
- maxlen = i;
- /* parse timeout */
- if (g_str_has_prefix (&session_id[i], ";timeout=")) {
- gint timeout;
-
- /* if we parsed something valid, configure */
- if ((timeout = atoi (&session_id[i + 9])) > 0)
- conn->timeout = timeout;
- }
+ if (rtsp_message_get_header (msg, RTSP_HDR_SESSION,
+ &session_id, 0) == RTSP_OK) {
+ gint maxlen, i;
+
+ /* default session timeout */
+ conn->timeout = 60;
+
+ maxlen = sizeof (conn->session_id) - 1;
+ /* the sessionid can have attributes marked with ;
+ * Make sure we strip them */
+ for (i = 0; session_id[i] != '\0'; i++) {
+ if (session_id[i] == ';') {
+ maxlen = i;
+ /* parse timeout */
+ do {
+ i++;
+ } while (g_ascii_isspace (session_id[i]));
+ if (g_str_has_prefix (&session_id[i], "timeout=")) {
+ gint to;
+
+ /* if we parsed something valid, configure */
+ if ((to = atoi (&session_id[i + 9])) > 0)
+ conn->timeout = to;
}
+ break;
}
-
- /* make sure to not overflow */
- strncpy (conn->session_id, session_id, maxlen);
- conn->session_id[maxlen] = '\0';
}
+
+ /* make sure to not overflow */
+ strncpy (conn->session_id, session_id, maxlen);
+ conn->session_id[maxlen] = '\0';
}
}
return res;
msg->type = RTSP_MESSAGE_REQUEST;
msg->type_data.request.method = method;
msg->type_data.request.uri = g_strdup (uri);
+ msg->type_data.request.version = RTSP_VERSION_1_0;
msg->hdr_fields = g_array_new (FALSE, FALSE, sizeof (RTSPKeyValue));
return RTSP_OK;
msg->type = RTSP_MESSAGE_RESPONSE;
msg->type_data.response.code = code;
msg->type_data.response.reason = g_strdup (reason);
+ msg->type_data.response.version = RTSP_VERSION_1_0;
msg->hdr_fields = g_array_new (FALSE, FALSE, sizeof (RTSPKeyValue));
if (request) {
case RTSP_MESSAGE_REQUEST:
g_print ("RTSP request message %p\n", msg);
g_print (" request line:\n");
- g_print (" method: '%s'\n",
+ g_print (" method: '%s'\n",
rtsp_method_as_text (msg->type_data.request.method));
- g_print (" uri: '%s'\n", msg->type_data.request.uri);
+ g_print (" uri: '%s'\n", msg->type_data.request.uri);
+ g_print (" version: '%s'\n",
+ rtsp_version_as_text (msg->type_data.request.version));
g_print (" headers:\n");
key_value_foreach (msg->hdr_fields, dump_key_value, NULL);
g_print (" body:\n");
case RTSP_MESSAGE_RESPONSE:
g_print ("RTSP response message %p\n", msg);
g_print (" status line:\n");
- g_print (" code: '%d'\n", msg->type_data.response.code);
- g_print (" reason: '%s'\n", msg->type_data.response.reason);
+ g_print (" code: '%d'\n", msg->type_data.response.code);
+ g_print (" reason: '%s'\n", msg->type_data.response.reason);
+ g_print (" version: '%s'\n",
+ rtsp_version_as_text (msg->type_data.response.version));
g_print (" headers:\n");
key_value_foreach (msg->hdr_fields, dump_key_value, NULL);
rtsp_message_get_body (msg, &data, &size);