2

When clicking a button on my web server, I need a C script executed.

I'm using CGI but I'm getting:

[Tue Dec 10 11:17:57 2013] [error] [client 172.17.223.62] Premature end of script headers: set01.cgi, referer:

The C program is this:

#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>

int main(){

   int handle = serialOpen ("/dev/ttyAMA0", 19200) ;

   if (handle == -1){

      printf("Error opening TTYAMA0");
  }
  serialPutchar (handle, 0xC1);
  usleep(100);
  serialPutchar (handle, 0x7f);

  return 1;

}
2
  • 2
    C is not a scripting language. You need to compile the program and execute the resulting file. I don't know CGI so I can't tell you how to accomplish this.
    – Kenneth
    Commented Dec 10, 2013 at 12:04
  • @Kenneth : Scripting is sometimes associated with CGI processes because "scripting languages" are commonly used to implement them, but the language used does not matter. CGI is just an interface protocol, and an executable is an executable, whether directly compiled or thanks to a shebang (#!) and interpreter.
    – goldilocks
    Commented Dec 11, 2013 at 10:41

2 Answers 2

4

It's not clear from your question whether the C program is intended to act as the CGI executable or not. However, if it is, you haven't fulfilled the CGI interface. A CGI executable must output (via stdout) a properly formatted HTTP response.

There are various C libraries for helping with CGI, although it is not necessary that you use one. CGI passes information from the server via environment variables, which you can access with functions such as getenv(). Here is a GNU lib C guide to environment access; note the interface itself is ISO Standard C.

If the C program is not suppose to handle the CGI stuff, it is not the problem. "Premature end of script headers" indicates that whatever is doing the CGI stuff hasn't properly formatted the HTTP response.

2

As stated in @Kenneth's comment you need to compile your C program and you can then execute it using a CGI script. Here's an example of running the date command from a CGI script from http://www.cyberciti.biz/tips/executing-linuxunix-commands-from-web-page-part-i.html

$ cd /usr/lib/cgi-bin
$ vi first.cgi

Input the source below:

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Bash as CGI"
echo "</title></head><body>"
echo "<h1>Hello world</h1>"
echo "Today is $(date)"
echo "</body></html>"
Save and close the file. Setup execute permission on the script:

$ chmod +x first.cgi

In your case instead of calling the date program you can just call your program, such as echo "C program returned: $(yourCprogram)"

You can compile your C program by placing the source into a file such as myCprogram.c and then use the command gcc myCprogram.c -o myCprogram to compile and link it into an executable called myCprogram.

2
  • @goldilocks I hear what you're saying and I see that the bash echo only outputs a LF character at the end. However, using Google I don't see any of the bash CGI examples use echo -e "Hello there\r" to output a CR LF at the end of the line. Also, the "Content-length" header isn't required on a response as the message length of a response can be determined by the server closing the HTTP connection as a last resort. This is obviously not an ideal situation as the client can't reuse the existing connection for a new request, but again, this is a simple interface. Commented Dec 10, 2013 at 17:11
  • All apologies! You're right about the headers -- if you use just Content-type the server will deal with the status line and length. WRT echo, in this RFC: ietf.org/rfc/rfc3875 sec. 6.3 it's implicit the server also correct the line endings, since headers must simply "be specified on a single line" and CRLF doesn't appear anywhere in that doc. +1 [Previous comment deleted]
    – goldilocks
    Commented Dec 10, 2013 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.