Solved

CGI with C

Posted on 1998-02-25
6
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

752 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