new data interface for catchAll and Document

This commit is contained in:
Adam D. Ruppe 2011-12-01 13:11:11 -05:00
parent 9b0efcd743
commit 3f092d36d3
2 changed files with 53 additions and 10 deletions

36
dom.d
View File

@ -2304,8 +2304,15 @@ struct Html {
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) {
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
/*
void normalize() {

27
web.d
View File

@ -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.
/// (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);
}
private string _errorMessageForCatchAll;
private Document _catchallEntry(string path, string funName, string errorMessage) {
private FileResource _catchallEntry(string path, string funName, string errorMessage) {
if(!errorMessage.length) {
string allFuncs, allObjs;
foreach(n, f; reflection.functions)
@ -625,13 +628,14 @@ immutable(ReflectionInfo*) prepareReflectionImpl(alias PM, alias Parent)(Parent
f.returnTypeIsDocument = true;
reflection.functions["/"] = cast(immutable) f;
/+
// catchAll here too
f = new FunctionInfo;
f.parentObject = reflection;
f.dispatcher = generateWrapper!(PM, "_catchAll", PM._catchAll)(reflection, instantiation);
f.returnTypeIsDocument = true;
reflection.functions["/_catchAll"] = cast(immutable) f;
+/
}}
// derivedMembers is changed from allMembers
@ -954,14 +958,19 @@ void run(Provider)(Cgi cgi, Provider instantiation, size_t pathInfoStartingPoint
"");
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;
// envelopeFormat = "no-processing";
// return;
}
assert(fun !is null);