[02/10] lib/url: Check URL file extensions

Message ID 20170825055940.24134-3-sam@mendozajonas.com
State New
Headers show

Commit Message

Samuel Mendoza-Jonas Aug. 25, 2017, 5:59 a.m.
Add pb_url_check_extension() which compares a pb_url against a given
extension string.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
---
 lib/url/url.c | 15 +++++++++++++++
 lib/url/url.h |  1 +
 2 files changed, 16 insertions(+)

Comments

Cyril Bur Aug. 29, 2017, 4:43 a.m. | #1
On Fri, 2017-08-25 at 15:59 +1000, Samuel Mendoza-Jonas wrote:
> Add pb_url_check_extension() which compares a pb_url against a given
> extension string.
> 

Looks like it works just fine, very much a shame libc doesn't provide a
strrstr().

A strstr() based version could be worth considering but meh.

Reviewed-by: Cyril Bur <cyrilbur@gmail.com>

> Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
> ---
>  lib/url/url.c | 15 +++++++++++++++
>  lib/url/url.h |  1 +
>  2 files changed, 16 insertions(+)
> 
> diff --git a/lib/url/url.c b/lib/url/url.c
> index 6eeced3..4908122 100644
> --- a/lib/url/url.c
> +++ b/lib/url/url.c
> @@ -300,3 +300,18 @@ const char *pb_url_scheme_name(enum pb_url_scheme scheme)
>  	const struct pb_scheme_info *info = pb_url_scheme_info(scheme);
>  	return info ? info->str : NULL;
>  }
> +
> +bool pb_url_check_extension(const struct pb_url *pb_url, const char *ext)
> +{
> +	char *pos;
> +
> +	if (!pb_url || !pb_url->file || !ext)
> +		return false;
> +
> +	if (strlen(pb_url->file) < strlen(ext))
> +		return false;
> +
> +	pos = pb_url->file + strlen(pb_url->file) - strlen(ext);
> +
> +	return strncmp(pos, ext, strlen(ext)) == 0;
> +}
> diff --git a/lib/url/url.h b/lib/url/url.h
> index 9043615..6334763 100644
> --- a/lib/url/url.h
> +++ b/lib/url/url.h
> @@ -67,5 +67,6 @@ struct pb_url *pb_url_join(void *ctx, const struct pb_url *url, const char *s);
>  char *pb_url_to_string(struct pb_url *url);
>  
>  const char *pb_url_scheme_name(enum pb_url_scheme scheme);
> +bool pb_url_check_extension(const struct pb_url *pb_url, const char *ext);
>  
>  #endif

Patch

diff --git a/lib/url/url.c b/lib/url/url.c
index 6eeced3..4908122 100644
--- a/lib/url/url.c
+++ b/lib/url/url.c
@@ -300,3 +300,18 @@  const char *pb_url_scheme_name(enum pb_url_scheme scheme)
 	const struct pb_scheme_info *info = pb_url_scheme_info(scheme);
 	return info ? info->str : NULL;
 }
+
+bool pb_url_check_extension(const struct pb_url *pb_url, const char *ext)
+{
+	char *pos;
+
+	if (!pb_url || !pb_url->file || !ext)
+		return false;
+
+	if (strlen(pb_url->file) < strlen(ext))
+		return false;
+
+	pos = pb_url->file + strlen(pb_url->file) - strlen(ext);
+
+	return strncmp(pos, ext, strlen(ext)) == 0;
+}
diff --git a/lib/url/url.h b/lib/url/url.h
index 9043615..6334763 100644
--- a/lib/url/url.h
+++ b/lib/url/url.h
@@ -67,5 +67,6 @@  struct pb_url *pb_url_join(void *ctx, const struct pb_url *url, const char *s);
 char *pb_url_to_string(struct pb_url *url);
 
 const char *pb_url_scheme_name(enum pb_url_scheme scheme);
+bool pb_url_check_extension(const struct pb_url *pb_url, const char *ext);
 
 #endif