Пример #1
0
        public static void doService(string method, string query, ServletInfo info,
                                     Map <string, string> requestHeader,
                                     Stream input, Stream output)
        {
            if (info.servlet == null)
            {
                info.servlet = createServlet(info);
            }

            MemoryStream            outputBuffer = new MemoryStream();
            HttpServletResponseImpl resp
                = new HttpServletResponseImpl(outputBuffer);

            HttpServletRequest req;

            if (method == "GET")
            {
                Map <string, string[]> map;
                map = stringToMap(query);
                req = new HttpServletRequestImpl("GET", requestHeader, map, resp, info.webApp);
            }
            else if (method == "POST")
            {
                string contentType = requestHeader.get("CONTENT-TYPE");
                int    contentLength
                    = Int32.Parse(requestHeader.get("CONTENT-LENGTH"));
                if (contentType.ToUpper().StartsWith("MULTIPART/FORM-DATA"))
                {
                    req = MultiPartParser.parse(requestHeader, input,
                                                contentType, contentLength,
                                                resp, info.webApp);
                }
                else
                {
                    Map <string, string[]> map;
                    string line = readToSize(input, contentLength);
                    map = stringToMap(line);
                    req = new HttpServletRequestImpl("POST", requestHeader, map,
                                                     resp, info.webApp);
                }
            }
            else
            {
                throw new Exception("BAD METHOD:" + method);
            }

            info.servlet.service(req, resp);

            if (resp.status == HttpServletResponse.SC_OK)
            {
                ResponseHeaderGenerator hg
                    = new ResponseHeaderGeneratorImpl(resp.cookies);
                SendResponse.sendOkResponseHeader(output, resp.contentType, hg);

                if (resp.printWriter != null)
                {
                    resp.printWriter.Flush();
                }
                byte[] outputBytes = outputBuffer.ToArray();
                foreach (byte b in outputBytes)
                {
                    output.WriteByte(b);
                }
            }
            else if (resp.status == HttpServletResponse.SC_FOUND)
            {
                string redirectLocation;
                if (resp.redirectLocation.StartsWith("/"))
                {
                    string host = requestHeader.get("HOST");
                    redirectLocation = "http://"
                                       + ((host != null) ? host : Constants.SERVER_NAME)
                                       + resp.redirectLocation;
                }
                else
                {
                    redirectLocation = resp.redirectLocation;
                }
                SendResponse.sendFoundResponse(output, redirectLocation);
            }
        }