public string[] getParameterValues(string name)
        {
            string[] decoded;
            if (this.parameterMap != null)
            {
                string[] values = this.parameterMap.get(name);
                if (values == null)
                {
                    return(null);
                }
                decoded = new string[values.Length];

                for (int i = 0; i < values.Length; i++)
                {
                    decoded[i] = MyURLDecoder.decode(values[i],
                                                     this.characterEncoding);
                }
            }
            else
            {
                byte[][] data = this.byteParameterMap.get(name);
                if (data == null)
                {
                    return(null);
                }
                decoded = new string[data.Length];

                for (int i = 0; i < data.Length; i++)
                {
                    decoded[i] = Encoding.GetEncoding(this.characterEncoding).GetString(data[0]);
                }
            }
            return(decoded);
        }
Пример #2
0
        public void Run()
        {
            Stream output = null;
            Stream input  = null;

            try
            {
                input = new NetworkStream(socket);

                string line;
                string requestLine = null;
                string method      = null;
                Map <string, string> requestHeader = new HashMap <string, string>();
                while ((line = Util.readLine(input)) != null)
                {
                    if (line == "")
                    {
                        break;
                    }
                    if (line.StartsWith("GET"))
                    {
                        method      = "GET";
                        requestLine = line;
                    }
                    else if (line.StartsWith("POST"))
                    {
                        method      = "POST";
                        requestLine = line;
                    }
                    else
                    {
                        Util.parseHeader(requestHeader, line);
                    }
                }

                if (requestLine == null)
                {
                    return;
                }


                string reqUri = MyURLDecoder.decode(requestLine.Split(' ')[1],
                                                    "UTF-8");
                string[] pathAndQuery = reqUri.Split('?');
                string   path         = pathAndQuery[0];
                string   query        = null;
                if (pathAndQuery.Length > 1)
                {
                    query = pathAndQuery[1];
                }
                output = new NetworkStream(socket);

                string         appDir = path.Substring(1).Split('/')[0];
                WebApplication webApp = WebApplication.searchWebApplication(appDir);
                if (webApp != null)
                {
                    ServletInfo servletInfo
                        = webApp.searchServlet(path.Substring(appDir.Length + 1));
                    if (servletInfo != null)
                    {
                        ServletService.doService(method, query, servletInfo,
                                                 requestHeader, input, output);
                        return;
                    }
                }
                string   ext = null;
                string[] tmp = reqUri.Split('.');
                ext = tmp[tmp.Length - 1];

                if (path.EndsWith("/"))
                {
                    path += "index.html";
                    ext   = "html";
                }

                string realPath;
                try
                {
                    realPath = Path.GetFullPath(DOCUMENT_ROOT + path);
                }
                catch
                {
                    SendResponse.sendNotFoundResponse(output, ERROR_DOCUMENT);
                    return;
                }

                if (!realPath.StartsWith(Directory.GetCurrentDirectory() + "\\" + DOCUMENT_ROOT))
                {
                    SendResponse.sendNotFoundResponse(output, ERROR_DOCUMENT);
                    return;
                }
                else if (Directory.Exists(realPath))
                {
                    string host     = requestHeader.get("HOST");
                    string location = "http://"
                                      + ((host != null) ? host : Constants.SERVER_NAME)
                                      + path + "/";
                    SendResponse.sendMovePermanentlyResponse(output, location);
                    return;
                }
                try
                {
                    using (FileStream fis = new FileStream(realPath, FileMode.Open))
                    {
                        SendResponse.sendOkResponse(output, fis, ext);
                        fis.Close();
                    }
                }
                catch
                {
                    SendResponse.sendNotFoundResponse(output, ERROR_DOCUMENT);
                    return;
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally
            {
                try
                {
                    if (output != null)
                    {
                        output.Close();
                    }
                    if (input != null)
                    {
                        input.Close();
                    }
                    socket.Close();
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }
            }
        }