3 Replies Latest reply on Sep 6, 2012 11:42 AM by Miguel-F

    cfhttp from application.cfm?

    richardELeach Level 1

      Hi Gang-

       

      I inherited some code that I'm trying to clean up and get production-ready.

       

      Because of conditions I can't change I'm looking for a short term solution for the following:

       

      The application in question was originally written with web server url rewriting on IIS 6 and CF8, among other things that are no longer supported.

       

      I finally configured the application so that the home page loads but here's the rub: The app was originally architected with all of its links in the folder "/webroot/links/" however thanks to url rewriting the "/links/" never appeared in the urls - they were previously hidden from the urls thanks to url rewriting. (The new server won't have the url rewriting).  Because of this I have to manaully change all the links/hrefs in the code to run from

      "http://localhost/subjectmatter/" to "http://localhost/links/subjectmatter", which isn't desireable, but a fix for now.

       

      I wrote the following code to drop into the application.cfm file of the app so that all page requests could be checked. The problem? This code won't run from the application.cfm, it seems to run from any other page I manually paste it into but when it's in application.cfm it just times out with no error message. Anyone have any ideas why this times out inside of application.cfm but not when called otherwise?

       

      START CODE

      <!--- REQUEST CHECKER: CHECK THE REQUESTED URL FOR 404 BECAUSE OF MISSING "/LINKS" IN URL. IF TRUE REDIRECT REQUEST BUT WITH ADDED "/LINKS" IN URL. --->

       

      <!--- put the requested url into a var called "testUrl" --->

      <cfset requestedUrl="http://"&"#server_name#"&"#cgi.PATH_INFO#">

       

      <!--- Attempt to retrieve the testURL --->

      <cfhttp method="head" url="#requestedUrl#" resolveurl="no" throwonerror="no" />

       

      <!--- Check status code returned and is 404 --->

      <cfif IsDefined("cfhttp.responseheader.status_code")>

                <cfif cfhttp.responseheader.status_code EQ "404">

                    <!--- if the requested url doesn't contain "links" as the first subdirectory  --->

                    <cfif #listFirst(cgi.PATH_INFO,"/")# IS NOT "links">

                                    <!--- Attempt to retrieve the requested url with "/links" added to the requested file path --->

                  <cfset newUrl="http://"&"#server_name#"&"/links"&"#cgi.PATH_INFO#">

                  <cfhttp method="head" url="#newUrl#" resolveurl="no" throwonerror="no" name="newUrlCheck" />

                  <cfif IsDefined("newUrlCheck.responseheader.status_code") AND newUrlCheck.responseheader.status_code NEQ "404">

                      <cflocation url="#newUrl#">

                  <cfelse>

                      404 from application.cfm: PAGE NOT FOUND!

                      <cfabort>  

                  </cfif>

              <cfelse>

              Requested url already contains "links"!

                          </cfif>

          </cfif>

      </cfif>

       

      END CODE

       

      Thanks in advance for your help!

       

      Rich

        • 1. Re: cfhttp from application.cfm?
          Miguel-F Level 3

          Not sure if it is just a typo when you posted but on your second cfhttp request you are specifying a 'name' attribute.  That does not exist for that tag.  From your code it looks like you are trying to assign the response to a different variable so you should use the 'result' attribute.

           

          You have:

                      <cfhttp method="head" url="#newUrl#" resolveurl="no" throwonerror="no" name="newUrlCheck" />

           

          It should be:

                      <cfhttp method="head" url="#newUrl#" resolveurl="no" throwonerror="no" result="newUrlCheck" />

           

          Edit:

          Sorry just read documentation and the 'name' attribute does exist however the documentation states that it is only for GET and POST methods only.

           

          Message was edited by: Miguel-F

          • 2. Re: cfhttp from application.cfm?
            richardELeach Level 1

            Miguel

             

            ... sadly, it never even makes it to that point (although I certainly made your suggested correction, thanks). It always times out at the first cfhttp call.... but only when it's running inside of the application.cfm, when run from other files it runs as expected. Makes me wonder about an infinite loop of some kind, am I using the correct vars to identify the requested page? I tested that and believe so but still, the timeout....

             

            Thanks so much for your reply!

             

            Rich

            • 3. Re: cfhttp from application.cfm?
              Miguel-F Level 3

              I also noticed that you are referencing a variable 'server_name'.  How is that being set?  Did you mean 'cgi.server_name' ?

               

              Also, you can set the throwOnError to yes instead of no and see if you get any error details.

               

              You can also set a timeout attribute on the cfhttp tag if you wish.

               

              At this point I guess I would put a cftry/cfcatch block around that cfhttp call to see what it is complaining about.