如何使 apache 让我用位置标头定义"200 OK"响应的正文?



以下是我在cgi-bin目录中的内容:

cgi-bin> cat test1
#!/bin/sh
echo "Status: 200 OK"
echo "Content-type: text/plain"
echo
echo "Hello world 1"
cgi-bin> cat test2
#!/bin/sh
echo "Status: 200 OK"
echo "Content-type: text/plain"
echo "Location: /cgi-bin/test1"
echo
echo "Hello world 2"
cgi-bin> cat test3
#!/bin/sh
echo "Status: 201 Created"
echo "Content-type: text/plain"
echo "Location: /cgi-bin/test1"
echo
echo "Hello world 3"

当我对每一个进行get时,我会得到以下结果:

cgi-bin> curl -i https://localhost/cgi-bin/test1
HTTP/1.1 200 OK
Date: Wed, 20 Nov 2019 15:56:43 GMT
Server: Apache
Transfer-Encoding: chunked
Content-Type: text/plain
Hello world 1
cgi-bin> curl -i https://localhost/cgi-bin/test2
HTTP/1.1 200 OK
Date: Wed, 20 Nov 2019 15:56:44 GMT
Server: Apache
Transfer-Encoding: chunked
Content-Type: text/plain
Hello world 1
cgi-bin> curl -i https://localhost/cgi-bin/test3
HTTP/1.1 201 Created
Date: Wed, 20 Nov 2019 15:56:45 GMT
Server: Apache
Location: /cgi-bin/test1
Transfer-Encoding: chunked
Content-Type: text/plain
Hello world 3

请注意/cgi-bin/test2忽略shell脚本打印的"Hello world 2"主体,而是显示test1的输出。如何在不更改Status或Location标头的情况下使用"Hello world 2"作为响应?

编辑:更新了curl示例以包含HTTP头。刚刚注意到/cgi-bin/test2在响应中也不包括Location标头。

如我所愿:

[root@pe610 cgi-bin]# curl http://localhost/cgi-bin/test2
Hello world 1
[root@pe610 cgi-bin]# curl http://localhost/cgi-bin/test1
Hello world 1
[root@pe610 cgi-bin]# apachectl -V
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov  5 2018 01:47:09
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
threaded:     no
forked:     yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"

Location响应标头指示页面重定向到的URL。它只在提供3xx(重定向(或201时提供意义(已创建(状态响应。

在您的情况下,您发送了一个带有位置的200,这是不符合要求的。Curl可能假设它是201,所以下面是应用的内容:

HTTP 201创建成功状态响应代码指示请求已成功,并导致创建了一个资源。这个在发送回此响应之前,有效地创建了新资源并且新资源在消息的正文中返回位置是请求的URL或位置标头。

由于您正在发送冲突的标头,因此无法要求Curl以有意义的方式处理此问题。

出于好奇,你为什么还要这么做?资源应该是一个200201或重定向,而不是它们的混合。如果您想将Location标头用于其他内容,那么您需要发送一个自定义标头,如my-custom-location,并对其进行相应处理。

相关内容

最新更新