~retropikzel/fcgi-bridge

76dc3df324a5db9acc04a39c463ca5f183eaaefa — retropikzel 6 months ago 2967ff8
Make the SCGI socket to be permanent
M Makefile => Makefile +1 -0
@@ 33,6 33,7 @@ deploy: dist/fcgi2scgi
	rm -rf sftp/public_html/test/fcgi2scgi
	cp dist/fcgi2scgi sftp/public_html/test/fcgi2scgi
	cp bridge.cgi sftp/public_html/test/bridge.cgi
	cp test.cgi sftp/public_html/test/test.cgi
	cp test/scgiclient/src/main.scm sftp/public_html/test/main.scm
	cp -r test/scgiclient/schubert sftp/public_html/test/


M dist/fcgi2scgi => dist/fcgi2scgi +0 -0
M src/fcgi2scgi.c => src/fcgi2scgi.c +94 -74
@@ 1,5 1,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include <unistd.h>


@@ 21,99 23,117 @@ int count_leading_spaces(char* str) {
    return spaces;
}

static int build_scgi_message(char* message, char* body, int body_length, char **envpointer) {
    int index = 6;
    int param_str_length = 0;
    char* equals_index = NULL;
    char message_length_str[6];

    //Open the netstring, leaving space for message size
    message[0] = ' ';
    message[1] = ' ';
    message[2] = ' ';
    message[3] = ' ';
    message[4] = ' ';
    message[5] = ':';

    for ( ; *envpointer != NULL; envpointer++) {
        param_str_length = strlen(*envpointer);
        equals_index = strchr(*envpointer, '=');
        memcpy(equals_index, "\0", 1);
        memcpy(&message[index], *envpointer, param_str_length);
        index += param_str_length + 1;
        memcpy(&message[index], "\0", 1);
    }
    message[index] = ',';
    index += 1;

    //Add message size to beginning of message
    sprintf(&message[index], "%i:%s,", body_length, body);
    sprintf(message_length_str, "%5i", index);
    memcpy(&message[0], message_length_str, 5);

    //Add body
    memcpy(&message[index], body, body_length);
    index += body_length;

    return index;
}



