Re: How to simple exit in CGI?

Pascal Bourguignon <[email protected]> Wed, 29 Mar 2017 02:15:04 +0200
Newsgroups gmane.lisp.clisp.general
Message-ID <[email protected]>
> On 29 Mar 2017, at 00:58, Jean Louis <[email protected]> wrote:
> 
> On Wed, Mar 29, 2017 at 12:57:00AM +0200, Pascal Bourguignon wrote
>> Then you have to debug it.
>> Have a look at http://www.cliki.net/TutorialClispDebugger <http://www.cliki.net/TutorialClispDebugger>
>> and try to find what function returned this error.
> 
> I did not find any reference on how to debug over HTTP. I would like
> to find the cause or find out how to avoid it.
> 
> In console it is giving redirect. On HTTP, it is hanging for longer
> time, then I get like now, in this attempt:
> 
> admin-> wget -O /dev/shm/test "http://localhost/test.cgi"; less /dev/shm/test 
> --2017-03-29 01:56:37--  http://localhost/test.cgi
> Resolving localhost... 127.0.0.1
> Connecting to localhost|127.0.0.1|:80... connected.
> HTTP request sent, awaiting response... 200 OK
> Length: unspecified
> Saving to: ‘/dev/shm/test’
> 
> /dev/shm/test        [ <=>      ]      38  --.-KB/s    in 0s      
> 
> 2017-03-29 01:57:13 (3.93 MB/s) - ‘/dev/shm/test’ saved [38]
> 
> *** - Ctrl-C: User break
> Break 1 [1]> 

Well, it looks like test.cgi was hung, not printing anything, and that your web server decided to kill it with a SIGINT.  This was interpreted as a C-c by clisp, and entered into the debugger, then the web server took that output and returned it.

Here’s how I catch and report errors, saving them into files:



