Skip to content

Commit

Permalink
Implement partial support for URL modification
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Oct 12, 2024
1 parent 02f5c6b commit acb1a98
Show file tree
Hide file tree
Showing 18 changed files with 989 additions and 126 deletions.
60 changes: 60 additions & 0 deletions ext/dom/lexbor/lexbor/url/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -4398,3 +4398,63 @@ lxb_url_serialize_fragment(const lxb_url_t *url,

return LXB_STATUS_OK;
}

lxb_url_t *
lxb_url_clone(lexbor_mraw_t *mraw, lxb_url_t *url)
{
lxb_status_t status;
lxb_url_t *new_url;

new_url = lexbor_mraw_calloc(mraw, sizeof(lxb_url_t));
if (new_url == NULL) {
return NULL;
}

new_url->mraw = mraw;

status = lxb_url_scheme_copy(&url->scheme, &new_url->scheme, mraw);
if (status != LXB_STATUS_OK) {
goto failed;
}

status = lxb_url_username_copy(&url->username, &new_url->username, mraw);
if (status != LXB_STATUS_OK) {
goto failed;
}

status = lxb_url_password_copy(&url->password, &new_url->password, mraw);
if (status != LXB_STATUS_OK) {
goto failed;
}

status = lxb_url_host_copy(&url->host, &new_url->host, mraw);
if (status != LXB_STATUS_OK) {
goto failed;
}

new_url->port = url->port;
new_url->has_port = url->has_port;

status = lxb_url_path_copy(url, new_url);
if (status != LXB_STATUS_OK) {
goto failed;
}

status = lxb_url_query_copy(&url->query, &new_url->query, mraw);
if (status != LXB_STATUS_OK) {
goto failed;
}

status = lxb_url_str_copy(&url->fragment, &new_url->fragment, mraw);
if (status != LXB_STATUS_OK) {
goto failed;
}

return new_url;

failed:

lxb_url_destroy(new_url);

return NULL;
}
13 changes: 13 additions & 0 deletions ext/dom/lexbor/lexbor/url/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,19 @@ LXB_API lxb_status_t
lxb_url_serialize_fragment(const lxb_url_t *url,
lexbor_serialize_cb_f cb, void *ctx);

/*
* Creates a clone of the object's URL.
*
* For lexbor_mraw_t *, use url->mraw or another lexbor_mraw_t * object.
*
* @param[in] lexbor_mraw_t *.
* @param[in] lxb_url_t *.
*
* @return a new URL object if successful, otherwise NULL value.
*/
LXB_API lxb_url_t *
lxb_url_clone(lexbor_mraw_t *mraw, lxb_url_t *url);


