internal CommunicationModule _GetCommunicationModuleByName(string moduleName)
        {
            CommunicationModule module = null;

            m_CommunicationModules.TryGetValue(moduleName, out module);
            return(module);
        }
Example #2
0
        /// <summary>
        /// The primary http handler that routes the request to the proper handler.
        /// </summary>
        private void _HttpHandler(HttpListenerContext context)
        {
            //  Record the context into the thread-static storage
            s_current_http_ctx = context;

            //  The raw url will be starting with "/", trimming all authority part.
            var url           = Uri.UnescapeDataString(context.Request.RawUrl);
            var separator_idx = url.IndexOf('/', 1);

            //  If url is "" or "/", or "/{Service_Name}/ cannot be determined (lacking the second separator),
            //  it means that the request is pointing to the root endpoint.
            if (url.Length < 2 || url[1] == '?' || separator_idx == -1)
            {
                RootHttpHandler(context);
                goto cleanup;
            }

            var endpoint_name = url.Substring(1, separator_idx - 1);

            //  This request might be of the form "/{instance_id}/..."
            uint instance_id;

            if (UInt32.TryParse(endpoint_name, out instance_id))
            {
                //  In this case, we relay this message to the desired instance.
                m_HttpServer.RelayRequest((int)instance_id, context);
                goto cleanup;
            }

            //  This request might be of the form "/{module_name}/..."
            CommunicationModule module = null;

            if (m_CommunicationModules.TryGetValue(endpoint_name, out module))
            {
                /* Swallow the module_name part and reset the variables as if we're working on the root. */
                url           = url.Substring(separator_idx);
                separator_idx = url.IndexOf('/', 1);

                if (url.Length < 2 || url[1] == '?' || separator_idx == -1)
                {
                    module.GetRootHttpHandler()(context);
                }
                else
                {
                    endpoint_name = url.Substring(1, separator_idx - 1);
                    module.GetHttpRequestDispatcher()(context, endpoint_name, url.Substring(separator_idx + 1));
                }

                goto cleanup;
            }

            //  Otherwise, this request should be dispatched by
            DispatchHttpRequest(context, endpoint_name, url.Substring(separator_idx + 1));

cleanup:
            //  Erase the context as it is not being processed anymore.
            s_current_http_ctx = null;
        }