I’ve left the call to (error "Simulated error.”)  for your tests.  
You must remove it (and recompile with make) before trying it for real.

In this case, we want to catch the errors before issuing the headers, so I collect them into a string errout, and then depending on whether there was an error to compute query, I print an error web page, or perform the normal processing.


    [pjb@despina :0.0 jean]$ rm /tmp/jean-cgi.*
    [pjb@despina :0.0 jean]$ make
    make: Nothing to be done for `all'.
    [pjb@despina :0.0 jean]$ ./jean.cgi </dev/null
    Content-Type: text/plain


    2017-03-29 02:04:20

    <1/89> #<system-function show-stack> 3
    <2/82> #<compiled-function system::print-backtrace>
    <3/78> #<compiled-function jean-cgi::print-backtrace>
    [64] frame binding variables (~ = dynamically):
      | ~ *print-level* <--> nil
    Printed 3 frames
    ERROR while ((setf jean-cgi::query (or (error "Simulated error.") (getenv "QUERY_STRING") (alexandria.0.dev:read-stream-content-into-string *standard-input*)))):
    Simulated error.

    [pjb@despina :0.0 jean]$ cat /tmp/jean-cgi.trace
    Found error Simulated error.

    2017-03-29 02:04:20

    <1/89> #<system-function show-stack> 3
    <2/82> #<compiled-function system::print-backtrace>
    <3/78> #<compiled-function jean-cgi::print-backtrace>
    [64] frame binding variables (~ = dynamically):
      | ~ *print-level* <--> nil
    Printed 3 frames
    ERROR while ((setf jean-cgi::query (or (error "Simulated error.") (getenv "QUERY_STRING") (alexandria.0.dev:read-stream-content-into-string *standard-input*)))):
    Simulated error.

    [pjb@despina :0.0 jean]$ cat /tmp/jean-cgi.errors

    2017-03-29 02:04:20

    <1/89> #<system-function show-stack> 3
    <2/82> #<compiled-function system::print-backtrace>
    <3/78> #<compiled-function jean-cgi::print-backtrace>
    [64] frame binding variables (~ = dynamically):
      | ~ *print-level* <--> nil
    Printed 3 frames
    ERROR while ((setf jean-cgi::query (or (error "Simulated error.") (getenv "QUERY_STRING") (alexandria.0.dev:read-stream-content-into-string *standard-input*)))):
    Simulated error.

    [pjb@despina :0.0 jean]$ wget --save-headers http://localhost/~pjb/cgi/jean/jean.cgi -O /tmp/out
    --2017-03-29 02:04:50--  http://localhost/~pjb/cgi/jean/jean.cgi
    Resolving localhost... ::1, 127.0.0.1
    Connecting to localhost|::1|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: unspecified [text/plain]
    Saving to: '/tmp/out'

    /tmp/out                           [ <=>                                                ]      68  --.-KB/s    in 0.005s  

    2017-03-29 02:04:50 (14.5 KB/s) - '/tmp/out' saved [68]

    [pjb@despina :0.0 jean]$ cat /tmp/out
    HTTP/1.1 200 OK
    Date: Wed, 29 Mar 2017 00:04:50 GMT
    Server: Apache/2.2.32 (Unix) mod_ssl/2.2.32 OpenSSL/1.0.2k DAV/2
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Transfer-Encoding: chunked
    Content-Type: text/plain

    *** - UNIX error 13 (EACCES): Permission denied
    Exiting on signal 6

This error is due to the fact that the web server doesn’t run the CGI under the same user as yourself, so the files created don’t have the right access rights and owner.  Let’s remove them:

    [pjb@despina :0.0 jean]$ sudo rm /tmp/jean-cgi.*

and try again:

    [pjb@despina :0.0 jean]$ wget --save-headers http://localhost/~pjb/cgi/jean/jean.cgi -O /tmp/out
    --2017-03-29 02:05:04--  http://localhost/~pjb/cgi/jean/jean.cgi
    Resolving localhost... ::1, 127.0.0.1
    Connecting to localhost|::1|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: unspecified [text/plain]
    Saving to: '/tmp/out'

    /tmp/out                           [ <=>                                                ]     441  --.-KB/s    in 0s      

    2017-03-29 02:05:04 (32.4 MB/s) - '/tmp/out' saved [441]

    [pjb@despina :0.0 jean]$ cat /tmp/out
    HTTP/1.1 200 OK
    Date: Wed, 29 Mar 2017 00:05:04 GMT
    Server: Apache/2.2.32 (Unix) mod_ssl/2.2.32 OpenSSL/1.0.2k DAV/2
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Transfer-Encoding: chunked
    Content-Type: text/plain


    2017-03-29 02:05:04

    <1/89> #<system-function show-stack> 3
    <2/82> #<compiled-function system::print-backtrace>
    <3/78> #<compiled-function jean-cgi::print-backtrace>
    [64] frame binding variables (~ = dynamically):
      | ~ *print-level* <--> nil
    Printed 3 frames
    ERROR while ((setf jean-cgi::query (or (error "Simulated error.") (getenv "QUERY_STRING") (alexandria.0.dev:read-stream-content-into-string *standard-input*)))):
    Simulated error.

    [pjb@despina :0.0 jean]$ 

Works nicely, the error is detected and reported.
After having removed the call to error, it works as expected:

    [pjb@despina :0.0 jean]$ sudo rm /tmp/jean-cgi.*
    Password:
    [pjb@despina :0.0 jean]$ ./jean.cgi </dev/null
    Status: 302 Found
    Location: http://localhost


    Got query: ""
    at url: "http://localhost”

Again, remove the files created when you switch between shell runs and cgi runs:

    [pjb@despina :0.0 jean]$ sudo rm /tmp/jean-cgi.*

    [pjb@despina :0.0 jean]$ wget --save-headers http://localhost/~pjb/cgi/jean/jean.cgi -O /tmp/out
    --2017-03-29 02:09:49--  http://localhost/~pjb/cgi/jean/jean.cgi
    Resolving localhost... ::1, 127.0.0.1
    Connecting to localhost|::1|:80... connected.
    HTTP request sent, awaiting response... 302 Found
    Location: http://localhost [following]
    --2017-03-29 02:09:49--  http://localhost/
    Reusing existing connection to [localhost]:80.
    HTTP request sent, awaiting response... 200 OK
    Length: 44 [text/html]
    Saving to: '/tmp/out'

    /tmp/out                       100%[===================================================>]      44  --.-KB/s    in 0s      

    2017-03-29 02:09:49 (8.39 MB/s) - '/tmp/out' saved [44/44]

    [pjb@despina :0.0 jean]$ cat /tmp/out
    HTTP/1.1 200 OK
    Date: Wed, 29 Mar 2017 00:09:49 GMT
    Server: Apache/2.2.32 (Unix) mod_ssl/2.2.32 OpenSSL/1.0.2k DAV/2
    Last-Modified: Tue, 28 Mar 2017 18:22:36 GMT
    ETag: "168d58e-2c-54bce8b4d5300"
    Accept-Ranges: bytes
    Content-Length: 44
    Keep-Alive: timeout=5, max=99
    Connection: Keep-Alive
    Content-Type: text/html

    <html><body><h1>Welcome!</h1></body></html>
    [pjb@despina :0.0 jean]$ 

-- 
__Pascal J. Bourguignon__



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
clisp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/clisp-list