Solved

CGI with C

Posted on 1998-02-25
6
242 Views
Last Modified: 2013-12-25
I am going to test a CGI-script. I've written two files:
1) test.html
********************************
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Developer Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>

<!-- Insert HTML here -->
<form method=get action="test.cgi">
Enter some text here:<br>
<input type="text" name="sometext" value="">
<p>
<input type="submit" value="Send">
</form>

</BODY>
</HTML>
********************************

2) test.c
********************************
#include <stdio.h>
#include <stdlib.h>

void header(), footer();

int main()
{
      header();
      printf("Querry String: %s\n\n", getenv("QUERY_STRING"));
      footer();

      return 0;
}

void header()
{
      printf("Content-type: text/html\n\n");
      printf("<html>\n");
      printf("<head>\n<title>Example CGI Output</title>\n</head>\n");
      printf("<body>\n\n");
}

void footer()
{
      printf("</body>\n");
      printf("</html>\n");
}
********************************

I build the c-fiel successed and get "test.exe". I change it to "test.cgi". Then I haven't permit to our server, so I save the files under the same directory.

I open the "test.html" with Netscape browser. I tip some text in the input window. But as I click the "Sene"-button, it shows some strange text which I can't understand.

How can I solve the problem?


chen wei

0
Comment
Question by:chenwei
  • 3
  • 2
6 Comments
 

Author Comment

by:chenwei
ID: 1832032
Edited text of question
0
 

Author Comment

by:chenwei
ID: 1832033
Edited text of question
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1832034
Your program should show something like:

sometext=the_text_you_typed_in

with sequences of "%xx" (where xx is the ASCII code in hexadecimal) replacing any non-printable chars.

If this is what you get, then this is how CGI works.
You need your string parsing routines to exctract and decode key-value pairs from the query string.

BTW, the getenv() function can fail and return NULL, so you should do something like:

--
int main(void) {
  char *query_string = getenv("QUERY_STRING");

  header();

  if(query_string) printf("Query string: %s\n", query_string);
  else puts("Cannot get query string!");

  footer();

  return 0;
}
--//--

Cheers.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:chenwei
ID: 1832035
First I would like to thank julio's answer. But it dosen't help.
I was told (and I am sure) that the files I wrote are correct. The problem may be that I should save the cgi-file(test.cgi) under the directory:/cgi-bin by the server, then the cgi-script can only be executed by server not by client. But I haven't the access to the server.

I do want to test the cgi-scripts I have written, but I don't know where and how.
0
 
LVL 1

Expert Comment

by:Michel021497
ID: 1832036
I think you should ask your provider for a solution for this problem. He can give you access to your cgi-bin directory.
0
 
LVL 5

Accepted Solution

by:
julio011597 earned 50 total points
ID: 1832037
Yes chenwei, CGI is a protocol stating how a web server can communicate with an external program; i.e., no server... no CGI.

To test your scripts locally, you should install a web server on your PC and work with it (you can download a web server for free from both Netscape and Microsoft).

Anyway, as Michel says, you could first try to talk to your Internet Provider and see if they can help you.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question