mirror of https://github.com/adamdruppe/arsd.git
data uri support
This commit is contained in:
parent
1681684f3e
commit
855a3f6f65
61
http2.d
61
http2.d
|
@ -840,6 +840,8 @@ struct Uri {
|
|||
/// Browsers use a function like this to figure out links in html.
|
||||
Uri basedOn(in Uri baseUrl) const {
|
||||
Uri n = this; // copies
|
||||
if(n.scheme == "data")
|
||||
return n;
|
||||
// n.uriInvalidated = true; // make sure we regenerate...
|
||||
|
||||
// userinfo is not inherited... is this wrong?
|
||||
|
@ -1139,6 +1141,50 @@ class HttpRequest {
|
|||
}
|
||||
}
|
||||
|
||||
if(this.where.scheme == "data") {
|
||||
void error(string content) {
|
||||
responseData.code = 400;
|
||||
responseData.codeText = "Bad Request";
|
||||
responseData.contentType = "text/plain";
|
||||
responseData.content = cast(ubyte[]) content;
|
||||
responseData.contentText = content;
|
||||
state = State.complete;
|
||||
return;
|
||||
}
|
||||
|
||||
auto thing = this.where.path;
|
||||
// format is: type,data
|
||||
// type can have ;base64
|
||||
auto comma = thing.indexOf(",");
|
||||
if(comma == -1)
|
||||
return error("Invalid data uri, no comma found");
|
||||
|
||||
auto type = thing[0 .. comma];
|
||||
auto data = thing[comma + 1 .. $];
|
||||
if(type.length == 0)
|
||||
type = "text/plain";
|
||||
|
||||
import std.uri;
|
||||
auto bdata = cast(ubyte[]) decodeComponent(data);
|
||||
|
||||
if(type.indexOf(";base64") != -1) {
|
||||
import std.base64;
|
||||
try {
|
||||
bdata = Base64.decode(bdata);
|
||||
} catch(Exception e) {
|
||||
return error(e.msg);
|
||||
}
|
||||
}
|
||||
|
||||
responseData.code = 200;
|
||||
responseData.codeText = "OK";
|
||||
responseData.contentType = type;
|
||||
responseData.content = bdata;
|
||||
responseData.contentText = cast(string) responseData.content;
|
||||
state = State.complete;
|
||||
return;
|
||||
}
|
||||
|
||||
string headers;
|
||||
|
||||
headers ~= to!string(requestParameters.method);
|
||||
|
@ -5347,6 +5393,21 @@ version(Windows) {
|
|||
}
|
||||
}
|
||||
|
||||
unittest {
|
||||
auto client = new HttpClient();
|
||||
auto response = client.navigateTo(Uri("data:,Hello%2C%20World%21")).waitForCompletion();
|
||||
assert(response.contentTypeMimeType == "text/plain", response.contentType);
|
||||
assert(response.contentText == "Hello, World!", response.contentText);
|
||||
|
||||
response = client.navigateTo(Uri("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==")).waitForCompletion();
|
||||
assert(response.contentTypeMimeType == "text/plain", response.contentType);
|
||||
assert(response.contentText == "Hello, World!", response.contentText);
|
||||
|
||||
response = client.navigateTo(Uri("data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E")).waitForCompletion();
|
||||
assert(response.contentTypeMimeType == "text/html", response.contentType);
|
||||
assert(response.contentText == "<h1>Hello, World!</h1>", response.contentText);
|
||||
}
|
||||
|
||||
version(arsd_http2_unittests)
|
||||
unittest {
|
||||
import core.thread;
|
||||
|
|
Loading…
Reference in New Issue