Test drive using std::string to replace dstr.

Looks like it worked within the URL code.

Now we can really start to slice-and-dice.
This commit is contained in:
2025-04-12 04:25:55 -04:00
parent 441a04609b
commit 26b903f02f

View File

@ -51,6 +51,8 @@
#include "misc.hh" #include "misc.hh"
#include "msg.hh" #include "msg.hh"
#include <string>
static const char *HEX = "0123456789ABCDEF"; static const char *HEX = "0123456789ABCDEF";
/* URL-field compare methods */ /* URL-field compare methods */
@ -221,9 +223,10 @@ DilloUrl::~DilloUrl()
static Dstr *Url_resolve_relative(const char *RelStr, static Dstr *Url_resolve_relative(const char *RelStr,
const char *BaseStr) const char *BaseStr)
{ {
char *p, *s, *e; const char *p, *s, *e;
int i; int i;
Dstr *SolvedUrl, *Path; Dstr *SolvedUrl;
std::string Path;
DilloUrl *RelUrl, *BaseUrl = NULL; DilloUrl *RelUrl, *BaseUrl = NULL;
/* parse relative URL */ /* parse relative URL */
@ -235,7 +238,6 @@ static Dstr *Url_resolve_relative(const char *RelStr,
} }
SolvedUrl = dStr_sized_new(64); SolvedUrl = dStr_sized_new(64);
Path = dStr_sized_new(64);
/* path empty && scheme and authority undefined */ /* path empty && scheme and authority undefined */
if (!RelUrl->path && !RelUrl->scheme && !RelUrl->authority) { if (!RelUrl->path && !RelUrl->scheme && !RelUrl->authority) {
@ -264,36 +266,39 @@ static Dstr *Url_resolve_relative(const char *RelStr,
} else if (RelUrl->authority) { /* authority */ } else if (RelUrl->authority) { /* authority */
// Set the Path buffer and goto "STEP 7"; // Set the Path buffer and goto "STEP 7";
if (RelUrl->path) if (RelUrl->path)
dStr_append(Path, RelUrl->path); Path+= RelUrl->path;
} else { } else {
if (RelUrl->path && RelUrl->path[0] == '/') { /* absolute path */ if (RelUrl->path && RelUrl->path[0] == '/') { /* absolute path */
; /* Ignore BaseUrl path */ ; /* Ignore BaseUrl path */
} else if (BaseUrl->path) { /* relative path */ } else if (BaseUrl->path) { /* relative path */
dStr_append(Path, BaseUrl->path); Path+= BaseUrl->path;
for (i = Path->len; --i >= 0 && Path->str[i] != '/'; ) ; for (i = Path.size(); --i >= 0 && Path.at( i ) != '/'; ) ;
if (i >= 0 && Path->str[i] == '/') if (i >= 0 && Path.at(i) == '/')
dStr_truncate(Path, ++i); {
++i;
if( i < Path.size() ) Path.resize( i );
}
} }
if (RelUrl->path) if (RelUrl->path)
dStr_append(Path, RelUrl->path); Path+= RelUrl->path;
// erase "./" // erase "./"
while ((p=strstr(Path->str, "./")) && while ((p=strstr(Path.c_str(), "./")) &&
(p == Path->str || p[-1] == '/')) (p == Path.c_str() || p[-1] == '/'))
dStr_erase(Path, p - Path->str, 2); Path.erase( p - Path.c_str(), 2 );
// erase last "." // erase last "."
if (Path->len && Path->str[Path->len - 1] == '.' && if (Path.size() && Path.at(Path.size() - 1) == '.' &&
(Path->len == 1 || Path->str[Path->len - 2] == '/')) (Path.size() == 1 || Path.at(Path.size() - 2) == '/'))
dStr_truncate(Path, Path->len - 1); Path.pop_back();
// erase "<segment>/../" and "<segment>/.." // erase "<segment>/../" and "<segment>/.."
s = p = Path->str; s = p = Path.c_str();
while ( (p = strstr(p, "/..")) != NULL ) { while ( (p = strstr(p, "/..")) != NULL ) {
if (p[3] == '/' || !p[3]) { // "/../" | "/.." if (p[3] == '/' || !p[3]) { // "/../" | "/.."
for (e = p + 3 ; p > s && p[-1] != '/'; --p) ; for (e = p + 3 ; p > s && p[-1] != '/'; --p) ;
dStr_erase(Path, p - Path->str, e - p + (p > s && *e != 0)); Path.erase( p - Path.c_str(), e - p + (p > s && *e != 0) );
p -= (p > Path->str); p -= (p > Path.c_str());
} else } else
p += 3; p += 3;
} }
@ -319,10 +324,10 @@ static Dstr *Url_resolve_relative(const char *RelStr,
/* path */ /* path */
if ((RelUrl->authority || BaseUrl->authority) && if ((RelUrl->authority || BaseUrl->authority) &&
((Path->len == 0 && (RelUrl->query || RelUrl->fragment)) || ((Path.size() == 0 && (RelUrl->query || RelUrl->fragment)) ||
(Path->len && Path->str[0] != '/'))) (Path.size() && Path.at(0) != '/')))
dStr_append_c(SolvedUrl, '/'); /* hack? */ dStr_append_c(SolvedUrl, '/'); /* hack? */
dStr_append(SolvedUrl, Path->str); dStr_append(SolvedUrl, Path.c_str());
/* query */ /* query */
if (RelUrl->query) { if (RelUrl->query) {
@ -337,7 +342,6 @@ static Dstr *Url_resolve_relative(const char *RelStr,
} }
done: done:
dStr_free(Path, TRUE);
delete RelUrl; delete RelUrl;
delete BaseUrl; delete BaseUrl;
return SolvedUrl; return SolvedUrl;