Bypassing CloudFlare's time-out of 100 seconds

后端 未结 2 2112
心在旅途
心在旅途 2021-02-10 03:43

I am attempting to AJAX-ify my reports in order to bypass the 100 seconds time-out that CloudFlare imposes on requests that run through its site.

See Is it possible to i

相关标签:
2条回答
  • 2021-02-10 04:08

    In case anyone else has the same problem, I am posting how I finally got this working. I gave up trying to use AJAX to run the report, but instead ran the report via a Thread, but used AJAX to "poll" to check if the report had been created. What I did was basically as follows.

    Note that I stripped a lot out of my code, e.g. security routines and error checking routines, just to give the basic framework.

    I created a java class called ThreadMyReport

    public class ThreadMyReport implements Runnable {
    
        String fileID = "";
        Date dateOfReport = null;
    
        public ThreadMyReport(Date dateOfReport) {
            this.fileID= "MyReport_" + UUID.randomUUID();
            this.dateOfReport = dateOfReport;
        }
    
        public void run() {
            int a = ReportMyReport.loadAndSaveMyReport(dateOfReport, fileID);
        }
    
    
        public String getFileID() {
            return fileID;
        }
    }
    

    All my original code to generate the report is found in ReportMyReport.loadAndSaveMyReport. When the report is finished, then it saves a file with fileName fileID on the server.

    I then started a thread going to run the report

        ThreadMyReport a  = new ThreadMyReport(theDate);
        Thread theThread=new Thread(a);
        theThread.start();
        fileID=a.getFileID();
    

    I then added a javascript routine to check via AJAX every second whether the file had been created, and if it had been created then to display the report.

    <SCRIPT language="javascript">
    
    
        var myVar;
        myVar=setInterval(function (){
        $.post("post/post_checkReportReady_xml.jsp", {
           generatedReport: '<%=fileID%>'
        }, function(data) {
           if (data.indexOf("produced")>-1) {
               clearInterval(myVar);
               //display report
           }
           if (data.indexOf("failed")>-1) {
               clearInterval(myVar);
           }
        });
    
    }, 1000);
            </SCRIPT>
    

    The AJAX code looks like this:

         <%
        String result="";
    
        String generatedReport=(request.getParameter("generatedReport"));
    
        if(!"".equals(generatedReport)) {
            String fileName2="My directory/"+generatedReport+".xlsm"; 
            java.io.File f = new java.io.File(fileName2);
            if(f.exists()) { 
                result="produced";
            }
        }
    }
    
    %>
    <%=result%>
    
    0 讨论(0)
  • 2021-02-10 04:09

    Since cloudflare does not cache html or xhr, you can greycloud a subdomain but on the server use that as an alias. For example...

    In CloudFlare dns:

    • www 123.123.123.123 = orange (protected)
    • ajax 123.123.123.123 = grey (dns only)

    In your website control panel add ajax.mydomain.com as an alias.

    Finally, in your code use the fqdn that hits your server bypassing cloudflare.

    function ajaxReport() {
        var seconds = prompt("Please enter how many seconds you want the report to run", "5");
        $('#imgWaiting').show();
        $.post("//ajax.mydomain.com/post/post_ajaxReport.jsp",
      {
        theParam:seconds
      },function(data) {
        $('#imgWaiting').hide();
        window.location=data;
     });
    }
    

    This does expose your IP address. But there should be little to no performance penalty, depending on the content returned.

    0 讨论(0)
提交回复
热议问题