mirror of https://github.com/adamdruppe/arsd.git
new data interface for catchAll and Document
This commit is contained in:
parent
9b0efcd743
commit
3f092d36d3
36
dom.d
36
dom.d
|
@ -2304,8 +2304,15 @@ struct Html {
|
||||||
string source;
|
string source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
interface FileResource {
|
||||||
|
string contentType() const;
|
||||||
|
immutable(ubyte)[] getData() const;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
///.
|
///.
|
||||||
class Document {
|
class Document : FileResource {
|
||||||
///.
|
///.
|
||||||
this(string data, bool caseSensitive = false, bool strict = false) {
|
this(string data, bool caseSensitive = false, bool strict = false) {
|
||||||
parse(data, caseSensitive, strict);
|
parse(data, caseSensitive, strict);
|
||||||
|
@ -2318,6 +2325,33 @@ class Document {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string _contentType = "text/html";
|
||||||
|
|
||||||
|
/// If you're using this for some other kind of XML, you can
|
||||||
|
/// set the content type here.
|
||||||
|
///
|
||||||
|
/// Note: this has no impact on the function of this class.
|
||||||
|
/// It is only used if the document is sent via a protocol like HTTP.
|
||||||
|
///
|
||||||
|
/// This may be called by parse() if it recognizes the data. Otherwise,
|
||||||
|
/// if you don't set it, it assumes text/html.
|
||||||
|
string contentType(string mimeType) {
|
||||||
|
_contentType = mimeType;
|
||||||
|
return _contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// implementing the FileResource interface, useful for sending via
|
||||||
|
/// http automatically.
|
||||||
|
override string contentType() const {
|
||||||
|
return _contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// implementing the FileResource interface; it calls toString.
|
||||||
|
override immutable(ubyte)[] getData() const {
|
||||||
|
return cast(immutable(ubyte)[]) this.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Concatenates any consecutive text nodes
|
/// Concatenates any consecutive text nodes
|
||||||
/*
|
/*
|
||||||
void normalize() {
|
void normalize() {
|
||||||
|
|
27
web.d
27
web.d
|
@ -404,12 +404,15 @@ class ApiProvider : WebDotDBaseType {
|
||||||
|
|
||||||
/// Overriding it might be useful if you want to serve generic filenames or an opDispatch kind of thing.
|
/// Overriding it might be useful if you want to serve generic filenames or an opDispatch kind of thing.
|
||||||
/// (opDispatch itself won't work because it's name argument needs to be known at compile time!)
|
/// (opDispatch itself won't work because it's name argument needs to be known at compile time!)
|
||||||
Document _catchAll(string path) {
|
///
|
||||||
|
/// Note that you can return Documents here as they implement
|
||||||
|
/// the FileResource interface too.
|
||||||
|
FileResource _catchAll(string path) {
|
||||||
throw new NoSuchPageException(_errorMessageForCatchAll);
|
throw new NoSuchPageException(_errorMessageForCatchAll);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string _errorMessageForCatchAll;
|
private string _errorMessageForCatchAll;
|
||||||
private Document _catchallEntry(string path, string funName, string errorMessage) {
|
private FileResource _catchallEntry(string path, string funName, string errorMessage) {
|
||||||
if(!errorMessage.length) {
|
if(!errorMessage.length) {
|
||||||
string allFuncs, allObjs;
|
string allFuncs, allObjs;
|
||||||
foreach(n, f; reflection.functions)
|
foreach(n, f; reflection.functions)
|
||||||
|
@ -625,13 +628,14 @@ immutable(ReflectionInfo*) prepareReflectionImpl(alias PM, alias Parent)(Parent
|
||||||
f.returnTypeIsDocument = true;
|
f.returnTypeIsDocument = true;
|
||||||
reflection.functions["/"] = cast(immutable) f;
|
reflection.functions["/"] = cast(immutable) f;
|
||||||
|
|
||||||
|
/+
|
||||||
// catchAll here too
|
// catchAll here too
|
||||||
f = new FunctionInfo;
|
f = new FunctionInfo;
|
||||||
f.parentObject = reflection;
|
f.parentObject = reflection;
|
||||||
f.dispatcher = generateWrapper!(PM, "_catchAll", PM._catchAll)(reflection, instantiation);
|
f.dispatcher = generateWrapper!(PM, "_catchAll", PM._catchAll)(reflection, instantiation);
|
||||||
f.returnTypeIsDocument = true;
|
f.returnTypeIsDocument = true;
|
||||||
reflection.functions["/_catchAll"] = cast(immutable) f;
|
reflection.functions["/_catchAll"] = cast(immutable) f;
|
||||||
|
+/
|
||||||
}}
|
}}
|
||||||
|
|
||||||
// derivedMembers is changed from allMembers
|
// derivedMembers is changed from allMembers
|
||||||
|
@ -954,14 +958,19 @@ void run(Provider)(Cgi cgi, Provider instantiation, size_t pathInfoStartingPoint
|
||||||
"");
|
"");
|
||||||
|
|
||||||
result.success = true;
|
result.success = true;
|
||||||
JSONValue res;
|
|
||||||
res.type = JSON_TYPE.STRING;
|
|
||||||
res.str = (d is null) ? "" : d.toString();
|
|
||||||
result.result = res;
|
|
||||||
|
|
||||||
|
if(d !is null) {
|
||||||
|
auto doc = cast(Document) d;
|
||||||
|
if(doc)
|
||||||
|
instantiation._postProcess(doc);
|
||||||
|
|
||||||
|
cgi.setResponseLocation(d.contentType());
|
||||||
|
cgi.write(d.getData(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// we did everything we need above...
|
||||||
|
envelopeFormat = "no-processing";
|
||||||
goto do_nothing_else;
|
goto do_nothing_else;
|
||||||
// envelopeFormat = "no-processing";
|
|
||||||
// return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(fun !is null);
|
assert(fun !is null);
|
||||||
|
|
Loading…
Reference in New Issue