Skip to content

Commit

Permalink
ppb_url_util: use uri_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
i-rinat committed Sep 27, 2014
1 parent 65536af commit 4702431
Showing 1 changed file with 15 additions and 139 deletions.
154 changes: 15 additions & 139 deletions src/ppb_url_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,165 +28,41 @@
#include "ppb_message_loop.h"
#include <stdlib.h>
#include <string.h>
#include <uriparser/Uri.h>
#include "uri_parser/uri_parser.h"
#include "trace.h"
#include "tables.h"


static
void
reset_components(struct PP_URLComponents_Dev *components)
{
if (!components)
return;
components->scheme.begin = 0; components->scheme.len = -1;
components->username.begin = 0; components->username.len = -1;
components->password.begin = 0; components->password.len = -1;
components->host.begin = 0; components->host.len = -1;
components->port.begin = 0; components->port.len = -1;
components->path.begin = 0; components->path.len = -1;
components->query.begin = 0; components->query.len = -1;
components->ref.begin = 0; components->ref.len = -1;
}

static
void
parse_url_string(const char *s, struct PP_URLComponents_Dev *components)
{
UriParserStateA ups;
UriUriA uri;

ups.uri = &uri;
if (uriParseUriA(&ups, s) != URI_SUCCESS) {
uriFreeUriMembersA(&uri);
trace_warning("failure at %s\n", __func__);
return;
}

reset_components(components);

#define C_PARSE(c1, c2) \
components->c1.begin = uri.c2.first ? uri.c2.first - s + 1 : 0; \
components->c1.len = uri.c2.first ? uri.c2.afterLast - uri.c2.first : -1;

C_PARSE(scheme, scheme);
C_PARSE(username, userInfo);
C_PARSE(password, userInfo);
if (components->username.begin > 0) {
const char *first = s + components->username.begin - 1;
const char *last = first + components->username.len;
const char *ptr = first;
while (ptr < last) {
if (*ptr == ':') {
components->username.len = ptr - first;
components->password.begin += ptr - first + 1;
components->password.len -= ptr - first + 1;
if (components->username.len == 0) {
components->username.begin = 0;
components->username.len = -1;
}
if (components->password.len == 0) {
components->password.begin = 0;
components->password.len = -1;
}
break;
}
ptr ++;
}
}
C_PARSE(host, hostText);
C_PARSE(port, portText);
components->path.begin = uri.pathHead ? uri.pathHead->text.first - s + 1 : 0;
components->path.len = uri.pathHead ? uri.pathTail->text.afterLast - uri.pathHead->text.first
: -1;
if (components->path.begin > 0) {
components->path.begin -= 1;
components->path.len += 1;
}
C_PARSE(query, query);
C_PARSE(ref, fragment);

#undef C_PARSE

uriFreeUriMembersA(&uri);
}

struct PP_Var
ppb_url_util_dev_canonicalize(struct PP_Var url, struct PP_URLComponents_Dev *components)
{
reset_components(components);
return PP_MakeUndefined();
if (components)
uri_parser_parse_uri(ppb_var_var_to_utf8(url, NULL), components);
return url;
}

