Solved

CGI with C

Posted on 1998-02-25
6
234 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now