~retropikzel/fcgi-bridge

2967ff8738e29bd1a76763c8d313a28874b7c87b — retropikzel 6 months ago 18b827b
Made to work for making more than one request
M bridge.cgi => bridge.cgi +1 -1
@@ 9,7 9,7 @@ PORT=$((3000 + $RANDOM % 1000))

#printf "Hello"

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


M dist/fcgi2scgi => dist/fcgi2scgi +0 -0
M src/fcgi2scgi.c => src/fcgi2scgi.c +15 -18
@@ 37,7 37,6 @@ static int build_scgi_message(char* message, char* body, int body_length, char *

    for ( ; *envpointer != NULL; envpointer++) {
        param_str_length = strlen(*envpointer);
        printf("%s, length %i\n", *envpointer, param_str_length);
        equals_index = strchr(*envpointer, '=');
        memcpy(equals_index, "\0", 1);
        memcpy(&message[index], *envpointer, param_str_length);


@@ 64,12 63,6 @@ static int build_scgi_message(char* message, char* body, int body_length, char *
int main(int argc, char *argv[]) {
    char **initialEnv = environ;

    int socket_desc = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
    struct sockaddr_in server;
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons((int)strtol(argv[1], (char **)NULL, 10));
    int connection_status = connect(socket_desc , (struct sockaddr *)&server , sizeof(server));
    int count = 0;
    char* old_message = " 72:CONTENT_LENGTH\0" "0\0SCGI\0" "1\0REQUEST_METHOD\0GET\0REQUEST_URI\0/hello\0,0:,";
    char server_reply[4000];


@@ 78,6 71,12 @@ int main(int argc, char *argv[]) {
    int message_length = 0;
    int message_leading_spaces = 0;
    while (FCGI_Accept() >= 0) {
        int socket_desc = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
        struct sockaddr_in server;
        server.sin_addr.s_addr = inet_addr("127.0.0.1");
        server.sin_family = AF_INET;
        server.sin_port = htons((int)strtol(argv[1], (char **)NULL, 10));
        int connection_status = connect(socket_desc , (struct sockaddr *)&server , sizeof(server));
        memset(scgi_message, 0, 4000);
        puts("Content-type: text/html\r\n\r\n");



@@ 91,32 90,30 @@ int main(int argc, char *argv[]) {

            for (i = 0; i < content_length; i++) {
                if ((ch = getchar()) < 0) {
                    printf("Error: Not enough bytes received on standard input<p>\n");
                    //printf("Error: Not enough bytes received on standard input<p>\n");
                    break;
                }
                body[i] = ch;
            }
        }
        //printf("Body: %s\n", body);
        if(connection_status < 0) {
            printf("Could not connect to SCGI server, status: %i</br>", connection_status);
            //printf("Could not connect to SCGI server, status: %i</br>", connection_status);
            return 1;
        }
        message_length = build_scgi_message(scgi_message, body, content_length, environ);
        message_leading_spaces = count_leading_spaces(scgi_message);
        printf("Connected to SCGI server</br>");
        //printf("Connected to SCGI server</br>");
        if(send(socket_desc, &scgi_message[message_leading_spaces], message_length - message_leading_spaces, 0) < 0) {
            printf("Send to SCGI server failed</br>");
            //printf("Send to SCGI server failed</br>");
            return 1;
        }
        printf("Data Send to SCGI Server</br>");
        //printf("Data Send to SCGI Server</br>");
        if(recv(socket_desc, server_reply, 4000, 0) < 0) {
            printf("Could not get answer from SCGI server</br>");
            //printf("Could not get answer from SCGI server</br>");
            return 1;
        }
        printf("Reply from SCGI server received</br>");
        printf("Server reply: %s", server_reply);

        return 0;
        //printf("Reply from SCGI server received</br>");
        //printf("Server reply: %s", server_reply);
        puts(server_reply);
    }
}

M test/scgiclient/schubert/retropikzel/scgi/v0-3-0/main.scm => test/scgiclient/schubert/retropikzel/scgi/v0-3-0/main.scm +26 -64
@@ 12,6 12,7 @@
  (import (scheme base)
          (scheme write)
          (scheme char)
          (scheme file)
          (scheme process-context)
          (srfi 106))
  (export scgi-start


@@ 160,6 161,11 @@
    (define read-all-from-socket
      (lambda (socket result)
        (let ((bytes (socket-recv socket 4000)))
          (with-output-to-file
            "scgi_output.txt"
            (lambda ()
              (write (utf8->string bytes))
              (newline)))
          (if (< (bytevector-length bytes) 4000)
            (bytevector-append result bytes)
            (read-all-from-socket socket (bytevector-append result bytes))))))


@@ 167,70 173,26 @@
    (define scgi-handle
      (lambda (client-socket handler)
        (let* ((request-bytes (read-all-from-socket client-socket (bytevector))))
        (write request-bytes)
        (newline)
        (write (utf8->string request-bytes))
        (newline)
          (let*(
               (request (scgi-netstring->list request-bytes))
               (request-method (if (not (null? request)) (cdr (assoc 'REQUEST_METHOD request)) ""))
               (request-uri (if (not (null? request)) (cdr (assoc 'REQUEST_URI request)) ""))
               (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) "")))
               )
            (write request)
            (newline)
            (write content-length)
            (newline)
          (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)
                    (display "Response: ")
                    (write response)
                    (newline)
                    (socket-send client-socket
                                 (string->utf8 (if (string? response)
                                                 response
                                                 ""))))
          #;(call-with-current-continuation
            (lambda (k)
              (with-exception-handler
                (lambda (ex)
                  (display "ERROR: " (current-error-port))
                  (newline (current-error-port))
                  (display request-uri (current-error-port))
                  (newline (current-error-port))
                  (display (error-object-message ex) (current-error-port))
                  (newline (current-error-port))
                  (display (error-object-irritants ex) (current-error-port))
                  (newline (current-error-port))
                  (write ex (current-error-port))
                  (newline (current-error-port))
                  (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)
                    (write response)
                    (newline)
                    (socket-send client-socket
                                 (string->utf8 (if (string? response)
                                                 response
                                                 ""))))))))))
        (socket-close client-socket)))
          (let*((request (scgi-netstring->list request-bytes))
                (request-method (if (not (null? request)) (cdr (assoc 'REQUEST_METHOD request)) ""))
                (request-uri (if (not (null? request)) (cdr (assoc 'REQUEST_URI request)) ""))
                (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
                                           ""))))))
            (socket-close client-socket)))

    (define scgi-listen
      (lambda (socket handler)

M test/scgiclient/src/main.scm => test/scgiclient/src/main.scm +3 -6
@@ 18,15 18,12 @@
(define hello-handler
  (lambda (request)
    (string-append ;"Content-type: text/html"
      ;"\r\n"
      ;"\r\n"
      "Hello world from Scheme SCGI server"
      )))
                   ;"\r\n"
                   ;"\r\n"
                   "Hello world from Scheme SCGI server")))

(define main
  (lambda (request)
    (display request)
    (newline)
    (let ((request-uri (cdr (assoc 'REQUEST_URI request))))
      (cond ((string=? request-uri "/hello")
             (hello-handler request))