예제 #1
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(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)
            {
                if (log.IsWarnEnabled)
                    log.Warn("Proxy-uri option malformed: " + e.Message);
                return new Response(StatusCode.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(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)
            {
                if (log.IsWarnEnabled)
                    log.Warn("Problems during the http/coap translation: " + e.Message);
                return new Response(StatusCode.BadGateway);
            }
        }