Skip to content

Commit

Permalink
Initialize parseURL and parseQueryString lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed Jul 6, 2017
1 parent 5a2fa8e commit 13d2000
Showing 1 changed file with 47 additions and 36 deletions.
83 changes: 47 additions & 36 deletions http/vibe/http/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ alias HTTPServerErrorPageHandler = void delegate(HTTPServerRequest req, HTTPServ
*/
enum HTTPServerOption {
none = 0,
/// Fills the `.path` and `.queryString` fields in the request
/// Deprecated: Fills the `.path` and `.queryString` fields in the request
parseURL = 1<<0,
/// Fills the `.query` field in the request
/// Deprecated: Fills the `.query` field in the request
parseQueryString = 1<<1 | parseURL,
/// Deprecated: Fills the `.form` field in the request
parseFormBody = 1<<2,
Expand Down Expand Up @@ -800,29 +800,51 @@ final class HTTPServerRequest : HTTPRequest {
*/
TLSCertificateInformation clientCertificate;

/** The _path part of the URL.
private bool urlParsed;
private void parseURL() @safe {
auto url = URL.parse(requestURL);
_path = urlDecode(url.pathString);
_queryString = url.queryString;
_username = url.username;
_password = url.password;
urlParsed = true;
}

Remarks: This field is only set if HTTPServerOption.parseURL is set.
/** The _path part of the URL.
*/
string path;
ref string path() @safe {
if (!urlParsed)
parseURL;
return _path;
}
private string _path;

/** The user name part of the URL, if present.
Remarks: This field is only set if HTTPServerOption.parseURL is set.
*/
string username;
ref string username() @safe {
if (!urlParsed)
parseURL;
return _username;
}
private string _username;

/** The _password part of the URL, if present.
Remarks: This field is only set if HTTPServerOption.parseURL is set.
*/
string password;
ref string password() @safe {
if (!urlParsed)
parseURL;
return _password;
}
private string _password;

/** The _query string part of the URL.
Remarks: This field is only set if HTTPServerOption.parseURL is set.
*/
string queryString;
ref string queryString() @safe {
if (!urlParsed)
parseURL;
return _queryString;
}
private string _queryString;

/** Contains the list of _cookies that are stored on the client.
Expand All @@ -846,10 +868,16 @@ final class HTTPServerRequest : HTTPRequest {
/** Contains all _form fields supplied using the _query string.
The fields are stored in the same order as they are received.
Remarks: This field is only set if HTTPServerOption.parseQueryString is set.
*/
FormFields query;
@property ref const(FormFields) query() @safe {
if (_query.isNull) {
_query = FormFields.init;
parseURLEncodedForm(queryString, _query);
}

return _query.get;
}
Nullable!FormFields _query;

import vibe.utils.dictionarylist;
/** A map of general parameters for the request.
Expand Down Expand Up @@ -977,8 +1005,7 @@ final class HTTPServerRequest : HTTPRequest {
Note that the port is currently not set, so that this only works if
the standard port is used.
*/
@property URL fullURL()
const @safe {
@property URL fullURL() @safe {
URL url;

auto xfh = this.headers.get("X-Forwarded-Host");
Expand Down Expand Up @@ -1046,7 +1073,7 @@ final class HTTPServerRequest : HTTPRequest {
The returned string always ends with a slash.
*/
@property string rootDir() const @safe {
@property string rootDir() @safe {
if (path.length == 0) return "./";
auto depth = count(path[1 .. $], '/');
return depth == 0 ? "./" : replicate("../", depth);
Expand Down Expand Up @@ -2107,22 +2134,6 @@ private bool handleRequest(InterfaceProxy!Stream http_stream, TCPConnection tcp_
}
}

// URL parsing if desired
if (settings.options & HTTPServerOption.parseURL) {
auto url = URL.parse(req.requestURL);
req.path = urlDecode(url.pathString);
req.queryString = url.queryString;
req.username = url.username;
req.password = url.password;
}

// query string parsing if desired
if (settings.options & HTTPServerOption.parseQueryString) {
if (!(settings.options & HTTPServerOption.parseURL))
logWarn("Query string parsing requested but URL parsing is disabled!");
parseURLEncodedForm(req.queryString, req.query);
}

// lookup the session
if (settings.sessionStore) {
// use the first cookie that contains a valid session ID in case
Expand Down

0 comments on commit 13d2000

Please sign in to comment.