0

I'm developing a web app for a reservation system and so far I achieved to send a mail with libcurl and display the form in HTML using fcgi_stdio.h, but both separately.

This is how it looks

#define __USE_XOPEN
#define _GNU_SOURCE

#include <stdio.h>
#include <fcgi_stdio.h>
#include <string.h>
#include <stdlib.h>

#include "mail.h"
#include "booking.h"

int
main()
{
    char to[20], name[20];
    //char *year, *mon, *day, *hour, *min;
    char date[20];
    char num_guest[4], phone[10];

    while (FCGI_Accept() >= 0) {
        printf("Content-type: text/html\n\n");
        printf("<h1>Welcome</h1>");

        char *method = getenv("REQUEST_METHOD");
        char form[2048] = {0};
        char output[2048] = {0};

        if(strcmp(method, "GET") == 0) {
            strcpy(form,
                "<form method='POST' action=''>"
                "<input id='name' name='name' type='text'></input>"
                "<br>"
                "<input id='to' name='to' type='email'></input>"
                "<br>"
                "<input id='date' type='datetime-local' name='date' />"
                "<br>"
                "<input type='tel' "
                    "id='phone' name='phone' size='20' "
                    "minlenght='9' maxlenght='14' required>"
                "<br>"
                "<input type='number' id='num_guest' name='num_guest'" 
                    "min='1' max='18'>"
                "<br>"
                "<input type='submit' value='Submit'>"
                "</form>"
            );
        } else if (strcmp(method, "POST") == 0) {
            int ilen = atoi(getenv("CONTENT_LENGTH"));
            //char *bufp = malloc(ilen);
            char *bufp = (char *) malloc(ilen * sizeof(char));
            fread(bufp, ilen, 1, stdin);
            printf("<h3>Thank you for your reservation!</h3>");
            strcpy(output, bufp);
            free(bufp);

                        /* declare data variables */
            sscanf(output, 
                "name=%[^&]&to=%[^&]&date=%[^&]&phone=%[^&]&num_guest=%[^&]", 
                name, to, date, phone, num_guest);

            replace(date, "%3A", ":"); // makes the date readable
            replace(to, "%40", "@");   // makes the mail readable
        }

        printf(
            "<div>"
            "%s"
            "</div>",
            form
        );
    }

    return 0;
}

The code above works fine, but I would like to call these functions after clicking the Submit button.

book_for(to, name, num_guest, phone, date);
send_mail("addr", "confirmation.mail");

Those functions alone and declaring before the arguments as char *name = 'some name'; work fine too sending the mail to the recipient.

Thanks for your help :)

Julio
  • 3
  • 1
  • C is a very unusual choice for this sort of thing and will only make your life exceedingly difficult. For example: There's an extraordinary number of potential buffer overflow bugs in this small code snippet. – tadman Jan 23 '23 at 00:42
  • Aside: Assuming ```malloc``` would never fail could lead to a potential bug. – Harith Jan 23 '23 at 01:02
  • How I understand the question, you have saved function name in a string and want to call that function by the name in the string. A google search landed me on this result, and might help you - https://stackoverflow.com/questions/1118705/call-a-function-named-in-a-string-variable-in-c. – L_R Jan 23 '23 at 01:15
  • Thanks for your observations! I'll check the link @L_R – Julio Jan 23 '23 at 02:26
  • fastcgi is probably an evolutionary dead end at this point, and may prefer to proxy requests from your web server to have your program and have implement an http server (libmicrohttpd for instance; there are many others). node.js is basically pre-build for this use case. I don't now easy it would be to call c-code from node.js though. – Allan Wind Jan 23 '23 at 06:39
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 23 '23 at 13:00

0 Answers0