~retropikzel/fcgi-bridge

ccfdfcfcba2943e675110901fa9ef29efb4ab4f2 — retropikzel 4 months ago 6d15b7f
Added debugging stuff
M bridge.cgi => bridge.cgi +2 -9
@@ 2,16 2,9 @@
# vim: ft=bash

PORT=$((3000 + $RANDOM % 1000))

#printf "Content-Type: text/html"
#printf "\r\n"
#printf "\r\n"

#printf "Hello"

/usr/www/users/retrops/programs/bin/gosh -I ./schubert main.scm $PORT > scgi_bridge_output.txt 2>&1 &
/usr/www/users/retrops/programs/bin/gosh -I ./schubert main.scm $PORT
sleep 2
exec ./fcgi2scgi $PORT --development > fcgi2scgi_output.txt 2>&1
exec ./fcgi2scgi $PORT --debug




M dist/fcgi2scgi => dist/fcgi2scgi +0 -0
M src/fcgi2scgi.c => src/fcgi2scgi.c +1 -8
@@ 59,11 59,6 @@ int main(int argc, char *argv[]) {

    while (FCGI_Accept() >= 0) {
        memset(scgi_message, 0, scgi_message_memory_size);
        if(argc > 2) {
            if(strcmp(argv[2], "--debug") == 0) {
                puts("Content-type: text/html\r\n\r\n");
            }
        }

        content_length = 0;
        if(getenv("CONTENT_LENGTH") != NULL) {


@@ 145,9 140,7 @@ int main(int argc, char *argv[]) {
        scgi_message_memory_size = original_scgi_message_memory_size;
        scgi_message = realloc(scgi_message, scgi_message_memory_size);
        if(argc > 2) {
            if(strcmp(argv[2], "--development") == 0) {
                exit(0);
            } else if(strcmp(argv[2], "--debug") == 0) {
            if(strcmp(argv[2], "--debug") == 0) {
                exit(0);
            }
        }

M test/scgiclient/schubert/retropikzel/scgi/v0-3-0/main.scm => test/scgiclient/schubert/retropikzel/scgi/v0-3-0/main.scm +65 -16
@@ 16,11 16,14 @@
          (scheme process-context)
          (srfi 106))
  (export scgi-start
          scgi-start-debug
          scgi-start-bridged
          scgi-start-debug-bridged
          scgi-add-request-middleware
          scgi-add-response-middleware)
  (begin

    (define debug? #f)
    (define bridged? #f)
    (define request-middleware (list))
    (define response-middleware (list))


@@ 55,6 58,28 @@

    (define decode-replacements (map reverse encode-replacements))

    (define make-html-error-message
      (lambda (ex request body handler)
        (parameterize ((current-output-port (open-output-string)))
          (display "Content-type: text/html\r\n\r\n")
          (display "<h1>Error</h1>")
          (display "<br/>")
          (display "<h2>Message</h2>")
          (display "<p>")
          (display (error-object-message ex))
          (display "</p>")
          (display "<h2>Irritants</h2>")
          (display "<p>")
          (display (error-object-irritants ex))
          (display "</p>")
          (display "<br/>")
          (display "<h2>Request</h2>")
          (display "<br/>")
          (display "<p>")
          (display request)
          (display "</p>")
          (get-output-string (current-output-port)))))

    (define get-replacement
      (lambda (key mode)
        (let ((r (if (string=? mode "encode")


@@ 177,22 202,33 @@
                (content-length (if (not (null? request)) (string->number (cdr (assoc 'CONTENT_LENGTH request))) 0))
                (body (url-decode (if (> content-length 0) (scgi-get-request-body request-bytes content-length) ""))))
            (set! request (append request (list (cons 'BODY body))))
            (for-each
              (lambda (middleware-procedure)
                (set! request (middleware-procedure request)))
              request-middleware)
            (let ((response (handler request)))
              (for-each
                (lambda (middleware-procedure)
                  (set! response (middleware-procedure response)))
                response-middleware)
              (socket-send client-socket
                           (string->utf8 (if (string? response)
                                           response
                                           "")))
              (if bridged?
                (scgi-handle client-socket handler)
                (socket-close client-socket)))))))
            (call-with-current-continuation
              (lambda (k)
                (with-exception-handler
                  (lambda (ex)
                    (let ((error-message (make-html-error-message ex request body handler)))
                      (if debug?
                        (socket-send client-socket
                                     (string->utf8 error-message)))
                      (display error-message)
                      (k 'exception)))
                  (lambda ()
                    (for-each
                      (lambda (middleware-procedure)
                        (set! request (middleware-procedure request)))
                      request-middleware)
                    (let ((response (handler request)))
                      (for-each
                        (lambda (middleware-procedure)
                          (set! response (middleware-procedure response)))
                        response-middleware)
                      (socket-send client-socket
                                   (string->utf8 (if (string? response)
                                                   response
                                                   ""))))))))
            (if bridged?
              (scgi-handle client-socket handler)
              (socket-close client-socket))))))

    (define scgi-listen
      (lambda (socket handler)


@@ 204,12 240,25 @@
        (let ((socket (make-server-socket port)))
          (scgi-listen socket handler))))

    (define scgi-start-debug
      (lambda (port handler)
        (set! debug? #t)
        (let ((socket (make-server-socket port)))
          (scgi-listen socket handler))))

    (define scgi-start-bridged
      (lambda (port handler)
        (set! bridged? #t)
        (let ((socket (make-server-socket port)))
          (scgi-listen socket handler))))

    (define scgi-start-debug-bridged
      (lambda (port handler)
        (set! bridged? #t)
        (set! debug? #t)
        (let ((socket (make-server-socket port)))
          (scgi-listen socket handler))))

    (define scgi-add-request-middleware
      (lambda (middleware-procedure)
        (set! request-middleware (append request-middleware (list middleware-procedure)))))

M test/scgiclient/src/main.scm => test/scgiclient/src/main.scm +6 -7
@@ 17,24 17,23 @@
      ((current-output-port
         (open-output-string)))
      (write request)
      (get-output-string (current-output-port)))
    )))
      (get-output-string (current-output-port))))))

(define hello-handler
  (lambda (request)
    (string-append ;"Content-type: text/html"
                   ;"\r\n"
                   ;"\r\n"
    (string-append "Content-type: text/html"
                   "\r\n"
                   "\r\n"
                   "Hello world from Scheme SCGI server")))

(define main
  (lambda (request)
    (let ((request-uri (cdr (assoc 'REQUEST_URI request))))
      (cond ((string=? request-uri "/hello")
      (cond ((string=? request-uri "/hello.scm")
             (hello-handler request))
            (else (no-endpoint-handler request))))))

(define port (list-ref (command-line) 1))

(scgi-start-bridged port main)
(scgi-start-debug-bridged port main)