使用“Location”头进行CGI URL转发 - 只部分转发?

0 投票
1 回答
606 浏览
提问于 2025-04-16 15:39

大家好,

我有一个用Python写的CGI脚本,里面使用了

print "Location: [nextfilename]"
print

在“转发”之后(这里的引号是有原因的,稍后会解释),我能看到它转发到的页面的HTML内容没问题,但所有的图片等都不显示。地址栏里仍然显示的是CGI脚本的地址,而不是HTML文件的地址。如果我直接访问HTML文件,它显示得很好。

基本上,这个存放在cgi-bin里的CGI脚本,试图用相对链接来渲染图片,但这些链接是坏的。

我该如何真正转发到下一个页面,而不仅仅是通过CGI脚本渲染下一个页面呢?

我仔细检查了脚本,确保没有使用任何会干扰这个过程的print htmlDoc命令。

相关的代码部分:

def get_nextStepName():
        """Generates a random filename."""
        nextStepBuilder  = ["../htdocs/bcc/"]
        fileLength = random.randrange(10)+5
        for i in range(fileLength):
                j = random.choice(varLists.ALPHANUM)
                nextStepBuilder.append(j)
        nextStepName = ""
        for char in nextStepBuilder:
                nextStepName += char
        nextStepName += ".html"
        return nextStepName

def make_step2(user, password, email, headerContent, mainContent, sideSetup, sideContent, footerContent):
        """Creates the next step of user registration and logs valid data to a pickle for later confirmation."""
        nextStepName = get_nextStepName()
        mainContent = "<h1>Step Two: The Nitty Gritty</h1>"
        mainContent += "<p>User Name: %s </p>" % (user)
        mainContent += """\
               [HTML CODE GOES HERE]
                        """
        htmlDoc = htmlGlue.glue(headerContent, mainContent, sideSetup, sideContent, footerContent)
        f = open(nextStepName, "w")
        f.write(htmlDoc)
        f.close()

        nextStepName = nextStepName[9:] #truncates the ../htdocs part of the filename to fix a relational link issue when redirecting
        gotoNext(nextStepName)

def gotoNext(filename):
        nextLocation = "Location:"
        nextLocation += filename
        print(nextLocation)
        print

有什么想法吗?非常感谢。CGI对我来说是个新东西。

1 个回答

2

你还需要发送一个30X的Status头信息。具体细节可以参考RFC 2616。

撰写回答