Skip to content

Commit

Permalink
Resolve issue where uri_create_uri() returns an malformed URI when pa…
Browse files Browse the repository at this point in the history
…ssed only a rebased URI.

Fix missing const qualifiers
  • Loading branch information
nevali committed Feb 9, 2015
1 parent 9c96b75 commit e63b195
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 35 deletions.
28 changes: 14 additions & 14 deletions liburi.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,51 +59,51 @@ extern "C" {
int uri_destroy(URI *uri);

/* Return 1 if the URI specified is absolute, 0 if it is relative */
int uri_absolute(URI *uri);
int uri_absolute(const URI *uri);

/* Return 1 if the path in the URI specified is absolute, 0 if it is
* relative
*/
int uri_absolute_path(URI *uri);
int uri_absolute_path(const URI *uri);

/* Copy the URI's scheme into the buffer provided */
size_t uri_scheme(URI *restrict uri, char *restrict buf, size_t buflen);
size_t uri_scheme(const URI *restrict uri, char *restrict buf, size_t buflen);

/* Copy the URI's authentication information into the buffer provided */
size_t uri_auth(URI *restrict uri, char *restrict buf, size_t buflen);
size_t uri_auth(const URI *restrict uri, char *restrict buf, size_t buflen);

/* Copy the URI's hostname into the buffer provided */
size_t uri_host(URI *restrict uri, char *restrict buf, size_t buflen);
size_t uri_host(const URI *restrict uri, char *restrict buf, size_t buflen);

/* Copy the URI's port into the buffer provided */
size_t uri_port(URI *restrict uri, char *restrict buf, size_t buflen);
size_t uri_port(const URI *restrict uri, char *restrict buf, size_t buflen);

/* Copy the URI's path into the buffer provided */
size_t uri_path(URI *restrict uri, char *restrict buf, size_t buflen);
size_t uri_path(const URI *restrict uri, char *restrict buf, size_t buflen);

/* Copy the URI's query string into the buffer provided */
size_t uri_query(URI *restrict uri, char *restrict buf, size_t buflen);
size_t uri_query(const URI *restrict uri, char *restrict buf, size_t buflen);

/* Copy the URI's fragment into the buffer provided */
size_t uri_fragment(URI *restrict uri, char *restrict buf, size_t buflen);
size_t uri_fragment(const URI *restrict uri, char *restrict buf, size_t buflen);

/* Return the URI's port number, if present, as an integer */
int uri_portnum(URI *uri);
int uri_portnum(const URI *uri);

/* Copy the whole URI, as a string, into the buffer provided */
size_t uri_str(URI *restrict uri, char *restrict buf, size_t buflen);
size_t uri_str(const URI *restrict uri, char *restrict buf, size_t buflen);

/* Allocate a new string using malloc() and copy the URI into it */
char *uri_stralloc(URI *restrict uri);
char *uri_stralloc(const URI *restrict uri);

/* Copy the various parts of the URI to a URI_INFO structure */
URI_INFO *uri_info(URI *uri);
URI_INFO *uri_info(const URI *uri);

/* Free a URI_INFO structure */
int uri_info_destroy(URI_INFO *info);

/* Compare two URIs and test for equality */
int uri_equal(URI *a, URI *b);
int uri_equal(const URI *a, const URI *b);

# if defined(__cplusplus)
}
Expand Down
6 changes: 6 additions & 0 deletions p_liburi.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright 2015 BBC
*/

/*
* Copyright 2012 Mo McRoberts.
*
Expand All @@ -19,6 +24,7 @@

# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <wchar.h>
# include <errno.h>
# include <limits.h>
Expand Down
8 changes: 7 additions & 1 deletion parse.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright 2015 BBC
*/

/*
* Copyright 2012 Mo McRoberts.
*
Expand Down Expand Up @@ -227,6 +232,7 @@ uri_create_uri(const URI *restrict source, const URI *restrict base)
{
URI *uri;
UriParserStateA state;
char *p;

uri = (URI *) calloc(1, sizeof(URI));
if(!uri)
Expand All @@ -243,7 +249,7 @@ uri_create_uri(const URI *restrict source, const URI *restrict base)
}
else
{
uri->buf = strdup(source->buf);
uri->buf = uri_stralloc(source);
if(!uri->buf)
{
uri_destroy(uri);
Expand Down
33 changes: 19 additions & 14 deletions props.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright 2015 BBC
*/

