コード例 #1
0
ファイル: HttpServer.cs プロジェクト: xjc90s/libopenmetaverse
        public void AddHandler(string method, string contentType, string path, HttpRequestCallback callback)
        {
            HttpRequestSignature signature = new HttpRequestSignature();

            signature.Method      = method;
            signature.ContentType = contentType;
            signature.Path        = path;
            AddHandler(new HttpRequestHandler(signature, callback));
        }
コード例 #2
0
        public EventQueueServer(HttpServer server, string path)
        {
            HttpRequestSignature signature = new HttpRequestSignature();

            signature.Method      = "post";
            signature.ContentType = String.Empty;
            signature.Path        = path;
            HttpServer.HttpRequestCallback callback = new HttpServer.HttpRequestCallback(EventQueueHandler);
            HttpServer.HttpRequestHandler  handler  = new HttpServer.HttpRequestHandler(signature, callback);
            server.AddHandler(handler);
        }
コード例 #3
0
 protected void EventQueueHandler(HttpRequestSignature signature, ref HttpListenerContext context)
 {
 }
コード例 #4
0
ファイル: HttpServer.cs プロジェクト: xjc90s/libopenmetaverse
 public HttpRequestHandler(HttpRequestSignature signature, HttpRequestCallback callback)
 {
     Signature = signature;
     Callback  = callback;
 }
コード例 #5
0
ファイル: HttpServer.cs プロジェクト: xjc90s/libopenmetaverse
        protected void BeginGetContextCallback(IAsyncResult result)
        {
            HttpListenerContext context = null;

            // Retrieve the incoming request
            try { context = server.EndGetContext(result); }
            catch (Exception)
            {
            }

            if (isRunning)
            {
                // Immediately start listening again
                try { server.BeginGetContext(serverCallback, server); }
                catch (Exception)
                {
                    // Something went wrong, can't resume listening. Bail out now
                    // since this is a shutdown (whether it was meant to be or not)
                    return;
                }

                // Process the incoming request
                if (context != null)
                {
                    // Create a request signature
                    HttpRequestSignature signature = new HttpRequestSignature(context);

                    // Look for a signature match in our handlers
                    for (int i = 0; i < requestHandlers.Count; i++)
                    {
                        HttpRequestHandler handler = requestHandlers[i];

                        if (signature == handler.Signature)
                        {
                            // Request signature matched, handle it
                            try
                            {
                                handler.Callback(signature, ref context);
                            }
                            catch (Exception e)
                            {
                                try
                                {
                                    context.Response.StatusCode        = (int)HttpStatusCode.InternalServerError;
                                    context.Response.StatusDescription = e.ToString();
                                }
                                catch (Exception)
                                {
                                    Logger.Log(e.ToString(), Helpers.LogLevel.Error);
                                }
                            }
                            finally
                            {
                                context.Response.Close();
                            }

                            return;
                        }
                    }

                    // No registered handler matched this request's signature. Send a 404
                    context.Response.StatusCode        = (int)HttpStatusCode.NotFound;
                    context.Response.StatusDescription = String.Format(
                        "No request handler registered for Method=\"{0}\", Content-Type=\"{1}\", Path=\"{2}\"",
                        signature.Method, signature.ContentType, signature.Path);
                    context.Response.Close();
                }
            }
        }