/*
* Inline functions.
Expand Down
14 changes: 12 additions & 2 deletions ext/standard/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,18 @@ static zend_result parse_url_init_parser(void)
return SUCCESS;
}

static void parse_url_instantiate_uri(zval *zv)
static zend_class_entry *parse_url_get_uri_ce(void)
{
ZEND_UNREACHABLE();

return NULL;
}

static void *parse_url_clone_uri(void *uri_object_internal)
{
ZEND_UNREACHABLE();

return NULL;
}

static void *parse_url_parse_uri(const zend_string *uri_str, const zend_string *base_uri_str, zval *errors)
Expand All @@ -472,7 +481,8 @@ const uri_handler_t parse_url_uri_handler = {
"parse_url",
parse_url_init_parser,
parse_url_parse_uri,
parse_url_instantiate_uri,
parse_url_get_uri_ce,
parse_url_clone_uri,
parse_url_uri_to_string,
parse_url_free_uri,
parse_url_destroy_parser,
Expand Down
58 changes: 37 additions & 21 deletions ext/uri/php_lexbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
#include <arpa/inet.h>
#endif

static zend_result lexbor_init_parser(void);
static void *lexbor_parse_uri(const zend_string *url_str, const zend_string *base_url_str, zval *errors);
static zend_class_entry *lexbor_get_uri_ce(void);
static void *lexbor_clone_uri(void *uri_object_internal);
static zend_string *lexbor_to_string(void *uri_object_internal);
static void lexbor_free_uri(void *uri_object_internal);
static zend_result lexbor_destroy_parser(void);

HashTable lexbor_property_handlers;

lxb_url_parser_t *lexbor_parser;
Expand All @@ -32,7 +40,8 @@ const uri_handler_t lexbor_uri_handler = {
"whatwg",
lexbor_init_parser,
lexbor_parse_uri,
lexbor_instantiate_uri,
lexbor_get_uri_ce,
lexbor_clone_uri,
lexbor_to_string,
lexbor_free_uri,
lexbor_destroy_parser,
Expand Down Expand Up @@ -61,6 +70,13 @@ static zend_result lexbor_read_scheme(void *uri_object_internal, zval *retval)
return SUCCESS;
}

static zend_result lexbor_write_scheme(void *uri_object_internal, zval *value)
{
//lxb_url_t *lxb_url = (lxb_url_t *) uri_object_internal;

return SUCCESS;
}

static zend_result lexbor_read_user(void *uri_object_internal, zval *retval)
{
lxb_url_t *lxb_url = (lxb_url_t *) uri_object_internal;
Expand Down Expand Up @@ -133,16 +149,10 @@ static zend_result lexbor_read_path(void *uri_object_internal, zval *retval)
{
lxb_url_t *lxb_url = (lxb_url_t *) uri_object_internal;

if (lxb_url->path.length && (lxb_url->path.list[0]->length || lxb_url->path.length > 1)) {
smart_str str = {0};

smart_str_appendl(&str, (const char *) lxb_url->path.list[0]->data, lxb_url->path.list[0]->length);
for (int i = 1; i < lxb_url->path.length; i++) {
smart_str_appends(&str, "/");
smart_str_appendl(&str, (const char *) lxb_url->path.list[i]->data, lxb_url->path.list[i]->length);
}

ZVAL_STR(retval, smart_str_extract(&str));
if (lxb_url->path.opaque) {
ZVAL_STRINGL(retval, (const char *) lxb_url->path.str.data, lxb_url->path.str.length);
} else if (lxb_url->path.str.length > 1) {
ZVAL_STRINGL(retval, (const char *) lxb_url->path.str.data + 1, lxb_url->path.str.length - 1);
} else {
ZVAL_NULL(retval);
}
Expand Down Expand Up @@ -176,7 +186,7 @@ static zend_result lexbor_read_fragment(void *uri_object_internal, zval *retval)
return SUCCESS;
}

zend_result lexbor_init_parser(void)
static zend_result lexbor_init_parser(void)
{
lexbor_mraw_t *mraw = lexbor_mraw_create();
lxb_status_t status = lexbor_mraw_init(mraw, 4096 * 2);
Expand All @@ -197,7 +207,7 @@ zend_result lexbor_init_parser(void)

zend_hash_init(&lexbor_property_handlers, 0, NULL, NULL, true);

URI_REGISTER_PROPERTY_READ_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_SCHEME), lexbor_read_scheme);
URI_REGISTER_PROPERTY_READ_WRITE_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_SCHEME), lexbor_read_scheme, lexbor_write_scheme);
URI_REGISTER_PROPERTY_READ_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_USER), lexbor_read_user);
URI_REGISTER_PROPERTY_READ_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_PASSWORD), lexbor_read_password);
URI_REGISTER_PROPERTY_READ_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_HOST), lexbor_read_host);
Expand Down Expand Up @@ -232,7 +242,7 @@ void fill_errors(zval *errors)
zval_ptr_dtor(&errors_tmp);
}

void *lexbor_parse_uri(const zend_string *url_str, const zend_string *base_url_str, zval *errors)
static void *lexbor_parse_uri(const zend_string *url_str, const zend_string *base_url_str, zval *errors)
{
lexbor_cleanup_parser();

Expand All @@ -255,12 +265,19 @@ void *lexbor_parse_uri(const zend_string *url_str, const zend_string *base_url_s
return url;
}

void lexbor_instantiate_uri(zval *zv)
static zend_class_entry *lexbor_get_uri_ce(void)
{
object_init_ex(zv, whatwg_uri_ce);
return whatwg_uri_ce;
}

static void *lexbor_clone_uri(void *uri_object_internal)
{
lxb_url_t *lxb_url = (lxb_url_t *) uri_object_internal;

return lxb_url_clone(lexbor_parser->mraw, lxb_url);
}

lxb_status_t lexbor_serialize_callback(const lxb_char_t *data, size_t length, void *ctx)
static lxb_status_t lexbor_serialize_callback(const lxb_char_t *data, size_t length, void *ctx)
{
smart_str *uri_str = (smart_str *) ctx;

Expand All @@ -269,7 +286,7 @@ lxb_status_t lexbor_serialize_callback(const lxb_char_t *data, size_t length, vo
return LXB_STATUS_OK;
}

zend_string *lexbor_to_string(void *uri_object_internal)
static zend_string *lexbor_to_string(void *uri_object_internal)
{
lxb_url_t *lxb_url = (lxb_url_t *) uri_object_internal;
smart_str uri_str = {0};
Expand All @@ -279,12 +296,11 @@ zend_string *lexbor_to_string(void *uri_object_internal)
return smart_str_extract(&uri_str);
}

void lexbor_free_uri(void *uri_object_internal)
static void lexbor_free_uri(void *uri_object_internal)
{
//lxb_url_t *url = (lxb_url_t *) uri_object_internal;
}

zend_result lexbor_destroy_parser(void)
static zend_result lexbor_destroy_parser(void)
{
lxb_url_parser_memory_destroy(lexbor_parser);
lxb_url_parser_destroy(lexbor_parser, true);
Expand Down
7 changes: 0 additions & 7 deletions ext/uri/php_lexbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
#include <php_uri_common.h>
#include <lexbor/url/url.h>

zend_result lexbor_init_parser(void);
void *lexbor_parse_uri(const zend_string *url_str, const zend_string *base_url_str, zval *errors);
void lexbor_instantiate_uri(zval *zv);
zend_string *lexbor_to_string(void *uri_object_internal);
void lexbor_free_uri(void *uri_object_internal);
zend_result lexbor_destroy_parser(void);

extern const uri_handler_t lexbor_uri_handler;

#endif
Loading

0 comments on commit acb1a98

Please sign in to comment.