public void GetCoapRequest_BadMethod()
        {
            HttpRequest http = new HttpRequest("FOOBAR /foo HTTP/1.1\n" + Host);

            TranslationException e = Assert.Throws <TranslationException>(() =>
                                                                          HttpTranslator.GetCoapRequest(http, "xx", true));

            Assert.That(e.Message, Is.EqualTo("FOOBAR method not mapped"));
        }
        public void GetCoapRequest_SimpleForm2()
        {
            string      proxyUri = "coaps://coap.example.com/resource";
            HttpRequest http     = new HttpRequest("GET /hc/?target=" + proxyUri + " HTTP/1.1\n" + Host);

            Request req = HttpTranslator.GetCoapRequest(http, "hc/?target={+tu}", true);

            Assert.That(req.Method, Is.EqualTo(Method.GET));
            Assert.That(req.ProxyUri.ToString(), Is.EqualTo(proxyUri));
        }
Beispiel #3
0
        /// <summary>
        /// Code to do the mapping and send out the request to an http server
        /// </summary>
        /// <param name="incomingCoapRequest">request to be mapped</param>
        /// <returns>response to return</returns>
        protected override Response ForwardRequest(Request incomingCoapRequest)
        {
            // check the invariant: the request must have the proxy-uri set
            if (!incomingCoapRequest.HasOption(OptionType.ProxyUri))
            {
                _Log.Warn("Proxy-uri option not set.");
                return(new Response(StatusCode.BadOption));
            }

            // remove the fake uri-path
            incomingCoapRequest.RemoveOptions(OptionType.UriPath); // HACK

            // get the proxy-uri set in the incoming coap request
            Uri proxyUri;

            try {
                proxyUri = incomingCoapRequest.ProxyUri;
            }
            catch (UriFormatException e) {
                _Log.Warn(m => m("Proxy-uri option malformed: {0}", e.Message));
                return(new Response(StatusCode.BadOption));
            }

            WebRequest httpRequest;

            try {
                httpRequest = HttpTranslator.GetHttpRequest(incomingCoapRequest);
            }
            catch (TranslationException e) {
                _Log.Warn(m => m("Problems during the http/coap translation: {0}", e.Message));
                return(new Response(StatusCode.BadGateway));
            }

            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

            DateTime timestamp = DateTime.Now;

            try {
                Response coapResponse = HttpTranslator.GetCoapResponse(httpResponse, incomingCoapRequest);
                coapResponse.Timestamp = timestamp;
                return(coapResponse);
            }
            catch (TranslationException e) {
                _Log.Warn(m => m("Problems during the http/coap translation: {0}", e.Message));
                return(new Response(StatusCode.BadGateway));
            }
        }
        public void GetCoapRequest_SimpleForm()
        {
            HttpRequest http;

            string proxyUri = "coap://coap.example.com/resource";

            http = new HttpRequest("GET /hc/" + proxyUri + " HTTP/1.1\n" + Host);

            Request req = HttpTranslator.GetCoapRequest(http, "hc/{+tu}", true);

            Assert.That(req.Method, Is.EqualTo(Method.GET));
            Assert.That(req.ProxyUri.ToString(), Is.EqualTo(proxyUri));


            proxyUri = "//coap.example.com/resource";
            http     = new HttpRequest("GET /hc/" + proxyUri + " HTTP/1.1\n" + Host);

            TranslationException e = Assert.Throws <TranslationException>(() =>
                                                                          HttpTranslator.GetCoapRequest(http, "hc/{+tu}", true));

            Assert.That(e.Message, Is.EqualTo("Schema is required"));

            proxyUri = "coap://coap.example.com/?query=1";
            http     = new HttpRequest("GET /hc/" + proxyUri + " HTTP/1.1\n" + Host);

            req = HttpTranslator.GetCoapRequest(http, "hc/{+tu}", true);
            Assert.That(req.Method, Is.EqualTo(Method.GET));
            Assert.That(req.ProxyUri.ToString(), Is.EqualTo(proxyUri));

            proxyUri = "coap://coap.example.com/resource?query=1";
            http     = new HttpRequest("GET /hc/" + proxyUri + " HTTP/1.1\n" + Host);

            req = HttpTranslator.GetCoapRequest(http, "hc/{+tu}", true);
            Assert.That(req.Method, Is.EqualTo(Method.GET));
            Assert.That(req.ProxyUri.ToString(), Is.EqualTo(proxyUri));

            proxyUri = "coap://coap.example.com:5848/resource";
            http     = new HttpRequest("GET /hc/" + proxyUri + " HTTP/1.1\n" + Host);

            req = HttpTranslator.GetCoapRequest(http, "hc/{+tu}", true);
            Assert.That(req.Method, Is.EqualTo(Method.GET));
            Assert.That(req.ProxyUri.ToString(), Is.EqualTo(proxyUri));
        }
        private CoapDotNetHttpResponse Proxy(CoapDotNetHttpRequest request, Uri coapUri)
        {
            var coapRequest = HttpTranslator.GetCoapRequest(request, Request.Url.SiteBase, true);

            coapRequest.URI = coapUri;

            //Setup response handler
            Response response = null;
            EventHandler <ResponseEventArgs> responseHandler = null;

            responseHandler = (_, e) =>
            {
                response             = e.Response;
                coapRequest.Respond -= responseHandler;
            };
            coapRequest.Respond += responseHandler;

            //send request
            coapRequest.Send();

            DateTime start = DateTime.Now;

            // ReSharper disable once LoopVariableIsNeverChangedInsideLoop
            while (response == null && (DateTime.Now - start).TotalSeconds < 30)
            {
                Thread.Sleep(1);
            }

            if (response == null)
            {
                return(null);
            }

            //Turn COAP response into HTTP response
            var httpResponse = new CoapDotNetHttpResponse {
                MaxAge = response.MaxAge
            };

            HttpTranslator.GetHttpResponse(request, response, httpResponse);

            return(httpResponse);
        }
