Yield IPlugEndpoint.Invoke(Plug plug, string verb, XUri uri, DreamMessage request, Result <DreamMessage> response) { // we only support GET as verb DreamMessage reply; if (verb != Verb.GET) { reply = new DreamMessage(DreamStatus.MethodNotAllowed, null, null); reply.Headers.Allow = Verb.GET; } else { XDoc doc; lock (_map) { if (!_map.TryGetValue(uri, out doc)) { reply = DreamMessage.NotFound("resource has been removed"); } else { reply = DreamMessage.Ok(doc); } } } response.Return(reply); yield break; }
public Yield Invoke(Plug plug, string verb, XUri uri, DreamMessage request, Result <DreamMessage> response) { // we only support GET as verb DreamMessage reply; if ((verb != Verb.GET) && (verb != Verb.HEAD)) { reply = new DreamMessage(DreamStatus.MethodNotAllowed, null, null); reply.Headers.Allow = Verb.GET + "," + Verb.HEAD; } else { bool head = (verb == Verb.HEAD); // try to load the assembly System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(uri.Host); Version version = assembly.GetName().Version; DateTime timestamp = new DateTime(2000, 1, 1).AddDays(version.Build).AddSeconds(version.Revision * 2); // check if request is just about re-validation if (!head && request.CheckCacheRevalidation(timestamp)) { reply = DreamMessage.NotModified(); } else { try { System.IO.Stream stream = assembly.GetManifestResourceStream(uri.Path.Substring(1)); if (stream != null) { MimeType mime = MimeType.New(uri.GetParam(DreamOutParam.TYPE, null)) ?? MimeType.BINARY; reply = new DreamMessage(DreamStatus.Ok, null, mime, stream.Length, head ? System.IO.Stream.Null : stream); if (head) { stream.Close(); } else { reply.SetCacheMustRevalidate(timestamp); } } else { reply = DreamMessage.NotFound("could not find resource"); } } catch (System.IO.FileNotFoundException) { reply = DreamMessage.NotFound("could not find resource"); } catch (Exception e) { reply = DreamMessage.InternalError(e); } } } response.Return(reply); yield break; }
//--- Constructors --- /// <summary> /// Create a new instance for a <see cref="DreamStatus.NotFound"/> condition. /// </summary> /// <param name="message">Text message to use for <see cref="Exception.Message"/> and the internal <see cref="DreamMessage"/>.</param> public DreamNotFoundException(string message) : base(DreamMessage.NotFound(message), message) { }