int main(int argc, char *argv[]) {
    char **initialEnv = environ;

    int count = 0;
    char* old_message = " 72:CONTENT_LENGTH\0" "0\0SCGI\0" "1\0REQUEST_METHOD\0GET\0REQUEST_URI\0/hello\0,0:,";
    char **envpointer = NULL;
    char server_reply[4000];
    char* scgi_message = malloc(4000);
    char* body = malloc(4000);
    int message_length = 0;
    int message_leading_spaces = 0;
    int scgi_message_index = 6;
    int param_str_length = 0;
    char* equals_index = NULL;
    char message_length_str[6];
    int original_body_memory_size = 4000;
    int body_memory_size = original_body_memory_size;
    char* body = malloc(body_memory_size);
    int original_scgi_message_memory_size = 4000;
    int scgi_message_memory_size = original_scgi_message_memory_size;
    char* scgi_message = malloc(scgi_message_memory_size);
    int content_length = 0;
    int body_index = 0;
    int body_character = 0;
    struct sockaddr_in server;

    int socket_desc = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
    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));

    connect(socket_desc , (struct sockaddr *)&server , sizeof(server));

    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);
        memset(scgi_message, 0, scgi_message_memory_size);
        puts("Content-type: text/html\r\n\r\n");

        int content_length = 0;
        content_length = 0;
        if(getenv("CONTENT_LENGTH") != NULL) {
            content_length = strtol(getenv("CONTENT_LENGTH"), NULL, 10);
            while(content_length >= body_memory_size) {
                printf("fcgi2scgi [DEBUG] Increasing body memory size to %i\n", body_memory_size);
                body_memory_size = body_memory_size * 2;
                body = realloc(body, body_memory_size);
            }
        }

        if (content_length > 0) {
            int i = 0;
            int ch = 0;
            body_index = 0;
            body_character = 0;
            for (body_index = 0; body_index < content_length; body_index++) {
                if ((body_character = getchar()) < 0) {
                    fprintf(stderr, "fcgi2scgi [ERROR] Not enough bytes received on standard input<p>\n");
                    exit(1);
                }
                body[body_index] = body_character;
            }
        }

        // Buil SCGI message
        scgi_message_index = 6;
        param_str_length = 0;
        equals_index = NULL;
        envpointer = environ;

            for (i = 0; i < content_length; i++) {
                if ((ch = getchar()) < 0) {
                    //printf("Error: Not enough bytes received on standard input<p>\n");
                    break;
        //Open the netstring, leaving space for message size
        scgi_message[0] = ' ';
        scgi_message[1] = ' ';
        scgi_message[2] = ' ';
        scgi_message[3] = ' ';
        scgi_message[4] = ' ';
        scgi_message[5] = ':';

        for ( ; *envpointer != NULL; envpointer++) {
            param_str_length = strlen(*envpointer);
            while(scgi_message_index + param_str_length >= scgi_message_memory_size) {
                printf("fcgi2scgi [DEBUG] Increasing scgi message memory size to %i\n", scgi_message_memory_size);
                scgi_message = realloc(scgi_message, scgi_message_memory_size * 2);
                if(scgi_message == NULL) {
                    printf("fcgi2scgi [ERROR] Failed reallocating new scgi_message size");
                }
                body[i] = ch;
                scgi_message_memory_size = scgi_message_memory_size * 2;
            }
            equals_index = strchr(*envpointer, '=');
            memcpy(equals_index, "\0", 1);
            memcpy(&scgi_message[scgi_message_index], *envpointer, param_str_length);
            scgi_message_index += param_str_length + 1;
            memcpy(&scgi_message[scgi_message_index], "\0", 1);
        }
        if(connection_status < 0) {
            //printf("Could not connect to SCGI server, status: %i</br>", connection_status);
            return 1;
        scgi_message[scgi_message_index] = ',';
        scgi_message_index += 1;

        //Add message size to beginning of message
        sprintf(&scgi_message[scgi_message_index], "%i:%s,", content_length, body);
        sprintf(message_length_str, "%5i", scgi_message_index);
        memcpy(&scgi_message[0], message_length_str, 5);

        //Add body
        while(scgi_message_index + content_length + 1 >= scgi_message_memory_size ) {
            scgi_message_memory_size = scgi_message_memory_size * 2;
            fprintf(stderr, "fcgi2scgi [DEBUG] Increasing scgi message memory size to %i\n", scgi_message_memory_size);
            scgi_message = realloc(scgi_message, scgi_message_memory_size);
        }
        message_length = build_scgi_message(scgi_message, body, content_length, environ);
        memcpy(&scgi_message[scgi_message_index], body, content_length);
        scgi_message_index += content_length;

        message_leading_spaces = count_leading_spaces(scgi_message);
        //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>");
            return 1;
        }
        //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>");
            return 1;
        }
        //printf("Reply from SCGI server received</br>");
        //printf("Server reply: %s", server_reply);

        send(socket_desc, &scgi_message[message_leading_spaces], scgi_message_index - message_leading_spaces, 0);

        recv(socket_desc, server_reply, 4000, 0);
        puts(server_reply);
        body_memory_size = original_body_memory_size;
        body = realloc(body, body_memory_size);
        scgi_message_memory_size = original_scgi_message_memory_size;
        scgi_message = realloc(scgi_message, scgi_message_memory_size);
    }
}

M test.cgi => test.cgi +2 -1
@@ 6,5 6,6 @@ printf "\r\n"
printf "\r\n"
echo "Hello"

ldd fcgi2scgi.fcgi
killall fcgi2scgi
killall gosh


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

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


@@ 161,12 163,8 @@
    (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)
          (if (or (eof-object? bytes)
                  (< (bytevector-length bytes) 4000))
            (bytevector-append result bytes)
            (read-all-from-socket socket (bytevector-append result bytes))))))



@@ 191,8 189,10 @@
              (socket-send client-socket
                           (string->utf8 (if (string? response)
                                           response
                                           ""))))))
            (socket-close client-socket)))
                                           "")))
              (if bridged?
                (scgi-handle client-socket handler)
                (socket-close client-socket)))))))

    (define scgi-listen
      (lambda (socket handler)


@@ 204,6 204,12 @@
        (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-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 +1 -1
@@ 31,5 31,5 @@

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

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