Beispiel #6
0
        protected override Response ForwardRequest(Request incomingCoapRequest)
        {
            // check the invariant: the request must have the proxy-uri set
            if (!incomingCoapRequest.HasOption(OptionType.ProxyUri))
            {
                if (log.IsWarnEnabled)
                {
                    log.Warn("Proxy-uri option not set.");
                }
                return(new Response(Code.BadOption));
            }

            // remove the fake uri-path
            incomingCoapRequest.RemoveOptions(OptionType.UriPath); // HACK

            // get the proxy-uri set in the incoming coap request
            Uri proxyUri;

            try
            {
                proxyUri = incomingCoapRequest.ProxyUri;
            }
            catch (UriFormatException e)
            {
                if (log.IsWarnEnabled)
                {
                    log.Warn("Proxy-uri option malformed: " + e.Message);
                }
                return(new Response(Code.BadOption));
            }

            WebRequest httpRequest = null;

            try
            {
                httpRequest = HttpTranslator.GetHttpRequest(incomingCoapRequest);
            }
            catch (TranslationException e)
            {
                if (log.IsWarnEnabled)
                {
                    log.Warn("Problems during the http/coap translation: " + e.Message);
                }
                return(new Response(Code.BadGateway));
            }

            // accept the request sending a separate response to avoid the timeout
            // in the requesting client
            incomingCoapRequest.Accept();

            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            Int64           timestamp    = DateTime.Now.Ticks;

            try
            {
                Response coapResponse = HttpTranslator.GetCoapResponse(httpResponse, incomingCoapRequest);
                coapResponse.Timestamp = timestamp;
                return(coapResponse);
            }
            catch (TranslationException e)
            {
                if (log.IsWarnEnabled)
                {
                    log.Warn("Problems during the http/coap translation: " + e.Message);
                }
                return(new Response(Code.BadGateway));
            }
        }
Beispiel #7
0
 public void Process(IHttpRequest httpRequest, IHttpResponse httpResponse)
 {
     Request coapRequest = HttpTranslator.GetCoapRequest(httpRequest, _localResource, _proxyingEnabled);
 }