Exemplo n.º 1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override protected void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException
        protected internal override void doPost(HttpServletRequest req, HttpServletResponse resp)
        {
            LOG.debug("Resource POST REQUEST ========= " + req.ToString());
            echoHeader(req, resp);
            string uri  = sanitise(req.RequestURI);
            string type = getType(uri);

            try
            {
                string contentType = req.ContentType;
                if (contentType.Equals("application/octet-stream"))
                {
                    LOG.debug("Resource POST REQUEST is a file upload");
                    processFileUpload(req, resp);
                }
                else if (contentType.StartsWith("multipart", StringComparison.Ordinal))
                {
                    LOG.debug("Resource POST REQUEST is a multipart file upload");
                    processMultiPart(req, resp);
                }
                else
                {
                    string content = getContent(req.InputStream);
                    if (contentType.Contains("application/x-www-form-urlencoded"))
                    {
                        try
                        {
                            generateResponse(resp, type, noddyKvpToXml(content, "UTF-8"));
                        }
                        catch (Exception)
                        {
                            LOG.warn("the content passed in isn't encoded as application/x-www-form-urlencoded: " + content);
                            resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
                        }
                    }
                    else if (content.Trim().StartsWith("<", StringComparison.Ordinal) || content.Trim().EndsWith("}", StringComparison.Ordinal))
                    {
                        generateResponse(resp, type, content);
                    }
                    else
                    {
                        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
                    }
                }
            }
            catch (Exception)
            {
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
            LOG.debug("Resource POST RESPONSE ========= " + req.ToString());
        }
Exemplo n.º 2
0
        public override void service(ServletRequest request, ServletResponse response)

        {
            HttpServletRequest  req = (HttpServletRequest)request;
            HttpServletResponse res = (HttpServletResponse)response;

            res.setContentType("text/html");

            if (_path == null)
            {
                res.sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            }

            string fileName = req.getParameter("file");

            if (fileName != null)
            {
                printFile(fileName, req, res);
                return;
            }

            PrintWriter @out = res.getWriter();

            @out.println("<pre>");

            printPath(@out, _path, 0);

            @out.println("</pre>");
        }
Exemplo n.º 3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException
        protected internal override void doGet(HttpServletRequest req, HttpServletResponse resp)
        {
            LOG.debug("Resource GET REQUEST ========= " + req.ToString());
            string uri       = sanitise(req.RequestURI);
            string id        = getId(uri);
            string type      = getType(uri);
            string extension = getExtension(uri);

            echoHeader(req, resp);
            echoQString(req, resp);
            try
            {
                if (string.ReferenceEquals(id, null))
                {
                    list(resp, type, extension);
                    headers(resp, extension, DEF_CHARSET);
                }
                else if (resources.get(type, id) == null)
                {
                    notFound(resp);
                }
                else
                {
                    if (resources.get(type, id).Deleted)
                    {
                        notFound(resp);
                    }
                    else
                    {
                        found(resp, type, id);
                        headers(resp, extension, DEF_CHARSET);
                    }
                }
            }
            catch (Exception)
            {
                resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
            }
            finally
            {
                LOG.debug("Resource GET RESPONSE ========= " + resp.ToString());
            }
        }
        /**
         * Service.
         */
        public override void service(HttpServletRequest request,
                                     HttpServletResponse response)

        {
            Env         env = null;
            WriteStream ws  = null;

            QuercusHttpServletRequest  req = new QuercusHttpServletRequestImpl(request);
            QuercusHttpServletResponse res = new QuercusHttpServletResponseImpl(response);

            try {
                string path = getPath(req);

                QuercusPage page;

                try {
                    page = getQuercus().parse(path);
                }
                catch (FileNotFoundException e) {
                    // php/2001
                    log.log(Level.FINER, e.ToString(), e);

                    response.sendError(HttpServletResponse.SC_NOT_FOUND);

                    return;
                }

                ws = openWrite(response);

                // php/2002
                // for non-Resin containers
                // for servlet filters that do post-request work after Quercus
                ws.setDisableCloseSource(true);

                // php/6006
                ws.setNewlineString("\n");

                QuercusContext quercus = getQuercus();

                env = quercus.createEnv(page, ws, req, res);

                // php/815d
                env.setPwd(path.getParent());

                quercus.setServletContext(new QuercusServletContextImpl(_servletContext));

                try {
                    env.start();

                    // php/2030, php/2032, php/2033
                    // Jetty hides server classes from web-app
                    // http://docs.codehaus.org/display/JETTY/Classloading
                    //
                    // env.setGlobalValue("request", env.wrapJava(request));
                    // env.setGlobalValue("response", env.wrapJava(response));
                    // env.setGlobalValue("servletContext", env.wrapJava(_servletContext));

                    StringValue prepend
                        = quercus.getIniValue("auto_prepend_file").ToStringValue(env);
                    if (prepend.length() > 0)
                    {
                        string prependPath = env.lookup(prepend);

                        if (prependPath == null)
                        {
                            env.error(L.l("auto_prepend_file '{0}' not found.", prepend));
                        }
                        else
                        {
                            QuercusPage prependPage = getQuercus().parse(prependPath);
                            prependPage.executeTop(env);
                        }
                    }

                    env.executeTop();

                    StringValue append
                        = quercus.getIniValue("auto_append_file").ToStringValue(env);
                    if (append.length() > 0)
                    {
                        string appendPath = env.lookup(append);

                        if (appendPath == null)
                        {
                            env.error(L.l("auto_append_file '{0}' not found.", append));
                        }
                        else
                        {
                            QuercusPage appendPage = getQuercus().parse(appendPath);
                            appendPage.executeTop(env);
                        }
                    }
                    //   return;
                }
                catch (QuercusExitException e) {
                    throw e;
                }
                catch (QuercusErrorException e) {
                    throw e;
                }
                catch (QuercusLineRuntimeException e) {
                    log.log(Level.FINE, e.ToString(), e);

                    ws.println(e.getMessage());
                    //  return;
                }
                catch (QuercusValueException e) {
                    log.log(Level.FINE, e.ToString(), e);

                    ws.println(e.ToString());

                    //  return;
                }
                catch (StackOverflowError e) {
                    RuntimeException myException
                        = new RuntimeException(L.l("StackOverflowError at {0}", env.getLocation()), e);

                    throw myException;
                }
                catch (Throwable e) {
                    if (response.isCommitted())
                    {
                        e.printStackTrace(ws.getPrintWriter());
                    }

                    ws = null;

                    throw e;
                }
                finally {
                    if (env != null)
                    {
                        env.close();
                    }

                    // don't want a flush for an exception
                    if (ws != null && env.getDuplex() == null)
                    {
                        ws.close();
                    }
                }
            }
            catch (QuercusDieException e) {
                // normal exit
                log.log(Level.FINE, e.ToString(), e);
            }
            catch (QuercusExitException e) {
                // normal exit
                log.log(Level.FINER, e.ToString(), e);
            }
            catch (QuercusErrorException e) {
                // error exit
                log.log(Level.FINE, e.ToString(), e);
            }
            catch (RuntimeException e) {
                throw e;
            }
            catch (Throwable e) {
                handleThrowable(response, e);
            }
        }
        public override void service(ServletRequest request, ServletResponse response)

        {
            QuercusHttpServletRequest req
                = new QuercusHttpServletRequestImpl((HttpServletRequest)request);
            HttpServletResponse res = (HttpServletResponse)response;

            string uri = QuercusRequestAdapter.getPageURI(req);

            CacheEntry entry = _cache.get(uri);

            if (entry == null)
            {
                string path    = getPath(req);
                string relPath = path.getUserPath();

                string mimeType = _context.getMimeType(uri);

                entry = new CacheEntry(path, relPath, mimeType);
                _cache.put(uri, entry);
            }
            else if (entry.isModified())
            {
                entry = new CacheEntry(entry.getPath(),
                                       entry.getRelPath(),
                                       entry.getMimeType());

                _cache.put(uri, entry);
            }

            string ifMatch = req.getHeader("If-None-Match");
            string etag    = entry.getEtag();

            if (ifMatch != null && ifMatch.equals(etag))
            {
                res.addHeader("ETag", etag);
                res.sendError(HttpServletResponse.SC_NOT_MODIFIED);
                return;
            }

            string lastModified = entry.getLastModifiedString();

            if (ifMatch == null)
            {
                string ifModified = req.getHeader("If-Modified-Since");

                bool isModified = true;

                if (ifModified == null)
                {
                }
                else if (ifModified.equals(lastModified))
                {
                    isModified = false;
                }
                else
                {
                    long ifModifiedTime;

                    QDate date = QDate.allocateLocalDate();

                    try {
                        ifModifiedTime = date.parseDate(ifModified);
                    } catch (Exception e) {
                        log.log(Level.FINER, e.ToString(), e);

                        ifModifiedTime = 0;
                    }

                    QDate.freeLocalDate(date);

                    isModified = ifModifiedTime == 0 ||
                                 ifModifiedTime != entry.getLastModified();
                }

                if (!isModified)
                {
                    if (etag != null)
                    {
                        res.addHeader("ETag", etag);
                    }

                    res.sendError(HttpServletResponse.SC_NOT_MODIFIED);
                    return;
                }
            }

            res.addHeader("ETag", etag);
            res.addHeader("Last-Modified", lastModified);

            string mime = entry.getMimeType();

            if (mime != null)
            {
                res.setContentType(mime);
            }

            res.setContentLength((int)entry.getLength());

            string method = req.getMethod();

            if (method.equalsIgnoreCase("HEAD"))
            {
                return;
            }

            string path = entry.getPath();

            if (path.isDirectory())
            {
                res.sendError(HttpServletResponse.SC_NOT_FOUND);

                return;
            }
            else if (!path.canRead())
            {
                res.sendError(HttpServletResponse.SC_NOT_FOUND);

                return;
            }

            OutputStream os = response.getOutputStream();

            path.writeToStream(os);
        }