skip to Main Content

Here is what I have in a cgi-bin directory:

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"

And here is what I get when I do a GET against each one:

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

Notice that /cgi-bin/test2 ignores the “Hello world 2” body that shell script prints, and instead displays the output of test1. How can I make it use “Hello world 2” as its response, without changing the Status or Location headers?

Edit: updated curl examples to include HTTP headers. Just noticed /cgi-bin/test2 does not include the Location header in the response either.

2

Answers


  1. The Location response header indicates the URL to redirect a page to.
    It only provides a meaning when served with a 3xx (redirection) or 201
    (created) status response.

    In your case, you send a 200 with a Location, which is not compliant. Curl probably assumes it’s a 201, so here’s what’s applied:

    The HTTP 201 Created success status response code indicates that the
    request has succeeded and has led to the creation of a resource. The
    new resource is effectively created before this response is sent back
    and the new resource is returned in the body of the message, its
    location being either the URL of the request, or the content of the
    Location header.

    Since you’re sending conflicting headers, there’s no way you can ask Curl to handle that in a meaningful manner.

    Out of curiosity, why would you even want to do that? The resource should either be a 200, 201 or redirect, not a mix of them. If you want to use the Location header for something else, then you’ll need to send a custom header like my-custom-location and treat it accordingly.

    Login or Signup to reply.
  2. Works as expected for me:

    [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"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search