Search results for 'QUERY_STRING'. 2 post(s) found.

  1. 2007/08/27 Simple CGI Programming in C
  2. 2007/08/27 How to send binary data through C# CGI app?
2007/08/27 14:15

Simple CGI Programming in C


Here's the simplest C based CGI Example.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char *data;

    printf("Content-Type:text/html;\xd\xa"); // HTML out start !!

    printf("<TITLE>Multiplication results</TITLE>\n");
    printf("<H3>Multiplication results</H3>\n");

    data = getenv("QUERY_STRING"); // getting parameters

    if(data == NULL)
      printf("<P>No Query string found");
    else
      printf("<P>%s", data);

    return 0;
}


If you want to run this application on Apache platform, you can test this application by copying to Apache CGI-bin directory.

If it's compiled as test.exe and installed in Apache installed local PC, you can test it by following URL:
http://127.0.0.1/CGI-bin/test.exe


http://127.0.0.1/CGI-bin/test.exe?helloworld

Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 00:20 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 08:47 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:44 delete

    moneyideas

2007/08/27 13:05

How to send binary data through C# CGI app?


Following example implemented binary data downloading.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace cscgi
{
  class CsCGI
  {
    private static string dbgString = "id=1234&pic=123.gif";
    private List<string> lstKeys = new List<string>();
    private List<string> lstVals = new List<string>();
    static void Main(string[] args)
    {
      CsCGI app = new CsCGI();
      ParseParams(app);
      string sID = GetParam(app, "id");
      string sFile = GetParam(app, "pic");
      try
      {
        FileStream fs = new FileStream(sFile, FileMode.Open);
        BinaryReader brFile = new BinaryReader(fs, Encoding.UTF8);
        byte[] bBuff = new byte[fs.Length];
        brFile.Read(bBuff, 0, (int)fs.Length);
        string sBuff = Encoding.ASCII.GetString(bBuff);
        // if it's just text file, change binary/plain to text/html
        Console.Write("Content-Type: binary/plain\n");
        Console.Write("Content-Length: " + fs.Length.ToString());
        Console.Write("\n\n");
        Console.Write(sBuff);
      }
      catch (Exception e)
      {
        Console.Write(e.Message + "\n");
      }
      finally
      {
        //
      }
    }

    private static void ParseParams(CsCGI app)
    {
      //string queryString = Environment.GetEnvironmentVariable("QUERY_STRING");
      string queryString = dbgString;
      string[] sParams = queryString.Split('&');
      string[] sParamsSeperated;
      for (int i = 0; i < sParams.Length; i++)
      {
        sParamsSeperated = sParamsIdea.Split('=');
        app.lstKeys.Add(sParamsSeperated[0]);
        app.lstVals.Add(sParamsSeperated[1]);
      }
    }

    private static string GetParam(CsCGI app, string key)
    {
      int idx = app.lstKeys.BinarySearch(key);
      if (idx >= 0)
        return app.lstVals[idx];
      return "";
    }
  }
}


If you are using Apache and wanna test it just put the EXE in the cgi-bin folder under Apache's directory. Also note that the parameters are static just for this example.
Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 02:25 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 10:53 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:44 delete

    moneyideas