示例#1
0
 internal HttpContext(HttpClient client)
 {
     Server = client.Server.ServerUtility;
     Request = new HttpRequest(client);
     Response = new HttpResponse(this);
 }
示例#2
0
        private void nServer_RequestReceived(object sender, HttpRequestEventArgs e)
        {
            e.Response.Headers.Add("Connection", "close");
            e.Response.Headers.Add("Content-Encoding", "gzip");
            e.Response.Headers.Add("Content-Type", "application/xml;charset=utf-8");
            e.Response.Headers.Add("Status-Code", "200");

            log.Info(String.Format("Received Http-{0} request from {1}.", e.Request.HttpMethod, e.Request.RawUrl));

            Byte[] baResponseArray = null;
            List<String> splittedPath = new List<String>(e.Request.Path.Split('/'));

            String ioPath = Path.Combine(DataEx.dir_Server, e.Request.Path.Substring(1) + ".xml");

            if (splittedPath.Count >= 3)
            {
                String targetClassString = changeCaseFirst(splittedPath[2], true);
                if (splittedPath.Count == 3)
                {
                    splittedPath.Insert(0, "");
                    targetClassString = "Root";
                }
                Double dummy;
                Boolean isNumber = Double.TryParse(splittedPath[3], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out dummy);
                String targetMethodString = changeCaseFirst(isNumber ? splittedPath[4] : splittedPath[3], false);
                if (!supportedMethods.Contains(targetMethodString))
                {
                    log.Warn(String.Format("Method for {0} wasn't found, using fallback XML method.", targetMethodString));
                    if (File.Exists(ioPath))
                    {
                        log.Info(String.Format("Reading XML file {0}.", ioPath));
                        baResponseArray = getResponseData(File.ReadAllText(ioPath, Encoding.UTF8));
                    }
                    else
                    {
                        log.Warn(String.Format("File {0} wasn't found, sending only 200OK.", ioPath));
                    }
                }
                else
                {
                    Type targetClass = Type.GetType("OfflineServer.Servers.Http.Classes." + targetClassString);
                    MethodInfo targetMethod = targetClass.GetMethod(targetMethodString);
                    request = e.Request;
                    log.Info(String.Format("Processing OfflineServer.HttpServer.Classes.{0}.{1}().", targetClassString, targetMethodString));
                    baResponseArray = getResponseData((string)targetMethod.Invoke(null, null));
                }
            }
            else
            {
                if (File.Exists(ioPath))
                {
                    log.Info(String.Format("Reading XML file {0}.", ioPath));
                    baResponseArray = getResponseData(File.ReadAllText(ioPath, Encoding.UTF8));
                }
                else
                {
                    log.Warn(String.Format("File {0} wasn't found, sending only 200OK.", ioPath));
                }
            }

            if (baResponseArray == null) baResponseArray = getResponseData(" ");
            e.Response.OutputStream.Write(baResponseArray, 0, baResponseArray.Length);
            e.Response.OutputStream.Flush();

            // e.Request.RequestType gives the method used, GET - POST - PUSH etc.
            // e.Request.Url gives the full Uri including EVERYTHING
            // e.Request.RawUrl gives the Path following the IP. EX: if 127.0.0.1:4444/test/path.xml?test=true then /test/path.xml?test=true
            // e.Request.Path gives only the Path, not adding the params at the end. EX: if 127.0.0.1:4444/test/path.xml?test=true then /test/path.xml
            // e.Request.Params gives only the Params, not adding anything else.
        }