/*
* Copyright 2012 Mo McRoberts.
*
Expand All @@ -20,47 +25,47 @@

#include "p_liburi.h"

static ssize_t uri_get_(UriTextRangeA *restrict range, char *restrict buf, size_t bufsize);
static ssize_t uri_get_(const UriTextRangeA *restrict range, char *restrict buf, size_t bufsize);
static int uri_addch_(int ch, char *restrict *restrict buf, size_t *restrict buflen);

size_t
uri_scheme(URI *restrict uri, char *restrict buf, size_t buflen)
uri_scheme(const URI *restrict uri, char *restrict buf, size_t buflen)
{
return uri_get_(&(uri->uri.scheme), buf, buflen);
}

size_t
uri_auth(URI *restrict uri, char *restrict buf, size_t buflen)
uri_auth(const URI *restrict uri, char *restrict buf, size_t buflen)
{
return uri_get_(&(uri->uri.userInfo), buf, buflen);
}

size_t
uri_host(URI *restrict uri, char *restrict buf, size_t buflen)
uri_host(const URI *restrict uri, char *restrict buf, size_t buflen)
{
return uri_get_(&(uri->uri.hostText), buf, buflen);
}

size_t
uri_port(URI *restrict uri, char *restrict buf, size_t buflen)
uri_port(const URI *restrict uri, char *restrict buf, size_t buflen)
{
return uri_get_(&(uri->uri.portText), buf, buflen);
}

size_t
uri_query(URI *restrict uri, char *restrict buf, size_t buflen)
uri_query(const URI *restrict uri, char *restrict buf, size_t buflen)
{
return uri_get_(&(uri->uri.query), buf, buflen);
}

size_t
uri_fragment(URI *restrict uri, char *restrict buf, size_t buflen)
uri_fragment(const URI *restrict uri, char *restrict buf, size_t buflen)
{
return uri_get_(&(uri->uri.fragment), buf, buflen);
}

int
uri_absolute_path(URI *uri)
uri_absolute_path(const URI *uri)
{
if(uri->uri.absolutePath == URI_TRUE)
{
Expand All @@ -78,7 +83,7 @@ uri_absolute_path(URI *uri)
}

int
uri_absolute(URI *uri)
uri_absolute(const URI *uri)
{
if(uri->uri.scheme.first)
{
Expand All @@ -88,7 +93,7 @@ uri_absolute(URI *uri)
}

size_t
uri_path(URI *restrict uri, char *restrict buf, size_t buflen)
uri_path(const URI *restrict uri, char *restrict buf, size_t buflen)
{
size_t total, len;
char *bp;
Expand Down Expand Up @@ -149,7 +154,7 @@ uri_path(URI *restrict uri, char *restrict buf, size_t buflen)
}

int
uri_portnum(URI *uri)
uri_portnum(const URI *uri)
{
char buffer[32], *t;
size_t len;
Expand All @@ -173,7 +178,7 @@ uri_portnum(URI *uri)
}

URI_INFO *
uri_info(URI *uri)
uri_info(const URI *uri)
{
URI_INFO *p;
char *buf;
Expand Down Expand Up @@ -251,13 +256,13 @@ uri_info_destroy(URI_INFO *info)

/* Compare two URIs and test for equality */
int
uri_equal(URI *a, URI *b)
uri_equal(const URI *a, const URI *b)
{
return uriEqualsUriA(&(a->uri), &(b->uri));
}

static ssize_t
uri_get_(UriTextRangeA *restrict range, char *restrict buf, size_t bufsize)
uri_get_(const UriTextRangeA *restrict range, char *restrict buf, size_t bufsize)
{
size_t len, slen;

Expand Down
4 changes: 2 additions & 2 deletions recompose.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "p_liburi.h"

size_t
uri_str(URI *restrict uri, char *restrict buf, size_t buflen)
uri_str(const URI *restrict uri, char *restrict buf, size_t buflen)
{
int bufsize;

Expand All @@ -43,7 +43,7 @@ uri_str(URI *restrict uri, char *restrict buf, size_t buflen)

/* Allocate a new string using malloc() and copy the URI into it */
char *
uri_stralloc(URI *restrict uri)
uri_stralloc(const URI *restrict uri)
{
size_t needed;
char *str;
Expand Down
13 changes: 9 additions & 4 deletions t/match.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright 2015 BBC
*/

/*
* Copyright 2012 Mo McRoberts.
*
Expand All @@ -20,9 +25,9 @@

#include "p_testsuite.h"

static int testlen(const char *restrict file, const char *name, URI *restrict uri, struct urimatch *restrict test, size_t (*fn)(URI *restrict, char *restrict, size_t), size_t expected);
static int testlen(const char *restrict file, const char *name, const URI *restrict uri, struct urimatch *restrict test, size_t (*fn)(const URI *restrict, char *restrict, size_t), size_t expected);

static int teststr(const char *restrict file, const char *name, URI *restrict uri, char *restrict buf, size_t buflen, struct urimatch *restrict test, size_t (*fn)(URI *restrict, char *restrict, size_t), const char *restrict expected);
static int teststr(const char *restrict file, const char *name, const URI *restrict uri, char *restrict buf, size_t buflen, struct urimatch *restrict test, size_t (*fn)(const URI *restrict, char *restrict, size_t), const char *restrict expected);

int
test_urimatch(const char *file, struct urimatch *tests)
Expand Down Expand Up @@ -143,7 +148,7 @@ test_urimatch(const char *file, struct urimatch *tests)
}

static int
testlen(const char *restrict file, const char *name, URI *restrict uri, struct urimatch *restrict test, size_t (*fn)(URI *restrict, char *restrict, size_t), size_t expected)
testlen(const char *restrict file, const char *name, const URI *restrict uri, struct urimatch *restrict test, size_t (*fn)(const URI *restrict, char *restrict, size_t), size_t expected)
{
size_t r;

Expand All @@ -157,7 +162,7 @@ testlen(const char *restrict file, const char *name, URI *restrict uri, struct u
}

static int
teststr(const char *restrict file, const char *name, URI *restrict uri, char *restrict buffer, size_t buflen, struct urimatch *restrict test, size_t (*fn)(URI *restrict, char *restrict, size_t), const char *restrict expected)
teststr(const char *restrict file, const char *name, const URI *restrict uri, char *restrict buffer, size_t buflen, struct urimatch *restrict test, size_t (*fn)(const URI *restrict, char *restrict, size_t), const char *restrict expected)
{
size_t r;

Expand Down

0 comments on commit e63b195

Please sign in to comment.