-1

I'm trying to make a login script in C. I want to know, how I can pass the HTML form data to my back-end c script. I'm new to C (coming from perl)

Just do this?

<form action="test.c"> or <form action="test.a"> 

How can the script explicitly differ between username and password? Earlier, I just had to do this (in perl):

$username = $cgi->param("username");

How can I do this now in C. I'm trying to avoid using CGI at all.

Edit 1:

  1. It seems I also need to know, how CGI basically works (in short). I'm guessing, the browser is a sort-of compiler for CGI? if not, then is it apache?

  2. Since C is not a interpreted language, how does CGI work with C? In perl, I could simply do:

    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    print "Hello, World.";
    

How do I do this in C?

Edit2: Ok, I went over to previously answered questions under the cgi tag here on stackoverflow.

This seems to be the most voted one: What is Common Gateway Interface (CGI)?

But it still doesn't clear a few things, which I'm asking here.

Since RFC3875 is only a informational doc, and there is no finalized standard.

  1. Who implements CGI protocol? Who defines its "standard behavior" on servers such as Apache.

  2. How does C files work with CGI in modern environment. Please elaborate using a "Hello World!" as a response to a form submission.

Ahmad Bilal
  • 380
  • 1
  • 2
  • 15
  • 3
    If you aren't using CGI, how is your code running? – SLaks Jun 06 '18 at 01:02
  • @SLaks I appreciate the irony, but can you elaborate please? Also, I updated the question. – Ahmad Bilal Jun 06 '18 at 12:49
  • CGI means that the web server launches your code (whether interpreted or compiled), passes the request via stdin, and expects a response via stdout. – SLaks Jun 06 '18 at 17:10
  • @SLaks Who and what describes its "defined behavior" ? Since I'm using apache, is apache's implementation of CGI protocol .. is what I'm using? – Ahmad Bilal Jun 06 '18 at 20:34

1 Answers1

2

Trying to help you is a bit difficult. Your question, in particular the statement "I'm trying to avoid using CGI at all" implies that you're missing a conceptual overview. In order to 'avoid CGI' you need to replace it with a mechanism to receive, parse and respond to HTTP requests. That would mean (potentially) writing an HTTP server. C is a compiled language. When you wonder if you can do action="test.c" it shows me that you're missing the fact that C isn't an interpreted language.

You could use something like tiny httpd to get you started.

This site about cgi using C will help you. How CGI is implemented ends up being at the discretion of the implementor. A hello world program is as simple as the compiled executable of

#include <stdio.h>
int main(void) {
  printf("Content-Type: text/plain;charset=us-ascii\n\n");
  printf("Hello world\n\n");
  return 0;
}
Leonard
  • 13,269
  • 9
  • 45
  • 72
  • can you please answer the two points I edited in. A theoretical answer would be enough for me, and I will accept your answer to close this question down. – Ahmad Bilal Jun 06 '18 at 20:06
  • strange, I can't seem to tag you – Ahmad Bilal Jun 06 '18 at 20:35
  • who would be a implementor in this case? Sorry, I didn't understand what you said. – Ahmad Bilal Jun 06 '18 at 23:03
  • The implementor is anyone who writes the software. I'm sorry you don't understand me, but maybe this will make it clear: I don't know the names of the people that wrote Apache, or the Perl CGI module, or who wrote tiny httpd, but those are the implementors. – Leonard Jun 07 '18 at 05:19
  • For perl, there is CGI.pm module, it resides on your computer when you install perl (including CGI.pm). That defines the CGI's "standard behavior". In my case, I want to know, where is the CGI files actually located. Inside apache ? What is the standard behavior of it? – Ahmad Bilal Jun 07 '18 at 19:09
  • I have accepted this answer, even though it does not answer 100% to my query, but still indirectly points at some useful leads. I must say, CGI as implemented by Apache is badly documented. Horribly short is the right expression I think. – Ahmad Bilal Jun 18 '18 at 12:13