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