Skip to content

Commit

Permalink
ae.net.http.common: Add path, queryString, urlParameters to HttpRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberShadow committed Mar 21, 2012
1 parent 5be1495 commit c5e7d09
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
33 changes: 33 additions & 0 deletions net/http/common.d
Expand Up @@ -65,11 +65,13 @@ public:
this.resource = resource;
}

/// Resource part of URL (everything after the hostname)
@property string resource()
{
return _resource;
}

/// Setting the resource to a full URL will fill in the Host header, as well.
@property void resource(string value)
{
_resource = value;
Expand Down Expand Up @@ -97,6 +99,7 @@ public:
}
}

/// The hostname, without the port number
@property string host()
{
string _host = headers["Host"];
Expand All @@ -110,6 +113,7 @@ public:
headers["Host"] = _port==80 ? _host : _host ~ ":" ~ text(_port);
}

/// Port number, from Host header (defaults to 80)
@property ushort port()
{
if ("Host" in headers)
Expand All @@ -135,6 +139,33 @@ public:
this._port = _port;
}

/// Path part of request (until the ?)
@property string path()
{
auto p = resource.indexOf('?');
if (p >= 0)
return resource[0..p];
else
return resource;
}

/// Query string part of request (atfer the ?)
@property string queryString()
{
auto p = resource.indexOf('?');
if (p >= 0)
return resource[p+1..$];
else
return null;
}

/// AA of query string parameters
string[string] urlParameters()
{
return decodeUrlParameters(queryString);
}

/// Reconstruct full URL from host, port and resource
@property string url()
{
return "http://" ~ host ~ (port==80 ? null : to!string(port)) ~ resource;
Expand All @@ -156,6 +187,7 @@ public:
return 80;
}

/// Parse the first line in a HTTP request ("METHOD /resource HTTP/1.x").
void parseRequestLine(string reqLine)
{
enforce(reqLine.length > 10, "Request line too short");
Expand All @@ -173,6 +205,7 @@ public:
protocolVersion = protocol[5..$];
}

/// Decodes submitted form data, and returns an AA of values.
string[string] decodePostData()
{
auto data = cast(string)data.joinToHeap();
Expand Down
5 changes: 2 additions & 3 deletions net/http/server.d
Expand Up @@ -280,16 +280,15 @@ unittest
// Sum "a" from GET and "b" from POST
auto s = new HttpServer;
s.handleRequest = (HttpRequest request, HttpServerConnection conn) {
auto queryString = request.resource[request.resource.indexOf('?')+1..$];
auto get = decodeUrlParameters(queryString);
auto get = request.urlParameters;
auto post = request.decodePostData();
auto response = new HttpResponseEx;
conn.sendResponse(response.serveJson(to!int(get["a"]) + to!int(post["b"])));
s.close();
};
auto port = s.listen(0, "127.0.0.1");

httpPost("http://127.0.0.1:" ~ to!string(port) ~ "/?a=2", ["b":"3"], (string s) { assert(s=="5"); }, null);
httpPost("http://127.0.0.1:" ~ to!string(port) ~ "/?" ~ encodeUrlParameters(["a":"2"]), ["b":"3"], (string s) { assert(s=="5"); }, null);

socketManager.loop();
}

0 comments on commit c5e7d09

Please sign in to comment.