Ejemplo n.º 1
0
        /// <summary>
        /// Gets the coap request. Creates the CoAP request from the HTTP method and
	    /// mapping it through the properties file. The uri is translated using
	    /// regular expressions, the uri format expected is either the embedded
	    /// mapping (http://proxyname.domain:80/proxy/coapserver:5683/resource
	    /// converted in coap://coapserver:5683/resource) or the standard uri to
	    /// indicate a local request not to be forwarded. The method uses a decoder
	    /// to translate the application/x-www-form-urlencoded format of the uri. The
	    /// CoAP options are set translating the headers. If the HTTP message has an
	    /// enclosing entity, it is converted to create the payload of the CoAP
	    /// message; finally the content-type is set accordingly to the header and to
        /// the entity type.
        /// </summary>
        /// <param name="httpRequest">the http request</param>
        /// <param name="proxyResource"></param>
        /// <param name="proxyingEnabled"></param>
        /// <returns></returns>
        public static Request GetCoapRequest(IHttpRequest httpRequest, String proxyResource, Boolean proxyingEnabled)
        {
            if (httpRequest == null)
                throw ThrowHelper.ArgumentNull("httpRequest");
            if (proxyResource == null)
                throw ThrowHelper.ArgumentNull("proxyResource");

            Method coapMethod;
            if (!http2coapMethod.TryGetValue(httpRequest.Method, out coapMethod))
                throw ThrowHelper.TranslationException(httpRequest.Method + " method not mapped");

            // create the request
            Request coapRequest = new Request(coapMethod);

            // get the uri
            String uriString = httpRequest.RequestUri;
            // remove the initial "/"
            if (uriString.StartsWith("/"))
                uriString = uriString.Substring(1);
            // TODO URLDecode

            // if the uri contains the proxy resource name, the request should be
            // forwarded and it is needed to get the real requested coap server's
            // uri
            // e.g.:
            // /proxy/[::1]:5684/helloWorld
            // proxy resource: /proxy
            // coap server: [::1]:5684
            // coap resource: helloWorld
            Regex regex = new Regex(".?" + proxyResource + ".*");
            if (regex.IsMatch(uriString))
            {
                // find the first occurrence of the proxy resource
                Int32 index = uriString.IndexOf(proxyResource);
                // delete the slash
                index = uriString.IndexOf('/', index);
                uriString = uriString.Substring(index + 1);

                if (proxyingEnabled)
                {
                    // if the uri hasn't the indication of the scheme, add it
                    if (!uriString.StartsWith("coap://") &&
                        !uriString.StartsWith("coaps://"))
                    {
                        uriString = "coap://" + uriString;
                    }

                    // the uri will be set as a proxy-uri option
                    // set the proxy-uri option to allow the lower layers to underes
                    coapRequest.SetOption(Option.Create(OptionType.ProxyUri, uriString));
                }
                else
                {
                    coapRequest.URI = new Uri(uriString);
                }

                // TODO set the proxy as the sender to receive the response correctly
                //coapRequest.PeerAddress = new EndpointAddress(IPAddress.Loopback);
            }
            else
            {
                // if the uri does not contains the proxy resource, it means the
                // request is local to the proxy and it shouldn't be forwarded

                // set the uri string as uri-path option
                coapRequest.UriPath = uriString;
            }

            // translate the http headers in coap options
            IEnumerable<Option> coapOptions = GetCoapOptions(httpRequest.Headers);
            coapRequest.SetOptions(coapOptions);

            // the payload
            if (httpRequest.InputStream != null)
            {
                Byte[] tmp = new Byte[4096];
                MemoryStream ms = new MemoryStream(tmp.Length);
                Int32 read;
                while ((read = httpRequest.InputStream.Read(tmp, 0, tmp.Length)) > 0)
                {
                    ms.Write(tmp, 0, read);
                }
                coapRequest.Payload = ms.ToArray();
                coapRequest.ContentType = GetCoapMediaType(httpRequest.Headers["content-type"]);
            }

            return coapRequest;
        }