~retropikzel/fcgi-bridge

18b827b320ef25642480db28125659d27b57e7f3 — retropikzel 6 months ago d258dac
fcgi2scgi bridge now sends correct SCGI request to SCGI server
4 files changed, 74 insertions(+), 45 deletions(-)

M Makefile
M dist/fcgi2scgi
M src/fcgi2scgi.c
M test/scgiclient/src/main.scm
M Makefile => Makefile +1 -1
@@ 29,7 29,7 @@ clean-all:
	rm -rf libs
	rm -rf dist/*

deploy:
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

M dist/fcgi2scgi => dist/fcgi2scgi +0 -0
M src/fcgi2scgi.c => src/fcgi2scgi.c +66 -38
@@ 8,21 8,59 @@

extern char **environ;

static void PrintEnv(char *label, char **envp)
{
    printf("%s:<br>\n<pre>\n", label);
    for ( ; *envp != NULL; envp++) {
        printf("%s\n", *envp);
int count_leading_spaces(char* str) {
    int spaces = 0;
    int str_length = strlen(str);
    for(int i = 0; 0 < str_length; i++) {
        if(str[i] != ' ') {
            return spaces;
        } else {
            spaces += 1;
        }
    }
    printf("</pre><p>\n");
    return spaces;
}

static void build_scgi_message(char* message, char **envpointer) {
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++) {
        printf("%s\n", *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);
        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;



@@ 33,52 71,41 @@ int main(int argc, char *argv[]) {
    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* old_message = " 72:CONTENT_LENGTH\0" "0\0SCGI\0" "1\0REQUEST_METHOD\0GET\0REQUEST_URI\0/hello\0,0:,";
    char server_reply[4000];
    char* scgi_message = malloc(0);
    char* scgi_message = malloc(4000);
    char* body = malloc(4000);
    int message_length = 0;
    int message_leading_spaces = 0;
    while (FCGI_Accept() >= 0) {
        printf("Content-type: text/html\r\n"
                "\r\n"
                "<title>FastCGI echo</title>"
                "<h1>FastCGI echo</h1>\n"
                "Request number %d\n, port: %s</br>", count, argv[1]);
        memset(scgi_message, 0, 4000);
        puts("Content-type: text/html\r\n\r\n");

        build_scgi_message(scgi_message, environ);
        char *contentLength = getenv("CONTENT_LENGTH");
        int len;
        if (contentLength != NULL) {
            len = strtol(contentLength, NULL, 10);
        }
        else {
            len = 0;
        int content_length = 0;
        if(getenv("CONTENT_LENGTH") != NULL) {
            content_length = strtol(getenv("CONTENT_LENGTH"), NULL, 10);
        }
        if (content_length > 0) {
            int i = 0;
            int ch = 0;


        if (len <= 0) {
            printf("No data from standard input.<p>\n");
        }
        else {
            int i, ch;

            printf("Standard input:<br>\n<pre>\n");
            for (i = 0; i < len; i++) {
            for (i = 0; i < content_length; i++) {
                if ((ch = getchar()) < 0) {
                    printf("Error: Not enough bytes received on standard input<p>\n");
                    break;
                }
                putchar(ch);
                body[i] = ch;
            }
            printf("\n</pre><p>\n");
        }
        PrintEnv("Request environment", environ);
        PrintEnv("Initial environment", initialEnv);

        //printf("Body: %s\n", body);
        if(connection_status < 0) {
            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>");
        if(send(socket_desc, old_message, 68, 0) < 0) {
        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;
        }


@@ 89,6 116,7 @@ int main(int argc, char *argv[]) {
        }
        printf("Reply from SCGI server received</br>");
        printf("Server reply: %s", server_reply);

        return 0;
    }
}

M test/scgiclient/src/main.scm => test/scgiclient/src/main.scm +7 -6
@@ 10,17 10,18 @@

(define no-endpoint-handler
  (lambda (request)
    (string-append "Content-type: text/html"
                   "\r\n"
                   "\r\n"
    (string-append ;"Content-type: text/html"
                   ;"\r\n"
                   ;"\r\n"
                   "No such endpoint")))

(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)