예제 #1
0
        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;
        }