struct PP_Var
ppb_url_util_dev_resolve_relative_to_url(struct PP_Var base_url, struct PP_Var relative_string,
struct PP_URLComponents_Dev *components)
{
reset_components(components);
struct PP_Var var = PP_MakeNull();

if (base_url.type != PP_VARTYPE_STRING) {
trace_warning("%s, base_url is not a string\n", __func__);
return PP_MakeNull();
}

if (relative_string.type != PP_VARTYPE_STRING) {
trace_warning("%s, relative_string is not a string\n", __func__);
return PP_MakeNull();
}

const char *s_base_url = ppb_var_var_to_utf8(base_url, NULL);
const char *s_relative_string = ppb_var_var_to_utf8(relative_string, NULL);

UriParserStateA ups;
UriUriA uri_base, uri_rel, uri_result;

ups.uri = &uri_base;
if (uriParseUriA(&ups, s_base_url) != URI_SUCCESS) {
trace_warning("%s, can't parse s_base_url\n", __func__);
trace_warning("%s, s_base_url=%s\n", __func__, s_base_url);
goto err_1;
}
gchar *res = uri_parser_merge_uris(s_base_url, s_relative_string);

ups.uri = &uri_rel;
if (uriParseUriA(&ups, s_relative_string) != URI_SUCCESS) {
trace_warning("%s, can't parse s_relative_string\n", __func__);
trace_warning("%s, s_relative_string=%s\n", __func__,s_relative_string);
goto err_2;
}

if (uriAddBaseUriA(&uri_result, &uri_rel, &uri_base) != URI_SUCCESS) {
trace_warning("%s, can't merge base and rel\n", __func__);
trace_warning("%s, s_base_url=%s\n", __func__, s_base_url);
trace_warning("%s, s_relative_string=%s\n", __func__,s_relative_string);
goto err_3;
}
if (components)
uri_parser_parse_uri(res, components);

int len;
uriToStringCharsRequiredA(&uri_result, &len);
len++;
char *str = malloc(len);
uriToStringA(str, &uri_result, len, NULL);
var = ppb_var_var_from_utf8_z(str);
struct PP_Var var = ppb_var_var_from_utf8_z(res);
g_free(res);

if (components)
parse_url_string(str, components);
free(str);

err_3:
uriFreeUriMembersA(&uri_result);
err_2:
uriFreeUriMembersA(&uri_rel);
err_1:
uriFreeUriMembersA(&uri_base);
return var;
}

struct PP_Var
ppb_url_util_dev_resolve_relative_to_document(PP_Instance instance, struct PP_Var relative_string,
struct PP_URLComponents_Dev *components)
{
reset_components(components);
struct PP_Var base = ppb_url_util_dev_get_document_url(instance, NULL);
return ppb_url_util_dev_resolve_relative_to_url(base, relative_string, components);
}
Expand All @@ -212,15 +88,14 @@ ppb_url_util_dev_document_can_access_document(PP_Instance active, PP_Instance ta
struct PP_Var
ppb_url_util_dev_get_document_url(PP_Instance instance, struct PP_URLComponents_Dev *components)
{
reset_components(components);
struct pp_instance_s *pp_i = tables_get_pp_instance(instance);
if (!pp_i) {
trace_error("%s, bad instance\n", __func__);
return PP_MakeUndefined();
}

if (components)
parse_url_string(ppb_var_var_to_utf8(pp_i->document_url, NULL), components);
uri_parser_parse_uri(ppb_var_var_to_utf8(pp_i->document_url, NULL), components);

return pp_i->document_url;
}
Expand All @@ -229,7 +104,6 @@ struct PP_Var
ppb_url_util_dev_get_plugin_instance_url(PP_Instance instance,
struct PP_URLComponents_Dev *components)
{
reset_components(components);
struct pp_instance_s *pp_i = tables_get_pp_instance(instance);
if (!pp_i) {
trace_error("%s, bad instance\n", __func__);
Expand All @@ -240,7 +114,7 @@ ppb_url_util_dev_get_plugin_instance_url(PP_Instance instance,
ppb_var_add_ref(var);

if (components)
parse_url_string(ppb_var_var_to_utf8(var, NULL), components);
uri_parser_parse_uri(ppb_var_var_to_utf8(var, NULL), components);

return var;
}
Expand All @@ -249,8 +123,10 @@ struct PP_Var
ppb_url_util_dev_get_plugin_referrer_url(PP_Instance instance,
struct PP_URLComponents_Dev *components)
{
reset_components(components);
return PP_MakeUndefined();
const char *url = "";
if (components)
uri_parser_parse_uri(url, components);
return ppb_var_var_from_utf8_z(url);
}


Expand Down

0 comments on commit 4702431

Please sign in to comment.