ad_serve_html_page

one of the documented procedures in this installation of the ACS
Usage:
ad_serve_html_page   ignore
What it does:
The procedure that actually serves all the HTML pages on an ACS. It looks first to see if the file is in one of the naked_html directories. If so, it simply returns the raw bytes. It then looks to see if the ad_dnd_tag ("do not disturb") comment pattern is present. Again, if so, it simply returns. Otherwise, the procedure tries to add comments and related links. If the database is busy, it will simply add links to comments and related links.
Defined in: /web/philip/tcl/ad-html.tcl

Source code:


    set url_stub [ad_conn full_url]
    if { [empty_string_p $url_stub] } {
        set url_stub [ns_conn url]
    }

    set full_filename [ad_conn file]
    if { [empty_string_p $full_filename] } {   
	set full_filename [ns_url2file $url_stub]
    }

    foreach naked_pattern [ad_naked_html_patterns] {
	if [string match $naked_pattern $url_stub] {
	    ns_returnfile 200 text/html $full_filename
	    return
	}
    }

    if { ![file exists $full_filename]} {
	# check to see if the file exists
	# if not, return a "file not found" message
	set file_name_url "[ad_parameter GlobalURLStub "" "/global"]/file-not-found.html"
	set full_path [ns_url2file $file_name_url]
	if [file exists $full_path] {
	    ns_returnfile 404 text/html $full_path
	} else {
	    ns_return 404 text/plain "File not found"
	}
	return
    }

    set stream [open $full_filename r]
    set whole_page [read $stream]
    close $stream

    ## sometimes we don't want comments to come up
    ## for a given page
    if {[string first [ad_dnd_tag] $whole_page] != -1} {
	ns_return 200 text/html $whole_page
	return
    }

    # let's put in Amazon ads
    regsub -all -nocase "<!--AMAZON_CANON_EOS_SYSTEM-->" $whole_page [amazon_canon_eos_big_block] whole_page

    # let's figure out if we're going to try to insert Google ads
    set insert_google_ad_p 0
    foreach google_ad_pattern [ad_google_ad_patterns] {
	if [string match $google_ad_pattern $url_stub] {
	    set insert_google_ad_p 1
	    # matches at least one pattern, let's break out of the loop and stop wasting CPU
	    break
	}
    }
    # let's check to see if this is the index file or a directory (ends in /)
    set just_the_filename [file rootname [file tail $url_stub]]
    if { $just_the_filename == "index" || [file tail $url_stub] == "" } {
	# we don't want ads in the index files, which are carefully formatted
	set insert_google_ad_p 0
    }
    if {[string first [ad_no_ads_tag] $whole_page] != -1} {
	set insert_google_ad_p 0
    }
    if {[string first [ad_insert_ads_tag] $whole_page] != -1 } {
	# whatever else happened, possibly on an index page, the publisher wants ads
	set insert_google_ad_p 1
    }


    if { [regexp -nocase {(.*)</body>(.*)} $whole_page match pre_body post_body] } {
	# there was a "close body" tag, let's try to insert a comment
	# link at least
	# before we do anything else, let's stream out what we can
	if $insert_google_ad_p {
	    # let's try to insert a Google ad
	    # ns_log Notice "trying to insert a Google ad in $url_stub"
	    # we don't do "-all" so this should just be the first HR tag
	    regsub "<hr>" $pre_body "<hr>\n[google_right_column_ad]" pre_body
	    append pre_body "\n" [google_center_bottom_ad]
	}
	ad_return_top_of_page [static_add_curriculum_bar_if_necessary $pre_body]
	
	if { [catch { set db [ns_db gethandle -timeout -1] } errmsg] || [empty_string_p $db] } {
	    # the non-blocking call to gethandle raised a Tcl error; this
	    # means a db conn isn't free right this moment, so let's just
	    # return the page with a link
	    ns_log Notice "DB handle wasn't available in ad_serve_html_page"
	    ns_write "
<hr width=300>
<center>
<a href=\"/comments/for-one-page?url_stub=[ns_urlencode $url_stub]\">View/Add Comments</a> |
<a href=\"/links/for-one-page?url_stub=[ns_urlencode $url_stub]\">Related Links</a>
</center>
[google_analytics_code]
</body>$post_body"
        } else {
	    # we got a db connection
	    set moby_list [static_get_comments_and_links $db $url_stub $post_body]
	    # Release the DB handle
	    ns_db releasehandle $db
	    set comment_link_options_fragment [static_format_comments_and_links $moby_list]
	    # now decide what to do with the comments and links we're queried from the database
	    ns_write "$comment_link_options_fragment\n\n[google_analytics_code]</body>$post_body"
	}
    } else {
	# couldn't find a </body> tag
	ns_return 200 text/html $whole_page
    }


philg@mit.edu