Skip to content

Commit 8bf1722

Browse files
committed
Try to incorporate uriparser_copy_uri into uriparser
1 parent 42c6e58 commit 8bf1722

File tree

2 files changed

+31
-62
lines changed

2 files changed

+31
-62
lines changed

ext/uri/php_uriparser.c

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -28,73 +28,15 @@ static inline size_t get_text_range_length(const UriTextRangeA *range)
2828
return range->afterLast - range->first;
2929
}
3030

31-
static void uriparser_copy_text_range(UriTextRangeA *text_range, UriTextRangeA *new_text_range, bool use_safe)
32-
{
33-
if (text_range->first == NULL || text_range->afterLast == NULL || (text_range->first > text_range->afterLast && !use_safe)) {
34-
new_text_range->first = NULL;
35-
new_text_range->afterLast = NULL;
36-
} else if (text_range->first >= text_range->afterLast && use_safe) {
37-
new_text_range->first = uriSafeToPointToA;
38-
new_text_range->afterLast = uriSafeToPointToA;
39-
} else {
40-
size_t length = get_text_range_length(text_range);
41-
char *dup = emalloc(length);
42-
memcpy(dup, text_range->first, length);
43-
44-
new_text_range->first = dup;
45-
new_text_range->afterLast = dup + length;
46-
}
47-
}
48-
4931
static UriUriA *uriparser_copy_uri(UriUriA *uriparser_uri) // TODO add to uriparser
5032
{
5133
UriUriA *new_uriparser_uri = emalloc(sizeof(UriUriA));
5234

53-
uriparser_copy_text_range(&uriparser_uri->scheme, &new_uriparser_uri->scheme, false);
54-
uriparser_copy_text_range(&uriparser_uri->userInfo, &new_uriparser_uri->userInfo, false);
55-
uriparser_copy_text_range(&uriparser_uri->hostText, &new_uriparser_uri->hostText, true);
56-
if (uriparser_uri->hostData.ip4 == NULL) {
57-
new_uriparser_uri->hostData.ip4 = NULL;
58-
} else {
59-
new_uriparser_uri->hostData.ip4 = emalloc(sizeof(UriIp4));
60-
*(new_uriparser_uri->hostData.ip4) = *(uriparser_uri->hostData.ip4);
61-
}
62-
if (uriparser_uri->hostData.ip6 == NULL) {
63-
new_uriparser_uri->hostData.ip6 = NULL;
64-
} else {
65-
new_uriparser_uri->hostData.ip6 = emalloc(sizeof(UriIp6));
66-
*(new_uriparser_uri->hostData.ip6) = *(uriparser_uri->hostData.ip6);
67-
}
68-
uriparser_copy_text_range(&uriparser_uri->hostData.ipFuture, &new_uriparser_uri->hostData.ipFuture, false);
69-
uriparser_copy_text_range(&uriparser_uri->portText, &new_uriparser_uri->portText, false);
70-
71-
if (uriparser_uri->pathHead != NULL && uriparser_uri->pathTail != NULL) {
72-
new_uriparser_uri->pathHead = emalloc(sizeof(UriPathSegmentA));
73-
uriparser_copy_text_range(&uriparser_uri->pathHead->text, &new_uriparser_uri->pathHead->text, true);
74-
new_uriparser_uri->pathHead->reserved = NULL;
75-
76-
UriPathSegmentA *p = uriparser_uri->pathHead->next;
77-
UriPathSegmentA *new_p = new_uriparser_uri->pathHead;
78-
while (p != NULL && (p->text.first != p->text.afterLast || p->text.first == uriSafeToPointToA)) {
79-
new_p->next = emalloc(sizeof(UriPathSegmentA));
80-
new_p = new_p->next;
81-
uriparser_copy_text_range(&p->text, &new_p->text, true);
82-
new_p->reserved = NULL;
83-
p = p->next;
84-
}
85-
new_p->next = NULL;
86-
new_uriparser_uri->pathTail = new_p;
87-
} else {
88-
new_uriparser_uri->pathHead = NULL;
89-
new_uriparser_uri->pathTail = NULL;
35+
if (uriCopyUriA(new_uriparser_uri, uriparser_uri) != URI_SUCCESS) {
36+
efree(new_uriparser_uri);
37+
return NULL; /* TODO check for null on call sites */
9038
}
9139

92-
uriparser_copy_text_range(&uriparser_uri->query, &new_uriparser_uri->query, false);
93-
uriparser_copy_text_range(&uriparser_uri->fragment, &new_uriparser_uri->fragment, false);
94-
new_uriparser_uri->absolutePath = uriparser_uri->absolutePath;
95-
new_uriparser_uri->owner = true;
96-
new_uriparser_uri->reserved = NULL;
97-
9840
return new_uriparser_uri;
9941
}
10042

@@ -407,7 +349,7 @@ void *uriparser_parse_uri_ex(const zend_string *uri_str, const uriparser_uris_t
407349

408350
UriUriA *absolute_uri = emalloc(sizeof(UriUriA));
409351

410-
if (uriAddBaseUriExA(absolute_uri, uriparser_uri, uriparser_base_urls->uri, URI_RESOLVE_STRICTLY) != URI_SUCCESS) {
352+
if (uriAddBaseUriA(absolute_uri, uriparser_uri, uriparser_base_urls->uri) != URI_SUCCESS) {
411353
uriFreeUriMembersA(uriparser_uri);
412354
efree(uriparser_uri);
413355
efree(absolute_uri);

ext/uri/uriparser/include/uriparser/Uri.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,33 @@ URI_PUBLIC int URI_FUNC(ToString)(URI_CHAR * dest, const URI_TYPE(Uri) * uri,
654654
int maxChars, int * charsWritten);
655655

656656

657+
/**
658+
* Copies a %URI structure.
659+
*
660+
* @param destUri <b>OUT</b>: Output destination
661+
* @param sourceUri <b>IN</b>: %URI to copy
662+
* @param memory <b>IN</b>: Memory manager to use, NULL for default libc
663+
* @return Error code or 0 on success
664+
*
665+
* @since 0.9.8
666+
*/
667+
URI_PUBLIC int URI_FUNC(CopyUriMm)(URI_TYPE(Uri) * destUri,
668+
const URI_TYPE(Uri) * sourceUri, UriMemoryManager * memory);
669+
670+
671+
672+
/**
673+
* Copies a %URI structure.
674+
*
675+
* @param destUri <b>OUT</b>: Output destination
676+
* @param sourceUri <b>IN</b>: %URI to copy
677+
* @return Error code or 0 on success
678+
*
679+
* @since 0.9.8
680+
*/
681+
URI_PUBLIC int URI_FUNC(CopyUri)(URI_TYPE(Uri) * destUri, const URI_TYPE(Uri) * sourceUri);
682+
683+
657684

658685
/**
659686
* Copies a %URI structure.

0 commit comments

Comments
 (0)