Example #1
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));
            }
        }
Example #2
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));
            }
        }