Beispiel #1
0
        public void Init()
        {
            mailResponder = new MailResponder();

            server = new HttpListenerServer(new string[] { URL }, mailResponder);
            server.Start();

            HttpWebRequest requestTemplate = (HttpWebRequest)HttpWebRequest.Create(URL);
            requestTemplate.Timeout = 6000;
            requestTemplate.Proxy = null;
            transceiver = new HttpTransceiver(requestTemplate);
            proxy = new GenericRequestor(transceiver, MailResponder.Protocol);
        }
        //TODO: apparently this doesn't compile in Mono - investigate
        //public Action<Exception, IAsyncResult> ExceptionHandler { get; set; }

        protected void HttpListenerCallback(IAsyncResult result)
        {
            try
            {
                HttpListener listener = (HttpListener)result.AsyncState;
                if (_listener != listener) //the server which began this callback was stopped - just exit
                {
                    return;
                }
                HttpListenerContext context = listener.EndGetContext(result);

                listener.BeginGetContext(HttpListenerCallback, listener); //spawn listening for next request so it can be processed while we are dealing with this one

                //process this request
                if (!context.Request.HttpMethod.Equals("POST"))
                {
                    throw new AvroRuntimeException("HTTP method must be POST");
                }
                if (!context.Request.ContentType.Equals("avro/binary"))
                {
                    throw new AvroRuntimeException("Content-type must be avro/binary");
                }

                byte[] intBuffer = new byte[4];
                var    buffers   = HttpTransceiver.ReadBuffers(context.Request.InputStream, intBuffer);

                buffers = _responder.Respond(buffers);
                context.Response.ContentType     = "avro/binary";
                context.Response.ContentLength64 = HttpTransceiver.CalculateLength(buffers);

                HttpTransceiver.WriteBuffers(buffers, context.Response.OutputStream);

                context.Response.OutputStream.Close();
                context.Response.Close();
            }
            catch (Exception ex)
            {
                //TODO: apparently this doesn't compile in Mono - investigate
                //if (ExceptionHandler != null)
                //    ExceptionHandler(ex, result);
                //else
                //    Debug.Print("Exception occured while processing a request, no exception handler was provided - ignoring", ex);
                Debug.Print("Exception occured while processing a web request, skipping this request: ", ex);
            }
        }