summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrendan Long <self@brendanlong.com>2014-07-29 17:00:03 (GMT)
committerBrendan Long <self@brendanlong.com>2014-07-29 17:00:03 (GMT)
commitd24a53ab0c0aca66a1e6ea9c96c7045a106b35f9 (patch)
tree95fd9870db7b74dea93dbd9c3d3447045b7fe94c
parent71ad50ae1cbc60d68d585f06394b60f249825a9b (diff)
parent805ff11f8c6d9f1f7adbadd7d71369634b0668a5 (diff)
downloadgst-plugins-dlnasrc-d24a53ab0c0aca66a1e6ea9c96c7045a106b35f9.tar.gz
gst-plugins-dlnasrc-d24a53ab0c0aca66a1e6ea9c96c7045a106b35f9.tar.xz
Merge pull request #135 from gdesmott/util-refactor
Util refactor
-rw-r--r--src/util.c97
-rw-r--r--tests/test-util.c2
2 files changed, 55 insertions, 44 deletions
diff --git a/src/util.c b/src/util.c
index 306e671..82b516a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -109,10 +109,8 @@ dlna_src_parse_npt_range (GstDlnaSrc * dlna_src, const gchar * field_str,
gchar ** start_str, gchar ** stop_str, gchar ** total_str,
guint64 * start, guint64 * stop, guint64 * total)
{
- gchar *header = NULL;
- gchar *header_value = NULL;
-
- gint ret_code = 0;
+ gchar *field, *cursor;
+ gint ret_code;
gchar tmp1[32] = { 0 };
gchar tmp2[32] = { 0 };
gchar tmp3[32] = { 0 };
@@ -128,54 +126,65 @@ dlna_src_parse_npt_range (GstDlnaSrc * dlna_src, const gchar * field_str,
*stop = 0;
*total = 0;
+ /* Convert everything to upper case */
+ field = g_ascii_strup (field_str, -1);
+
/* Extract NPT portion of header value */
- header =
- strstr (g_ascii_strup (field_str, strlen (field_str)), "NPT");
- if (header)
- header_value = strstr (header, "=");
- if (header_value)
- header_value++;
- else {
- GST_WARNING_OBJECT (dlna_src,
- "Problems parsing npt from HEAD response field header value: %s",
- field_str);
- return FALSE;
+ cursor = strstr (field, "NPT");
+ if (!cursor)
+ goto fail;
+
+ cursor = strstr (cursor, "=");
+ if (!cursor)
+ goto fail;
+
+ cursor++; /* '=' */
+
+ /* Read start value and '-' */
+ ret_code = sscanf (cursor, "%31[^-]-%*s", tmp1);
+ if (ret_code == -1)
+ goto fail;
+
+ cursor += strlen (tmp1) + 1;
+
+ *start_str = g_strdup (tmp1);
+ if (!dlna_src_npt_to_nanos (dlna_src, *start_str, start))
+ goto fail;
+
+ /* Read stop value, if any */
+ if (g_ascii_isdigit (cursor[0])) {
+ ret_code = sscanf (cursor, "%31[^/ ]%*s", tmp2);
+ if (ret_code == -1)
+ goto fail;
+
+ cursor += strlen (tmp2);
+
+ *stop_str = g_strdup (tmp2);
+ if (!dlna_src_npt_to_nanos (dlna_src, *stop_str, stop))
+ goto fail;
}
- /* Determine if npt string includes total */
- if (strstr (header_value, "/")) {
- /* Extract start and end and total NPT */
- if ((ret_code =
- sscanf (header_value, "%31[^-]-%31[^/]/%31s %*s", tmp1, tmp2,
- tmp3)) != 3) {
- GST_WARNING_OBJECT (dlna_src,
- "Problems parsing NPT from HEAD response field header %s, value: %s, retcode: %d, tmp: %s, %s, %s",
- field_str, header_value, ret_code, tmp1, tmp2, tmp3);
- return FALSE;
- }
+ /* Do we have the total length? */
+ if (cursor[0] == '/') {
+ cursor++; /* '/' */
+ ret_code = sscanf (cursor, "%31s %*s", tmp3);
+ if (ret_code == -1)
+ goto fail;
*total_str = g_strdup (tmp3);
+
if (strcmp (*total_str, "*") != 0)
if (!dlna_src_npt_to_nanos (dlna_src, *total_str, total))
- return FALSE;
- } else {
- /* Extract start and end (there is no total) NPT */
- if ((ret_code = sscanf (header_value, "%31[^-]-%31s %*s", tmp1, tmp2)) != 2) {
- GST_WARNING_OBJECT (dlna_src,
- "Problems parsing NPT from HEAD response field header %s, value: %s, retcode: %d, tmp: %s, %s",
- field_str, header_value, ret_code, tmp1, tmp2);
- return FALSE;
- }
+ goto fail;
}
- *start_str = g_strdup (tmp1);
- if (!dlna_src_npt_to_nanos (dlna_src, *start_str, start))
- return FALSE;
-
- *stop_str = g_strdup (tmp2);
- if (!dlna_src_npt_to_nanos (dlna_src, *stop_str, stop))
- return FALSE;
+ g_free (field);
return TRUE;
-}
-
+fail:
+ GST_WARNING_OBJECT (dlna_src,
+ "Problems parsing npt from HEAD response field header value: %s",
+ field_str);
+ g_free (field);
+ return FALSE;
+}
diff --git a/tests/test-util.c b/tests/test-util.c
index 84031ce..f685275 100644
--- a/tests/test-util.c
+++ b/tests/test-util.c
@@ -36,6 +36,8 @@ test_parse_ntp_range (void)
"335.1", "336.1", "40445.4", 335099985920, 336099999744, 40445400842240);
do_test_parse_ntp_range ("availableSeekRange.dlna.org: 0 npt=0:00:00.000-0:00:48.716 bytes=0-5219255 cleartextbytes=0-5219255",
"0:00:00.000", "0:00:48.716", NULL, 0, 48716001280, 0);
+ do_test_parse_ntp_range ("npt=10.0-/* bytes=24409920-198755327/198755328",
+ "10.0", NULL, "*", 10000000000, 0, 0);
}
int